Skip to content

Commit

Permalink
[12] Integrate Moko Resources to share project resources between plat…
Browse files Browse the repository at this point in the history
…forms
  • Loading branch information
AVI5HEK committed Sep 14, 2023
1 parent b69e044 commit c52faf4
Show file tree
Hide file tree
Showing 24 changed files with 183 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import co.nimblehq.kmm.template.Greeting
import co.nimblehq.kmm.template.ui.theme.ComposeTheme
import co.nimblehq.kmm.template.Strings
import co.nimblehq.kmm.template.android.ui.theme.ComposeTheme
import co.nimblehq.kmm.template.getPlatform
import co.nimblehq.kmm.template.sharedres.SharedRes

@Composable
fun HomeScreen() {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Text(text = Greeting().greet())
Text(text = Strings(LocalContext.current).get(id = SharedRes.strings.greeting, args = listOf(getPlatform().name)))
}
}

Expand Down
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ plugins {
kotlin(Plugins.KOTLIN_SERIALIZATION) version Versions.KOTLIN
}

buildscript {
dependencies {
classpath(Dependencies.Moko.RESOURCES_GENERATOR)
}
}

detekt {
toolVersion = Versions.DETEKT
config.setFrom("detekt.yml")
Expand Down
9 changes: 9 additions & 0 deletions buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ object Dependencies {
const val TIMBER = "com.jakewharton.timber:timber:${Versions.TIMBER}"
}

object Moko {
const val RESOURCES_GENERATOR = "dev.icerock.moko:resources-generator:${Versions.MOKO_RESOURCES}"
const val RESOURCES = "dev.icerock.moko:resources:${Versions.MOKO_RESOURCES}"
const val RESOURCES_COMPOSE = "dev.icerock.moko:resources-compose:${Versions.MOKO_RESOURCES}"
const val GRAPHICS = "dev.icerock.moko:graphics:${Versions.MOKO_GRAPHICS}"
}

object Test {
const val COMPOSE_UI_TEST_JUNIT = "androidx.compose.ui:ui-test-junit4:${Versions.COMPOSE}"
const val COMPOSE_UI_TEST_MANIFEST = "androidx.compose.ui:ui-test-manifest:${Versions.COMPOSE}"
Expand All @@ -60,6 +67,8 @@ object Dependencies {
const val MOCKK = "io.mockk:mockk:${Versions.MOCKK}"
const val MOCKK_ANDROID = "io.mockk:mockk-android:${Versions.MOCKK}"

const val MOKO_RESOURCES_TEST = "dev.icerock.moko:resources-test:${Versions.MOKO_RESOURCES}"

const val ROBOLECTRIC = "org.robolectric:robolectric:${Versions.ROBOLECTRIC}"

const val TURBINE = "app.cash.turbine:turbine:${Versions.TURBINE}"
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/java/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ object Plugins {
const val KOVER = "org.jetbrains.kotlinx.kover"
const val KSP = "com.google.devtools.ksp"

const val MOKO = "dev.icerock.mobile.multiplatform-resources"

const val MULTIPLATFORM = "multiplatform"
}
3 changes: 3 additions & 0 deletions buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ object Versions {
const val MOCKATIVE = "1.3.0"
const val MOCKK = "1.13.3"

const val MOKO_RESOURCES = "0.23.0"
const val MOKO_GRAPHICS = "0.9.0"

const val NAPIER = "2.6.1"

const val ROBOLECTRIC = "4.9.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import co.nimblehq.kmm.template.Greeting
import co.nimblehq.kmm.template.Strings
import co.nimblehq.kmm.template.getPlatform
import co.nimblehq.kmm.template.sharedres.SharedRes
import co.nimblehq.kmm.template.ui.theme.ComposeTheme

@Composable
Expand All @@ -14,7 +17,7 @@ fun HomeScreen() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Text(text = Greeting().greet())
Text(text = Strings(LocalContext.current).get(id = SharedRes.strings.greeting, args = listOf(getPlatform().name)))
}
}

Expand Down
6 changes: 6 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ plugins {
kotlin(Plugins.KOTLIN_SERIALIZATION) version Versions.KOTLIN
}

buildscript {
dependencies {
classpath(Dependencies.Moko.RESOURCES_GENERATOR)
}
}

detekt {
toolVersion = Versions.DETEKT
config.setFrom("detekt.yml")
Expand Down
9 changes: 9 additions & 0 deletions sample/buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ object Dependencies {
const val TIMBER = "com.jakewharton.timber:timber:${Versions.TIMBER}"
}

object Moko {
const val RESOURCES_GENERATOR = "dev.icerock.moko:resources-generator:${Versions.MOKO_RESOURCES}"
const val RESOURCES = "dev.icerock.moko:resources:${Versions.MOKO_RESOURCES}"
const val RESOURCES_COMPOSE = "dev.icerock.moko:resources-compose:${Versions.MOKO_RESOURCES}"
const val GRAPHICS = "dev.icerock.moko:graphics:${Versions.MOKO_GRAPHICS}"
}

object Test {
const val COMPOSE_UI_TEST_JUNIT = "androidx.compose.ui:ui-test-junit4:${Versions.COMPOSE}"
const val COMPOSE_UI_TEST_MANIFEST = "androidx.compose.ui:ui-test-manifest:${Versions.COMPOSE}"
Expand All @@ -60,6 +67,8 @@ object Dependencies {
const val MOCKK = "io.mockk:mockk:${Versions.MOCKK}"
const val MOCKK_ANDROID = "io.mockk:mockk-android:${Versions.MOCKK}"

const val MOKO_RESOURCES_TEST = "dev.icerock.moko:resources-test:${Versions.MOKO_RESOURCES}"

const val ROBOLECTRIC = "org.robolectric:robolectric:${Versions.ROBOLECTRIC}"

const val TURBINE = "app.cash.turbine:turbine:${Versions.TURBINE}"
Expand Down
2 changes: 2 additions & 0 deletions sample/buildSrc/src/main/java/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ object Plugins {
const val KOVER = "org.jetbrains.kotlinx.kover"
const val KSP = "com.google.devtools.ksp"

const val MOKO = "dev.icerock.mobile.multiplatform-resources"

const val MULTIPLATFORM = "multiplatform"
}
3 changes: 3 additions & 0 deletions sample/buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ object Versions {
const val MOCKATIVE = "1.3.0"
const val MOCKK = "1.13.3"

const val MOKO_RESOURCES = "0.23.0"
const val MOKO_GRAPHICS = "0.9.0"

const val NAPIER = "2.6.1"

const val ROBOLECTRIC = "4.9.1"
Expand Down
8 changes: 6 additions & 2 deletions sample/ios/sample/Configurations/Plists/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
<array>
<string>armv7</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
14 changes: 14 additions & 0 deletions sample/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
id(Plugins.KOVER)
id(Plugins.KSP)
id(Plugins.BUILD_KONFIG)
id(Plugins.MOKO)
}

@OptIn(org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi::class)
Expand All @@ -36,6 +37,8 @@ kotlin {
podfile = project.file("../ios/Podfile")
framework {
baseName = "shared"
export(Dependencies.Moko.RESOURCES)
export(Dependencies.Moko.GRAPHICS)
}

xcodeConfigurationToNativeBuildType["Debug Staging"] = NativeBuildType.DEBUG
Expand All @@ -59,6 +62,11 @@ kotlin {
implementation(AUTH)
}

with(Dependencies.Moko) {
api(RESOURCES)
// api(RESOURCES_COMPOSE) FIXME: Cannot build the shared module with this dependency
}

implementation(Dependencies.Log.NAPIER)
}
}
Expand All @@ -71,6 +79,7 @@ kotlin {
implementation(KOTLINX_RESOURCES)
implementation(MOCKATIVE)
implementation(TURBINE)
implementation(MOKO_RESOURCES_TEST)
}
}
}
Expand Down Expand Up @@ -145,3 +154,8 @@ buildkonfig {
)
}
}

