Skip to content

Commit 05cc82f

Browse files
author
Morteza Taghdisi
committed
Added view pager to home screen
1 parent effade5 commit 05cc82f

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

components/ui/src/main/java/bluevelvet/composents/ui/navigation/ComposentsDrawerMenu.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ fun ComposentsDrawerMenu(
5151
badge: @Composable ((ComposentsMenuItem) -> Unit)? = null,
5252
content: @Composable () -> Unit,
5353
) {
54-
var currentSelectedItemId by remember { mutableStateOf(selectedItemId) }
55-
5654
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
5755

5856
ModalNavigationDrawer(
@@ -63,12 +61,11 @@ fun ComposentsDrawerMenu(
6361
drawerTitleContent = drawerHeadlineContent,
6462
coroutineScope = coroutineScope,
6563
drawerState = drawerState,
66-
selectedItemId = currentSelectedItemId,
64+
selectedItemId = selectedItemId,
6765
menuItems = menuItems,
6866
badge = badge,
6967
onDrawerItemClick = {
7068
onMenuItemSelected(it)
71-
currentSelectedItemId = it.id
7269
},
7370
)
7471
},

example/src/main/java/bluevelvet/composents/example/activity/home/HomeActivity.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import androidx.compose.material.icons.outlined.Favorite
1111
import androidx.compose.material3.*
1212
import androidx.compose.runtime.*
1313
import androidx.compose.ui.Modifier
14+
import androidx.navigation.NavController
15+
import androidx.navigation.NavHostController
16+
import androidx.navigation.compose.rememberNavController
1417
import bluevelvet.composents.example.activity.auth.AuthActivity
1518
import bluevelvet.composents.example.core.createJob
1619
import bluevelvet.composents.foundation.*
@@ -23,6 +26,7 @@ import bluevelvet.composents.foundation.menu.ComposentsMenuItem
2326
import bluevelvet.composents.foundation.menu.ComposentsMenuItemType
2427
import bluevelvet.composents.ui.navigation.ComposentsDrawerMenu
2528
import bluevelvet.composents.ui.navigation.ComposentsTopAppBar
29+
import kotlinx.coroutines.CoroutineScope
2630
import kotlinx.coroutines.delay
2731

2832
class HomeActivity : ComponentActivity() {
@@ -32,17 +36,26 @@ class HomeActivity : ComponentActivity() {
3236

3337
setContent {
3438
ComposentsExampleTheme {
35-
DrawerMenuLayout()
39+
val navController = rememberNavController()
40+
val coroutineScope = rememberCoroutineScope()
41+
42+
DrawerMenuLayout(
43+
navController = navController,
44+
coroutineScope = coroutineScope
45+
)
3646
//AppbarMenuLayout()
3747
}
3848
}
3949
}
4050

4151
@Composable
42-
private fun DrawerMenuLayout() {
43-
val drawerHomeMenuItem = ComposentsMenuItem(title = "Home", icon = Icons.Filled.Home, id = "home")
44-
val drawerInboxMenuItem = ComposentsMenuItem(title = "Inbox", icon = Icons.Filled.Inbox, id = "inbox", badgeCount = 4)
45-
val drawerSearchMenuItem = ComposentsMenuItem(title = "Search", icon = Icons.Filled.Search, id = "search")
52+
private fun DrawerMenuLayout(
53+
navController: NavHostController,
54+
coroutineScope: CoroutineScope,
55+
) {
56+
val drawerHomeMenuItem = ComposentsMenuItem(title = "Home", icon = Icons.Filled.Home, id = Destinations.HOME)
57+
val drawerInboxMenuItem = ComposentsMenuItem(title = "Inbox", icon = Icons.Filled.Inbox, id = Destinations.INBOX, badgeCount = 4)
58+
val drawerSearchMenuItem = ComposentsMenuItem(title = "Search", icon = Icons.Filled.Search, id = Destinations.SEARCH)
4659
val drawerExitMenuItem = ComposentsMenuItem(title = "Logout", icon = Icons.Filled.ExitToApp, id = "logout")
4760
val drawerMenuItems = listOf(drawerHomeMenuItem, drawerInboxMenuItem, drawerSearchMenuItem, drawerExitMenuItem)
4861

@@ -53,10 +66,22 @@ class HomeActivity : ComponentActivity() {
5366
drawerHeadline = "Choose an item",
5467
menuItems = drawerMenuItems,
5568
selectedItemId = selectedDrawerMenuItem.id,
69+
coroutineScope = coroutineScope,
5670
onMenuItemSelected = {
57-
selectedDrawerMenuItem = it
71+
if (it.id == drawerExitMenuItem.id) {
72+
// TODO: Log out the user
73+
} else {
74+
selectedDrawerMenuItem = it
75+
navController.navigate(it.id)
76+
}
5877
},
5978
) {
79+
HomeNavGraph(
80+
navController = navController,
81+
selectedDrawerItem = selectedDrawerMenuItem
82+
)
83+
84+
/* You also can use this approach instead of using NavHostGraph
6085
when(selectedDrawerMenuItem.id) {
6186
drawerHomeMenuItem.id -> HomeScreen()
6287
drawerInboxMenuItem.id -> InboxScreen()
@@ -65,6 +90,7 @@ class HomeActivity : ComponentActivity() {
6590
HomeScreen()
6691
}
6792
}
93+
*/
6894
}
6995
}
7096

example/src/main/java/bluevelvet/composents/example/activity/home/HomeNavGraph.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.navigation.compose.rememberNavController
88
import bluevelvet.composents.foundation.menu.ComposentsMenuItem
99
import bluevelvet.composents.example.screen.HomeScreen
1010
import bluevelvet.composents.example.screen.InboxScreen
11+
import bluevelvet.composents.example.screen.SearchScreen
1112

1213
/**
1314
* Nav graph for the example app.
@@ -19,11 +20,12 @@ import bluevelvet.composents.example.screen.InboxScreen
1920
object Destinations {
2021
const val HOME = "home"
2122
const val INBOX = "inbox"
23+
const val SEARCH = "search"
2224
}
2325

2426
@Composable
25-
fun AppNavGraph(
26-
navController: NavHostController = rememberNavController(),
27+
fun HomeNavGraph(
28+
navController: NavHostController,
2729
selectedDrawerItem: ComposentsMenuItem
2830
) {
2931
NavHost(
@@ -36,5 +38,8 @@ fun AppNavGraph(
3638
composable(Destinations.INBOX) {
3739
InboxScreen()
3840
}
41+
composable(Destinations.SEARCH) {
42+
SearchScreen()
43+
}
3944
}
4045
}

0 commit comments

Comments
 (0)