From e27f5acf2bb82754a7791a951231ed274534d8bc Mon Sep 17 00:00:00 2001 From: Marc-Antoine Fortier Date: Fri, 1 Dec 2023 15:31:00 -0500 Subject: [PATCH] Expose Platform information as expect-actual (locale, system, version) --- androidApp/build.gradle.kts | 4 +-- .../com/mirego/kmp/boilerplate/Greeting.kt | 6 ++-- gradle.properties | 3 ++ ios/Podfile.lock | 4 +-- ios/iosApp.xcodeproj/project.pbxproj | 4 +-- shared/Shared.podspec | 2 +- shared/build.gradle.kts | 8 ++++- .../com/mirego/kmp/boilerplate/Platform.kt | 5 --- .../kmp/boilerplate/platform/Platform.kt | 25 ++++++++++++++ .../AndroidGreetingTest.kt | 14 -------- .../com/mirego/kmp/boilerplate/Greeting.kt | 12 ++++++- .../com/mirego/kmp/boilerplate/Platform.kt | 5 --- .../kmp/boilerplate/platform/Platform.kt | 24 ++++++++++++++ .../kmp/boilerplate/CommonGreetingTest.kt | 14 -------- .../com/mirego/kmp/boilerplate/Platform.kt | 8 ----- .../kmp/boilerplate/platform/Platform.kt | 33 +++++++++++++++++++ .../mirego/kmp/boilerplate/IosGreetingTest.kt | 14 -------- 17 files changed, 112 insertions(+), 73 deletions(-) delete mode 100644 shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt create mode 100644 shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt delete mode 100644 shared/src/androidUnitTest/kotlin/com.mirego.kmp.boilerplate/AndroidGreetingTest.kt delete mode 100644 shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt create mode 100644 shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt delete mode 100644 shared/src/commonTest/kotlin/com/mirego/kmp/boilerplate/CommonGreetingTest.kt delete mode 100644 shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt create mode 100644 shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt delete mode 100644 shared/src/iosTest/kotlin/com/mirego/kmp/boilerplate/IosGreetingTest.kt diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 9d4863e..4d0997c 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -15,8 +15,8 @@ android { targetSdk = 34 applicationId = "com.mirego.kmp.boilerplate" - versionCode = 1 - versionName = "0.1" + versionCode = project.property("versionCode") as? Int + versionName = project.property("versionName") as? String } buildTypes { debug { diff --git a/androidApp/src/main/java/com/mirego/kmp/boilerplate/Greeting.kt b/androidApp/src/main/java/com/mirego/kmp/boilerplate/Greeting.kt index 404d91e..4195f8b 100644 --- a/androidApp/src/main/java/com/mirego/kmp/boilerplate/Greeting.kt +++ b/androidApp/src/main/java/com/mirego/kmp/boilerplate/Greeting.kt @@ -6,7 +6,6 @@ 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) { @@ -15,9 +14,8 @@ fun Greeting(textFlow: Flow) { Text(text = text) } -@Preview +@Preview(showSystemUi = true) @Composable fun PreviewGreeting() { - val textFlow = flowOf("Hello, Android 31") - Greeting(textFlow) + Greeting(Greeting().greeting()) } diff --git a/gradle.properties b/gradle.properties index d0fdbb0..0fecfef 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,3 +9,6 @@ android.useAndroidX=true kotlin.mpp.androidSourceSetLayoutVersion=2 kotlin.mpp.enableCInteropCommonization=true kotlin.mpp.stability.nowarn=true +#Version +versionName=0.0.1 +versionCode=1 diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 2389a29..b58df42 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - Shared (0.1) + - Shared (0.0.1) - SwiftLint (0.52.4) DEPENDENCIES: @@ -15,7 +15,7 @@ EXTERNAL SOURCES: :path: "../shared" SPEC CHECKSUMS: - Shared: 9cee99d7ec6f327294a149403bec06a18fc5db29 + Shared: 4d22deb908e7a9ed9a64ea2d3fcc53255a207993 SwiftLint: 1cc5cd61ba9bacb2194e340aeb47a2a37fda00b3 PODFILE CHECKSUM: 83ebf5d7b61ce65029f18160027c7392430ee27f diff --git a/ios/iosApp.xcodeproj/project.pbxproj b/ios/iosApp.xcodeproj/project.pbxproj index 072959b..83dd924 100644 --- a/ios/iosApp.xcodeproj/project.pbxproj +++ b/ios/iosApp.xcodeproj/project.pbxproj @@ -377,7 +377,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.1; + MARKETING_VERSION = 0.0.1; PRODUCT_BUNDLE_IDENTIFIER = com.mirego.kmp.boilerplate; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -399,7 +399,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 0.1; + MARKETING_VERSION = 0.0.1; PRODUCT_BUNDLE_IDENTIFIER = com.mirego.kmp.boilerplate; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; diff --git a/shared/Shared.podspec b/shared/Shared.podspec index 9e1ee67..b8df6e9 100644 --- a/shared/Shared.podspec +++ b/shared/Shared.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'Shared' - spec.version = '0.1' + spec.version = '0.0.1' spec.homepage = 'https://github.com/mirego/your-project' spec.source = { :http=> ''} spec.authors = '' diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index a2d40a9..6dfc212 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -10,7 +10,7 @@ plugins { alias(libs.plugins.ktlint) } -version = "0.1" +version = project.property("versionName") as String kotlin { jvmToolchain(17) @@ -69,11 +69,17 @@ android { compileSdk = 34 defaultConfig { minSdk = 21 + + buildConfigField("Integer", "VERSION_CODE", "${project.property("versionCode")}") + buildConfigField("String", "VERSION_NAME", "\"$version\"") } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } + buildFeatures { + buildConfig = true + } } ktlint { diff --git a/shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt b/shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt deleted file mode 100644 index 83f3306..0000000 --- a/shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.mirego.kmp.boilerplate - -actual class Platform actual constructor() { - actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT}" -} diff --git a/shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt b/shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt new file mode 100644 index 0000000..c91d283 --- /dev/null +++ b/shared/src/androidMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt @@ -0,0 +1,25 @@ +package com.mirego.kmp.boilerplate.platform + +import android.os.Build +import com.mirego.kmp.boilerplate.common.BuildConfig + +actual fun Platform(): Platform = AndroidPlatform() + +private class AndroidPlatform : Platform { + override val system = System( + name = "Android", + version = Build.VERSION.SDK_INT.toString() + ) + + override val locale = java.util.Locale.getDefault().let { + Locale( + languageCode = it.language, + regionCode = it.country + ) + } + + override val version = Version( + name = BuildConfig.VERSION_NAME, + code = BuildConfig.VERSION_CODE + ) +} diff --git a/shared/src/androidUnitTest/kotlin/com.mirego.kmp.boilerplate/AndroidGreetingTest.kt b/shared/src/androidUnitTest/kotlin/com.mirego.kmp.boilerplate/AndroidGreetingTest.kt deleted file mode 100644 index 714b84f..0000000 --- a/shared/src/androidUnitTest/kotlin/com.mirego.kmp.boilerplate/AndroidGreetingTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.mirego.kmp.boilerplate - -import kotlin.test.Test -import kotlin.test.assertTrue -import kotlinx.coroutines.flow.single -import kotlinx.coroutines.test.runTest - -class AndroidGreetingTest { - - @Test - fun testExample() = runTest { - assertTrue(Greeting().greeting().single().contains("Android"), "Check Android is mentioned") - } -} diff --git a/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Greeting.kt b/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Greeting.kt index 2b88fdf..03fb89c 100644 --- a/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Greeting.kt +++ b/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Greeting.kt @@ -1,9 +1,19 @@ package com.mirego.kmp.boilerplate +import com.mirego.kmp.boilerplate.platform.Platform import com.mirego.kmp.boilerplate.utils.CFlow import com.mirego.kmp.boilerplate.utils.wrap import kotlinx.coroutines.flow.flowOf class Greeting { - fun greeting(): CFlow = flowOf("Hello, ${Platform().platform}!").wrap() + private val platform = Platform() + + private val greetingText = buildString { + appendLine("Hello! 👋") + appendLine(platform.system) + appendLine(platform.locale) + appendLine(platform.version) + } + + fun greeting(): CFlow = flowOf(greetingText).wrap() } diff --git a/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt b/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt deleted file mode 100644 index 514e746..0000000 --- a/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.mirego.kmp.boilerplate - -expect class Platform() { - val platform: String -} diff --git a/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt b/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt new file mode 100644 index 0000000..aeea0f5 --- /dev/null +++ b/shared/src/commonMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt @@ -0,0 +1,24 @@ +package com.mirego.kmp.boilerplate.platform + +expect fun Platform(): Platform + +interface Platform { + val system: System + val locale: Locale + val version: Version +} + +data class Locale( + val languageCode: String, + val regionCode: String? +) + +data class System( + val name: String, + val version: String +) + +data class Version( + val name: String, + val code: Int +) diff --git a/shared/src/commonTest/kotlin/com/mirego/kmp/boilerplate/CommonGreetingTest.kt b/shared/src/commonTest/kotlin/com/mirego/kmp/boilerplate/CommonGreetingTest.kt deleted file mode 100644 index 77b73f6..0000000 --- a/shared/src/commonTest/kotlin/com/mirego/kmp/boilerplate/CommonGreetingTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.mirego.kmp.boilerplate - -import kotlin.test.Test -import kotlin.test.assertTrue -import kotlinx.coroutines.flow.single -import kotlinx.coroutines.test.runTest - -class CommonGreetingTest { - - @Test - fun testExample() = runTest { - assertTrue(Greeting().greeting().single().contains("Hello"), "Check 'Hello' is mentioned") - } -} diff --git a/shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt b/shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt deleted file mode 100644 index f876564..0000000 --- a/shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/Platform.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.mirego.kmp.boilerplate - -import platform.UIKit.UIDevice - -actual class Platform actual constructor() { - actual val platform: String = - "${UIDevice.currentDevice.systemName()} ${UIDevice.currentDevice.systemVersion}" -} diff --git a/shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt b/shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt new file mode 100644 index 0000000..c051c85 --- /dev/null +++ b/shared/src/iosMain/kotlin/com/mirego/kmp/boilerplate/platform/Platform.kt @@ -0,0 +1,33 @@ +package com.mirego.kmp.boilerplate.platform + +import platform.Foundation.NSBundle +import platform.Foundation.NSLocale +import platform.Foundation.currentLocale +import platform.Foundation.languageCode +import platform.Foundation.regionCode +import platform.UIKit.UIDevice + +actual fun Platform(): Platform = IOSPlatform() + +private class IOSPlatform : Platform { + override val system = UIDevice.currentDevice.let { + System( + name = it.systemName, + version = it.systemVersion + ) + } + + override val locale = NSLocale.currentLocale.let { + Locale( + languageCode = it.languageCode, + regionCode = it.regionCode + ) + } + + override val version = NSBundle.mainBundle().infoDictionary()!!.let { + Version( + name = it["CFBundleShortVersionString"] as String, + code = (it["CFBundleVersion"] as String).toInt() + ) + } +} diff --git a/shared/src/iosTest/kotlin/com/mirego/kmp/boilerplate/IosGreetingTest.kt b/shared/src/iosTest/kotlin/com/mirego/kmp/boilerplate/IosGreetingTest.kt deleted file mode 100644 index 8f79e2d..0000000 --- a/shared/src/iosTest/kotlin/com/mirego/kmp/boilerplate/IosGreetingTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.mirego.kmp.boilerplate - -import kotlin.test.Test -import kotlin.test.assertTrue -import kotlinx.coroutines.flow.single -import kotlinx.coroutines.test.runTest - -class IosGreetingTest { - - @Test - fun testExample() = runTest { - assertTrue(Greeting().greeting().single().contains("iOS"), "Check iOS is mentioned") - } -}