Fixes need for suspending function on uploading
This commit is contained in:
+26
-33
@@ -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.actions.uploads.SelectedMedia
|
||||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||||
import com.vitorpamplona.amethyst.ui.stringRes
|
import com.vitorpamplona.amethyst.ui.stringRes
|
||||||
import com.vitorpamplona.quartz.nip01Core.signers.SignerExceptions
|
|
||||||
import com.vitorpamplona.quartz.nip39ExtIdentities.GitHubIdentity
|
import com.vitorpamplona.quartz.nip39ExtIdentities.GitHubIdentity
|
||||||
import com.vitorpamplona.quartz.nip39ExtIdentities.MastodonIdentity
|
import com.vitorpamplona.quartz.nip39ExtIdentities.MastodonIdentity
|
||||||
import com.vitorpamplona.quartz.nip39ExtIdentities.TwitterIdentity
|
import com.vitorpamplona.quartz.nip39ExtIdentities.TwitterIdentity
|
||||||
@@ -144,12 +143,12 @@ class NewUserMetadataViewModel : ViewModel() {
|
|||||||
) {
|
) {
|
||||||
accountViewModel.launchSigner {
|
accountViewModel.launchSigner {
|
||||||
upload(
|
upload(
|
||||||
uri,
|
galleryUri = uri,
|
||||||
context,
|
context = context,
|
||||||
onUploading = { isUploadingImageForPicture = it },
|
|
||||||
onUploaded = { picture.value = it },
|
|
||||||
onError = onError,
|
onError = onError,
|
||||||
)
|
)?.let {
|
||||||
|
picture.value = it
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,12 +159,12 @@ class NewUserMetadataViewModel : ViewModel() {
|
|||||||
) {
|
) {
|
||||||
accountViewModel.launchSigner {
|
accountViewModel.launchSigner {
|
||||||
upload(
|
upload(
|
||||||
uri,
|
galleryUri = uri,
|
||||||
context,
|
context = context,
|
||||||
onUploading = { isUploadingImageForBanner = it },
|
|
||||||
onUploaded = { banner.value = it },
|
|
||||||
onError = onError,
|
onError = onError,
|
||||||
)
|
)?.let {
|
||||||
|
banner.value = it
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,30 +176,27 @@ class NewUserMetadataViewModel : ViewModel() {
|
|||||||
load()
|
load()
|
||||||
accountViewModel.launchSigner {
|
accountViewModel.launchSigner {
|
||||||
upload(
|
upload(
|
||||||
uri,
|
galleryUri = uri,
|
||||||
context,
|
context = context,
|
||||||
onUploading = { isUploadingImageForPicture = it },
|
|
||||||
onUploaded = {
|
|
||||||
picture.value = it
|
|
||||||
create()
|
|
||||||
},
|
|
||||||
onError = onError,
|
onError = onError,
|
||||||
)
|
)?.let {
|
||||||
|
picture.value = it
|
||||||
|
}
|
||||||
|
|
||||||
|
create()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun upload(
|
private suspend fun upload(
|
||||||
galleryUri: SelectedMedia,
|
galleryUri: SelectedMedia,
|
||||||
context: Context,
|
context: Context,
|
||||||
onUploading: (Boolean) -> Unit,
|
|
||||||
onUploaded: (String) -> Unit,
|
|
||||||
onError: (String, String) -> Unit,
|
onError: (String, String) -> Unit,
|
||||||
) {
|
): String? {
|
||||||
onUploading(true)
|
isUploadingImageForPicture = true
|
||||||
|
|
||||||
val compResult = MediaCompressor().compress(galleryUri.uri, galleryUri.mimeType, CompressorQuality.MEDIUM, context.applicationContext)
|
val compResult = MediaCompressor().compress(galleryUri.uri, galleryUri.mimeType, CompressorQuality.MEDIUM, context.applicationContext)
|
||||||
|
|
||||||
try {
|
return try {
|
||||||
val result =
|
val result =
|
||||||
if (account.settings.defaultFileServer.type == ServerType.NIP96) {
|
if (account.settings.defaultFileServer.type == ServerType.NIP96) {
|
||||||
Nip96Uploader().upload(
|
Nip96Uploader().upload(
|
||||||
@@ -229,20 +225,17 @@ class NewUserMetadataViewModel : ViewModel() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.url != null) {
|
if (result.url == null) {
|
||||||
onUploading(false)
|
|
||||||
onUploaded(result.url)
|
|
||||||
} else {
|
|
||||||
onUploading(false)
|
|
||||||
onError(stringRes(context, R.string.failed_to_upload_media_no_details), stringRes(context, R.string.server_did_not_provide_a_url_after_uploading))
|
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)
|
result.url
|
||||||
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))
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
onUploading(false)
|
|
||||||
onError(stringRes(context, R.string.failed_to_upload_media_no_details), e.message ?: e.javaClass.simpleName)
|
onError(stringRes(context, R.string.failed_to_upload_media_no_details), e.message ?: e.javaClass.simpleName)
|
||||||
|
null
|
||||||
|
} finally {
|
||||||
|
isUploadingImageForPicture = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -191,3 +191,7 @@ class RelayUrlNormalizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun String.normalizeRelayUrl() = RelayUrlNormalizer.normalize(this)
|
||||||
|
|
||||||
|
fun String.normalizeRelayUrlOrNull() = RelayUrlNormalizer.normalizeOrNull(this)
|
||||||
|
|||||||
+3
-3
@@ -23,7 +23,7 @@ package com.vitorpamplona.quartz.nip01Core.relay
|
|||||||
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
|
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.sendAndWaitForResponse
|
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.nip01Core.signers.NostrSignerInternal
|
||||||
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -48,13 +48,13 @@ class NostrClientSendAndWaitTest : BaseNostrClientTest() {
|
|||||||
val resultDamus =
|
val resultDamus =
|
||||||
client.sendAndWaitForResponse(
|
client.sendAndWaitForResponse(
|
||||||
event = event,
|
event = event,
|
||||||
relayList = setOf(RelayUrlNormalizer.normalize("wss://relay.damus.io")),
|
relayList = setOf("wss://relay.damus.io".normalizeRelayUrl()),
|
||||||
)
|
)
|
||||||
|
|
||||||
val resultNos =
|
val resultNos =
|
||||||
client.sendAndWaitForResponse(
|
client.sendAndWaitForResponse(
|
||||||
event = event,
|
event = event,
|
||||||
relayList = setOf(RelayUrlNormalizer.normalize("wss://nos.lol")),
|
relayList = setOf("wss://nos.lol".normalizeRelayUrl()),
|
||||||
)
|
)
|
||||||
|
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user