Skip to content

Commit

Permalink
#62 StringDesc rework for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed May 4, 2020
1 parent deca68c commit 951aa2a
Show file tree
Hide file tree
Showing 31 changed files with 468 additions and 278 deletions.
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ object Deps {
iosX64 = "dev.icerock.moko:resources-iosx64:${Versions.Libs.MultiPlatform.mokoResources}",
iosArm64 = "dev.icerock.moko:resources-iosarm64:${Versions.Libs.MultiPlatform.mokoResources}"
)
val mokoParcelize = MultiPlatformLibrary(
common = "dev.icerock.moko:parcelize:${Versions.Libs.MultiPlatform.mokoParcelize}",
iosX64 = "dev.icerock.moko:parcelize-iosx64:${Versions.Libs.MultiPlatform.mokoParcelize}",
iosArm64 = "dev.icerock.moko:parcelize-iosarm64:${Versions.Libs.MultiPlatform.mokoParcelize}"
)
}

object Jvm {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Versions {

object MultiPlatform {
const val mokoResources = Versions.mokoResources
const val mokoParcelize = "0.3.0"
}

object Jvm {
Expand Down
2 changes: 2 additions & 0 deletions resources/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ android {
dependencies {
mppLibrary(Deps.Libs.MultiPlatform.kotlinStdLib)

mppLibrary(Deps.Libs.MultiPlatform.mokoParcelize)

androidLibrary(Deps.Libs.Android.appCompat)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context

actual class CompositionStringDesc actual constructor(
val args: List<StringDesc>,
val separator: String?
) : StringDesc {
override fun toString(context: Context): String {
return StringBuilder().apply {
args.forEachIndexed { index, stringDesc ->
if (index != 0 && separator != null) {
append(separator)
}
append(stringDesc.toString(context))
}
}.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context
import dev.icerock.moko.resources.PluralsResource

actual class PluralFormattedStringDesc actual constructor(
val pluralsRes: PluralsResource,
val number: Int,
val args: List<Any>
) : StringDesc {
override fun toString(context: Context): String {
@Suppress("SpreadOperator")
return Utils.resourcesForContext(context).getQuantityString(
pluralsRes.resourceId,
number,
*Utils.processArgs(args, context)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context
import dev.icerock.moko.parcelize.Parcelable
import dev.icerock.moko.parcelize.Parcelize
import dev.icerock.moko.resources.PluralsResource

@Parcelize
actual class PluralStringDesc actual constructor(
val pluralsRes: PluralsResource,
val number: Int
) : StringDesc, Parcelable {
override fun toString(context: Context): String {
return Utils.resourcesForContext(context).getQuantityString(pluralsRes.resourceId, number)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context
import dev.icerock.moko.parcelize.Parcelable
import dev.icerock.moko.parcelize.Parcelize

@Parcelize
actual class RawStringDesc actual constructor(
val string: String
) : StringDesc, Parcelable {
override fun toString(context: Context): String = string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context
import dev.icerock.moko.resources.StringResource

actual class ResourceFormattedStringDesc actual constructor(
val stringRes: StringResource,
val args: List<Any>
) : StringDesc {
override fun toString(context: Context): String {
@Suppress("SpreadOperator")
return Utils.resourcesForContext(context).getString(
stringRes.resourceId,
*Utils.processArgs(args, context)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context
import dev.icerock.moko.parcelize.Parcelable
import dev.icerock.moko.parcelize.Parcelize
import dev.icerock.moko.resources.StringResource

@Parcelize
actual class ResourceStringDesc actual constructor(
val stringRes: StringResource
) : StringDesc, Parcelable {
override fun toString(context: Context): String {
return Utils.resourcesForContext(context).getString(stringRes.resourceId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,131 +5,26 @@
package dev.icerock.moko.resources.desc

import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.os.Parcelable
import dev.icerock.moko.resources.PluralsResource
import dev.icerock.moko.resources.StringResource
import kotlinx.android.parcel.Parcelize
import java.util.*
import java.util.Locale

actual sealed class StringDesc {
protected fun processArgs(args: List<Any>, context: Context): Array<out Any> {
return args.toList().map { (it as? StringDesc)?.toString(context) ?: it }.toTypedArray()
}

@Parcelize
actual data class Resource actual constructor(val stringRes: StringResource) : StringDesc(), Parcelable {
override fun toString(context: Context): String {
return resourcesForContext(context).getString(stringRes.resourceId)
}
}

actual data class ResourceFormatted actual constructor(
val stringRes: StringResource,
val args: List<Any>
) : StringDesc() {
override fun toString(context: Context): String {
return resourcesForContext(context).getString(
stringRes.resourceId, *processArgs(args, context)
)
}

actual constructor(stringRes: StringResource, vararg args: Any) : this(
stringRes,
args.toList()
)
}

@Parcelize
actual data class Plural actual constructor(
val pluralsRes: PluralsResource,
val number: Int
) : StringDesc(), Parcelable {
override fun toString(context: Context): String {
return resourcesForContext(context).getQuantityString(pluralsRes.resourceId, number)
}
}

actual data class PluralFormatted actual constructor(
val pluralsRes: PluralsResource,
val number: Int,
val args: List<Any>
) : StringDesc() {
override fun toString(context: Context): String {
return resourcesForContext(context).getQuantityString(
pluralsRes.resourceId,
number,
*processArgs(args, context)
)
}

actual constructor(pluralsRes: PluralsResource, number: Int, vararg args: Any) : this(
pluralsRes,
number,
args.toList()
)
}

@Parcelize
actual data class Raw actual constructor(
val string: String
) : StringDesc(), Parcelable {
override fun toString(context: Context): String {
return string
}
}

actual data class Composition actual constructor(val args: List<StringDesc>, val separator: String?) :
StringDesc() {
override fun toString(context: Context): String {
return StringBuilder().apply {
args.forEachIndexed { index, stringDesc ->
if (index != 0 && separator != null) {
append(separator)
}
append(stringDesc.toString(context))
}
}.toString()
}
}

abstract fun toString(context: Context): String
actual interface StringDesc {
fun toString(context: Context): String

actual sealed class LocaleType {
actual object System : LocaleType() {
override val systemLocale: Locale? = null
}

actual class Custom actual constructor(locale: String) :
LocaleType() {
actual class Custom actual constructor(
locale: String
) : LocaleType() {
override val systemLocale: Locale = Locale(locale)
}

abstract val systemLocale: Locale?
}

actual companion object {
private fun localizedContext(context: Context): Context {
if (localeType.systemLocale == null) return context

val resources = context.resources
val config = resources.configuration

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(localeType.systemLocale)
context.createConfigurationContext(config)
} else {
config.locale = localeType.systemLocale
resources.updateConfiguration(config, resources.displayMetrics)
context
}
}

private fun resourcesForContext(context: Context): Resources {
return localizedContext(context).resources
}

actual var localeType: LocaleType = LocaleType.System
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import android.content.Context
import android.content.res.Resources
import android.os.Build

object Utils {
fun processArgs(args: List<Any>, context: Context): Array<out Any> {
return args.toList().map { (it as? StringDesc)?.toString(context) ?: it }.toTypedArray()
}

fun resourcesForContext(context: Context): Resources {
return localizedContext(context).resources
}

private fun localizedContext(context: Context): Context {
if (StringDesc.localeType.systemLocale == null) return context

val resources = context.resources
val config = resources.configuration

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(StringDesc.localeType.systemLocale)
context.createConfigurationContext(config)
} else {
config.locale = StringDesc.localeType.systemLocale
resources.updateConfiguration(config, resources.displayMetrics)
context
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

expect class CompositionStringDesc(args: List<StringDesc>, separator: String? = null) : StringDesc

@Suppress("FunctionName")
fun StringDesc.Companion.Composition(
args: List<StringDesc>,
separator: String? = null
) = CompositionStringDesc(args, separator)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import dev.icerock.moko.resources.PluralsResource

expect class PluralFormattedStringDesc(pluralsRes: PluralsResource, number: Int, args: List<Any>) : StringDesc

@Suppress("FunctionName")
fun StringDesc.Companion.PluralFormatted(
pluralsRes: PluralsResource,
number: Int,
args: List<Any>
) = PluralFormattedStringDesc(pluralsRes, number, args)

@Suppress("FunctionName")
fun StringDesc.Companion.PluralFormatted(
pluralsRes: PluralsResource,
number: Int,
vararg args: Any
) = PluralFormattedStringDesc(pluralsRes, number, args.asList())
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import dev.icerock.moko.parcelize.Parcelable
import dev.icerock.moko.resources.PluralsResource

expect class PluralStringDesc(pluralsRes: PluralsResource, number: Int) : StringDesc, Parcelable

@Suppress("FunctionName")
fun StringDesc.Companion.Plural(pluralsRes: PluralsResource, number: Int) = PluralStringDesc(pluralsRes, number)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.resources.desc

import dev.icerock.moko.parcelize.Parcelable

expect class RawStringDesc(string: String) : StringDesc, Parcelable

@Suppress("FunctionName")
fun StringDesc.Companion.Raw(string: String) = RawStringDesc(string)

0 comments on commit 951aa2a

Please sign in to comment.