-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(wip) Setup Router with screen flow binding
- Loading branch information
1 parent
d5d29bf
commit e8d9bd5
Showing
10 changed files
with
116 additions
and
34 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
23 changes: 23 additions & 0 deletions
23
androidApp/src/main/java/com/mirego/kmp/boilerplate/android/composables/Greeting.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,23 @@ | ||
package com.mirego.kmp.boilerplate.android.composables | ||
|
||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
@Composable | ||
fun Greeting(textFlow: Flow<String>) { | ||
val text: String by textFlow.collectAsState("initial") | ||
|
||
Text(text = text) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun PreviewGreeting() { | ||
val textFlow = flowOf("Hello, Android 31") | ||
Greeting(textFlow) | ||
} |
9 changes: 9 additions & 0 deletions
9
androidApp/src/main/java/com/mirego/kmp/boilerplate/android/composables/Home.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,9 @@ | ||
package com.mirego.kmp.boilerplate.android.composables | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.mirego.kmp.boilerplate.Greeting | ||
|
||
@Composable | ||
fun Home() { | ||
Greeting(textFlow = Greeting().greeting()) | ||
} |
16 changes: 16 additions & 0 deletions
16
androidApp/src/main/java/com/mirego/kmp/boilerplate/android/composables/Main.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,16 @@ | ||
package com.mirego.kmp.boilerplate.android.composables | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import com.mirego.kmp.boilerplate.routing.MainRouter | ||
import com.mirego.kmp.boilerplate.routing.Screen | ||
|
||
@Composable | ||
fun Main() { | ||
val screen: Screen by MainRouter.screen.collectAsState(initial = Screen.Home) | ||
|
||
when (screen) { | ||
is Screen.Home -> Home() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import SwiftUI | ||
import shared | ||
|
||
struct Main: View { | ||
|
||
@ObservedObject var screen = ObservableFlowWrapper<Screen>(MainRouter().screen, initial: Screen.Home()) | ||
|
||
var body: some View { | ||
switch screen.value { | ||
case is Screen.Home: | ||
Home() | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import SwiftUI | |
struct iOSApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
ContentView() | ||
Main() | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/routing/Router.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,25 @@ | ||
package com.mirego.kmp.boilerplate.routing | ||
|
||
import com.mirego.kmp.boilerplate.utils.CFlow | ||
import com.mirego.kmp.boilerplate.utils.wrap | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
|
||
interface Router { | ||
val screen: CFlow<Screen> | ||
|
||
fun push(screen: Screen) | ||
} | ||
|
||
object MainRouter : Router { | ||
|
||
private val _screens = MutableStateFlow<List<Screen>>( | ||
listOf(Screen.Home) | ||
) | ||
|
||
override val screen = _screens.map { it.last() }.wrap() | ||
|
||
override fun push(screen: Screen) { | ||
_screens.value += screen | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/routing/Screen.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,5 @@ | ||
package com.mirego.kmp.boilerplate.routing | ||
|
||
sealed class Screen { | ||
object Home: Screen() | ||
} |