Skip to content

Commit

Permalink
Expose Platform information as expect-actual (locale, system, version) (
Browse files Browse the repository at this point in the history
  • Loading branch information
marcantoinefortier authored Dec 6, 2023
1 parent 4b073b4 commit 680deae
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 73 deletions.
4 changes: 2 additions & 2 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
Expand All @@ -15,9 +14,8 @@ fun Greeting(textFlow: Flow<String>) {
Text(text = text)
}

@Preview
@Preview(showSystemUi = true)
@Composable
fun PreviewGreeting() {
val textFlow = flowOf("Hello, Android 31")
Greeting(textFlow)
Greeting(Greeting().greeting())
}
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Shared (0.1)
- Shared (0.0.1)
- SwiftLint (0.52.4)

DEPENDENCIES:
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: "../shared"

SPEC CHECKSUMS:
Shared: 9cee99d7ec6f327294a149403bec06a18fc5db29
Shared: 4d22deb908e7a9ed9a64ea2d3fcc53255a207993
SwiftLint: 1cc5cd61ba9bacb2194e340aeb47a2a37fda00b3

PODFILE CHECKSUM: 83ebf5d7b61ce65029f18160027c7392430ee27f
Expand Down
4 changes: 2 additions & 2 deletions ios/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion shared/Shared.podspec
Original file line number Diff line number Diff line change
@@ -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 = ''
Expand Down
8 changes: 7 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
alias(libs.plugins.ktlint)
}

version = "0.1"
version = project.property("versionName") as String

kotlin {
jvmToolchain(17)
Expand Down Expand Up @@ -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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String> = 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<String> = flowOf(greetingText).wrap()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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()
)
}
}

This file was deleted.

0 comments on commit 680deae

Please sign in to comment.