Skip to content

Commit

Permalink
Changes dark mode in the main activity based on the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriy-chernysh committed Jan 4, 2025
1 parent e0b909c commit 7181bdf
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dependencies {
exclude(group = "com.google.firebase", module = "firebase-crashlytics")
}

implementation(projects.feature.settingsCore)
implementation(projects.feature.main)
}


Expand Down
29 changes: 27 additions & 2 deletions app/src/main/kotlin/com/mobiledevpro/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@ package com.mobiledevpro.app
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.mobiledevpro.app.di.mainModule
import com.mobiledevpro.app.ui.MainApp
import com.mobiledevpro.di.koinScope
import com.mobiledevpro.main.view.state.MainUIState
import com.mobiledevpro.main.view.vm.MainViewModel
import com.mobiledevpro.ui.theme.AppTheme
import org.koin.compose.KoinContext
import org.koin.core.context.loadKoinModules
import kotlin.getValue

class MainActivity : ComponentActivity() {

private val viewModel: MainViewModel by koinScope<MainActivity>().inject()

init {
loadKoinModules(mainModule)
}

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
Expand All @@ -19,9 +36,17 @@ class MainActivity : ComponentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
// val darkModeState by darkModeState.collectAsStateWithLifecycle()
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

var darkMode by remember { mutableStateOf(true) }

if (uiState is MainUIState.Success) {
(uiState as MainUIState.Success).settings.let {
darkMode = it.darkMode
}
}

AppTheme(darkTheme = true) {
AppTheme(darkTheme = darkMode) {
KoinContext {
MainApp()
}
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/kotlin/com/mobiledevpro/app/di/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@
*/
package com.mobiledevpro.app.di

import com.mobiledevpro.app.MainActivity
import com.mobiledevpro.main.view.vm.MainViewModel
import com.mobiledevpro.settings.core.datastore.AppSettingsManager
import com.mobiledevpro.settings.core.datastore.ImplAppSettingsManager
import com.mobiledevpro.settings.core.usecase.GetAppSettingsUseCase
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.scopedOf
import org.koin.core.module.dsl.singleOf
import org.koin.core.module.dsl.viewModelOf
import org.koin.dsl.module

val coreModule = module {
singleOf(::ImplAppSettingsManager) { bind<AppSettingsManager>() }
}
}

val mainModule = module {
scope<MainActivity> {
viewModelOf(::MainViewModel)
scopedOf(::GetAppSettingsUseCase)
}
}
4 changes: 4 additions & 0 deletions feature/main/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id("feature-module")
}

dependencies {
api(projects.feature.settingsCore)
}
2 changes: 2 additions & 0 deletions feature/main/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 | Dmitri Chernysh | https://mobile-dev.pro
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.mobiledevpro.main.view.state

import com.mobiledevpro.settings.core.model.Settings
import com.mobiledevpro.ui.state.UIState


sealed interface MainUIState : UIState {
data object Empty : MainUIState

data class Success(
val settings: Settings
) : MainUIState
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2023 | Dmitri Chernysh | https://mobile-dev.pro
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.mobiledevpro.main.view.vm

import androidx.lifecycle.viewModelScope
import com.mobiledevpro.main.view.state.MainUIState
import com.mobiledevpro.settings.core.usecase.GetAppSettingsUseCase
import com.mobiledevpro.ui.vm.BaseViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlin.onSuccess


class MainViewModel(
private val getAppSettingsUseCase: GetAppSettingsUseCase
) : BaseViewModel<MainUIState>() {

override fun initUIState(): MainUIState = MainUIState.Empty

init {
observeSettings()
}

private fun observeSettings() {
viewModelScope.launch {
getAppSettingsUseCase.execute()
.collectLatest { result ->
result.onSuccess { settings ->
_uiState.value = MainUIState.Success(settings)
}
}
}
}
}

0 comments on commit 7181bdf

Please sign in to comment.