fix: share image as file (#1126)

This commit is contained in:
Christopher 2024-07-14 01:30:10 -04:00 committed by GitHub
parent d797e05954
commit 3debfe77c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,9 +11,9 @@ import com.github.diegoberaldin.raccoonforlemmy.core.utils.gallery.GalleryHelper
import com.github.diegoberaldin.raccoonforlemmy.core.utils.gallery.download import com.github.diegoberaldin.raccoonforlemmy.core.utils.gallery.download
import com.github.diegoberaldin.raccoonforlemmy.core.utils.imagepreload.ImagePreloadManager import com.github.diegoberaldin.raccoonforlemmy.core.utils.imagepreload.ImagePreloadManager
import com.github.diegoberaldin.raccoonforlemmy.core.utils.share.ShareHelper import com.github.diegoberaldin.raccoonforlemmy.core.utils.share.ShareHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO import kotlinx.coroutines.IO
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -44,7 +44,7 @@ class ZoomableImageViewModel(
is NotificationCenterEvent.ShareImageModeSelected.ModeFile -> shareFile(event.url, event.source) is NotificationCenterEvent.ShareImageModeSelected.ModeFile -> shareFile(event.url, event.source)
is NotificationCenterEvent.ShareImageModeSelected.ModeUrl -> shareUrl(event.url) is NotificationCenterEvent.ShareImageModeSelected.ModeUrl -> shareUrl(event.url)
} }
}.launchIn(this) }.launchIn(CoroutineScope(Dispatchers.IO))
} }
} }
@ -103,16 +103,14 @@ class ZoomableImageViewModel(
val idx = s.lastIndexOf(".").takeIf { it >= 0 } ?: s.length val idx = s.lastIndexOf(".").takeIf { it >= 0 } ?: s.length
s.substring(idx).takeIf { it.isNotEmpty() } ?: ".jpeg" s.substring(idx).takeIf { it.isNotEmpty() } ?: ".jpeg"
} }
val path =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
galleryHelper.saveToGallery( val path = galleryHelper.saveToGallery(
bytes = bytes, bytes = bytes,
name = "${epochMillis()}$extension", name = "${epochMillis()}$extension",
additionalPathSegment = folder.takeIf { imageSourcePath }, additionalPathSegment = folder.takeIf { imageSourcePath },
) )
}
// if done too early no image is found withContext(Dispatchers.Main) {
delay(750)
updateState { it.copy(loading = false) } updateState { it.copy(loading = false) }
if (path != null) { if (path != null) {
@ -120,6 +118,8 @@ class ZoomableImageViewModel(
} else { } else {
emitEffect(ZoomableImageMviModel.Effect.ShareFailure) emitEffect(ZoomableImageMviModel.Effect.ShareFailure)
} }
}
}
} catch (e: Throwable) { } catch (e: Throwable) {
e.printStackTrace() e.printStackTrace()
updateState { it.copy(loading = false) } updateState { it.copy(loading = false) }