From bd692be52544afba4d9482886e28e660fd671062 Mon Sep 17 00:00:00 2001 From: Harald Gutsche Date: Wed, 16 Nov 2022 03:50:12 +0100 Subject: [PATCH] use a typealias to allow switching MutableComposable*Flow implementations, use MutableComposableStateFlow (it does not need to start the game on freshStart) --- .../backup/activities/MainActivityX.kt | 10 +++++++--- .../backup/ui/compose/extensions.kt | 20 ++++++++++--------- .../backup/viewmodels/AppSheetViewModel.kt | 10 +++------- .../backup/viewmodels/MainViewModel.kt | 16 +++++---------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/machiav3lli/backup/activities/MainActivityX.kt b/app/src/main/java/com/machiav3lli/backup/activities/MainActivityX.kt index c86d947dcb..0983940675 100644 --- a/app/src/main/java/com/machiav3lli/backup/activities/MainActivityX.kt +++ b/app/src/main/java/com/machiav3lli/backup/activities/MainActivityX.kt @@ -66,6 +66,7 @@ import com.machiav3lli.backup.preferences.pref_catchUncaughtException import com.machiav3lli.backup.preferences.pref_refreshOnStart import com.machiav3lli.backup.tasks.AppActionWork import com.machiav3lli.backup.tasks.FinishWork +import com.machiav3lli.backup.ui.compose.MutableComposableSharedFlow import com.machiav3lli.backup.ui.compose.icons.Phosphor import com.machiav3lli.backup.ui.compose.icons.phosphor.ArrowsClockwise import com.machiav3lli.backup.ui.compose.icons.phosphor.FunnelSimple @@ -201,9 +202,12 @@ class MainActivityX : BaseActivity() { // runs later Timber.w("******************** freshStart LaunchedEffect(viewModel) ********************") //TODO hg42 I guess this shouldn't be necessary, but no better solution to start the flow game, yet - viewModel.searchQuery.value = "" - viewModel.modelSortFilter.value = OABX.context.sortFilterModel - if (pref_refreshOnStart.value) + //TODO indeed it doesn't seem to be necessary with MutableStateFlow under the hood + if (viewModel.searchQuery is MutableComposableSharedFlow<*>) + viewModel.searchQuery.value = "" + if (viewModel.modelSortFilter is MutableComposableSharedFlow<*>) + viewModel.modelSortFilter.value = OABX.context.sortFilterModel + if(pref_refreshOnStart.value) refreshPackages() } } diff --git a/app/src/main/java/com/machiav3lli/backup/ui/compose/extensions.kt b/app/src/main/java/com/machiav3lli/backup/ui/compose/extensions.kt index f925c6e153..a3262c58b2 100644 --- a/app/src/main/java/com/machiav3lli/backup/ui/compose/extensions.kt +++ b/app/src/main/java/com/machiav3lli/backup/ui/compose/extensions.kt @@ -9,8 +9,9 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import timber.log.Timber @@ -44,13 +45,11 @@ fun SelectionContainerX(modifier: Modifier = Modifier, content: @Composable () - class MutableComposableSharedFlow( var initial: T, val scope: CoroutineScope, - val label: String = "ComposableSharedFlow", - val onEach: (T) -> Unit = {} + val label: String = "ComposableSharedFlow" ) { var flow = MutableSharedFlow() var state = flow - .onEach(onEach) .stateIn( scope, SharingStarted.Eagerly, @@ -59,7 +58,7 @@ class MutableComposableSharedFlow( var value: T get() { - var value = state.value + val value = state.value Timber.w("*** $label => $value") return value } @@ -81,21 +80,24 @@ class MutableComposableStateFlow( ) { var flow = MutableStateFlow(initial) - var state = flow + val state = flow.asStateFlow() var value: T get() { - var value = state.value + val value = state.value Timber.w("*** $label => $value") return value } set(value: T) { Timber.w("*** $label <= $value") - initial = value - scope.launch { flow.emit(value) } + //initial = value + scope.launch { flow.update { value } } } init { value = initial } } + +//typealias MutableComposableFlow = MutableComposableSharedFlow +typealias MutableComposableFlow = MutableComposableStateFlow diff --git a/app/src/main/java/com/machiav3lli/backup/viewmodels/AppSheetViewModel.kt b/app/src/main/java/com/machiav3lli/backup/viewmodels/AppSheetViewModel.kt index 186c58e41a..274d672fea 100644 --- a/app/src/main/java/com/machiav3lli/backup/viewmodels/AppSheetViewModel.kt +++ b/app/src/main/java/com/machiav3lli/backup/viewmodels/AppSheetViewModel.kt @@ -31,8 +31,7 @@ import com.machiav3lli.backup.handler.LogsHandler import com.machiav3lli.backup.handler.ShellCommands import com.machiav3lli.backup.handler.showNotification import com.machiav3lli.backup.items.Package -import com.machiav3lli.backup.preferences.pref_traceFlows -import com.machiav3lli.backup.ui.compose.MutableComposableSharedFlow +import com.machiav3lli.backup.ui.compose.MutableComposableFlow import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.SharingStarted @@ -63,14 +62,11 @@ class AppSheetViewModel( AppExtras(app?.packageName ?: "") ) - val snackbarText = MutableComposableSharedFlow( + val snackbarText = MutableComposableFlow( "", viewModelScope, "snackBarText" - ) { - if (pref_traceFlows.value) - Timber.w("*** snackbarText <<- '${it}'") - } + ) private var notificationId: Int = System.currentTimeMillis().toInt() val refreshNow = mutableStateOf(false) diff --git a/app/src/main/java/com/machiav3lli/backup/viewmodels/MainViewModel.kt b/app/src/main/java/com/machiav3lli/backup/viewmodels/MainViewModel.kt index b5036dc993..937444ef82 100755 --- a/app/src/main/java/com/machiav3lli/backup/viewmodels/MainViewModel.kt +++ b/app/src/main/java/com/machiav3lli/backup/viewmodels/MainViewModel.kt @@ -35,7 +35,7 @@ import com.machiav3lli.backup.items.Package import com.machiav3lli.backup.items.Package.Companion.invalidateCacheForPackage import com.machiav3lli.backup.preferences.pref_traceFlows import com.machiav3lli.backup.preferences.pref_usePackageCacheOnUpdate -import com.machiav3lli.backup.ui.compose.MutableComposableSharedFlow +import com.machiav3lli.backup.ui.compose.MutableComposableFlow import com.machiav3lli.backup.utils.TraceUtils.trace import com.machiav3lli.backup.utils.applyFilter import com.machiav3lli.backup.utils.sortFilterModel @@ -148,25 +148,19 @@ class MainViewModel( emptyList() ) - val searchQuery = MutableComposableSharedFlow( + val searchQuery = MutableComposableFlow( //------------------------------------------------------------------------------------------ searchQuery "", viewModelScope, "searchQuery" - ) { - if (pref_traceFlows.value) - Timber.w("*** searchQuery <<- '${it}'") - } + ) - var modelSortFilter = MutableComposableSharedFlow( + var modelSortFilter = MutableComposableFlow( //------------------------------------------------------------------------------------------ modelSortFilter OABX.context.sortFilterModel, viewModelScope, "modelSortFilter" - ) { - if (pref_traceFlows.value) - Timber.w("*** modelSortFilter <<- ${it}") - } + ) val filteredList = combine(notBlockedList, modelSortFilter.flow, searchQuery.flow) { p, f, s -> //========================================================================================== filteredList