Skip to content

Commit

Permalink
Merge pull request #160 from Tlaster/setup_rework
Browse files Browse the repository at this point in the history
rework for precompose setup
  • Loading branch information
Tlaster authored Nov 2, 2023
2 parents 3e41077 + fa1d30e commit 4db9b3b
Show file tree
Hide file tree
Showing 29 changed files with 522 additions and 427 deletions.
172 changes: 88 additions & 84 deletions docs/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,61 @@ This sample demonstrates the following features:
```kotlin
@Composable
fun App() {
val navigator = rememberNavigator()
MaterialTheme {
NavHost(
navigator = navigator,
initialRoute = "/home"
) {
scene(route = "/home") {
val homeViewModel = viewModel {
HomeViewModel()
}
val name by homeViewModel.name.observeAsState()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Greet Me!",
style = MaterialTheme.typography.h6
)
Spacer(modifier = Modifier.height(30.dp))
TextField(
value = name,
maxLines = 1,
label = { Text(text = "Enter your name") },
onValueChange = {
homeViewModel.setName(it)
}
)
Spacer(modifier = Modifier.height(30.dp))
Button(
onClick = {
navigator.navigate(route = "/greeting/$name")
}
) {
Text(text = "GO!")
PreComposeApp {
val navigator = rememberNavigator()
MaterialTheme {
NavHost(
navigator = navigator,
initialRoute = "/home"
) {
scene(route = "/home") {
val homeViewModel = viewModel {
HomeViewModel()
}
}
}
scene(route = "/greeting/{name}") { backStackEntry ->
backStackEntry.path<String>("name")?.let { name ->
val name by homeViewModel.name.observeAsState()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = name,
text = "Greet Me!",
style = MaterialTheme.typography.h6
)
Spacer(modifier = Modifier.height(30.dp))
Button(onClick = { navigator.goBack() }) {
Text(text = "GO BACK!")
TextField(
value = name,
maxLines = 1,
label = { Text(text = "Enter your name") },
onValueChange = {
homeViewModel.setName(it)
}
)
Spacer(modifier = Modifier.height(30.dp))
Button(
onClick = {
navigator.navigate(route = "/greeting/$name")
}
) {
Text(text = "GO!")
}
}
}
scene(route = "/greeting/{name}") { backStackEntry ->
backStackEntry.path<String>("name")?.let { name ->
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = name,
style = MaterialTheme.typography.h6
)
Spacer(modifier = Modifier.height(30.dp))
Button(onClick = { navigator.goBack() }) {
Text(text = "GO BACK!")
}
}
}
}
Expand All @@ -93,57 +95,59 @@ class HomeViewModel : ViewModel() {
```kotlin
@Composable
fun App() {
val navigator = rememberNavigator()
MaterialTheme {
NavHost(
navigator = navigator,
initialRoute = "/home"
) {
scene(route = "/home") {
val homeViewModel = viewModel {
HomeViewModel()
}
val name by homeViewModel.name.collectAsStateWithLifecycle()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Greet Me!",
style = MaterialTheme.typography.h6
)
Spacer(modifier = Modifier.height(30.dp))
TextField(
value = name,
maxLines = 1,
label = { Text(text = "Enter your name") },
onValueChange = homeViewModel::setName
)
Spacer(modifier = Modifier.height(30.dp))
Button(
onClick = {
navigator.navigate(route = "/greeting/$name")
}
) {
Text(text = "GO!")
PreComposeApp {
val navigator = rememberNavigator()
MaterialTheme {
NavHost(
navigator = navigator,
initialRoute = "/home"
) {
scene(route = "/home") {
val homeViewModel = viewModel {
HomeViewModel()
}
}
}
scene(route = "/greeting/{name}") { backStackEntry ->
backStackEntry.path<String>("name")?.let { name ->
val name by homeViewModel.name.collectAsStateWithLifecycle()
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = name,
text = "Greet Me!",
style = MaterialTheme.typography.h6
)
Spacer(modifier = Modifier.height(30.dp))
Button(onClick = navigator::goBack) {
Text(text = "GO BACK!")
TextField(
value = name,
maxLines = 1,
label = { Text(text = "Enter your name") },
onValueChange = homeViewModel::setName
)
Spacer(modifier = Modifier.height(30.dp))
Button(
onClick = {
navigator.navigate(route = "/greeting/$name")
}
) {
Text(text = "GO!")
}
}
}
scene(route = "/greeting/{name}") { backStackEntry ->
backStackEntry.path<String>("name")?.let { name ->
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = name,
style = MaterialTheme.typography.h6
)
Spacer(modifier = Modifier.height(30.dp))
Button(onClick = navigator::goBack) {
Text(text = "GO BACK!")
}
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ api("moe.tlaster:precompose:$precompose_version")
// api("moe.tlaster:precompose-koin:$precompose_version") // For Koin intergration
```
## Android
Change the `androidx.activity.compose.setContent` to `moe.tlaster.precompose.lifecycle.setContent`

## Desktop (JVM)
Change the `Window` to `moe.tlaster.precompose.PreComposeWindow`
## Wrap the `App()`

## iOS
Set the `UIWindow.rootViewController` to `PreComposeApplication`

## Native macOS
Change the `Window` to `moe.tlaster.precompose.PreComposeWindow`

## Web (Canvas)
Change the `Window` to `moe.tlaster.precompose.preComposeWindow`
Wrap your App with `PreComposApp` like this:
```Kotlin
fun App() {
PreComposeApp {
// your apps content gose here
}
}
```

## Done!
That's it! Enjoying the PreCompose! Now you can write all your business logic and ui code in `commonMain`
12 changes: 7 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[versions] # also check project root build.gradle.kts for versions
androidx-animation = "1.5.0"
androidx-foundation = "1.5.0"
androidx-animation = "1.5.3"
androidx-foundation = "1.5.3"
androidx-appcompat = "1.6.1"
androidx-coreKtx = "1.10.1"
androidxActivityVer = "1.7.2"
androidx-coreKtx = "1.12.0"
androidxActivityVer = "1.8.0"
androidGradlePlugin = "8.1.1"
junit = "4.13.2"
junitJupiterEngine = "5.10.0"
junitJupiterApi = "5.10.0"
kotlin = "1.9.20"
kotlinxCoroutinesCore = "1.7.3"
lifecycleRuntimeKtx = "2.6.1"
lifecycleRuntimeKtx = "2.6.2"
material = "1.5.0"
moleculeRuntime = "1.3.0"
savedstateKtx = "1.2.1"
Expand All @@ -22,9 +22,11 @@ koin-compose = "1.1.0"

[libraries]
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "androidxActivityVer" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidxActivityVer" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
androidx-coreKtx = { module = "androidx.core:core-ktx", version.ref = "androidx-coreKtx" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
androidx-material = { module = "androidx.compose.material:material", version.ref = "material" }
androidx-savedstate-ktx = { module = "androidx.savedstate:savedstate-ktx", version.ref = "savedstateKtx" }
animation = { module = "androidx.compose.animation:animation", version.ref = "androidx-animation" }
Expand Down
3 changes: 2 additions & 1 deletion precompose-koin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ group = "moe.tlaster"
version = rootProject.extra.get("precomposeVersion") as String

kotlin {
targetHierarchy.default {

applyDefaultHierarchyTemplate {
common {
group("jvmAndroid") {
withAndroidTarget()
Expand Down
20 changes: 3 additions & 17 deletions precompose-molecule/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ group = "moe.tlaster"
version = rootProject.extra.get("precomposeVersion") as String

kotlin {
applyDefaultHierarchyTemplate()
androidTarget {
publishLibraryVariants("release", "debug")
}
Expand Down Expand Up @@ -82,33 +83,18 @@ kotlin {
implementation(kotlin("test-js"))
}
}
val macosMain by creating {
val macosMain by getting {
dependsOn(commonMain)
dependencies {
implementation(compose.foundation)
}
}
val macosX64Main by getting {
dependsOn(macosMain)
}
val macosArm64Main by getting {
dependsOn(macosMain)
}
val iosMain by creating {
val iosMain by getting {
dependsOn(commonMain)
dependencies {
implementation(compose.foundation)
}
}
val iosX64Main by getting {
dependsOn(iosMain)
}
val iosArm64Main by getting {
dependsOn(iosMain)
}
val iosSimulatorArm64Main by getting {
dependsOn(iosMain)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private class PresenterHolder<T>(
RecompositionMode.ContextClock
}
private val scope = CoroutineScope(dispatcher)
val state = scope.launchMolecule(clock, body)
val state = scope.launchMolecule(mode = clock, body = body)

override fun close() {
scope.cancel()
Expand Down
2 changes: 1 addition & 1 deletion precompose-viewmodel/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ group = "moe.tlaster"
version = rootProject.extra.get("precomposeVersion") as String

kotlin {
targetHierarchy.default {
applyDefaultHierarchyTemplate {
common {
group("jvmAndroid") {
withAndroidTarget()
Expand Down
4 changes: 3 additions & 1 deletion precompose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ group = "moe.tlaster"
version = rootProject.extra.get("precomposeVersion") as String

kotlin {
targetHierarchy.default()
applyDefaultHierarchyTemplate()
macosArm64()
macosX64()
iosX64()
Expand Down Expand Up @@ -61,6 +61,8 @@ kotlin {
api(libs.androidx.appcompat)
implementation(libs.androidx.lifecycle.runtime.ktx)
api(libs.androidx.savedstate.ktx)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.activity.compose)
}
}
val androidUnitTest by getting {
Expand Down
Loading

0 comments on commit 4db9b3b

Please sign in to comment.