Fixes need for suspending function on uploading

This commit is contained in:
Vitor Pamplona
2026-02-28 11:43:31 -05:00
parent 30ffeaed34
commit 1bff239183
3 changed files with 33 additions and 36 deletions
@@ -36,7 +36,6 @@ import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerType
import com.vitorpamplona.amethyst.ui.actions.uploads.SelectedMedia
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.quartz.nip01Core.signers.SignerExceptions
import com.vitorpamplona.quartz.nip39ExtIdentities.GitHubIdentity
import com.vitorpamplona.quartz.nip39ExtIdentities.MastodonIdentity
import com.vitorpamplona.quartz.nip39ExtIdentities.TwitterIdentity
@@ -144,12 +143,12 @@ class NewUserMetadataViewModel : ViewModel() {
) {
accountViewModel.launchSigner {
upload(
uri,
context,
onUploading = { isUploadingImageForPicture = it },
onUploaded = { picture.value = it },
galleryUri = uri,
context = context,
onError = onError,
)
)?.let {
picture.value = it
}
}
}
@@ -160,12 +159,12 @@ class NewUserMetadataViewModel : ViewModel() {
) {
accountViewModel.launchSigner {
upload(
uri,
context,
onUploading = { isUploadingImageForBanner = it },
onUploaded = { banner.value = it },
galleryUri = uri,
context = context,
onError = onError,
)
)?.let {
banner.value = it
}
}
}
@@ -177,30 +176,27 @@ class NewUserMetadataViewModel : ViewModel() {
load()
accountViewModel.launchSigner {
upload(
uri,
context,
onUploading = { isUploadingImageForPicture = it },
onUploaded = {
picture.value = it
create()
},
galleryUri = uri,
context = context,
onError = onError,
)
)?.let {
picture.value = it
}
create()
}
}
private suspend fun upload(
galleryUri: SelectedMedia,
context: Context,
onUploading: (Boolean) -> Unit,
onUploaded: (String) -> Unit,
onError: (String, String) -> Unit,
) {
onUploading(true)
): String? {
isUploadingImageForPicture = true
val compResult = MediaCompressor().compress(galleryUri.uri, galleryUri.mimeType, CompressorQuality.MEDIUM, context.applicationContext)
try {
return try {
val result =
if (account.settings.defaultFileServer.type == ServerType.NIP96) {
Nip96Uploader().upload(
@@ -229,20 +225,17 @@ class NewUserMetadataViewModel : ViewModel() {
)
}
if (result.url != null) {
onUploading(false)
onUploaded(result.url)
} else {
onUploading(false)
if (result.url == null) {
onError(stringRes(context, R.string.failed_to_upload_media_no_details), stringRes(context, R.string.server_did_not_provide_a_url_after_uploading))
}
} catch (_: SignerExceptions.ReadOnlyException) {
onUploading(false)
onError(stringRes(context, R.string.failed_to_upload_media_no_details), stringRes(context, R.string.login_with_a_private_key_to_be_able_to_upload))
result.url
} catch (e: Exception) {
if (e is CancellationException) throw e
onUploading(false)
onError(stringRes(context, R.string.failed_to_upload_media_no_details), e.message ?: e.javaClass.simpleName)
null
} finally {
isUploadingImageForPicture = false
}
}
}
@@ -191,3 +191,7 @@ class RelayUrlNormalizer {
}
}
}
fun String.normalizeRelayUrl() = RelayUrlNormalizer.normalize(this)
fun String.normalizeRelayUrlOrNull() = RelayUrlNormalizer.normalizeOrNull(this)
@@ -23,7 +23,7 @@ package com.vitorpamplona.quartz.nip01Core.relay
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.sendAndWaitForResponse
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.normalizeRelayUrl
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
import kotlinx.coroutines.CoroutineScope
@@ -48,13 +48,13 @@ class NostrClientSendAndWaitTest : BaseNostrClientTest() {
val resultDamus =
client.sendAndWaitForResponse(
event = event,
relayList = setOf(RelayUrlNormalizer.normalize("wss://relay.damus.io")),
relayList = setOf("wss://relay.damus.io".normalizeRelayUrl()),
)
val resultNos =
client.sendAndWaitForResponse(
event = event,
relayList = setOf(RelayUrlNormalizer.normalize("wss://nos.lol")),
relayList = setOf("wss://nos.lol".normalizeRelayUrl()),
)
client.disconnect()