From dec16b1f21e86db2d0277103c2bd77c16746c2e8 Mon Sep 17 00:00:00 2001 From: Mugurell Date: Fri, 21 Jul 2023 17:09:40 +0300 Subject: [PATCH] For #305 - Use Compose Multiplatform to show the UI from :shared on iOS --- gradle/libs.versions.toml | 2 +- ios/KaMPKitiOS.xcodeproj/project.pbxproj | 36 +++++++------------ ios/KaMPKitiOS/AppDelegate.swift | 34 ------------------ ios/KaMPKitiOS/ComposeContentView.swift | 24 +++++++++++++ ios/KaMPKitiOS/ContentView.swift | 16 +++++++++ ios/KaMPKitiOS/IOSApp.swift | 23 ++++++++++++ ios/KaMPKitiOS/Info.plist | 6 ++-- ios/Pods/Pods.xcodeproj/project.pbxproj | 12 +++---- shared/build.gradle.kts | 3 +- .../kotlin/co/touchlab/kampkit/Koin.kt | 14 ++++++++ .../touchlab/kampkit/ui/MainViewController.kt | 20 +++++++++++ 11 files changed, 121 insertions(+), 69 deletions(-) delete mode 100644 ios/KaMPKitiOS/AppDelegate.swift create mode 100644 ios/KaMPKitiOS/ComposeContentView.swift create mode 100644 ios/KaMPKitiOS/ContentView.swift create mode 100644 ios/KaMPKitiOS/IOSApp.swift create mode 100644 shared/src/iosMain/kotlin/co/touchlab/kampkit/ui/MainViewController.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 695c9c94..ba5dac19 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -23,7 +23,7 @@ androidx-lifecycle = "2.6.1" junit = "4.13.2" -coroutines = "1.7.0" +coroutines = "1.7.2" kotlinx-datetime = "0.4.0" ktor = "2.3.2" diff --git a/ios/KaMPKitiOS.xcodeproj/project.pbxproj b/ios/KaMPKitiOS.xcodeproj/project.pbxproj index 957a685d..84b08187 100644 --- a/ios/KaMPKitiOS.xcodeproj/project.pbxproj +++ b/ios/KaMPKitiOS.xcodeproj/project.pbxproj @@ -7,12 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + 0AE285002A6AD6B7000B6B60 /* ComposeContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE284FF2A6AD6B7000B6B60 /* ComposeContentView.swift */; }; + 0AE285022A6AD7BD000B6B60 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE285012A6AD7BD000B6B60 /* ContentView.swift */; }; + 0AE285042A6AD7D0000B6B60 /* IOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE285032A6AD7D0000B6B60 /* IOSApp.swift */; }; 3DFF917C64A18A83DA010EE1 /* Pods_KaMPKitiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */; }; - 461C74AA2788F5F3004B1FFC /* CombineAdapters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461C74A92788F5F3004B1FFC /* CombineAdapters.swift */; }; - 46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */; }; - 46A5B60826B04921002EFEAA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 46A5B60626B04920002EFEAA /* Main.storyboard */; }; 46B5284D249C5CF400A7725D /* Koin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B5284C249C5CF400A7725D /* Koin.swift */; }; - F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */; }; F1465F0A23AA94BF0055F7C3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F1465F0923AA94BF0055F7C3 /* Assets.xcassets */; }; F1465F0D23AA94BF0055F7C3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */; }; F1465F1823AA94C00055F7C3 /* KaMPKitiOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1465F1723AA94C00055F7C3 /* KaMPKitiOSTests.swift */; }; @@ -37,16 +36,15 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 0AE284FF2A6AD6B7000B6B60 /* ComposeContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeContentView.swift; sourceTree = ""; }; + 0AE285012A6AD7BD000B6B60 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 0AE285032A6AD7D0000B6B60 /* IOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOSApp.swift; sourceTree = ""; }; 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 = ""; }; 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 = ""; }; - 461C74A92788F5F3004B1FFC /* CombineAdapters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineAdapters.swift; sourceTree = ""; }; - 46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedListScreen.swift; sourceTree = ""; }; - 46A5B60726B04920002EFEAA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 46B5284C249C5CF400A7725D /* Koin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Koin.swift; sourceTree = ""; }; B859F3FB23133D22AB9DD835 /* Pods_KaMPKitiOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_KaMPKitiOS.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = ""; }; F1465EFD23AA94BF0055F7C3 /* KaMPKitiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KaMPKitiOS.app; sourceTree = BUILT_PRODUCTS_DIR; }; - F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; F1465F0923AA94BF0055F7C3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; F1465F0C23AA94BF0055F7C3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; F1465F0E23AA94BF0055F7C3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -129,14 +127,13 @@ F1465EFF23AA94BF0055F7C3 /* KaMPKitiOS */ = { isa = PBXGroup; children = ( - F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */, - 46A5B60626B04920002EFEAA /* Main.storyboard */, 46B5284C249C5CF400A7725D /* Koin.swift */, F1465F0923AA94BF0055F7C3 /* Assets.xcassets */, F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */, F1465F0E23AA94BF0055F7C3 /* Info.plist */, - 46A5B5EE26AF54F7002EFEAA /* BreedListScreen.swift */, - 461C74A92788F5F3004B1FFC /* CombineAdapters.swift */, + 0AE284FF2A6AD6B7000B6B60 /* ComposeContentView.swift */, + 0AE285012A6AD7BD000B6B60 /* ContentView.swift */, + 0AE285032A6AD7D0000B6B60 /* IOSApp.swift */, ); path = KaMPKitiOS; sourceTree = ""; @@ -266,7 +263,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 46A5B60826B04921002EFEAA /* Main.storyboard in Resources */, F1465F0D23AA94BF0055F7C3 /* LaunchScreen.storyboard in Resources */, F1465F0A23AA94BF0055F7C3 /* Assets.xcassets in Resources */, ); @@ -354,9 +350,9 @@ buildActionMask = 2147483647; files = ( 46B5284D249C5CF400A7725D /* Koin.swift in Sources */, - 461C74AA2788F5F3004B1FFC /* CombineAdapters.swift in Sources */, - 46A5B5EF26AF54F7002EFEAA /* BreedListScreen.swift in Sources */, - F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */, + 0AE285002A6AD6B7000B6B60 /* ComposeContentView.swift in Sources */, + 0AE285022A6AD7BD000B6B60 /* ContentView.swift in Sources */, + 0AE285042A6AD7D0000B6B60 /* IOSApp.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -392,14 +388,6 @@ /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ - 46A5B60626B04920002EFEAA /* Main.storyboard */ = { - isa = PBXVariantGroup; - children = ( - 46A5B60726B04920002EFEAA /* Base */, - ); - name = Main.storyboard; - sourceTree = ""; - }; F1465F0B23AA94BF0055F7C3 /* LaunchScreen.storyboard */ = { isa = PBXVariantGroup; children = ( diff --git a/ios/KaMPKitiOS/AppDelegate.swift b/ios/KaMPKitiOS/AppDelegate.swift deleted file mode 100644 index 25924195..00000000 --- a/ios/KaMPKitiOS/AppDelegate.swift +++ /dev/null @@ -1,34 +0,0 @@ -// -// AppDelegate.swift -// KaMPKitiOS -// -// Created by Kevin Schildhorn on 12/18/19. -// Copyright © 2019 Touchlab. All rights reserved. -// - -import SwiftUI -import shared - -@UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate { - - var window: UIWindow? - - // Lazy so it doesn't try to initialize before startKoin() is called - lazy var log = koin.loggerWithTag(tag: "AppDelegate") - - func application(_ application: UIApplication, didFinishLaunchingWithOptions - launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - - startKoin() - - let viewController = UIHostingController(rootView: BreedListScreen()) - - self.window = UIWindow(frame: UIScreen.main.bounds) - self.window?.rootViewController = viewController - self.window?.makeKeyAndVisible() - - log.v(message: {"App Started"}) - return true - } -} diff --git a/ios/KaMPKitiOS/ComposeContentView.swift b/ios/KaMPKitiOS/ComposeContentView.swift new file mode 100644 index 00000000..733655b7 --- /dev/null +++ b/ios/KaMPKitiOS/ComposeContentView.swift @@ -0,0 +1,24 @@ +// +// ComposeContentView.swift +// KaMPKitiOS +// +// Created by Petru on 7/21/23. +// Copyright © 2023 Touchlab. All rights reserved. +// + +import Foundation +import shared +import SwiftUI + +struct ComposeContentView: UIViewControllerRepresentable { + func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { + // no-op + } + + func makeUIViewController(context: Context) -> some UIViewController { + MainViewControllerKt.MainViewController( + logger: koin.loggerWithTag(tag: "ViewController"), + viewModel: KotlinDependencies.shared.getBreedViewModel().viewModel + ) + } +} diff --git a/ios/KaMPKitiOS/ContentView.swift b/ios/KaMPKitiOS/ContentView.swift new file mode 100644 index 00000000..05074fbe --- /dev/null +++ b/ios/KaMPKitiOS/ContentView.swift @@ -0,0 +1,16 @@ +// +// ContentView.swift +// KaMPKitiOS +// +// Created by Petru on 7/21/23. +// Copyright © 2023 Touchlab. All rights reserved. +// + +import SwiftUI +import shared + +struct ContentView: View { + var body: some View { + ComposeContentView() + } +} diff --git a/ios/KaMPKitiOS/IOSApp.swift b/ios/KaMPKitiOS/IOSApp.swift new file mode 100644 index 00000000..8fd64b39 --- /dev/null +++ b/ios/KaMPKitiOS/IOSApp.swift @@ -0,0 +1,23 @@ +// +// iOSApp.swift +// KaMPKitiOS +// +// Created by Petru on 7/21/23. +// Copyright © 2023 Touchlab. All rights reserved. +// + +import SwiftUI + +@main +struct IOSApp: App { + + init() { + startKoin() + } + + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/ios/KaMPKitiOS/Info.plist b/ios/KaMPKitiOS/Info.plist index af8868b2..b79441c7 100644 --- a/ios/KaMPKitiOS/Info.plist +++ b/ios/KaMPKitiOS/Info.plist @@ -21,13 +21,13 @@ LSRequiresIPhoneOS UILaunchStoryboardName - LaunchScreen - UIMainStoryboardFile - Main + LaunchScreen.storyboard UIRequiredDeviceCapabilities armv7 + UIStatusBarStyle + UISupportedInterfaceOrientations UIInterfaceOrientationPortrait diff --git a/ios/Pods/Pods.xcodeproj/project.pbxproj b/ios/Pods/Pods.xcodeproj/project.pbxproj index bb9e0a26..04900b6c 100644 --- a/ios/Pods/Pods.xcodeproj/project.pbxproj +++ b/ios/Pods/Pods.xcodeproj/project.pbxproj @@ -383,7 +383,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.4; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -446,7 +446,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.4; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -464,7 +464,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -481,7 +481,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -542,7 +542,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS-Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 12.4; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -580,7 +580,7 @@ DYLIB_INSTALL_NAME_BASE = "@rpath"; INFOPLIST_FILE = "Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS-Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 12.4; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 6447a7c1..cad6e133 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -110,7 +110,8 @@ kotlin { summary = "Common library for the KaMP starter kit" homepage = "https://github.com/touchlab/KaMPKit" framework { - isStatic = false // SwiftUI preview requires dynamic framework + // Below line is needed for Compose Multiplatform, see https://github.com/JetBrains/compose-multiplatform/issues/3178 + isStatic = true linkerOpts("-lsqlite3") export(libs.touchlab.kermit.simple) export(libs.moko.resources) diff --git a/shared/src/commonMain/kotlin/co/touchlab/kampkit/Koin.kt b/shared/src/commonMain/kotlin/co/touchlab/kampkit/Koin.kt index db7c2df1..0c002b27 100644 --- a/shared/src/commonMain/kotlin/co/touchlab/kampkit/Koin.kt +++ b/shared/src/commonMain/kotlin/co/touchlab/kampkit/Koin.kt @@ -83,4 +83,18 @@ internal inline fun Scope.getWith(vararg params: Any?): T { // Simple function to clean up the syntax a bit fun KoinComponent.injectLogger(tag: String): Lazy = inject { parametersOf(tag) } +/** + * Get any instance of `T` if available in the dependencies graph. + * + * @param tag Optional name of allowing to get a specific + */ +inline fun injectInstance(tag: String? = null): T { + return object : KoinComponent { + val value: T by when (tag) { + null -> inject() + else -> inject { parametersOf(tag) } + } + }.value +} + expect val platformModule: Module diff --git a/shared/src/iosMain/kotlin/co/touchlab/kampkit/ui/MainViewController.kt b/shared/src/iosMain/kotlin/co/touchlab/kampkit/ui/MainViewController.kt new file mode 100644 index 00000000..186145e9 --- /dev/null +++ b/shared/src/iosMain/kotlin/co/touchlab/kampkit/ui/MainViewController.kt @@ -0,0 +1,20 @@ +package co.touchlab.kampkit.ui + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.ui.window.ComposeUIViewController +import co.touchlab.kampkit.injectInstance +import co.touchlab.kampkit.models.BreedViewModel +import co.touchlab.kampkit.ui.theme.KaMPKitTheme +import co.touchlab.kermit.Logger +import platform.UIKit.UIApplication +import platform.UIKit.UIScreen +import platform.UIKit.UIUserInterfaceStyle + +fun MainViewController(logger: Logger, viewModel: BreedViewModel) = ComposeUIViewController { + val isDarkThemeEnabled = + UIScreen.mainScreen.traitCollection.userInterfaceStyle == UIUserInterfaceStyle.UIUserInterfaceStyleDark + + KaMPKitTheme(isDarkThemeEnabled, false) { + MainScreen(viewModel, logger) + } +}