Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions JetNews/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.compose)
alias(libs.plugins.kotlin.serialization)
}

android {
Expand Down Expand Up @@ -92,6 +93,7 @@ dependencies {

implementation(libs.kotlin.stdlib)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.serialization.core)

implementation(libs.androidx.compose.animation)
implementation(libs.androidx.compose.foundation.layout)
Expand All @@ -116,7 +118,9 @@ dependencies {
implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.lifecycle.viewModelCompose)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.viewmodel.navigation3)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.navigation3.ui)
implementation(libs.androidx.window)

androidTestImplementation(libs.junit)
Expand Down
8 changes: 8 additions & 0 deletions JetNews/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
android:pathPrefix="/jetnews"
android:scheme="https" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="developer.android.com"
android:pathPrefix="/jetnews/home" />
</intent-filter>
</activity>

<receiver
Expand Down
11 changes: 7 additions & 4 deletions JetNews/app/src/main/java/com/example/jetnews/ui/AppDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation3.runtime.NavKey
import com.example.jetnews.R
import com.example.jetnews.ui.navigation.HomeKey
import com.example.jetnews.ui.navigation.InterestsKey
import com.example.jetnews.ui.theme.JetnewsTheme

@Composable
fun AppDrawer(
drawerState: DrawerState,
currentRoute: String,
currentKey: NavKey,
navigateToHome: () -> Unit,
navigateToInterests: () -> Unit,
closeDrawer: () -> Unit,
Expand All @@ -58,7 +61,7 @@ fun AppDrawer(
NavigationDrawerItem(
label = { Text(stringResource(id = R.string.home_title)) },
icon = { Icon(painterResource(R.drawable.ic_home), null) },
selected = currentRoute == JetnewsDestinations.HOME_ROUTE,
selected = currentKey is HomeKey,
onClick = {
navigateToHome()
closeDrawer()
Expand All @@ -68,7 +71,7 @@ fun AppDrawer(
NavigationDrawerItem(
label = { Text(stringResource(id = R.string.interests_title)) },
icon = { Icon(painterResource(R.drawable.ic_list_alt), null) },
selected = currentRoute == JetnewsDestinations.INTERESTS_ROUTE,
selected = currentKey == InterestsKey,
onClick = {
navigateToInterests()
closeDrawer()
Expand Down Expand Up @@ -102,7 +105,7 @@ fun PreviewAppDrawer() {
JetnewsTheme {
AppDrawer(
drawerState = rememberDrawerState(initialValue = DrawerValue.Open),
currentRoute = JetnewsDestinations.HOME_ROUTE,
currentKey = HomeKey(),
navigateToHome = {},
navigateToInterests = {},
closeDrawer = { },
Expand Down
40 changes: 19 additions & 21 deletions JetNews/app/src/main/java/com/example/jetnews/ui/JetnewsApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,37 @@ import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.rememberDrawerState
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation3.runtime.NavKey
import com.example.jetnews.data.AppContainer
import com.example.jetnews.ui.components.AppNavRail
import com.example.jetnews.ui.navigation.HomeKey
import com.example.jetnews.ui.navigation.JetnewsNavDisplay
import com.example.jetnews.ui.navigation.NavigationState
import com.example.jetnews.ui.navigation.Navigator
import com.example.jetnews.ui.navigation.rememberNavigationState
import com.example.jetnews.ui.theme.JetnewsTheme
import kotlinx.coroutines.launch

@Composable
fun JetnewsApp(appContainer: AppContainer, widthSizeClass: WindowWidthSizeClass) {
fun JetnewsApp(appContainer: AppContainer, widthSizeClass: WindowWidthSizeClass, deeplinkKey: HomeKey?) {
JetnewsTheme {
val navController = rememberNavController()
val navigationActions = remember(navController) {
JetnewsNavigationActions(navController)
}

val coroutineScope = rememberCoroutineScope()

val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute =
navBackStackEntry?.destination?.route ?: JetnewsDestinations.HOME_ROUTE

val isExpandedScreen = widthSizeClass == WindowWidthSizeClass.Expanded
val sizeAwareDrawerState = rememberSizeAwareDrawerState(isExpandedScreen)

val navigationState = rememberNavigationState(deeplinkKey ?: HomeKey())
val navigator = Navigator(navigationState)

ModalNavigationDrawer(
drawerContent = {
AppDrawer(
drawerState = sizeAwareDrawerState,
currentRoute = currentRoute,
navigateToHome = navigationActions.navigateToHome,
navigateToInterests = navigationActions.navigateToInterests,
currentKey = navigationState.currentKey,
navigateToHome = navigator::toHome,
navigateToInterests = navigator::toInterests,
closeDrawer = { coroutineScope.launch { sizeAwareDrawerState.close() } },
)
},
Expand All @@ -67,15 +64,16 @@ fun JetnewsApp(appContainer: AppContainer, widthSizeClass: WindowWidthSizeClass)
Row {
if (isExpandedScreen) {
AppNavRail(
currentRoute = currentRoute,
navigateToHome = navigationActions.navigateToHome,
navigateToInterests = navigationActions.navigateToInterests,
currentKey = navigationState.currentKey,
navigateToHome = navigator::toHome,
navigateToInterests = navigator::toInterests,
)
}
JetnewsNavGraph(
JetnewsNavDisplay(
navigationState = navigationState,
appContainer = appContainer,
onBack = navigator::pop,
isExpandedScreen = isExpandedScreen,
navController = navController,
openDrawer = { coroutineScope.launch { sizeAwareDrawerState.open() } },
)
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package com.example.jetnews.ui

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.navigation3.runtime.NavKey
import com.example.jetnews.JetnewsApplication
import com.example.jetnews.ui.navigation.HomeKey
import com.example.jetnews.ui.navigation.POST_ID

class MainActivity : ComponentActivity() {

Expand All @@ -34,7 +39,18 @@ class MainActivity : ComponentActivity() {
val appContainer = (application as JetnewsApplication).container
setContent {
val widthSizeClass = calculateWindowSizeClass(this).widthSizeClass
JetnewsApp(appContainer, widthSizeClass)
JetnewsApp(appContainer, widthSizeClass, getDeepLinkKey(intent))
}
}
}

private fun getDeepLinkKey(intent: Intent): HomeKey? {
val uri: Uri = intent.data ?: return null
val pathParams = uri.pathSegments
if (pathParams.lastOrNull() != "home") return null

val queryParams = uri.getQueryParameters(POST_ID)
if (queryParams.isEmpty() || queryParams.size > 1) return null
// "https://developer.android.com/jetnews/home?postId={$POST_ID}"
return HomeKey(postId = Uri.decode(queryParams.firstOrNull()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation3.runtime.NavKey
import com.example.jetnews.R
import com.example.jetnews.ui.JetnewsDestinations
import com.example.jetnews.ui.navigation.HomeKey
import com.example.jetnews.ui.navigation.InterestsKey
import com.example.jetnews.ui.theme.JetnewsTheme

@Composable
fun AppNavRail(currentRoute: String, navigateToHome: () -> Unit, navigateToInterests: () -> Unit, modifier: Modifier = Modifier) {
fun AppNavRail(currentKey: NavKey, navigateToHome: () -> Unit, navigateToInterests: () -> Unit, modifier: Modifier = Modifier) {
NavigationRail(
header = {
Icon(
Expand All @@ -49,14 +51,14 @@ fun AppNavRail(currentRoute: String, navigateToHome: () -> Unit, navigateToInter
) {
Spacer(Modifier.weight(1f))
NavigationRailItem(
selected = currentRoute == JetnewsDestinations.HOME_ROUTE,
selected = currentKey is HomeKey,
onClick = navigateToHome,
icon = { Icon(painterResource(id = R.drawable.ic_home), stringResource(R.string.home_title)) },
label = { Text(stringResource(R.string.home_title)) },
alwaysShowLabel = false,
)
NavigationRailItem(
selected = currentRoute == JetnewsDestinations.INTERESTS_ROUTE,
selected = currentKey == InterestsKey,
onClick = navigateToInterests,
icon = { Icon(painterResource(id = R.drawable.ic_list_alt), stringResource(R.string.interests_title)) },
label = { Text(stringResource(R.string.interests_title)) },
Expand All @@ -72,7 +74,7 @@ fun AppNavRail(currentRoute: String, navigateToHome: () -> Unit, navigateToInter
fun PreviewAppNavRail() {
JetnewsTheme {
AppNavRail(
currentRoute = JetnewsDestinations.HOME_ROUTE,
currentKey = HomeKey(),
navigateToHome = {},
navigateToInterests = {},
)
Expand Down
Loading