Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Moko Resources #50

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}

dependencies {
Expand All @@ -15,6 +16,7 @@ buildscript {
classpath(libs.com.android.tools.build.gradle)
classpath(libs.org.jetbrains.kotlin.serialization)
classpath(libs.com.squareup.sqldelight.gradle.plugin)
classpath(libs.dev.icerock.moko.resources.generator)
}
}

Expand Down
8 changes: 8 additions & 0 deletions data/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("com.android.library")
kotlin("plugin.serialization")
id("com.diffplug.spotless") version "6.11.0"
id("dev.icerock.mobile.multiplatform-resources")
}

spotless {
Expand Down Expand Up @@ -55,6 +56,7 @@ kotlin {
implementation(libs.io.ktor.serialization.kotlinx.json)
implementation(libs.io.ktor.client.content.negotiation)
implementation(libs.org.jetbrains.kotlinx.serialization.json)
implementation(libs.dev.icerock.moko.resources.common)
}
}

Expand Down Expand Up @@ -115,7 +117,13 @@ kotlin {
implementation(libs.org.jetbrains.kotlin.test.common)
implementation(libs.org.jetbrains.kotlin.test.annotations.common)
implementation(libs.org.jetbrains.kotlinx.coroutines.test)
implementation(libs.dev.icerock.moko.resources.test)
}
}
}
}