multiplatformResources {
multiplatformResourcesPackage = "co.nimblehq.kmm.template.sharedres"
multiplatformResourcesClassName = "SharedRes"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package co.nimblehq.kmm.template

import android.content.Context
import dev.icerock.moko.resources.StringResource
import dev.icerock.moko.resources.desc.Resource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.format

actual class Strings(private val context: Context) {
actual fun get(
id: StringResource,
args: List<Any>,
): String {
return if (args.isEmpty()) {
StringDesc.Resource(id).toString(context = context)
} else {
id.format(*args.toTypedArray()).toString(context)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.nimblehq.kmm.template

import dev.icerock.moko.resources.StringResource

expect class Strings {
fun get(id: StringResource, args: List<Any> = emptyList()): String
}
3 changes: 3 additions & 0 deletions sample/shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="greeting">Hello, %s!</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.nimblehq.kmm.template

import dev.icerock.moko.resources.StringResource
import dev.icerock.moko.resources.desc.Resource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.format

actual class Strings {
actual fun get(
id: StringResource,
args: List<Any>,
): String {
return if (args.isEmpty()) {
StringDesc.Resource(id).localized()
} else {
id.format(*args.toTypedArray()).localized()
}
}
}
14 changes: 14 additions & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
id(Plugins.KOVER)
id(Plugins.KSP)
id(Plugins.BUILD_KONFIG)
id(Plugins.MOKO)
}

@OptIn(org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi::class)
Expand All @@ -36,6 +37,8 @@ kotlin {
podfile = project.file("../ios/Podfile")
framework {
baseName = "shared"
export(Dependencies.Moko.RESOURCES)
export(Dependencies.Moko.GRAPHICS)
}

xcodeConfigurationToNativeBuildType["Debug Staging"] = NativeBuildType.DEBUG
Expand All @@ -59,6 +62,11 @@ kotlin {
implementation(AUTH)
}

with(Dependencies.Moko) {
api(RESOURCES)
// api(RESOURCES_COMPOSE) FIXME: Cannot build the shared module with this dependency
}

implementation(Dependencies.Log.NAPIER)
}
}
Expand All @@ -71,6 +79,7 @@ kotlin {
implementation(KOTLINX_RESOURCES)
implementation(MOCKATIVE)
implementation(TURBINE)
implementation(MOKO_RESOURCES_TEST)
}
}
}
Expand Down Expand Up @@ -145,3 +154,8 @@ buildkonfig {
)
}
}

