Skip to content

Commit

Permalink
Merge branch 'compose-1.1' into compose-1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Dec 29, 2022
2 parents 3ba4e25 + b20ba70 commit 5099573
Show file tree
Hide file tree
Showing 68 changed files with 1,752 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
53 changes: 53 additions & 0 deletions compose-destinations-wear/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
plugins {
id("com.android.library")
kotlin("android")
}

apply(from = "${rootProject.projectDir}/publish.gradle")

android {

compileSdk = libs.versions.compileSdk.get().toIntOrNull()

defaultConfig {
minSdk = libs.versions.minSdk.get().toIntOrNull()
targetSdk = libs.versions.targetSdk.get().toIntOrNull()

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles.add(File("consumer-rules.pro"))
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
}

dependencies {
api(project(mapOf("path" to ":compose-destinations")))

api(libs.wear.compose.navigation)
}
Empty file.
2 changes: 2 additions & 0 deletions compose-destinations-wear/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POM_ARTIFACT_ID=wear-core
POM_NAME=wear-core
21 changes: 21 additions & 0 deletions compose-destinations-wear/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
5 changes: 5 additions & 0 deletions compose-destinations-wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ramcosta.composedestinations.wear">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.ramcosta.composedestinations.wear

import android.annotation.SuppressLint
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.*
import androidx.wear.compose.navigation.*
import com.ramcosta.composedestinations.annotation.InternalDestinationsApi
import com.ramcosta.composedestinations.manualcomposablecalls.DestinationLambda
import com.ramcosta.composedestinations.manualcomposablecalls.ManualComposableCalls
import com.ramcosta.composedestinations.navigation.DependenciesContainerBuilder
import com.ramcosta.composedestinations.rememberNavHostEngine
import com.ramcosta.composedestinations.scope.DestinationScopeImpl
import com.ramcosta.composedestinations.spec.*

/**
* Returns the [WearNavHostEngine] to be used with Wear OS apps.
*/
@Composable
fun rememberWearNavHostEngine(
state: SwipeDismissableNavHostState = rememberSwipeDismissableNavHostState(),
): NavHostEngine {
val defaultNavHostEngine = rememberNavHostEngine()

return remember {
WearNavHostEngine(defaultNavHostEngine, state)
}
}

internal class WearNavHostEngine(
private val defaultNavHostEngine: NavHostEngine,
private val state: SwipeDismissableNavHostState,
) : NavHostEngine {

override val type = NavHostEngine.Type.WEAR

@Composable
override fun rememberNavController(
vararg navigators: Navigator<out NavDestination>
) =
androidx.navigation.compose.rememberNavController(remember { WearNavigator() }, *navigators)

@Composable
override fun NavHost(
modifier: Modifier,
route: String,
startRoute: Route,
navController: NavHostController,
builder: NavGraphBuilder.() -> Unit
) {
SwipeDismissableNavHost(
navController = navController,
startDestination = startRoute.route,
modifier = modifier,
route = route,
state = state,
builder = builder
)
}

override fun NavGraphBuilder.navigation(
navGraph: NavGraphSpec,
builder: NavGraphBuilder.() -> Unit
) {
with(defaultNavHostEngine) { navigation(navGraph, builder) }
}

@OptIn(ExperimentalAnimationApi::class, InternalDestinationsApi::class)
override fun <T> NavGraphBuilder.composable(
destination: DestinationSpec<T>,
navController: NavHostController,
dependenciesContainerBuilder: @Composable DependenciesContainerBuilder<*>.() -> Unit,
manualComposableCalls: ManualComposableCalls,
) {
when (destination.style) {
is DestinationStyle.Runtime,
is DestinationStyle.Default -> {
addComposable(
destination,
navController,
dependenciesContainerBuilder,
manualComposableCalls
)
}

is DestinationStyle.Activity -> {
with(defaultNavHostEngine) {
composable(destination, navController, dependenciesContainerBuilder, manualComposableCalls)
}
}

is DestinationStyle.Dialog,
is DestinationStyle.Animated,
is DestinationStyle.BottomSheet -> {
throw IllegalStateException("${destination.style.javaClass.name} cannot be used on Wear OS version of the core library!")
}
}
}

private fun <T> NavGraphBuilder.addComposable(
destination: DestinationSpec<T>,
navController: NavHostController,
dependenciesContainerBuilder: @Composable DependenciesContainerBuilder<*>.() -> Unit,
manualComposableCalls: ManualComposableCalls,
) {
@SuppressLint("RestrictedApi")
val contentLambda = manualComposableCalls[destination.baseRoute]

composable(
route = destination.route,
arguments = destination.arguments,
deepLinks = destination.deepLinks
) { navBackStackEntry ->
CallComposable(
destination,
navController,
navBackStackEntry,
dependenciesContainerBuilder,
contentLambda
)
}
}

internal class WearDestinationScope<T>(
destination: DestinationSpec<T>,
navBackStackEntry: NavBackStackEntry,
navController: NavController,
) : DestinationScopeImpl<T>(
destination,
navBackStackEntry,
navController,
)

@Suppress("UNCHECKED_CAST")
@Composable
private fun <T> CallComposable(
destination: DestinationSpec<T>,
navController: NavHostController,
navBackStackEntry: NavBackStackEntry,
dependenciesContainerBuilder: @Composable DependenciesContainerBuilder<*>.() -> Unit,
contentLambda: DestinationLambda<*>?
) {
val scope = remember {
WearDestinationScope(
destination,
navBackStackEntry,
navController
)
}

if (contentLambda == null) {
with(destination) { scope.Content(dependenciesContainerBuilder) }
} else {
contentLambda as DestinationLambda<T>
contentLambda(scope)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ManualComposableCallsBuilder internal constructor(
private fun ManualComposableCallsBuilder.validateAnimated(
destination: DestinationSpec<*>
) {
if (engineType == NavHostEngine.Type.DEFAULT) {
if (engineType != NavHostEngine.Type.ANIMATED) {
error("'animatedComposable' can only be called with a 'AnimatedNavHostEngine'")
}

Expand All @@ -125,7 +125,7 @@ private fun ManualComposableCallsBuilder.validateAnimated(
private fun ManualComposableCallsBuilder.validateBottomSheet(
destination: DestinationSpec<*>
) {
if (engineType == NavHostEngine.Type.DEFAULT) {
if (engineType != NavHostEngine.Type.ANIMATED) {
error("'bottomSheetComposable' can only be called with a 'AnimatedNavHostEngine'")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ interface NavHostEngine {
* The engine you get if using "io.github.raamcosta.compose-destinations:animations-core"
* and calling `rememberAnimatedNavHostEngine`
*/
ANIMATED
ANIMATED,

/**
* The engine you get if using "io.github.raamcosta.compose-destinations:wear-core"
* and calling `rememberWearNavHostEngine`
*/
WEAR
}

/**
Expand Down
8 changes: 8 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ktxSerialization = "1.4.1"
mockk = "1.13.2"

compileTesting = "1.4.9"
composeWear = "1.1.0"

[plugins]
dependencyCheckPlugin = { id = "com.github.ben-manes.versions", version.ref = "dependencyCheckPluginVersion" }
Expand Down Expand Up @@ -69,3 +70,10 @@ test-junit = { module = "junit:junit", version.ref = "junit" }
test-mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
test-kotlinCompile = { module = "com.github.tschuchortdev:kotlin-compile-testing", version.ref = "compileTesting" }
test-kotlinCompileKsp = { module = "com.github.tschuchortdev:kotlin-compile-testing-ksp", version.ref = "compileTesting" }

# Wear

wear-compose-navigation = { module = "androidx.wear.compose:compose-navigation", version.ref = "composeWear" }
wear-compose-foundation = { module = "androidx.wear.compose:compose-foundation", version.ref = "composeWear" }
wear-compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "composeWear" }
wear-input = { module = "androidx.wear:wear-input", version.ref = "composeWear" }
1 change: 1 addition & 0 deletions playground-core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
plugins {
id("java-library")
id("kotlin")
kotlin("plugin.serialization")
id("org.jetbrains.kotlin.jvm")
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ramcosta.samples.playgroundshared
package com.ramcosta.playground.core

data class BlogPostArgs(
val slug: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ramcosta.samples.playgroundshared
package com.ramcosta.playground.core

data class WithDefaultValueArgs(
val isCreate: Boolean = false,
Expand Down
2 changes: 1 addition & 1 deletion playground/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ android {
dependencies {

implementation(project(mapOf("path" to ":compose-destinations-animations")))
implementation(project(mapOf("path" to ":playground-shared")))
implementation(project(mapOf("path" to ":playground-core")))
ksp(project(":compose-destinations-ksp"))

implementation(libs.androidMaterial)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class ProcessorProviderTests {
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.samples.playgroundshared.BlogPostArgs
import com.ramcosta.playground.core.BlogPostArgs
@RootNavGraph(start = true)
@Destination(route = "test1")
Expand Down Expand Up @@ -398,7 +398,7 @@ class ProcessorProviderTests {
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.samples.playgroundshared.WithDefaultValueArgs
import com.ramcosta.playground.core.WithDefaultValueArgs
@RootNavGraph(start = true)
@Destination(route = "test1")
Expand Down
1 change: 1 addition & 0 deletions sample-wear/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading

0 comments on commit 5099573

Please sign in to comment.