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 f78241f
Show file tree
Hide file tree
Showing 25 changed files with 308 additions and 44 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,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 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
124 changes: 124 additions & 0 deletions sample/buildSrc/src/main/java/Dependencies.kt.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<<<<<<< HEAD
=======
object Versions {
const val ANDROID_COMPILE_SDK_VERSION = 33
const val ANDROID_MIN_SDK_VERSION = 24
const val ANDROID_TARGET_SDK_VERSION = 33
const val ANDROID_VERSION_CODE = 1
const val ANDROID_VERSION_NAME = "1.0.0"
const val ANDROIDX_ACTIVITY_COMPOSE = "1.7.1"

const val BUILD_KONFIG = "0.13.3"

const val COMPOSE = "1.4.3"
const val COMPOSE_COMPILER = "1.4.7"
const val COMPOSE_NAVIGATION = "2.6.0"

const val DETEKT = "1.23.0"

const val GRADLE = "8.0.2"

const val JUNIT = "4.13.2"

const val KOIN = "3.3.2"
const val KOIN_ANDROID = "3.3.2"
const val KOIN_ANDROIDX_COMPOSE = "3.4.1"
const val KOTLIN = "1.8.21"
const val KOTLIN_COROUTINES = "1.7.3"
const val KOTEST = "5.5.4"
const val KOTLINX_RESOURCES = "0.2.4"
const val KOVER = "0.7.3"
const val KSP = "1.8.21-1.0.11"
const val KTOR = "2.1.1"

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"

const val TIMBER = "5.0.1"
const val TURBINE = "0.12.1"
}

>>>>>>> 9d544fd ([12] Integrate Moko Resources to share project resources between platforms)
object Dependencies {

object AndroidX {
const val ACTIVITY_COMPOSE = "androidx.activity:activity-compose:${Versions.ANDROIDX_ACTIVITY_COMPOSE}"
}

object Compose {
const val FOUNDATION = "androidx.compose.foundation:foundation:${Versions.COMPOSE}"

const val MATERIAL = "androidx.compose.material:material:${Versions.COMPOSE}"

const val NAVIGATION = "androidx.navigation:navigation-compose:${Versions.COMPOSE_NAVIGATION}"

const val UI = "androidx.compose.ui:ui:${Versions.COMPOSE}"
const val UI_GRAPHICS = "androidx.compose.ui:ui-graphics:${Versions.COMPOSE}"
const val UI_TOOLING = "androidx.compose.ui:ui-tooling:${Versions.COMPOSE}"
const val UI_TOOLING_PREVIEW = "androidx.compose.ui:ui-tooling-preview:${Versions.COMPOSE}"
}

object Koin {
const val CORE = "io.insert-koin:koin-core:${Versions.KOIN}"
const val ANDROID = "io.insert-koin:koin-android:${Versions.KOIN_ANDROID}"
const val COMPOSE = "io.insert-koin:koin-androidx-compose:${Versions.KOIN_ANDROIDX_COMPOSE}"
}

object Ktor {
const val ANDROID = "io.ktor:ktor-client-android:${Versions.KTOR}"
const val AUTH = "io.ktor:ktor-client-auth:${Versions.KTOR}"

const val CIO = "io.ktor:ktor-client-cio:${Versions.KTOR}"
const val CORE = "io.ktor:ktor-client-core:${Versions.KTOR}"
const val CONTENT_NEGOTIATION = "io.ktor:ktor-client-content-negotiation:${Versions.KTOR}"

const val IOS = "io.ktor:ktor-client-ios:${Versions.KTOR}"

const val JSON = "io.ktor:ktor-serialization-kotlinx-json:${Versions.KTOR}"

const val LOGGING = "io.ktor:ktor-client-logging:${Versions.KTOR}"

const val SERIALIZATION = "io.ktor:ktor-client-serialization:${Versions.KTOR}"
}

object Log {
const val NAPIER = "io.github.aakira:napier:${Versions.NAPIER}"
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}"
const val COROUTINES = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.KOTLIN_COROUTINES}"

const val JUNIT = "junit:junit:${Versions.JUNIT}"

const val KOTEST = "io.kotest:kotest-assertions-core:${Versions.KOTEST}"
const val KOTLINX_RESOURCES = "com.goncalossilva:resources:${Versions.KOTLINX_RESOURCES}"

const val MOCKATIVE = "io.mockative:mockative:${Versions.MOCKATIVE}"
const val MOCKATIVE_PROCESSOR = "io.mockative:mockative-processor:${Versions.MOCKATIVE}"
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}"
}
}
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()
}
}
}
Loading

0 comments on commit f78241f

Please sign in to comment.