fix theme not changing after changing language

This commit is contained in:
greenart7c3
2023-07-10 15:07:12 -03:00
parent b31d2d49d6
commit 492b9494ec
6 changed files with 24 additions and 23 deletions
@@ -53,13 +53,13 @@ class MainActivity : AppCompatActivity() {
val startingPage = uriToRoute(uri) val startingPage = uriToRoute(uri)
LocalPreferences.migrateSingleUserPrefs() LocalPreferences.migrateSingleUserPrefs()
val themeViewModel = ThemeViewModel()
themeViewModel.onChange(LocalPreferences.getTheme())
val language = LocalPreferences.getPreferredLanguage() val language = LocalPreferences.getPreferredLanguage()
if (language.isNotBlank()) { if (language.isNotBlank()) {
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(language) val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(language)
AppCompatDelegate.setApplicationLocales(appLocale) AppCompatDelegate.setApplicationLocales(appLocale)
} }
val themeViewModel = ThemeViewModel()
themeViewModel.onChange(LocalPreferences.getTheme())
setContent { setContent {
AmethystTheme(themeViewModel) { AmethystTheme(themeViewModel) {
@@ -20,6 +20,7 @@ import com.vitorpamplona.amethyst.ui.screen.NostrHomeFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.NotificationViewModel import com.vitorpamplona.amethyst.ui.screen.NotificationViewModel
import com.vitorpamplona.amethyst.ui.screen.ThemeViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.BookmarkListScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.BookmarkListScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChannelScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChannelScreen
@@ -55,6 +56,7 @@ fun AppNavigation(
navController: NavHostController, navController: NavHostController,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
themeViewModel: ThemeViewModel,
nextPage: String? = null nextPage: String? = null
) { ) {
var actionableNextPage by remember { mutableStateOf<String?>(nextPage) } var actionableNextPage by remember { mutableStateOf<String?>(nextPage) }
@@ -225,7 +227,7 @@ fun AppNavigation(
composable(route.route, route.arguments, content = { composable(route.route, route.arguments, content = {
SettingsScreen( SettingsScreen(
accountViewModel = accountViewModel, accountViewModel = accountViewModel,
nav = nav themeViewModel
) )
}) })
} }
@@ -24,18 +24,18 @@ fun AccountScreen(accountStateViewModel: AccountStateViewModel, themeViewModel:
is AccountState.LoggedIn -> { is AccountState.LoggedIn -> {
val accountViewModel: AccountViewModel = viewModel( val accountViewModel: AccountViewModel = viewModel(
key = state.account.userProfile().pubkeyHex, key = state.account.userProfile().pubkeyHex,
factory = AccountViewModel.Factory(state.account, themeViewModel) factory = AccountViewModel.Factory(state.account)
) )
MainScreen(accountViewModel, accountStateViewModel, startingPage) MainScreen(accountViewModel, accountStateViewModel, themeViewModel, startingPage)
} }
is AccountState.LoggedInViewOnly -> { is AccountState.LoggedInViewOnly -> {
val accountViewModel: AccountViewModel = viewModel( val accountViewModel: AccountViewModel = viewModel(
key = state.account.userProfile().pubkeyHex, key = state.account.userProfile().pubkeyHex,
factory = AccountViewModel.Factory(state.account, themeViewModel) factory = AccountViewModel.Factory(state.account)
) )
MainScreen(accountViewModel, accountStateViewModel, startingPage) MainScreen(accountViewModel, accountStateViewModel, themeViewModel, startingPage)
} }
} }
} }
@@ -22,7 +22,6 @@ import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.model.LnZapEvent import com.vitorpamplona.amethyst.service.model.LnZapEvent
import com.vitorpamplona.amethyst.service.model.PayInvoiceErrorResponse import com.vitorpamplona.amethyst.service.model.PayInvoiceErrorResponse
import com.vitorpamplona.amethyst.service.model.ReportEvent import com.vitorpamplona.amethyst.service.model.ReportEvent
import com.vitorpamplona.amethyst.ui.screen.ThemeViewModel
import kotlinx.collections.immutable.ImmutableSet import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toImmutableSet import kotlinx.collections.immutable.toImmutableSet
@@ -33,7 +32,7 @@ import java.math.BigDecimal
import java.util.Locale import java.util.Locale
@Stable @Stable
class AccountViewModel(val account: Account, private val themeViewModel: ThemeViewModel) : ViewModel() { class AccountViewModel(val account: Account) : ViewModel() {
val accountLiveData: LiveData<AccountState> = account.live.map { it } val accountLiveData: LiveData<AccountState> = account.live.map { it }
val accountLanguagesLiveData: LiveData<AccountState> = account.liveLanguages.map { it } val accountLanguagesLiveData: LiveData<AccountState> = account.liveLanguages.map { it }
val accountLastReadLiveData: LiveData<AccountState> = account.liveLastRead.map { it } val accountLastReadLiveData: LiveData<AccountState> = account.liveLastRead.map { it }
@@ -41,14 +40,6 @@ class AccountViewModel(val account: Account, private val themeViewModel: ThemeVi
val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it } val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it }
val userRelays: LiveData<UserState> = account.userProfile().live().relays.map { it } val userRelays: LiveData<UserState> = account.userProfile().live().relays.map { it }
fun changeTheme(newValue: Int) {
themeViewModel.onChange(newValue)
}
fun currentTheme(): Int {
return themeViewModel.theme.value ?: 0
}
fun updateAutomaticallyStartPlayback( fun updateAutomaticallyStartPlayback(
automaticallyStartPlayback: Boolean? automaticallyStartPlayback: Boolean?
) { ) {
@@ -326,9 +317,9 @@ class AccountViewModel(val account: Account, private val themeViewModel: ThemeVi
} }
} }
class Factory(val account: Account, private val themeViewModel: ThemeViewModel) : ViewModelProvider.Factory { class Factory(val account: Account) : ViewModelProvider.Factory {
override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel { override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel {
return AccountViewModel(account, themeViewModel) as AccountViewModel return AccountViewModel(account) as AccountViewModel
} }
} }
} }
@@ -51,11 +51,17 @@ import com.vitorpamplona.amethyst.ui.screen.NostrHomeFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.NotificationViewModel import com.vitorpamplona.amethyst.ui.screen.NotificationViewModel
import com.vitorpamplona.amethyst.ui.screen.ThemeViewModel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterialApi::class) @OptIn(ExperimentalMaterialApi::class)
@Composable @Composable
fun MainScreen(accountViewModel: AccountViewModel, accountStateViewModel: AccountStateViewModel, startingPage: String? = null) { fun MainScreen(
accountViewModel: AccountViewModel,
accountStateViewModel: AccountStateViewModel,
themeViewModel: ThemeViewModel,
startingPage: String? = null
) {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val navController = rememberNavController() val navController = rememberNavController()
val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Closed)) val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Closed))
@@ -211,6 +217,7 @@ fun MainScreen(accountViewModel: AccountViewModel, accountStateViewModel: Accoun
userReactionsStatsModel = userReactionsStatsModel, userReactionsStatsModel = userReactionsStatsModel,
navController = navController, navController = navController,
accountViewModel = accountViewModel, accountViewModel = accountViewModel,
themeViewModel = themeViewModel,
nextPage = startingPage nextPage = startingPage
) )
} }
@@ -35,6 +35,7 @@ import androidx.compose.ui.unit.sp
import androidx.core.os.LocaleListCompat import androidx.core.os.LocaleListCompat
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.ThemeViewModel
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
import com.vitorpamplona.amethyst.ui.theme.StdPadding import com.vitorpamplona.amethyst.ui.theme.StdPadding
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
@@ -88,7 +89,7 @@ fun getLanguageIndex(languageEntries: Map<String, String>): Int {
@Composable @Composable
fun SettingsScreen( fun SettingsScreen(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit themeViewModel: ThemeViewModel
) { ) {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val selectedItens = persistentListOf( val selectedItens = persistentListOf(
@@ -114,7 +115,7 @@ fun SettingsScreen(
stringResource(R.string.light), stringResource(R.string.light),
stringResource(R.string.dark) stringResource(R.string.dark)
) )
val themeIndex = accountViewModel.currentTheme() val themeIndex = themeViewModel.theme.value ?: 0
val context = LocalContext.current val context = LocalContext.current
@@ -161,7 +162,7 @@ fun SettingsScreen(
placeholder = themeItens[themeIndex], placeholder = themeItens[themeIndex],
options = themeItens, options = themeItens,
onSelect = { onSelect = {
accountViewModel.changeTheme(it) themeViewModel.onChange(it)
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
LocalPreferences.updateTheme(it) LocalPreferences.updateTheme(it)
} }