mirror of
https://github.com/lone-cloud/prism-android
synced 2026-06-03 19:54:44 -07:00
Simplify viewModel: we always need MainViewModel
This commit is contained in:
parent
dbd3e8e654
commit
92eed71b56
2 changed files with 14 additions and 59 deletions
|
|
@ -12,11 +12,9 @@ import androidx.compose.material3.ExperimentalMaterial3Api
|
|||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
|
|
@ -24,13 +22,14 @@ import androidx.navigation.compose.NavHost
|
|||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import org.unifiedpush.android.distributor.ui.R as LibR
|
||||
import org.unifiedpush.android.distributor.ui.compose.AppBar
|
||||
import org.unifiedpush.distributor.sunup.R
|
||||
import org.unifiedpush.distributor.sunup.activities.DistribMigrationViewModel
|
||||
import org.unifiedpush.distributor.sunup.activities.MainViewModel
|
||||
import org.unifiedpush.distributor.sunup.activities.PreviewFactory
|
||||
import org.unifiedpush.distributor.sunup.activities.SettingsViewModel
|
||||
import org.unifiedpush.android.distributor.ui.R as LibR
|
||||
import org.unifiedpush.distributor.sunup.activities.ThemeViewModel
|
||||
|
||||
enum class AppScreen(@StringRes val title: Int) {
|
||||
Main(R.string.app_name),
|
||||
|
|
@ -51,23 +50,6 @@ private fun DefaultTopBar(
|
|||
modifier
|
||||
)
|
||||
|
||||
@Composable
|
||||
inline fun <reified VM : ViewModel> sharedViewModel(
|
||||
navController: NavHostController,
|
||||
screen: AppScreen,
|
||||
factory: ViewModelProvider.Factory
|
||||
): VM? {
|
||||
val currentBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
return if (currentBackStackEntry?.destination?.route == screen.name) {
|
||||
val parentEntry = remember(currentBackStackEntry) {
|
||||
navController.getBackStackEntry(screen.name)
|
||||
}
|
||||
viewModel(parentEntry, factory = factory)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun App(
|
||||
factory: ViewModelProvider.Factory,
|
||||
|
|
@ -80,20 +62,18 @@ fun App(
|
|||
)
|
||||
// shared with all views, no need to scope it
|
||||
val migrationViewModel = viewModel<DistribMigrationViewModel>(factory = factory)
|
||||
val mainViewModel = viewModel<MainViewModel>(factory = factory)
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
when (currentScreen) {
|
||||
AppScreen.Main -> {
|
||||
sharedViewModel<MainViewModel>(
|
||||
navController,
|
||||
currentScreen,
|
||||
factory
|
||||
)?.let {
|
||||
MainAppBarOrSelection(it, onGoToSettings = {
|
||||
MainAppBarOrSelection(
|
||||
mainViewModel,
|
||||
onGoToSettings = {
|
||||
navController.navigate(AppScreen.Settings.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
else -> null
|
||||
} ?: DefaultTopBar(
|
||||
|
|
@ -113,14 +93,14 @@ fun App(
|
|||
.padding(innerPadding)
|
||||
) {
|
||||
composable(route = AppScreen.Main.name) {
|
||||
sharedViewModel<MainViewModel>(navController, currentScreen, factory)?.let {
|
||||
MainScreen(it, migrationViewModel)
|
||||
}
|
||||
MainScreen(
|
||||
mainViewModel,
|
||||
migrationViewModel
|
||||
)
|
||||
}
|
||||
composable(route = AppScreen.Settings.name) {
|
||||
sharedViewModel<SettingsViewModel>(navController, currentScreen, factory)?.let {
|
||||
SettingsScreen(it, themeViewModel, migrationViewModel)
|
||||
}
|
||||
val vm = viewModel<SettingsViewModel>(factory = factory)
|
||||
SettingsScreen(vm, themeViewModel, migrationViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
package org.unifiedpush.distributor.sunup.activities.ui
|
||||
|
||||
import android.app.Application
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.unifiedpush.distributor.sunup.AppStore
|
||||
|
||||
class ThemeViewModel(val application: Application? = null): ViewModel() {
|
||||
var dynamicColors by mutableStateOf(
|
||||
application?.let {AppStore(it).dynamicColors} ?: false
|
||||
)
|
||||
|
||||
fun toggleDynamicColors() {
|
||||
viewModelScope.launch {
|
||||
dynamicColors = !dynamicColors
|
||||
application?.run {
|
||||
AppStore(this).dynamicColors = dynamicColors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue