-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split UiText into data struct and Vanilla Android parts
- Loading branch information
Showing
11 changed files
with
100 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiText.kt
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
dachlatten-android/src/main/kotlin/de/sipgate/dachlatten/android/text/UiTextExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 20 additions & 2 deletions
22
dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/text/UiTextExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
30 changes: 30 additions & 0 deletions
30
dachlatten-text/src/main/kotlin/de/sipgate/dachlatten/text/UiText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters