diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/activities/MainActivity.kt b/app/src/main/java/com/bnyro/wallpaper/ui/activities/MainActivity.kt index 97d5cf45..e2189418 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/activities/MainActivity.kt @@ -47,12 +47,11 @@ private fun MainContent() { val scope = rememberCoroutineScope() val drawerState = rememberDrawerState(DrawerValue.Closed) - val pages = DrawerScreens.screens // navigate to the last tab opened tab LaunchedEffect(navController) { val lastSelectedTab = Preferences.getString(Preferences.startTabKey, "") - val initialPage = pages.firstOrNull { it.route == lastSelectedTab } + val initialPage = DrawerScreens.screens.firstOrNull { it.route == lastSelectedTab } initialPage?.route?.runCatching { navController.navigate(this) } navController.addOnDestinationChangedListener { _, destination, _ -> if (!DrawerScreens.apiScreens.any { @@ -68,7 +67,8 @@ private fun MainContent() { NavigationDrawer( drawerState = drawerState, navController = navController, - pages = pages + viewModel = viewModel, + pages = DrawerScreens.screens ) { Scaffold( topBar = { diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt index 2acf1714..d5ff4dc4 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt @@ -27,11 +27,7 @@ import androidx.compose.material3.Text import androidx.compose.material3.contentColorFor import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -44,6 +40,7 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavController import com.bnyro.wallpaper.R +import com.bnyro.wallpaper.ui.models.MainModel import com.bnyro.wallpaper.ui.nav.DrawerScreens import kotlinx.coroutines.launch @@ -53,18 +50,19 @@ fun NavigationDrawer( drawerState: DrawerState, pages: List, navController: NavController, + viewModel: MainModel, content: @Composable () -> Unit ) { val scope = rememberCoroutineScope() - var selectedItem by remember { - val initialPage = pages.firstOrNull { navController.currentDestination?.route == it.route } - mutableStateOf(initialPage ?: DrawerScreens.Wallhaven) - } - LaunchedEffect(Unit) { navController.addOnDestinationChangedListener { _, destination, _ -> - pages.firstOrNull { it.route == destination.route }?.let { selectedItem = it } + pages.firstOrNull { it.route == destination.route }?.let { + viewModel.currentDestination = it + } + } + pages.firstOrNull { navController.currentDestination?.route == it.route }?.let { + viewModel.currentDestination = it } } @@ -90,8 +88,8 @@ fun NavigationDrawer( ) } Spacer(Modifier.height(20.dp)) - pages.forEach { - if (it.divideBefore) { + pages.forEach { screen -> + if (screen.divideBefore) { Divider( modifier = Modifier .padding(25.dp, 15.dp) @@ -100,18 +98,18 @@ fun NavigationDrawer( } NavigationDrawerItem( icon = { - Icon(it.icon, null) + Icon(screen.icon, null) }, label = { - Text(stringResource(it.titleResource)) + Text(stringResource(screen.titleResource)) }, - selected = it == selectedItem, + selected = screen == viewModel.currentDestination, onClick = { scope.launch { drawerState.close() - navController.navigate(it.route) + navController.navigate(screen.route) } - selectedItem = it + viewModel.currentDestination = screen }, modifier = Modifier .padding(NavigationDrawerItemDefaults.ItemPadding) diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt b/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt index 49e7731c..d7286058 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt @@ -24,7 +24,9 @@ class MainModel : ViewModel() { ).toInt() var themeMode by mutableStateOf(ThemeMode.values()[themeModeIndex]) - var api: Api = Preferences.getApiByRoute(DrawerScreens.apiScreens.first().route) + var currentDestination: DrawerScreens by mutableStateOf(DrawerScreens.Wallhaven) + var api = Preferences.getApiByRoute(DrawerScreens.Wallhaven.route) + var wallpapers by mutableStateOf( listOf() ) diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt b/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt index a502f6fa..76f55f49 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt @@ -34,7 +34,7 @@ sealed class DrawerScreens( object About : DrawerScreens(R.string.about, "about", Icons.Default.Info) companion object { - val apiScreens = listOf(Wallhaven, Unsplash, OWalls, Picsum, BingDaily, Reddit, Lemmy) - val screens = listOf(*apiScreens.toTypedArray(), Favorites, Settings, About) + val apiScreens by lazy { listOf(Wallhaven, Unsplash, OWalls, Picsum, BingDaily, Reddit, Lemmy) } + val screens by lazy { listOf(*apiScreens.toTypedArray(), Favorites, Settings, About) } } } diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/pages/WallpaperPage.kt b/app/src/main/java/com/bnyro/wallpaper/ui/pages/WallpaperPage.kt index a0e88914..fd5ab80d 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/pages/WallpaperPage.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/pages/WallpaperPage.kt @@ -16,6 +16,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -42,12 +43,19 @@ fun WallpaperPage( mutableStateOf(null) } + var fetchedWallpapers by rememberSaveable { + mutableStateOf(false) + } LaunchedEffect(Unit) { + if (fetchedWallpapers) return@LaunchedEffect + viewModel.wallpapers = listOf() viewModel.page = 1 viewModel.fetchWallpapers { Toast.makeText(context, it.localizedMessage, Toast.LENGTH_LONG).show() } + + fetchedWallpapers = true } Box(