-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Type safe compose navigation (#37)
* create compose navigation convention plugin * wire in the compose navigation convention plugin in presentation module + type safe nav * code cleanup * updated readme
- Loading branch information
Showing
11 changed files
with
87 additions
and
148 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
18 changes: 18 additions & 0 deletions
18
...src/main/kotlin/com/nisrulz/example/spacexapi/AndroidComposeNavigationConventionPlugin.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,18 @@ | ||
package com.nisrulz.example.spacexapi | ||
|
||
import com.nisrulz.example.spacexapi.ktx.configureAndroidComposeNavigation | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
|
||
class AndroidComposeNavigationConventionPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
with(target) { | ||
with(pluginManager) { | ||
apply("com.android.library") | ||
apply("org.jetbrains.kotlin.plugin.serialization") | ||
} | ||
|
||
configureAndroidComposeNavigation() | ||
} | ||
} | ||
} |
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
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
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
105 changes: 7 additions & 98 deletions
105
...on/src/main/java/com/nisrulz/example/spacexapi/presentation/navigation/NavigationRoute.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 |
---|---|---|
@@ -1,104 +1,13 @@ | ||
package com.nisrulz.example.spacexapi.presentation.navigation | ||
|
||
import androidx.compose.animation.core.LinearEasing | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.animation.fadeIn | ||
import androidx.compose.animation.fadeOut | ||
import androidx.navigation.NavBackStackEntry | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.compose.composable | ||
import com.nisrulz.example.spacexapi.presentation.features.bookmarkedlaunches.BookmarkedLaunchesScreen | ||
import com.nisrulz.example.spacexapi.presentation.features.launchdetail.LaunchDetailScreen | ||
import com.nisrulz.example.spacexapi.presentation.features.listoflaunches.ListOfLaunchesScreen | ||
import kotlinx.serialization.Serializable | ||
|
||
/* | ||
Read about Type safety in Navigation Compose: | ||
https://developer.android.com/guide/navigation/design/type-safety | ||
*/ | ||
internal object NavigationRoute { | ||
// Home | ||
const val HOME_ROUTE = "list_of_launches" | ||
|
||
// Bookmark | ||
private const val BOOKMARK_ROUTE = "bookmarked_launches" | ||
@Serializable | ||
object RouteHome | ||
|
||
// Details | ||
private const val NAV_ARG_LAUNCH_ID = "launchId" | ||
private const val DETAILS_ROUTE = "launch_detail/{$NAV_ARG_LAUNCH_ID}" | ||
@Serializable | ||
object RouteBookmark | ||
|
||
private fun buildDetailsRouteWithLaunchId(launchId: String) = DETAILS_ROUTE | ||
.replace("{$NAV_ARG_LAUNCH_ID}", launchId) | ||
|
||
// Functions | ||
private fun NavBackStackEntry.getArgLaunchId(): String = arguments | ||
?.getString(NAV_ARG_LAUNCH_ID) ?: "" | ||
|
||
fun NavGraphBuilder.homeScreen( | ||
onNavigateToDetails: (launchId: String) -> Unit, | ||
onNavigateToBookmarks: () -> Unit | ||
) { | ||
composable( | ||
HOME_ROUTE | ||
) { | ||
ListOfLaunchesScreen(navigateToDetails = { launchId -> | ||
onNavigateToDetails(launchId) | ||
}, navigateToBookmarks = { | ||
onNavigateToBookmarks() | ||
}) | ||
} | ||
} | ||
|
||
fun NavGraphBuilder.bookmarkScreen( | ||
onNavigateToDetails: (launchId: String) -> Unit, | ||
onBackAction: () -> Unit | ||
) { | ||
composable( | ||
BOOKMARK_ROUTE | ||
) { | ||
BookmarkedLaunchesScreen( | ||
navigateToDetails = { launchId -> | ||
onNavigateToDetails(launchId) | ||
}, | ||
navigateBack = onBackAction | ||
) | ||
} | ||
} | ||
|
||
fun NavGraphBuilder.detailsScreen(onBackAction: () -> Unit) { | ||
composable( | ||
DETAILS_ROUTE | ||
) { backStackEntry -> | ||
val id = backStackEntry.getArgLaunchId() | ||
if (id.isNotEmpty()) { | ||
LaunchDetailScreen(launchId = id, onBackAction = onBackAction) | ||
} | ||
} | ||
} | ||
|
||
fun NavController.navigateToBookmarks() { | ||
this.navigate(BOOKMARK_ROUTE) | ||
} | ||
|
||
fun NavController.navigateToLaunchDetail(launchId: String) { | ||
this.navigate(buildDetailsRouteWithLaunchId(launchId)) | ||
} | ||
|
||
fun NavController.navigateBack() { | ||
this.popBackStack() | ||
} | ||
|
||
private fun customFadeIn() = fadeIn( | ||
animationSpec = tween( | ||
300, | ||
easing = LinearEasing | ||
) | ||
) | ||
|
||
private fun customFadeOut() = fadeOut( | ||
animationSpec = tween( | ||
300, | ||
easing = LinearEasing | ||
) | ||
) | ||
} | ||
@Serializable | ||
data class RouteDetails(val launchId: String) |
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