Skip to content

Commit dec16b1

Browse files
committed
For touchlab#305 - Use Compose Multiplatform to show the UI from :shared on iOS
1 parent c0f6928 commit dec16b1

File tree

11 files changed

+121
-69
lines changed

11 files changed

+121
-69
lines changed

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ androidx-lifecycle = "2.6.1"
2323

2424
junit = "4.13.2"
2525

26-
coroutines = "1.7.0"
26+
coroutines = "1.7.2"
2727
kotlinx-datetime = "0.4.0"
2828
ktor = "2.3.2"
2929

ios/KaMPKitiOS.xcodeproj/project.pbxproj

+12-24
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0AE285002A6AD6B7000B6B60 /* ComposeContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE284FF2A6AD6B7000B6B60 /* ComposeContentView.swift */; };
11+
0AE285022A6AD7BD000B6B60 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE285012A6AD7BD000B6B60 /* ContentView.swift */; };
12+
0AE285042A6AD7D0000B6B60 /* IOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE285032A6AD7D0000B6B60 /* IOSApp.swift */; };
1013
3DFF917C64A18A83DA010EE1 /* Pods_KaMPKitiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */; };
11-
461C74AA2788F5F3004B1FFC /* CombineAdapters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461C74A92788F5F3004B1FFC /* CombineAdapters.swift */; };
12-
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */; };
13-
46A5B60826B04921002EFEAA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 46A5B60626B04920002EFEAA /* Main.storyboard */; };
1414
46B5284D249C5CF400A7725D /* Koin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B5284C249C5CF400A7725D /* Koin.swift */; };
15-
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */; };
1615
F1465F0A23AA94BF0055F7C3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F1465F0923AA94BF0055F7C3 /* Assets.xcassets */; };
1716
F1465F0D23AA94BF0055F7C3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */; };
1817
F1465F1823AA94C00055F7C3 /* KaMPKitiOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1465F1723AA94C00055F7C3 /* KaMPKitiOSTests.swift */; };
@@ -37,16 +36,15 @@
3736
/* End PBXContainerItemProxy section */
3837

3938
/* Begin PBXFileReference section */
39+
0AE284FF2A6AD6B7000B6B60 /* ComposeContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeContentView.swift; sourceTree = "<group>"; };
40+
0AE285012A6AD7BD000B6B60 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
41+
0AE285032A6AD7D0000B6B60 /* IOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOSApp.swift; sourceTree = "<group>"; };
4042
1DFCC00C8DAA719770A18D1A /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; };
4143
2A1ED6A4A2A53F5F75C58E5F /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; };
42-
461C74A92788F5F3004B1FFC /* CombineAdapters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineAdapters.swift; sourceTree = "<group>"; };
43-
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedListScreen.swift; sourceTree = "<group>"; };
44-
46A5B60726B04920002EFEAA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
4544
46B5284C249C5CF400A7725D /* Koin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Koin.swift; sourceTree = "<group>"; };
4645
B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_KaMPKitiOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4746
ED1F782AF3705197012D0C33 /* Pods-KaMPKitiOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.debug.xcconfig"; sourceTree = "<group>"; };
4847
F1465EFD23AA94BF0055F7C3 /* KaMPKitiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KaMPKitiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
49-
F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5048
F1465F0923AA94BF0055F7C3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5149
F1465F0C23AA94BF0055F7C3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
5250
F1465F0E23AA94BF0055F7C3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -129,14 +127,13 @@
129127
F1465EFF23AA94BF0055F7C3 /* KaMPKitiOS */ = {
130128
isa = PBXGroup;
131129
children = (
132-
F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */,
133-
46A5B60626B04920002EFEAA /* Main.storyboard */,
134130
46B5284C249C5CF400A7725D /* Koin.swift */,
135131
F1465F0923AA94BF0055F7C3 /* Assets.xcassets */,
136132
F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */,
137133
F1465F0E23AA94BF0055F7C3 /* Info.plist */,
138-
46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */,
139-
461C74A92788F5F3004B1FFC /* CombineAdapters.swift */,
134+
0AE284FF2A6AD6B7000B6B60 /* ComposeContentView.swift */,
135+
0AE285012A6AD7BD000B6B60 /* ContentView.swift */,
136+
0AE285032A6AD7D0000B6B60 /* IOSApp.swift */,
140137
);
141138
path = KaMPKitiOS;
142139
sourceTree = "<group>";
@@ -266,7 +263,6 @@
266263
isa = PBXResourcesBuildPhase;
267264
buildActionMask = 2147483647;
268265
files = (
269-
46A5B60826B04921002EFEAA /* Main.storyboard in Resources */,
270266
F1465F0D23AA94BF0055F7C3 /* LaunchScreen.storyboard in Resources */,
271267
F1465F0A23AA94BF0055F7C3 /* Assets.xcassets in Resources */,
272268
);
@@ -354,9 +350,9 @@
354350
buildActionMask = 2147483647;
355351
files = (
356352
46B5284D249C5CF400A7725D /* Koin.swift in Sources */,
357-
461C74AA2788F5F3004B1FFC /* CombineAdapters.swift in Sources */,
358-
46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */,
359-
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */,
353+
0AE285002A6AD6B7000B6B60 /* ComposeContentView.swift in Sources */,
354+
0AE285022A6AD7BD000B6B60 /* ContentView.swift in Sources */,
355+
0AE285042A6AD7D0000B6B60 /* IOSApp.swift in Sources */,
360356
);
361357
runOnlyForDeploymentPostprocessing = 0;
362358
};
@@ -392,14 +388,6 @@
392388
/* End PBXTargetDependency section */
393389

