From 2b5f39f7e0630ab843fb56c309b2b5a40ce9660f Mon Sep 17 00:00:00 2001 From: SanmerDev Date: Sat, 9 Mar 2024 22:45:47 +0800 Subject: [PATCH] Optimize `InstallScreen` --- .../sanmer/mrepo/ui/activity/InstallScreen.kt | 26 ++++--------------- .../mrepo/viewmodel/InstallViewModel.kt | 7 +++-- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallScreen.kt b/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallScreen.kt index c9a9dbaf..3474567f 100644 --- a/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallScreen.kt +++ b/app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallScreen.kt @@ -9,9 +9,6 @@ import androidx.compose.animation.scaleIn import androidx.compose.animation.scaleOut import androidx.compose.foundation.focusable import androidx.compose.foundation.horizontalScroll -import androidx.compose.foundation.interaction.MutableInteractionSource -import androidx.compose.foundation.interaction.PressInteraction -import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn @@ -56,7 +53,6 @@ import com.sanmer.mrepo.app.Event.Companion.isLoading import com.sanmer.mrepo.app.Event.Companion.isSucceeded import com.sanmer.mrepo.ui.component.NavigateUpTopBar import com.sanmer.mrepo.ui.utils.isScrollingUp -import com.sanmer.mrepo.ui.utils.none import com.sanmer.mrepo.utils.Utils import com.sanmer.mrepo.viewmodel.InstallViewModel import kotlinx.coroutines.launch @@ -88,14 +84,13 @@ fun InstallScreen( ) val context = LocalContext.current - val interactionSource = remember { MutableInteractionSource() } val launcher = rememberLauncherForActivityResult( ActivityResultContracts.CreateDocument("*/*") ) { uri -> if (uri == null) return@rememberLauncherForActivityResult scope.launch { - viewModel.saveLog(context, uri) + viewModel.writeLogsTo(context, uri) .onSuccess { val message = context.getString(R.string.install_logs_saved) snackbarHostState.showSnackbar( @@ -115,21 +110,12 @@ fun InstallScreen( } } - LaunchedEffect(interactionSource) { - interactionSource.interactions.collect { interaction -> - if (interaction is PressInteraction.Release) { - launcher.launch(viewModel.logfile) - } - } - } - Scaffold( modifier = Modifier .onKeyEvent { when (it.key) { Key.VolumeUp, Key.VolumeDown -> viewModel.event.isLoading - else -> false } } @@ -138,8 +124,8 @@ fun InstallScreen( .nestedScroll(scrollBehavior.nestedScrollConnection), topBar = { TopBar( + exportLog = { launcher.launch(viewModel.logfile) }, event = viewModel.event, - interactionSource = interactionSource, scrollBehavior = scrollBehavior ) }, @@ -158,8 +144,7 @@ fun InstallScreen( FloatingButton() } }, - snackbarHost = { SnackbarHost(snackbarHostState) }, - contentWindowInsets = WindowInsets.none + snackbarHost = { SnackbarHost(snackbarHostState) } ) { Console( list = viewModel.console.asReversed(), @@ -196,8 +181,8 @@ private fun Console( @Composable private fun TopBar( + exportLog: () -> Unit, event: Event, - interactionSource: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior ) = NavigateUpTopBar( title = stringResource(id = R.string.install_screen_title), @@ -211,8 +196,7 @@ private fun TopBar( actions = { if (event.isFinished) { IconButton( - interactionSource = interactionSource, - onClick = {} + onClick = exportLog ) { Icon( painter = painterResource(id = R.drawable.device_floppy), diff --git a/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt b/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt index 054405f3..851592fd 100644 --- a/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt +++ b/app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt @@ -12,16 +12,15 @@ import com.sanmer.mrepo.app.Event import com.sanmer.mrepo.provider.ProviderCompat import com.sanmer.mrepo.repository.ModulesRepository import com.sanmer.mrepo.repository.UserPreferencesRepository -import com.sanmer.mrepo.utils.extensions.now import dagger.hilt.android.lifecycle.HiltViewModel import dev.sanmer.mrepo.compat.stub.IInstallCallback import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import kotlinx.datetime.LocalDateTime import timber.log.Timber import java.io.File +import java.time.LocalDateTime import javax.inject.Inject @HiltViewModel @@ -34,13 +33,13 @@ class InstallViewModel @Inject constructor( var event by mutableStateOf(Event.LOADING) private set - val logfile get() = "module_install_log_${LocalDateTime.now()}.log" + val logfile get() = "Install_${LocalDateTime.now()}.log" init { Timber.d("InstallViewModel init") } - suspend fun saveLog(context: Context, uri: Uri) = withContext(Dispatchers.IO) { + suspend fun writeLogsTo(context: Context, uri: Uri) = withContext(Dispatchers.IO) { runCatching { val cr = context.contentResolver cr.openOutputStream(uri)?.use {