Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Optimize InstallScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Mar 9, 2024
1 parent d7ff762 commit 2b5f39f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
26 changes: 5 additions & 21 deletions app/src/main/kotlin/com/sanmer/mrepo/ui/activity/InstallScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
}
}
Expand All @@ -138,8 +124,8 @@ fun InstallScreen(
.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
TopBar(
exportLog = { launcher.launch(viewModel.logfile) },
event = viewModel.event,
interactionSource = interactionSource,
scrollBehavior = scrollBehavior
)
},
Expand All @@ -158,8 +144,7 @@ fun InstallScreen(
FloatingButton()
}
},
snackbarHost = { SnackbarHost(snackbarHostState) },
contentWindowInsets = WindowInsets.none
snackbarHost = { SnackbarHost(snackbarHostState) }
) {
Console(
list = viewModel.console.asReversed(),
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 2b5f39f

Please sign in to comment.