394390
/* Begin PBXVariantGroup section */
395-
46A5B60626B04920002EFEAA /* Main.storyboard */ = {
396-
isa = PBXVariantGroup;
397-
children = (
398-
46A5B60726B04920002EFEAA /* Base */,
399-
);
400-
name = Main.storyboard;
401-
sourceTree = "<group>";
402-
};
403391
F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */ = {
404392
isa = PBXVariantGroup;
405393
children = (

ios/KaMPKitiOS/AppDelegate.swift

-34
This file was deleted.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// ComposeContentView.swift
3+
// KaMPKitiOS
4+
//
5+
// Created by Petru on 7/21/23.
6+
// Copyright © 2023 Touchlab. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import shared
11+
import SwiftUI
12+
13+
struct ComposeContentView: UIViewControllerRepresentable {
14+
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
15+
// no-op
16+
}
17+
18+
func makeUIViewController(context: Context) -> some UIViewController {
19+
MainViewControllerKt.MainViewController(
20+
logger: koin.loggerWithTag(tag: "ViewController"),
21+
viewModel: KotlinDependencies.shared.getBreedViewModel().viewModel
22+
)
23+
}
24+
}

ios/KaMPKitiOS/ContentView.swift

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// ContentView.swift
3+
// KaMPKitiOS
4+
//
5+
// Created by Petru on 7/21/23.
6+
// Copyright © 2023 Touchlab. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
import shared
11+
12+
struct ContentView: View {
13+
var body: some View {
14+
ComposeContentView()
15+
}
16+
}

ios/KaMPKitiOS/IOSApp.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// iOSApp.swift
3+
// KaMPKitiOS
4+
//
5+
// Created by Petru on 7/21/23.
6+
// Copyright © 2023 Touchlab. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
@main
12+
struct IOSApp: App {
13+
14+
init() {
15+
startKoin()
16+
}
17+
18+
var body: some Scene {
19+
WindowGroup {
20+
ContentView()
21+
}
22+
}
23+
}

ios/KaMPKitiOS/Info.plist

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>UILaunchStoryboardName</key>
24-
<string>LaunchScreen</string>
25-
<key>UIMainStoryboardFile</key>
26-
<string>Main</string>
24+
<string>LaunchScreen.storyboard</string>
2725
<key>UIRequiredDeviceCapabilities</key>
2826
<array>
2927
<string>armv7</string>
3028
</array>
29+
<key>UIStatusBarStyle</key>
30+
<string></string>
3131
<key>UISupportedInterfaceOrientations</key>
3232
<array>
3333
<string>UIInterfaceOrientationPortrait</string>

ios/Pods/Pods.xcodeproj/project.pbxproj

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shared/build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ kotlin {
110110
summary = "Common library for the KaMP starter kit"
111111
homepage = "https://github.com/touchlab/KaMPKit"
112112
framework {
113-
isStatic = false // SwiftUI preview requires dynamic framework
113+
// Below line is needed for Compose Multiplatform, see https://github.com/JetBrains/compose-multiplatform/issues/3178
114+
isStatic = true
114115
linkerOpts("-lsqlite3")
115116
export(libs.touchlab.kermit.simple)
116117
export(libs.moko.resources)

shared/src/commonMain/kotlin/co/touchlab/kampkit/Koin.kt

+14
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,18 @@ internal inline fun <reified T> Scope.getWith(vararg params: Any?): T {
8383
// Simple function to clean up the syntax a bit
8484
fun KoinComponent.injectLogger(tag: String): Lazy<Logger> = inject { parametersOf(tag) }
8585

86+
/**
87+
* Get any instance of `T` if available in the dependencies graph.
88+
*
89+
* @param tag Optional name of allowing to get a specific
90+
*/
91+
inline fun <reified T: Any> injectInstance(tag: String? = null): T {
92+
return object : KoinComponent {
93+
val value: T by when (tag) {
94+
null -> inject()
95+
else -> inject { parametersOf(tag) }
96+
}
97+
}.value
98+
}
99+
86100
expect val platformModule: Module
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package co.touchlab.kampkit.ui
2+
3+
import androidx.compose.foundation.isSystemInDarkTheme
4+
import androidx.compose.ui.window.ComposeUIViewController
5+
import co.touchlab.kampkit.injectInstance
6+
import co.touchlab.kampkit.models.BreedViewModel
7+
import co.touchlab.kampkit.ui.theme.KaMPKitTheme
8+
import co.touchlab.kermit.Logger
9+
import platform.UIKit.UIApplication
10+
import platform.UIKit.UIScreen
11+
import platform.UIKit.UIUserInterfaceStyle
12+
13+
fun MainViewController(logger: Logger, viewModel: BreedViewModel) = ComposeUIViewController {
14+
val isDarkThemeEnabled =
15+
UIScreen.mainScreen.traitCollection.userInterfaceStyle == UIUserInterfaceStyle.UIUserInterfaceStyleDark
16+
17+
KaMPKitTheme(isDarkThemeEnabled, false) {
18+
MainScreen(viewModel, logger)
19+
}
20+
}

0 commit comments

Comments
 (0)