-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from SuhasDissa/navigation-support
Navigation support
Showing
17 changed files
with
574 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
app/src/main/java/com/bnyro/contacts/nav/NavContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.bnyro.contacts.nav | ||
|
||
import android.content.res.Configuration | ||
import androidx.activity.addCallback | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.NavigationBar | ||
import androidx.compose.material3.NavigationBarItem | ||
import androidx.compose.material3.NavigationRail | ||
import androidx.compose.material3.NavigationRailItem | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavController | ||
import androidx.navigation.compose.rememberNavController | ||
import com.bnyro.contacts.ui.activities.MainActivity | ||
|
||
val bottomNavItems = listOf( | ||
NavRoutes.Contacts, | ||
NavRoutes.Messages | ||
) | ||
|
||
@Composable | ||
fun NavContainer( | ||
initialTabIndex: Int | ||
) { | ||
val context = LocalContext.current | ||
val navController = rememberNavController() | ||
|
||
val initialTab = bottomNavItems[initialTabIndex.coerceIn(0, 1)] | ||
var selectedRoute by remember { | ||
mutableStateOf(initialTab) | ||
} | ||
LaunchedEffect(Unit) { | ||
val activity = context as MainActivity | ||
activity.onBackPressedDispatcher.addCallback { | ||
if (selectedRoute != NavRoutes.Settings && selectedRoute != NavRoutes.About) { | ||
activity.finish() | ||
} else { | ||
navController.popBackStack() | ||
} | ||
} | ||
} | ||
|
||
// listen for destination changes (e.g. back presses) | ||
DisposableEffect(Unit) { | ||
val listener = NavController.OnDestinationChangedListener { _, destination, _ -> | ||
allRoutes.firstOrNull { it.route == destination.route?.split("/")?.first() } | ||
?.let { selectedRoute = it } | ||
} | ||
navController.addOnDestinationChangedListener(listener) | ||
|
||
onDispose { | ||
navController.removeOnDestinationChangedListener(listener) | ||
} | ||
} | ||
|
||
val orientation = LocalConfiguration.current.orientation | ||
Scaffold( | ||
bottomBar = { | ||
if (orientation == Configuration.ORIENTATION_PORTRAIT && (selectedRoute == NavRoutes.Contacts || selectedRoute == NavRoutes.Messages)) { | ||
NavigationBar( | ||
tonalElevation = 5.dp | ||
) { | ||
bottomNavItems.forEach { | ||
NavigationBarItem( | ||
label = { | ||
Text(stringResource(it.stringRes!!)) | ||
}, | ||
icon = { | ||
Icon(it.icon!!, null) | ||
}, | ||
selected = it == selectedRoute, | ||
onClick = { | ||
selectedRoute = it | ||
navController.navigate(it.route) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
) { pV -> | ||
Row( | ||
Modifier | ||
.fillMaxSize() | ||
.padding(pV) | ||
) { | ||
if (orientation == Configuration.ORIENTATION_LANDSCAPE) { | ||
NavigationRail { | ||
bottomNavItems.forEach { | ||
NavigationRailItem(selected = it == selectedRoute, | ||
onClick = { | ||
selectedRoute = it | ||
navController.navigate(it.route) | ||
}, | ||
icon = { Icon(it.icon!!, null) }, | ||
label = { | ||
Text(stringResource(it.stringRes!!)) | ||
}) | ||
} | ||
} | ||
} | ||
AppNavHost( | ||
navController, | ||
startDestination = initialTab, | ||
modifier = Modifier | ||
.fillMaxSize() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.bnyro.contacts.nav | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.NavType | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.navArgument | ||
import com.bnyro.contacts.ui.models.ContactsModel | ||
import com.bnyro.contacts.ui.models.SmsModel | ||
import com.bnyro.contacts.ui.models.ThemeModel | ||
import com.bnyro.contacts.ui.screens.AboutScreen | ||
import com.bnyro.contacts.ui.screens.ContactsPage | ||
import com.bnyro.contacts.ui.screens.SettingsScreen | ||
import com.bnyro.contacts.ui.screens.SmsListScreen | ||
import com.bnyro.contacts.ui.screens.SmsThreadScreen | ||
|
||
@Composable | ||
fun AppNavHost( | ||
navController: NavHostController, | ||
startDestination: NavRoutes, | ||
modifier: Modifier = Modifier | ||
) { | ||
val smsModel: SmsModel = viewModel(factory = SmsModel.Factory) | ||
val contactsModel: ContactsModel = viewModel(factory = ContactsModel.Factory) | ||
val themeModel: ThemeModel = viewModel() | ||
|
||
val viewModelStoreOwner: ViewModelStoreOwner = LocalViewModelStoreOwner.current!! | ||
|
||
NavHost(navController, startDestination = startDestination.route, modifier = modifier) { | ||
composable(NavRoutes.About.route) { | ||
AboutScreen { | ||
navController.popBackStack() | ||
} | ||
} | ||
composable(NavRoutes.Settings.route) { | ||
SettingsScreen(themeModel = themeModel, smsModel = smsModel) { | ||
navController.popBackStack() | ||
} | ||
} | ||
composable(NavRoutes.Contacts.route) { | ||
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { | ||
ContactsPage(null, | ||
onNavigate = { | ||
navController.navigate(it.route) | ||
}) | ||
} | ||
} | ||
composable(NavRoutes.Messages.route) { | ||
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { | ||
SmsListScreen( | ||
smsModel = smsModel, | ||
contactsModel = contactsModel, | ||
scrollConnection = null, | ||
onNavigate = { | ||
navController.navigate(it.route) | ||
}, | ||
onClickMessage = { address, contactData -> | ||
smsModel.currentContactData = contactData | ||
navController.navigate("${NavRoutes.MessageThread.route}/$address") | ||
} | ||
) | ||
} | ||
} | ||
composable( | ||
"${NavRoutes.MessageThread.route}/{address}", | ||
listOf(navArgument("address") { type = NavType.StringType }) | ||
) { | ||
val address = it.arguments?.getString("address") | ||
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) { | ||
SmsThreadScreen( | ||
smsModel = smsModel, | ||
contactsModel = contactsModel, | ||
contactsData = remember { smsModel.currentContactData }, | ||
address = address.orEmpty() | ||
) { | ||
navController.popBackStack() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.bnyro.contacts.nav | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.rounded.Message | ||
import androidx.compose.material.icons.rounded.Person | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import com.bnyro.contacts.R | ||
|
||
sealed class NavRoutes( | ||
val route: String, | ||
@StringRes val stringRes: Int? = null, | ||
val icon: ImageVector? = null | ||
) { | ||
object About : NavRoutes("about", null, null) | ||
object Settings : NavRoutes("settings", null, null) | ||
object Contacts : NavRoutes("contacts", R.string.contacts, Icons.Rounded.Person) | ||
object Messages : NavRoutes("messages", R.string.messages, Icons.Rounded.Message) | ||
object MessageThread : NavRoutes("message_thread", null, null) | ||
} | ||
|
||
val allRoutes = listOf( | ||
NavRoutes.About, | ||
NavRoutes.Settings, | ||
NavRoutes.Contacts, | ||
NavRoutes.Messages, | ||
NavRoutes.MessageThread, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.