Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThemeChanging Functionality Added #2723

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions androidApp/src/main/kotlin/org/mifos/mobile/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -32,9 +33,11 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.mifos.mobile.HomeActivityUiState.Success
import org.mifos.mobile.core.data.utils.NetworkMonitor
import org.mifos.mobile.core.datastore.PreferencesHelper
import org.mifos.mobile.core.designsystem.theme.MifosMobileTheme
import org.mifos.mobile.core.designsystem.theme.darkScrim
import org.mifos.mobile.core.designsystem.theme.lightScrim
import org.mifos.mobile.core.model.enums.AppTheme
import org.mifos.mobile.navigation.MifosNavGraph.AUTH_GRAPH
import org.mifos.mobile.navigation.MifosNavGraph.PASSCODE_GRAPH
import org.mifos.mobile.navigation.RootNavGraph
Expand All @@ -47,6 +50,9 @@ class HomeActivity : ComponentActivity() {
@Inject
lateinit var networkMonitor: NetworkMonitor

@Inject
lateinit var preferenceHelper: PreferencesHelper
niyajali marked this conversation as resolved.
Show resolved Hide resolved

private val viewModel: HomeActivityViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -76,25 +82,31 @@ class HomeActivity : ComponentActivity() {
val navController = rememberNavController()

val appState = rememberMifosMobileState(networkMonitor = networkMonitor)
val darkTheme= isSystemInDarkTheme()

val navDestination = when (uiState) {
is Success -> if ((uiState as Success).userData.isAuthenticated) {
PASSCODE_GRAPH
} else {
AUTH_GRAPH
}

else -> AUTH_GRAPH
}

DisposableEffect(darkTheme) {
window?.statusBarColor = if (darkTheme) darkScrim.toArgb() else lightScrim.toArgb()
window?.navigationBarColor = if (darkTheme) darkScrim.toArgb() else lightScrim.toArgb()
val isSystemInDarkMode = isSystemInDarkTheme()
DisposableEffect(isSystemInDarkMode) {
window?.statusBarColor = if (isSystemInDarkMode) darkScrim.toArgb() else lightScrim.toArgb()
revanthkumarJ marked this conversation as resolved.
Show resolved Hide resolved
window?.navigationBarColor = if (isSystemInDarkMode) darkScrim.toArgb() else lightScrim.toArgb()
onDispose {}
}

val currentTheme by preferenceHelper.themeFlow.collectAsState()
val isDarkMode = when (currentTheme) {
niyajali marked this conversation as resolved.
Show resolved Hide resolved
AppTheme.DARK -> true
AppTheme.LIGHT -> false
AppTheme.SYSTEM -> isSystemInDarkMode
}
CompositionLocalProvider {
MifosMobileTheme {
MifosMobileTheme(isDarkMode) {
RootNavGraph(
appState = appState,
navHostController = navController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import androidx.multidex.MultiDexApplication
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.raizlabs.android.dbflow.config.FlowManager
import dagger.hilt.android.HiltAndroidApp
import org.mifos.mobile.core.datastore.PreferencesHelper
import org.mifos.mobile.feature.settings.applySavedTheme

@HiltAndroidApp
class MifosSelfServiceApp : MultiDexApplication() {
Expand All @@ -24,7 +22,6 @@ class MifosSelfServiceApp : MultiDexApplication() {
MultiDex.install(this)
FlowManager.init(this)
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)
PreferencesHelper(this).applySavedTheme()
}

override fun onTerminate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import android.text.TextUtils
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.callbackFlow
import org.mifos.mobile.core.model.enums.AppTheme
Expand All @@ -29,9 +31,23 @@ import javax.inject.Singleton
*/
@Singleton
class PreferencesHelper @Inject constructor(@ApplicationContext context: Context?) {
val themeFlowState: MutableStateFlow<AppTheme>
val themeFlow: StateFlow<AppTheme> get() = themeFlowState
niyajali marked this conversation as resolved.
Show resolved Hide resolved

private val sharedPreferences: SharedPreferences? =
PreferenceManager.getDefaultSharedPreferences(context)

init {
if (!sharedPreferences?.contains(APPLICATION_THEME)!!) {
putInt(APPLICATION_THEME, AppTheme.SYSTEM.ordinal)
}
themeFlowState = MutableStateFlow(
AppTheme.entries.getOrNull(
sharedPreferences.getInt(APPLICATION_THEME, AppTheme.SYSTEM.ordinal),
) ?: AppTheme.SYSTEM,
)
}

fun clear() {
val editor = sharedPreferences?.edit()
// prevent deletion of url and tenant
Expand Down Expand Up @@ -178,6 +194,7 @@ class PreferencesHelper @Inject constructor(@ApplicationContext context: Context
get() = getInt(APPLICATION_THEME, AppTheme.SYSTEM.ordinal) ?: AppTheme.SYSTEM.ordinal
set(value) {
putInt(APPLICATION_THEME, value)
themeFlowState.value = AppTheme.entries[value]
}

var language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ val LightSurfaceTint = Color(0xFF325CA8)
val DarkSurfaceTint = Color(0xFFAEC6FF)

val lightScrim = Color(0x80FFFFFF)
val darkScrim = Color(0x80000000)
val darkScrim = Color(0x80000000)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
package org.mifos.mobile.feature.settings

import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -70,15 +68,7 @@ internal class SettingsViewModel @Inject constructor(
}

fun updateTheme(theme: AppTheme) {
AppCompatDelegate.setDefaultNightMode(
when (theme) {
AppTheme.DARK -> AppCompatDelegate.MODE_NIGHT_YES
AppTheme.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
},
)
preferencesHelper.appTheme = theme.ordinal
preferencesHelper.applyTheme(theme)
}
}

Expand Down Expand Up @@ -124,27 +114,3 @@ internal enum class SettingsCardItem(
subclassOf = R.string.other,
),
}

fun PreferencesHelper.applySavedTheme() {
val applicationTheme = AppTheme.entries.find { it.ordinal == this.appTheme }
AppCompatDelegate.setDefaultNightMode(
when {
applicationTheme == AppTheme.DARK -> AppCompatDelegate.MODE_NIGHT_YES
applicationTheme == AppTheme.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
Build.VERSION.SDK_INT > Build.VERSION_CODES.P -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_NO
},
)
}

internal fun PreferencesHelper.applyTheme(applicationTheme: AppTheme) {
this.appTheme = applicationTheme.ordinal
AppCompatDelegate.setDefaultNightMode(
when {
applicationTheme == AppTheme.DARK -> AppCompatDelegate.MODE_NIGHT_YES
applicationTheme == AppTheme.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
Build.VERSION.SDK_INT > Build.VERSION_CODES.P -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
else -> AppCompatDelegate.MODE_NIGHT_NO
},
)
}
Loading