diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt index 5af93a12e..abeb8dd52 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt @@ -53,13 +53,13 @@ class MainActivity : AppCompatActivity() { val startingPage = uriToRoute(uri) LocalPreferences.migrateSingleUserPrefs() - val themeViewModel = ThemeViewModel() - themeViewModel.onChange(LocalPreferences.getTheme()) val language = LocalPreferences.getPreferredLanguage() if (language.isNotBlank()) { val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(language) AppCompatDelegate.setApplicationLocales(appLocale) } + val themeViewModel = ThemeViewModel() + themeViewModel.onChange(LocalPreferences.getTheme()) setContent { AmethystTheme(themeViewModel) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt index 0278f3646..8901064c4 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt @@ -20,6 +20,7 @@ import com.vitorpamplona.amethyst.ui.screen.NostrHomeFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel 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.BookmarkListScreen import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChannelScreen @@ -55,6 +56,7 @@ fun AppNavigation( navController: NavHostController, accountViewModel: AccountViewModel, + themeViewModel: ThemeViewModel, nextPage: String? = null ) { var actionableNextPage by remember { mutableStateOf(nextPage) } @@ -225,7 +227,7 @@ fun AppNavigation( composable(route.route, route.arguments, content = { SettingsScreen( accountViewModel = accountViewModel, - nav = nav + themeViewModel ) }) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountScreen.kt index 6ac35a240..82b872d7e 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/AccountScreen.kt @@ -24,18 +24,18 @@ fun AccountScreen(accountStateViewModel: AccountStateViewModel, themeViewModel: is AccountState.LoggedIn -> { val accountViewModel: AccountViewModel = viewModel( 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 -> { val accountViewModel: AccountViewModel = viewModel( 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) } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index fedfa79bf..3a297cfe3 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -22,7 +22,6 @@ import com.vitorpamplona.amethyst.service.model.Event import com.vitorpamplona.amethyst.service.model.LnZapEvent import com.vitorpamplona.amethyst.service.model.PayInvoiceErrorResponse import com.vitorpamplona.amethyst.service.model.ReportEvent -import com.vitorpamplona.amethyst.ui.screen.ThemeViewModel import kotlinx.collections.immutable.ImmutableSet import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.toImmutableSet @@ -33,7 +32,7 @@ import java.math.BigDecimal import java.util.Locale @Stable -class AccountViewModel(val account: Account, private val themeViewModel: ThemeViewModel) : ViewModel() { +class AccountViewModel(val account: Account) : ViewModel() { val accountLiveData: LiveData = account.live.map { it } val accountLanguagesLiveData: LiveData = account.liveLanguages.map { it } val accountLastReadLiveData: LiveData = account.liveLastRead.map { it } @@ -41,14 +40,6 @@ class AccountViewModel(val account: Account, private val themeViewModel: ThemeVi val userFollows: LiveData = account.userProfile().live().follows.map { it } val userRelays: LiveData = account.userProfile().live().relays.map { it } - fun changeTheme(newValue: Int) { - themeViewModel.onChange(newValue) - } - - fun currentTheme(): Int { - return themeViewModel.theme.value ?: 0 - } - fun updateAutomaticallyStartPlayback( 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 create(modelClass: Class): AccountViewModel { - return AccountViewModel(account, themeViewModel) as AccountViewModel + return AccountViewModel(account) as AccountViewModel } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt index 1bffcfed1..b3fef99eb 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt @@ -51,11 +51,17 @@ import com.vitorpamplona.amethyst.ui.screen.NostrHomeFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NotificationViewModel +import com.vitorpamplona.amethyst.ui.screen.ThemeViewModel import kotlinx.coroutines.launch @OptIn(ExperimentalMaterialApi::class) @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 navController = rememberNavController() val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Closed)) @@ -211,6 +217,7 @@ fun MainScreen(accountViewModel: AccountViewModel, accountStateViewModel: Accoun userReactionsStatsModel = userReactionsStatsModel, navController = navController, accountViewModel = accountViewModel, + themeViewModel = themeViewModel, nextPage = startingPage ) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/SettingsScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/SettingsScreen.kt index 11cdb5464..69b18e5ef 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/SettingsScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/SettingsScreen.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.unit.sp import androidx.core.os.LocaleListCompat import com.vitorpamplona.amethyst.LocalPreferences 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.StdPadding import kotlinx.collections.immutable.persistentListOf @@ -88,7 +89,7 @@ fun getLanguageIndex(languageEntries: Map): Int { @Composable fun SettingsScreen( accountViewModel: AccountViewModel, - nav: (String) -> Unit + themeViewModel: ThemeViewModel ) { val scope = rememberCoroutineScope() val selectedItens = persistentListOf( @@ -114,7 +115,7 @@ fun SettingsScreen( stringResource(R.string.light), stringResource(R.string.dark) ) - val themeIndex = accountViewModel.currentTheme() + val themeIndex = themeViewModel.theme.value ?: 0 val context = LocalContext.current @@ -161,7 +162,7 @@ fun SettingsScreen( placeholder = themeItens[themeIndex], options = themeItens, onSelect = { - accountViewModel.changeTheme(it) + themeViewModel.onChange(it) scope.launch(Dispatchers.IO) { LocalPreferences.updateTheme(it) }