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 4 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 @@ -2,6 +2,7 @@ plugins {
id("kotlin-multiplatform")
id("com.android.library")
kotlin("plugin.serialization")
id("dev.icerock.mobile.multiplatform-resources")
}

val targetSDKVersion: Int by rootProject.extra
Expand Down Expand Up @@ -46,6 +47,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 @@ -105,7 +107,13 @@ kotlin {
implementation(libs.io.ktor.client.mock)
implementation(libs.org.jetbrains.kotlin.test.common)
implementation(libs.org.jetbrains.kotlin.test.annotations.common)
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 @@ -4,6 +4,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 @@ -24,6 +25,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 @@ -10,58 +10,63 @@ import io.ktor.http.HttpStatusCode
import io.ktor.http.headersOf
import io.ktor.serialization.kotlinx.json.json
import io.ktor.utils.io.ByteReadChannel
import kotlin.test.Ignore
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import kotlin.test.Test

/**
* I was unable to read a .json file from commonTest, and so API tests must be done from
* desktopTest, androidTest, and/or iosTest.
*/
class MastodonApiTests {
// TODO: fix loading json from resources
// @Test
fun `Instance request should fail with invalid response`() = runBlocking {
// given
// val content: String = javaClass.classLoader.getResource("response_instance_invalid.json").readText()
val content: String = ""
val mastodonApi = MastodonApiImpl(
httpClient = createMockClient(
statusCode = HttpStatusCode.Unauthorized, content = ByteReadChannel(text = content)

@Test
fun `Instance request should fail with invalid response`() {
runBlocking {

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

// when
val result = mastodonApi.getInstance("")
// when
val result = mastodonApi.getInstance("")

// then
assertFalse(actual = result.isSuccess)
assertNull(actual = result.getOrNull()?.uri)
// then
assertFalse(actual = result.isSuccess)
assertNull(actual = result.getOrNull()?.uri)
}
}

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

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

// when
val result = mastodonApi.getInstance("")
// when
val result = mastodonApi.getInstance("")

// then
assertTrue(actual = result.isSuccess)
assertNotNull(actual = result.getOrNull()?.uri)
// then
assertTrue(actual = result.isSuccess)
assertNotNull(actual = result.getOrNull()?.uri)
}
}


private fun createMockClient(
statusCode: HttpStatusCode = HttpStatusCode.OK, content: ByteReadChannel
): HttpClient {
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -22,6 +23,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 @@ -39,6 +41,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
22 changes: 21 additions & 1 deletion ui/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ plugins {
id("kotlin-multiplatform")
id("org.jetbrains.compose")
id("com.android.library")
id("dev.icerock.mobile.multiplatform-resources")
}


val targetSDKVersion: Int by rootProject.extra
val minSDKVersion: Int by rootProject.extra
val compileSDKVersion: Int by rootProject.extra
Expand Down Expand Up @@ -44,19 +44,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 @@ -65,3 +77,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()
)
}
}