Skip to content

Commit

Permalink
Refactor and add tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Sep 28, 2022
1 parent 188fbe0 commit c50d20e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ import io.github.droidkaigi.confsched2022.feature.map.MapNavGraph
import io.github.droidkaigi.confsched2022.feature.map.mapGraph
import io.github.droidkaigi.confsched2022.feature.sessions.SessionsNavGraph
import io.github.droidkaigi.confsched2022.feature.sessions.sessionsNavGraph
import io.github.droidkaigi.confsched2022.feature.setting.AppUiModel
import io.github.droidkaigi.confsched2022.feature.setting.KaigiAppViewModel
import io.github.droidkaigi.confsched2022.feature.setting.SettingNavGraph
import io.github.droidkaigi.confsched2022.feature.setting.SharedSettingUiModel
import io.github.droidkaigi.confsched2022.feature.setting.SharedSettingViewModel
import io.github.droidkaigi.confsched2022.feature.setting.settingNavGraph
import io.github.droidkaigi.confsched2022.feature.sponsors.SponsorsNavGraph
import io.github.droidkaigi.confsched2022.feature.sponsors.sponsorsNavGraph
Expand All @@ -95,12 +95,12 @@ import kotlinx.coroutines.launch
@Composable
fun KaigiApp(
windowSizeClass: WindowSizeClass,
sharedSettingViewModel: SharedSettingViewModel = hiltViewModel(),
kaigiAppViewModel: KaigiAppViewModel = hiltViewModel(),
kaigiAppScaffoldState: KaigiAppScaffoldState = rememberKaigiAppScaffoldState(),
kaigiExternalNavigationController: KaigiExternalNavigationController =
rememberKaigiExternalNavigationController(),
) {
val sharedSettingState: SharedSettingUiModel by sharedSettingViewModel.uiModel
val sharedSettingState: AppUiModel by kaigiAppViewModel.uiModel

KaigiTheme(isDynamicColorEnabled = sharedSettingState.isDynamicColorEnabled) {
val usePersistentNavigationDrawer = windowSizeClass.usePersistentNavigationDrawer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.github.droidkaigi.confsched2022.data

import com.russhwolf.settings.coroutines.FlowSettings
import io.github.droidkaigi.confsched2022.model.DroidKaigi2022Day
import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.datetime.Clock

public class SettingsDatastore(private val flowSettings: FlowSettings) {

Expand Down Expand Up @@ -62,7 +64,11 @@ public class SettingsDatastore(private val flowSettings: FlowSettings) {
}

public fun dynamicColorEnabled(): Flow<Boolean> {
return flowSettings.getBooleanFlow(KEY_DYNAMIC_COLOR, false)
return flowSettings.getBooleanFlow(
key = KEY_DYNAMIC_COLOR,
// Add a trick
defaultValue = DroidKaigi2022Day.Day1.start < Clock.System.now()
)
}

public suspend fun setDynamicColorEnabled(dynamicColorEnabled: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.github.droidkaigi.confsched2022.feature.setting

data class SharedSettingUiModel(
data class AppUiModel(
val isDynamicColorEnabled: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,29 @@ import io.github.droidkaigi.confsched2022.model.DynamicColorSettingRepository
import io.github.droidkaigi.confsched2022.ui.moleculeComposeState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SharedSettingViewModel @Inject constructor(
class KaigiAppViewModel @Inject constructor(
private val dynamicColorSettingRepository: DynamicColorSettingRepository,
) : ViewModel() {
private val moleculeScope =
CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)

val uiModel: State<SharedSettingUiModel>
private val dynamicColorEnabledFlow: Flow<Boolean> =
dynamicColorSettingRepository.dynamicEnabledFlow()

init {
uiModel = moleculeScope.moleculeComposeState(clock = ContextClock) {
val dynamicColorEnabled
by dynamicColorEnabledFlow.collectAsState(initial = isSupportedDynamicColor())
SharedSettingUiModel(isDynamicColorEnabled = dynamicColorEnabled)
}
val uiModel: State<AppUiModel> = moleculeScope.moleculeComposeState(clock = ContextClock) {
val dynamicColorSettingEnabled by dynamicColorEnabledFlow.collectAsState(
initial = false
)
AppUiModel(isDynamicColorEnabled = dynamicColorSettingEnabled && isSupportedDynamicColor())
}

fun onDynamicColorToggle() {
fun onDynamicColorToggle(isDynamic: Boolean) {
viewModelScope.launch {
dynamicColorSettingRepository.setDynamicColorEnabled(!dynamicColorEnabledFlow.first())
dynamicColorSettingRepository.setDynamicColorEnabled(isDynamic)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ import io.github.droidkaigi.confsched2022.strings.Strings

@Composable
fun SettingScreenRoot(
viewModel: SharedSettingViewModel =
viewModel: KaigiAppViewModel =
hiltViewModel(viewModelStoreOwner = LocalContext.current as AppCompatActivity),
showNavigationIcon: Boolean = true,
onNavigationIconClick: () -> Unit = {}
) {
val state: SharedSettingUiModel by viewModel.uiModel
val state: AppUiModel by viewModel.uiModel

Setting(
sharedSettingUiModel = state,
appUiModel = state,
showNavigationIcon = showNavigationIcon,
onNavigationIconClick = onNavigationIconClick,
onDynamicToggleClick = viewModel::onDynamicColorToggle,
onDynamicColorToggle = viewModel::onDynamicColorToggle,
)
}

@Composable
fun Setting(
sharedSettingUiModel: SharedSettingUiModel,
appUiModel: AppUiModel,
showNavigationIcon: Boolean,
onNavigationIconClick: () -> Unit,
onDynamicToggleClick: () -> Unit,
onDynamicColorToggle: (Boolean) -> Unit,
modifier: Modifier = Modifier,
) {
KaigiScaffold(
Expand All @@ -90,8 +90,8 @@ fun Setting(
LanguageSetting()
if (VERSION.SDK_INT >= VERSION_CODES.S) {
DynamicColorSetting(
isDynamicColorEnabled = sharedSettingUiModel.isDynamicColorEnabled,
onDynamicToggleClick = onDynamicToggleClick,
isDynamicColorEnabled = appUiModel.isDynamicColorEnabled,
onDynamicColorToggle = onDynamicColorToggle,
)
}
}
Expand Down Expand Up @@ -185,7 +185,7 @@ private fun LanguageSelector(
@Composable
private fun DynamicColorSetting(
isDynamicColorEnabled: Boolean,
onDynamicToggleClick: () -> Unit,
onDynamicColorToggle: (Boolean) -> Unit,
modifier: Modifier = Modifier,
) {
Row(
Expand All @@ -203,7 +203,7 @@ private fun DynamicColorSetting(
Switch(
checked = isDynamicColorEnabled,
onCheckedChange = {
onDynamicToggleClick()
onDynamicColorToggle(it)
},
)
}
Expand Down

0 comments on commit c50d20e

Please sign in to comment.