Skip to content

Instantly share code, notes, and snippets.

@ImaginativeShohag
Created December 25, 2023 10:18
Show Gist options
  • Save ImaginativeShohag/063cfde5bfe4deabb31fb9ef4bd9d88c to your computer and use it in GitHub Desktop.
Save ImaginativeShohag/063cfde5bfe4deabb31fb9ef4bd9d88c to your computer and use it in GitHub Desktop.
Temp file (Delete later)
@Composable
fun HomeIndexScreen(
viewModel: HomeIndexViewModel,
goToBookDetailsScreen: (bookId: Long) -> Unit,
goToNoteDetailsScreen: (noteId: Long) -> Unit,
goToAddNewReadingScreen: () -> Unit,
goToAddNewBookScreen: () -> Unit,
goToAuthorsScreen: () -> Unit,
goToPublishersScreen: () -> Unit,
goToLendAddScreen: () -> Unit,
goToPersonScreen: () -> Unit,
goToProfileScreen: () -> Unit,
goToSyncScreen: () -> Unit,
goToRemixScreen: () -> Unit
) {
val context = LocalContext.current
// Note: This `NavHostController` is only for this screen.
// Don't use this in other screen or navigate to other screen.
val navController: NavHostController = rememberNavController()
LaunchedEffect(Unit) {
viewModel.initDatabase(context.resources)
}
// ================================================================
var showNotificationPermissionRationale by remember { mutableStateOf(false) }
val requestNotificationPermission =
rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { permissionGranted ->
showNotificationPermissionRationale = false
if (!permissionGranted) {
context.toast("Notification permission is denied. Some feature will not work.")
}
}
val askForNotificationPermission: () -> Unit = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_DENIED
) {
when {
ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED -> {
// no-op
}
ActivityCompat.shouldShowRequestPermissionRationale(
context as MainActivity,
Manifest.permission.POST_NOTIFICATIONS
) -> {
showNotificationPermissionRationale = true
}
else -> {
requestNotificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}
}
LaunchedEffect(Unit) {
askForNotificationPermission()
}
// ================================================================
HomeIndexScreenSkeleton(
navController = navController,
goToBookDetailsScreen = goToBookDetailsScreen,
goToNoteDetailsScreen = goToNoteDetailsScreen,
goToAddNewReadingScreen = goToAddNewReadingScreen,
goToAddNewBookScreen = goToAddNewBookScreen,
goToAuthorsScreen = goToAuthorsScreen,
goToPublishersScreen = goToPublishersScreen,
goToLendAddScreen = goToLendAddScreen,
goToPersonScreen = goToPersonScreen,
goToProfileScreen = goToProfileScreen,
goToSyncScreen = goToSyncScreen,
goToRemixScreen = goToRemixScreen
)
}
@Composable
fun HomeIndexScreenSkeleton(
navController: NavHostController = rememberNavController(),
goToBookDetailsScreen: (bookId: Long) -> Unit = {},
goToNoteDetailsScreen: (noteId: Long) -> Unit = {},
goToAddNewReadingScreen: () -> Unit = {},
goToAddNewBookScreen: () -> Unit = {},
goToAuthorsScreen: () -> Unit = {},
goToPublishersScreen: () -> Unit = {},
goToLendAddScreen: () -> Unit = {},
goToPersonScreen: () -> Unit = {},
goToProfileScreen: () -> Unit = {},
goToSyncScreen: () -> Unit = {},
goToRemixScreen: () -> Unit = {}
) {
val items = listOf(
HomeTabScreen.Reading,
HomeTabScreen.Library,
HomeTabScreen.Note,
HomeTabScreen.Lend
)
Scaffold(
Modifier
.background(Color.Blue),
bottomBar = {
NavigationBar {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
items.forEach { screen ->
NavigationBarItem(
icon = { Icon(screen.icon, contentDescription = screen.title) },
label = {
Text(
screen.title,
textAlign = TextAlign.Center
)
},
selected = currentDestination?.hierarchy?.any {
it.route == screen.route
} == true,
onClick = {
navController.navigate(screen.route) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
}
)
}
}
},
contentWindowInsets = WindowInsets(0.dp, 0.dp, 0.dp, 0.dp)
) { innerPadding ->
NavHost(
navController,
startDestination = ReadingScreen.ReadingList.route,
Modifier.padding(innerPadding)
) {
composable(ReadingScreen.ReadingList.route) {
val viewModel: ReadingListViewModel = hiltViewModel()
ReadingListScreen(
viewModel = viewModel,
goToBookDetails = goToBookDetailsScreen,
goToAddNewReadingScreen = goToAddNewReadingScreen,
goToProfileScreen = goToProfileScreen,
goToSyncScreen = goToSyncScreen
)
}
composable(BookScreen.BookList.route) {
val viewModel: BookListViewModel = hiltViewModel()
BookListScreen(
viewModel = viewModel,
goToBookDetails = goToBookDetailsScreen,
goToAddNewBookScreen = goToAddNewBookScreen,
goToAuthorsScreen = goToAuthorsScreen,
goToPublishersScreen = goToPublishersScreen
)
}
composable(NoteScreen.NoteListAll.route) {
val viewModel: NoteListViewModel = hiltViewModel()
NoteListScreen(
viewModel = viewModel,
goToNoteDetailsScreen = goToNoteDetailsScreen,
goToRemixScreen = goToRemixScreen
)
}
composable(LendScreen.LendList.route) {
val lendListViewModel: LendListViewModel = hiltViewModel()
LendListScreen(
viewModel = lendListViewModel,
goToBookDetails = goToBookDetailsScreen,
goToLendAddScreen = goToLendAddScreen,
goToPersonScreen = goToPersonScreen
)
}
}
}
}
// ----------------------------------------------------------------
// Tab Screens
// ----------------------------------------------------------------
private sealed class HomeTabScreen(val route: String, val title: String, val icon: ImageVector) {
data object Reading : HomeTabScreen(
ReadingScreen.ReadingList.route,
"Home",
Icons.Rounded.Home
)
data object Library : HomeTabScreen(
BookScreen.BookList.route,
"Library",
Icons.AutoMirrored.Rounded.LibraryBooks
)
data object Note : HomeTabScreen(
NoteScreen.NoteListAll.route,
"Notes",
Icons.AutoMirrored.Rounded.StickyNote2
)
data object Lend : HomeTabScreen(
LendScreen.LendList.route,
"Lend Histories",
Icons.Rounded.CollectionsBookmark
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment