From 9997d3e86df2d057ab7b332ff887303fe319d457 Mon Sep 17 00:00:00 2001 From: SkyD666 Date: Mon, 20 Nov 2023 22:39:42 +0800 Subject: [PATCH] [optimize] Show waiting dialog before sharing multiple stickers; Optimize code --- app/build.gradle | 4 ++-- .../ui/screen/home/searchbar/SearchResultList.kt | 12 ++++++++++-- app/src/main/java/com/skyd/rays/util/StickerUtil.kt | 13 ++++++------- .../skyd/rays/util/image/format/FormatStandard.kt | 4 ++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0a55d85..a57ff6c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -21,8 +21,8 @@ android { applicationId "com.skyd.rays" minSdk 24 targetSdk 34 - versionCode 45 - versionName "1.6-rc04" + versionCode 46 + versionName "1.6-rc05" flavorDimensions = ["versionName"] testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/com/skyd/rays/ui/screen/home/searchbar/SearchResultList.kt b/app/src/main/java/com/skyd/rays/ui/screen/home/searchbar/SearchResultList.kt index ee47bb3..ebbc55f 100644 --- a/app/src/main/java/com/skyd/rays/ui/screen/home/searchbar/SearchResultList.kt +++ b/app/src/main/java/com/skyd/rays/ui/screen/home/searchbar/SearchResultList.kt @@ -47,7 +47,6 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.SuggestionChip import androidx.compose.material3.Text import androidx.compose.material3.VerticalDivider -import androidx.compose.material3.minimumInteractiveComponentSize import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -73,6 +72,7 @@ import com.skyd.rays.ui.component.AnimatedPlaceholder import com.skyd.rays.ui.component.RaysFloatingActionButton import com.skyd.rays.ui.component.RaysIconButton import com.skyd.rays.ui.component.RaysImage +import com.skyd.rays.ui.component.dialog.WaitingDialog import com.skyd.rays.ui.local.LocalSearchResultReverse import com.skyd.rays.ui.local.LocalSearchResultSort import com.skyd.rays.ui.local.LocalWindowSizeClass @@ -268,14 +268,20 @@ internal fun MultiSelectActionBar( ) { val context = LocalContext.current val windowSizeClass = LocalWindowSizeClass.current + var showWaitingDialog by rememberSaveable { mutableStateOf(false) } + val items = remember { arrayOf<@Composable () -> Unit>( @Composable { RaysIconButton( onClick = { + showWaitingDialog = true context.sendStickersByUuids( uuids = selectedStickers.map { it.sticker.uuid }, - onSuccess = { selectedStickers.forEach { it.sticker.shareCount++ } } + onSuccess = { + selectedStickers.forEach { it.sticker.shareCount++ } + showWaitingDialog = false + } ) }, enabled = selectedStickers.isNotEmpty(), @@ -310,6 +316,8 @@ internal fun MultiSelectActionBar( items.forEachIndexed { _, function -> function() } } } + + WaitingDialog(visible = showWaitingDialog) } @Composable diff --git a/app/src/main/java/com/skyd/rays/util/StickerUtil.kt b/app/src/main/java/com/skyd/rays/util/StickerUtil.kt index 400f815..0b9a69b 100644 --- a/app/src/main/java/com/skyd/rays/util/StickerUtil.kt +++ b/app/src/main/java/com/skyd/rays/util/StickerUtil.kt @@ -128,7 +128,7 @@ fun Context.sendStickersByFiles( topActivityFullName = topActivityFullName, ) } - + }.invokeOnCompletion { onSuccess?.invoke() } } @@ -240,10 +240,9 @@ fun exportSticker(uuid: String, outputDir: Uri) { fun exportStickerToPictures(uri: Uri) { val contentResolver = appContext.contentResolver - val fileData = contentResolver.openInputStream(uri)?.use { inputStream -> - inputStream.readBytes().also { inputStream.close() } - } ?: throw IOException("can not open sticker file") - val extensionName = ImageFormatChecker.check(fileData).toString() + val extensionName = contentResolver.openInputStream(uri)?.use { inputStream -> + ImageFormatChecker.check(inputStream).toString() + } ?: throw IOException("Can not open sticker file") val filename = uri.lastPathSegment + "_" + Random.nextInt(0, Int.MAX_VALUE) + extensionName var outputStream: OutputStream @@ -260,10 +259,10 @@ fun exportStickerToPictures(uri: Uri) { contentResolver.also { resolver -> imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) outputStream = imageUri?.let { resolver.openOutputStream(it) } - ?: throw IOException("can not write file") + ?: throw IOException("Can not write file") + resolver.openInputStream(uri)?.use { inputStream -> inputStream.copyTo(outputStream) } } - outputStream.write(fileData) outputStream.close() contentValues.clear() diff --git a/app/src/main/java/com/skyd/rays/util/image/format/FormatStandard.kt b/app/src/main/java/com/skyd/rays/util/image/format/FormatStandard.kt index f6f5421..49cfdf1 100644 --- a/app/src/main/java/com/skyd/rays/util/image/format/FormatStandard.kt +++ b/app/src/main/java/com/skyd/rays/util/image/format/FormatStandard.kt @@ -199,7 +199,7 @@ sealed class FormatStandard( } m.forEach { - if (baseCheck(it, byteArrayOf(tested[8], tested[9], tested[10], tested[11]))) { + if (baseCheck(it, tested.copyOfRange(8, 12))) { return true } } @@ -229,7 +229,7 @@ sealed class FormatStandard( if (tested[i] != he[i - 8]) return false } icIxIsVcVx.forEach { - if (baseCheck(it, byteArrayOf(tested[10], tested[11]))) { + if (baseCheck(it, tested.copyOfRange(10, 12))) { return true } }