multiplatformResources {
multiplatformResourcesPackage = "social.androiddev.common.network" // required
multiplatformResourcesSourceSet = "commonTest" // optional, default "commonMain"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.ResponseException
import io.ktor.client.request.get
import io.ktor.serialization.*
import kotlinx.serialization.SerializationException
import social.androiddev.common.network.model.Instance

Expand All @@ -33,6 +34,8 @@ class MastodonApiImpl(
Result.failure(exception = exception)
} catch (exception: ResponseException) {
Result.failure(exception = exception)
} catch (exception: JsonConvertException) {
Result.failure(exception = exception)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,34 @@
*/
package social.androiddev.common.network

import io.ktor.client.HttpClient
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respond
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.headersOf
import io.ktor.serialization.kotlinx.json.json
import io.ktor.utils.io.ByteReadChannel
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.utils.io.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.Json
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.test.*

/**
* I was unable to read a .json file from commonTest, and so API tests must be done from
* desktopTest, androidTest, and/or iosTest.
*/
@OptIn(ExperimentalCoroutinesApi::class)
class MastodonApiTests {
// TODO: fix loading json from resources

@Test
@Ignore
fun `Instance request should fail with invalid response`() = runTest {
// given
// val content: String = javaClass.classLoader.getResource("response_instance_invalid.json").readText()
val content: String = ""

// given
val content = MR.files.response_account_required.readText()
val mastodonApi = MastodonApiImpl(
httpClient = createMockClient(
statusCode = HttpStatusCode.Unauthorized, content = ByteReadChannel(text = content)
statusCode = HttpStatusCode.Unauthorized,
content = ByteReadChannel(text = content)
)
)

Expand All @@ -52,14 +48,10 @@ class MastodonApiTests {
assertNull(actual = result.getOrNull()?.uri)
}

// TODO: fix loading json from resources
@Test
@Ignore
fun `Instance request should succeed with required field response`() = runTest {
// given
// val content: String = javaClass.classLoader.getResource("response_instance_valid.json").readText()
val content: String = ""

val content = MR.files.response_instance_valid.readText()
val mastodonApi = MastodonApiImpl(
httpClient = createMockClient(
statusCode = HttpStatusCode.Unauthorized, content = ByteReadChannel(text = content)
Expand All @@ -72,6 +64,7 @@ class MastodonApiTests {
// then
assertTrue(actual = result.isSuccess)
assertNotNull(actual = result.getOrNull()?.uri)

}

private fun createMockClient(
Expand All @@ -97,4 +90,5 @@ class MastodonApiTests {
}
}
}

}
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ androidx-appcompat = "1.5.1"
androidx-activity = "1.6.1"
androidx-compose-foundation = "1.2.1"
com-arkivanov-decompose = "1.0.0-alpha-05"
dev-icerock-moko-resources = "0.20.1"


[libraries]
Expand All @@ -23,6 +24,7 @@ org-jetbrains-kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gra
com-android-tools-build-gradle = { module = "com.android.tools.build:gradle", version.ref = "com-android-tools-build" }
org-jetbrains-kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "org-jetbrains-kotlin" }
com-squareup-sqldelight-gradle-plugin = { module = "com.squareup.sqldelight:gradle-plugin", version.ref = "com-squareup-sqldelight" }
dev-icerock-moko-resources-generator = { module = "dev.icerock.moko:resources-generator", version.ref = "dev-icerock-moko-resources"}

# libraries
io-ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "io-ktor" }
Expand All @@ -41,6 +43,10 @@ org-jetbrains-kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-j
org-jetbrains-kotlin-test-common = { module = "org.jetbrains.kotlin:kotlin-test-common", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlin-test-annotations-common = { module = "org.jetbrains.kotlin:kotlin-test-annotations-common", version.ref = "org-jetbrains-kotlin" }

dev-icerock-moko-resources-common = { module = "dev.icerock.moko:resources", version.ref = "dev-icerock-moko-resources"}
dev-icerock-moko-resources-jvm = { module = "dev.icerock.moko:resources-compose", version.ref = "dev-icerock-moko-resources"}
dev-icerock-moko-resources-test = { module = "dev.icerock.moko:resources-test", version.ref = "dev-icerock-moko-resources"}

com-squareup-sqldelight-android-driver = { module = "com.squareup.sqldelight:android-driver", version.ref = "com-squareup-sqldelight" }
com-squareup-sqldelight-native-driver = { module = "com.squareup.sqldelight:native-driver", version.ref = "com-squareup-sqldelight" }
com-squareup-sqldelight-sqlite-driver = { module = "com.squareup.sqldelight:sqlite-driver", version.ref = "com-squareup-sqldelight" }
Expand Down
21 changes: 21 additions & 0 deletions ui/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("org.jetbrains.compose")
id("com.android.library")
id("com.diffplug.spotless") version "6.11.0"
id("dev.icerock.mobile.multiplatform-resources")
}
spotless {
kotlin {
Expand Down Expand Up @@ -51,19 +52,31 @@ kotlin {
implementation(compose.material)
implementation(libs.com.arkivanov.decompose)
implementation(libs.com.arkivanov.decompose.extensions.compose.jetbrains)
implementation(libs.dev.icerock.moko.resources.common)
}
}

named("androidMain") {
dependencies {
// Workaround for https://github.com/JetBrains/compose-jb/issues/2340
implementation(libs.androidx.compose.foundation)
implementation(libs.dev.icerock.moko.resources.jvm)
}
}

named("desktopMain") {
dependencies {
implementation(compose.desktop.common)
implementation(libs.dev.icerock.moko.resources.jvm)
}
}

//Testing
named("commonTest") {
dependencies {
implementation(kotlin("test"))
implementation(libs.org.jetbrains.kotlin.test.common)
implementation(libs.org.jetbrains.kotlin.test.annotations.common)
}
}
}
Expand All @@ -72,3 +85,11 @@ kotlin {
kotlinOptions.jvmTarget = "11"
}
}

multiplatformResources {
multiplatformResourcesPackage = "social.androiddev.common" // required
// multiplatformResourcesClassName = "SharedRes" // optional, default MR
// multiplatformResourcesVisibility = dev.icerock.gradle.MRVisibility.Public // optional, default Public
// iosBaseLocalizationRegion = "en" // optional, default "en"
// multiplatformResourcesSourceSet = "commonMain" // optional, default "commonMain"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package social.androiddev.common.sharedres

import dev.icerock.moko.graphics.Color
import dev.icerock.moko.resources.ImageResource
import dev.icerock.moko.resources.desc.Resource
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.getImageByFileName
import social.androiddev.common.MR

object SharedResources {

/**
* A String that can be localised.
* On Android, call `toString(context)`, on iOS, call `localized()`
*
* @see https://github.com/icerockdev/moko-resources#example-1---simple-localization-string
*/
fun getLocalisedHello(): StringDesc {

return StringDesc.Resource(MR.strings.my_string)
}

/**
* Get an image resource by the file name, and falls back to mastodon logo if it fails
*
* @see https://github.com/icerockdev/moko-resources#example-7---pass-image
*/
fun getImageByFileName(name: String): ImageResource {
val fallbackImage = MR.images.mastodon_logo
return MR.images.getImageByFileName(name) ?: fallbackImage
}

/**
* Android: val color: Color = MR.colors.valueColor.getColor(context = this)
*
* iOS:val color: UIColor = MR.colors.valueColor.getColor(UIScreen.main.traitCollection.userInterfaceStyle)
*
* @see https://github.com/icerockdev/moko-resources#example-9---pass-colors
*/
fun getValueColor(): Color {
return MR.colors.valueColor.color
}



}
4 changes: 4 additions & 0 deletions ui/common/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="my_string">My default localization string</string>
</resources>
14 changes: 14 additions & 0 deletions ui/common/src/commonMain/resources/MR/colors/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- format: #RRGGBB[AA] or 0xRRGGBB[AA] or RRGGBB[AA] where [AA] - optional -->
<color name="valueColor">#B02743FF</color>
<color name="referenceColor">@color/valueColor</color>
<color name="themedColor">
<light>0xB92743FF</light>
<dark>7CCFEEFF</dark>
</color>
<color name="themedReferenceColor">
<light>@color/valueColor</light>
<dark>@color/referenceColor</dark>
</color>
</resources>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions ui/common/src/commonMain/resources/MR/ru/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="my_string">Моя строка локализации по умолчанию</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package social.androiddev.common.sharedres

import dev.icerock.moko.graphics.Color
import kotlin.test.assertEquals
import kotlin.test.Test

/**
* These tests aren't ideal, but they show that there's some file access going on
*/
class SharedResourcesTest {

@Test
fun `getLocalisedHello returns a stringresource with unknown content`() {

// This test only confirms that the resource is there. To get the content, it must run on androidTest, desktopTest
assertEquals(
"ResourceStringDesc(stringRes=StringResource(resourceId=2132017188))",
SharedResources.getLocalisedHello().toString()
)

}

@Test
fun `getting empty image still returns a fallback image`() {
assertEquals(
"dev.icerock.moko.resources.ImageResource@821330f",
SharedResources.getImageByFileName("").toString()
)
}

@Test
fun `getValueColor returns a specific Color class`() {
assertEquals(
Color(red=176, green=39, blue=67, alpha=255),
SharedResources.getValueColor()
)
}
}