Skip to content

Commit

Permalink
Scope view to safe args
Browse files Browse the repository at this point in the history
  • Loading branch information
erikeelde committed Sep 21, 2024
1 parent 13ab8e5 commit 4bf7996
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
21 changes: 13 additions & 8 deletions toggles-app/src/main/java/se/eelde/toggles/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.toRoute
import dagger.hilt.android.AndroidEntryPoint
import se.eelde.toggles.applications.applicationNavigations
import se.eelde.toggles.booleanconfiguration.BooleanValueView
import se.eelde.toggles.composetheme.TogglesTheme
import se.eelde.toggles.configurations.configurationsNavigations
import se.eelde.toggles.dialogs.scope.ScopeValueView
import se.eelde.toggles.dialogs.scope.ScopeViewModel
import se.eelde.toggles.enumconfiguration.EnumValueView
import se.eelde.toggles.enumconfiguration.EnumValueViewModel
import se.eelde.toggles.help.HelpView
Expand All @@ -43,6 +42,7 @@ import se.eelde.toggles.routes.EnumConfiguration
import se.eelde.toggles.routes.Help
import se.eelde.toggles.routes.IntegerConfiguration
import se.eelde.toggles.routes.Oss
import se.eelde.toggles.routes.Scope
import se.eelde.toggles.routes.StringConfiguration
import se.eelde.toggles.stringconfiguration.StringValueView

Expand Down Expand Up @@ -85,19 +85,24 @@ fun Navigation(
navController.navigate(EnumConfiguration(configurationId, scopeId))
},
navigateToScopeView = { applicationId: Long ->
navController.navigate("scopes/$applicationId")
navController.navigate(Scope(applicationId))
}
) { navController.popBackStack() }
composable<BooleanConfiguration> { backStackEntry ->
val booleanConfiguration: BooleanConfiguration = backStackEntry.toRoute()

BooleanValueView(booleanConfiguration) { navController.popBackStack() }
}
composable(
"scopes/{applicationId}",
arguments = listOf(navArgument("applicationId") { type = NavType.LongType })
) {
ScopeValueView { navController.popBackStack() }
composable<Scope> { backStackEntry ->
val scope: Scope = backStackEntry.toRoute()

ScopeValueView(
viewModel = hiltViewModel<ScopeViewModel, ScopeViewModel.Factory>(
creationCallback = { factory ->
factory.create(scope)
}
)
) { navController.popBackStack() }
}
composable<IntegerConfiguration> { backStackEntry ->
val integerConfiguration: IntegerConfiguration = backStackEntry.toRoute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import se.eelde.toggles.R
import se.eelde.toggles.database.WrenchScope

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScopeValueView(
viewModel: ScopeViewModel,
modifier: Modifier = Modifier,
viewModel: ScopeViewModel = hiltViewModel(),
back: () -> Unit
) {
val uiState = viewModel.state.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package se.eelde.toggles.dialogs.scope

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -11,8 +13,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import se.eelde.toggles.database.WrenchScope
import se.eelde.toggles.database.dao.application.TogglesScopeDao
import se.eelde.toggles.routes.Scope
import java.util.Date
import javax.inject.Inject

internal data class ViewState(
val title: String? = null,
Expand All @@ -30,13 +32,20 @@ private sealed class PartialViewState {
object Reverting : PartialViewState()
}

@HiltViewModel
class ScopeViewModel @Inject internal constructor(
private val savedStateHandle: SavedStateHandle,
private val scopeDao: TogglesScopeDao
@HiltViewModel(assistedFactory = ScopeViewModel.Factory::class)
class ScopeViewModel @AssistedInject internal constructor(
private val scopeDao: TogglesScopeDao,
@Assisted scope: Scope,
) : ViewModel() {

private val applicationId: Long = savedStateHandle.get<Long>("applicationId")!!
@AssistedFactory
interface Factory {
fun create(
scope: Scope
): ScopeViewModel
}

private val applicationId: Long = scope.applicationId

private val _state = MutableStateFlow(reduce(ViewState(), PartialViewState.Empty))

Expand Down

0 comments on commit 4bf7996

Please sign in to comment.