Skip to content

Commit

Permalink
refactor: simplify current destination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 23, 2023
1 parent 74b6dc6 commit 977a259
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -68,7 +67,8 @@ private fun MainContent() {
NavigationDrawer(
drawerState = drawerState,
navController = navController,
pages = pages
viewModel = viewModel,
pages = DrawerScreens.screens
) {
Scaffold(
topBar = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -53,18 +50,19 @@ fun NavigationDrawer(
drawerState: DrawerState,
pages: List<DrawerScreens>,
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
}
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Wallpaper>()
)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,12 +43,19 @@ fun WallpaperPage(
mutableStateOf<Wallpaper?>(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(
Expand Down

0 comments on commit 977a259

Please sign in to comment.