Skip to content

Commit

Permalink
(wip) Setup Router with screen flow binding
Browse files Browse the repository at this point in the history
  • Loading branch information
marcantoinefortier committed Apr 27, 2022
1 parent d5d29bf commit e8d9bd5
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,13 @@ package com.mirego.kmp.boilerplate.android
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
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 com.mirego.kmp.boilerplate.Greeting
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf

import com.mirego.kmp.boilerplate.android.composables.Main

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Greeting(textFlow = Greeting().greeting())
Main()
}
}
}

@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)
}
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)
}
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())
}
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()
}
}
22 changes: 17 additions & 5 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
058557BB273AAA24004C7B11 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557BA273AAA24004C7B11 /* Assets.xcassets */; };
058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; };
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; };
9B8ACFDB4E332DFCA8B97CBB /* Pods_iosApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4E4E1328B104D05A50A097EE /* Pods_iosApp.framework */; };
BC5CC7B427760F2C00426C97 /* Home.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* Home.swift */; };
BC64D820278C9DD700FAF397 /* Main.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC64D81F278C9DD700FAF397 /* Main.swift */; };
BC83B466276E4F080053E064 /* FlowUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC83B465276E4F080053E064 /* FlowUtils.swift */; };
/* End PBXBuildFile section */

Expand All @@ -22,8 +23,9 @@
308A8A1989CCC0B3DD133EE0 /* Pods-iosApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosApp.debug.xcconfig"; path = "Target Support Files/Pods-iosApp/Pods-iosApp.debug.xcconfig"; sourceTree = "<group>"; };
4E4E1328B104D05A50A097EE /* Pods_iosApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iosApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7555FF7B242A565900829871 /* iosApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iosApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
7555FF82242A565900829871 /* Home.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Home.swift; sourceTree = "<group>"; };
7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BC64D81F278C9DD700FAF397 /* Main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Main.swift; sourceTree = "<group>"; };
BC83B465276E4F080053E064 /* FlowUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlowUtils.swift; sourceTree = "<group>"; };
E232C917135C2C1E3BC8748A /* Pods-iosApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosApp.release.xcconfig"; path = "Target Support Files/Pods-iosApp/Pods-iosApp.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -69,16 +71,25 @@
7555FF7D242A565900829871 /* iosApp */ = {
isa = PBXGroup;
children = (
BC83B464276E4EF80053E064 /* Utils */,
058557BA273AAA24004C7B11 /* Assets.xcassets */,
7555FF82242A565900829871 /* ContentView.swift */,
7555FF8C242A565B00829871 /* Info.plist */,
2152FB032600AC8F00CF470E /* iOSApp.swift */,
BC5CC7B327760ED800426C97 /* Views */,
BC83B464276E4EF80053E064 /* Utils */,
058557D7273AAEEB004C7B11 /* Preview Content */,
);
path = iosApp;
sourceTree = "<group>";
};
BC5CC7B327760ED800426C97 /* Views */ = {
isa = PBXGroup;
children = (
BC64D81F278C9DD700FAF397 /* Main.swift */,
7555FF82242A565900829871 /* Home.swift */,
);
path = Views;
sourceTree = "<group>";
};
BC83B464276E4EF80053E064 /* Utils */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -201,8 +212,9 @@
buildActionMask = 2147483647;
files = (
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */,
7555FF83242A565900829871 /* ContentView.swift in Sources */,
BC83B466276E4F080053E064 /* FlowUtils.swift in Sources */,
BC64D820278C9DD700FAF397 /* Main.swift in Sources */,
BC5CC7B427760F2C00426C97 /* Home.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI
import shared

struct ContentView: View {
struct Home: View {

@ObservedObject var greet = ObservableFlowWrapper<NSString>(Greeting().greeting(), initial: "initial")

Expand All @@ -10,8 +10,8 @@ struct ContentView: View {
}
}

struct ContentView_Previews: PreviewProvider {
struct Home_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Home()
}
}
14 changes: 14 additions & 0 deletions iosApp/iosApp/Views/Main.swift
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()
}
}
}
4 changes: 2 additions & 2 deletions iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
struct iOSApp: App {
var body: some Scene {
WindowGroup {
ContentView()
Main()
}
}
}
}
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
}
}
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()
}

0 comments on commit e8d9bd5

Please sign in to comment.