multiplatformResources {
multiplatformResourcesPackage = "co.nimblehq.kmm.template.sharedres"
multiplatformResourcesClassName = "SharedRes"
}
20 changes: 20 additions & 0 deletions shared/src/androidMain/kotlin/co/nimblehq/kmm/template/Strings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package co.nimblehq.kmm.template

import android.content.Context
import dev.icerock.moko.resources.StringResource
import dev.icerock.moko.resources.desc.Resource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.format

actual class Strings(private val context: Context) {
actual fun get(
id: StringResource,
args: List<Any>,
): String {
return if (args.isEmpty()) {
StringDesc.Resource(id).toString(context = context)
} else {
id.format(*args.toTypedArray()).toString(context)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.nimblehq.kmm.template

import dev.icerock.moko.resources.StringResource

expect class Strings {
fun get(id: StringResource, args: List<Any> = emptyList()): String
}
3 changes: 3 additions & 0 deletions shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="greeting">Hello, %s!</string>
</resources>

This file was deleted.

19 changes: 19 additions & 0 deletions shared/src/iosMain/kotlin/co/nimblehq/kmm/template/Strings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.nimblehq.kmm.template

import dev.icerock.moko.resources.StringResource
import dev.icerock.moko.resources.desc.Resource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.format

actual class Strings {
actual fun get(
id: StringResource,
args: List<Any>,
): String {
return if (args.isEmpty()) {
StringDesc.Resource(id).localized()
} else {
id.format(*args.toTypedArray()).localized()
}
}
}
Loading

0 comments on commit c52faf4

Please sign in to comment.