Skip to content

Commit

Permalink
Split UiText into data struct and Vanilla Android parts
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Feb 22, 2024
1 parent 026f407 commit 705317d
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 62 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dachlatten-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ plugins {
}

dependencies {
compileOnly(project(":dachlatten-text"))
compileOnly(libs.androidx.activity)

implementation(libs.androidx.annotation)
implementation(libs.androidx.lifecycle.process)
implementation(libs.kotlinx.coroutines)

testImplementation(project(":dachlatten-text"))
testImplementation(libs.androidx.activity)
testImplementation(libs.androidx.core)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.sipgate.dachlatten.android.text

import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import de.sipgate.dachlatten.text.TranslatedText
import de.sipgate.dachlatten.text.UiText
import java.util.Locale

fun UiText.asString(resources: Resources) = when (this) {
is UiText.DynamicString -> value
is UiText.StringResource -> resources.getString(resId, *(args.toTypedArray()))
is UiText.MultiLangString -> {
val arguments = args.toTypedArray()
resources.configuration.getStringForLocales(language)?.format(*arguments)
?: fallbackResource?.let { resources.getString(it, *arguments) }
?: throw Resources.NotFoundException("Could not find multilang string")
}
}

private fun Configuration.getStringForLocales(translations: TranslatedText): String? {
val locale = resolveLocale(translations.keys)
return translations[locale?.language?.lowercase()] ?: translations[locale?.language?.uppercase()]
}

private fun Configuration.resolveLocale(supportedLanguages: Set<String>): Locale? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locales.getFirstMatch(supportedLanguages.toTypedArray())
} else {
@Suppress("DEPRECATION")
locale
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.sipgate.dachlatten.android.text

import android.content.res.Resources
import androidx.core.R
import de.sipgate.dachlatten.text.UiText
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.assertThrows
Expand Down
4 changes: 2 additions & 2 deletions dachlatten-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ plugins {
}

dependencies {
compileOnly(project(":dachlatten-android"))
compileOnly(project(":dachlatten-text"))
compileOnly(libs.androidx.compose.ui)

testImplementation(project(":dachlatten-android"))
testImplementation(project(":dachlatten-text"))
testImplementation(libs.bundles.androidx.compose.ui.test)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
package de.sipgate.dachlatten.compose.text

import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import de.sipgate.dachlatten.android.text.UiText
import de.sipgate.dachlatten.text.TranslatedText
import de.sipgate.dachlatten.text.UiText
import java.util.Locale

@Composable
fun UiText.asString() = when (this) {
is UiText.DynamicString -> value
is UiText.StringResource -> stringResource(id = resId, formatArgs = args.toTypedArray())
is UiText.MultiLangString -> {
val arguments = args.toTypedArray()
LocalConfiguration.current.getStringForLocales() ?.format(*arguments)
LocalConfiguration.current.getStringForLocales(language) ?.format(*arguments)
?: fallbackResource?.let { stringResource(id = it, formatArgs = arguments) }
?: throw Resources.NotFoundException("Could not find multilang string")
}
}

private fun Configuration.getStringForLocales(translations: TranslatedText): String? {
val locale = resolveLocale(translations.keys)
return translations[locale?.language?.lowercase()] ?: translations[locale?.language?.uppercase()]
}

private fun Configuration.resolveLocale(supportedLanguages: Set<String>): Locale? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locales.getFirstMatch(supportedLanguages.toTypedArray())
} else {
@Suppress("DEPRECATION")
locale
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package de.sipgate.dachlatten.compose.text
import android.content.res.Resources
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.core.R
import de.sipgate.dachlatten.android.text.UiText
import de.sipgate.dachlatten.text.UiText
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
Expand Down
9 changes: 9 additions & 0 deletions dachlatten-text/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("android-library-base")
id("android-library-unit-test")
id("android-library-release")
}

dependencies {
implementation(libs.androidx.annotation)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.sipgate.dachlatten.text

import androidx.annotation.StringRes

typealias TranslatedText = Map<String, String>

sealed interface UiText {
data class DynamicString(val value: String) : UiText

data class StringResource(
@StringRes val resId: Int,
val args: List<Any>,
) : UiText {
constructor(
@StringRes resId: Int,
vararg args: Any) : this(resId, args.asList())
}

data class MultiLangString(
val language: TranslatedText,
@StringRes val fallbackResource: Int? = null,
val args: List<Any>,
) : UiText {
constructor(
language: TranslatedText,
@StringRes fallbackResource: Int? = null,
vararg args: Any,
) : this(language, fallbackResource, args.asList())
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ include(":dachlatten-flow")
include(":dachlatten-google")
include(":dachlatten-primitives")
include(":dachlatten-retrofit")
include(":dachlatten-text")

0 comments on commit 705317d

Please sign in to comment.