Skip to content

Commit

Permalink
Enum value 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 0083b48 commit 13ab8e5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fun BooleanValueView(

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Suppress("LongParameterList")
@Suppress("LongParameterList", "LongMethod")
fun BooleanValueView(
viewState: ViewState,
save: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import kotlinx.coroutines.launch
@Composable
fun EnumValueView(
modifier: Modifier = Modifier,
viewModel: FragmentEnumValueViewModel = hiltViewModel(),
viewModel: EnumValueViewModel = hiltViewModel(),
back: () -> Unit,
) {
val viewState by viewModel.state.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package se.eelde.toggles.enumconfiguration

import android.app.Application
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.Job
Expand All @@ -19,8 +21,8 @@ import se.eelde.toggles.database.dao.application.TogglesConfigurationValueDao
import se.eelde.toggles.database.dao.application.TogglesPredefinedConfigurationValueDao
import se.eelde.toggles.provider.notifyInsert
import se.eelde.toggles.provider.notifyUpdate
import se.eelde.toggles.routes.EnumConfiguration
import java.util.Date
import javax.inject.Inject

data class ViewState(
val title: String? = null,
Expand All @@ -43,22 +45,29 @@ internal sealed class PartialViewState {
data object Reverting : PartialViewState()
}

@HiltViewModel
class FragmentEnumValueViewModel @Inject internal constructor(
private val savedStateHandle: SavedStateHandle,
@HiltViewModel(assistedFactory = EnumValueViewModel.Factory::class)
class EnumValueViewModel @AssistedInject internal constructor(
private val application: Application,
private val configurationDao: TogglesConfigurationDao,
private val configurationValueDao: TogglesConfigurationValueDao,
private val predefinedConfigurationValueDao: TogglesPredefinedConfigurationValueDao
private val predefinedConfigurationValueDao: TogglesPredefinedConfigurationValueDao,
@Assisted enumConfiguration: EnumConfiguration,
) : ViewModel() {

@AssistedFactory
interface Factory {
fun create(
enumConfiguration: EnumConfiguration
): EnumValueViewModel
}

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

val state: StateFlow<ViewState>
get() = _state

private val configurationId: Long = savedStateHandle.get<Long>("configurationId")!!
private val scopeId: Long = savedStateHandle.get<Long>("scopeId")!!
private val configurationId: Long = enumConfiguration.configurationId
private val scopeId: Long = enumConfiguration.scopeId

private var selectedConfigurationValue: WrenchConfigurationValue? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import kotlinx.coroutines.launch

@Preview
@Composable
private fun IntegerValueViewPreview() {
internal fun IntegerValueViewPreview() {
IntegerValueView(
uiState = ViewState(
title = "Integer value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ data class StringConfiguration(val configurationId: Long, val scopeId: Long)

@Serializable
data class Scope(val applicationId: Long)

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import se.eelde.toggles.routes.StringConfiguration

@Preview
@Composable
private fun StringValueViewPreview() {
internal fun StringValueViewPreview() {
TogglesTheme {
StringValueView(
viewState = ViewState(title = "The title", stringValue = "This is value"),
Expand Down
22 changes: 13 additions & 9 deletions toggles-app/src/main/java/se/eelde/toggles/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import se.eelde.toggles.composetheme.TogglesTheme
import se.eelde.toggles.configurations.configurationsNavigations
import se.eelde.toggles.dialogs.scope.ScopeValueView
import se.eelde.toggles.enumconfiguration.EnumValueView
import se.eelde.toggles.enumconfiguration.EnumValueViewModel
import se.eelde.toggles.help.HelpView
import se.eelde.toggles.integerconfiguration.IntegerValueView
import se.eelde.toggles.integerconfiguration.IntegerValueViewModel
import se.eelde.toggles.oss.OssView
import se.eelde.toggles.routes.Applications
import se.eelde.toggles.routes.BooleanConfiguration
import se.eelde.toggles.routes.Configurations
import se.eelde.toggles.routes.EnumConfiguration
import se.eelde.toggles.routes.Help
import se.eelde.toggles.routes.IntegerConfiguration
import se.eelde.toggles.routes.Oss
Expand Down Expand Up @@ -80,7 +82,7 @@ fun Navigation(
)
},
navigateToEnumConfiguration = { scopeId: Long, configurationId: Long ->
navController.navigate("configuration/$configurationId/$scopeId/enum")
navController.navigate(EnumConfiguration(configurationId, scopeId))
},
navigateToScopeView = { applicationId: Long ->
navController.navigate("scopes/$applicationId")
Expand Down Expand Up @@ -112,14 +114,16 @@ fun Navigation(

StringValueView(stringConfiguration) { navController.popBackStack() }
}
composable(
"configuration/{configurationId}/{scopeId}/enum",
arguments = listOf(
navArgument("configurationId") { type = NavType.LongType },
navArgument("scopeId") { type = NavType.LongType }
)
) {
EnumValueView { navController.popBackStack() }
composable<EnumConfiguration> { backStackEntry ->
val enumConfiguration: EnumConfiguration = backStackEntry.toRoute()

EnumValueView(
viewModel = hiltViewModel<EnumValueViewModel, EnumValueViewModel.Factory>(
creationCallback = { factory ->
factory.create(enumConfiguration)
}
)
) { navController.popBackStack() }
}
composable<Oss> {
Scaffold(
Expand Down

0 comments on commit 13ab8e5

Please sign in to comment.