Skip to content

Commit

Permalink
use a typealias to allow switching MutableComposable*Flow implementat…
Browse files Browse the repository at this point in the history
…ions, use MutableComposableStateFlow (it does not need to start the game on freshStart)
  • Loading branch information
hg42 committed Nov 16, 2022
1 parent 9a733ed commit bd692be
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/com/machiav3lli/backup/ui/compose/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -44,13 +45,11 @@ fun SelectionContainerX(modifier: Modifier = Modifier, content: @Composable () -
class MutableComposableSharedFlow<T>(
var initial: T,
val scope: CoroutineScope,
val label: String = "ComposableSharedFlow",
val onEach: (T) -> Unit = {}
val label: String = "ComposableSharedFlow"
) {
var flow = MutableSharedFlow<T>()

var state = flow
.onEach(onEach)
.stateIn(
scope,
SharingStarted.Eagerly,
Expand All @@ -59,7 +58,7 @@ class MutableComposableSharedFlow<T>(

var value: T
get() {
var value = state.value
val value = state.value
Timber.w("*** $label => $value")
return value
}
Expand All @@ -81,21 +80,24 @@ class MutableComposableStateFlow<T>(
) {
var flow = MutableStateFlow<T>(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<T> = MutableComposableSharedFlow<T>
typealias MutableComposableFlow<T> = MutableComposableStateFlow<T>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bd692be

Please sign in to comment.