-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#62 StringDesc rework for extensions
- Loading branch information
Showing
31 changed files
with
468 additions
and
278 deletions.
There are no files selected for viewing
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
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
23 changes: 23 additions & 0 deletions
23
resources/src/androidMain/kotlin/dev/icerock/moko/resources/desc/CompositionStringDesc.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,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() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...urces/src/androidMain/kotlin/dev/icerock/moko/resources/desc/PluralFormattedStringDesc.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,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) | ||
) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
resources/src/androidMain/kotlin/dev/icerock/moko/resources/desc/PluralStringDesc.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,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) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
resources/src/androidMain/kotlin/dev/icerock/moko/resources/desc/RawStringDesc.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,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 | ||
} |
21 changes: 21 additions & 0 deletions
21
...ces/src/androidMain/kotlin/dev/icerock/moko/resources/desc/ResourceFormattedStringDesc.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,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) | ||
) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
resources/src/androidMain/kotlin/dev/icerock/moko/resources/desc/ResourceStringDesc.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,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) | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
resources/src/androidMain/kotlin/dev/icerock/moko/resources/desc/Utils.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,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 | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
resources/src/commonMain/kotlin/dev/icerock/moko/resources/desc/CompositionStringDesc.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,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) |
23 changes: 23 additions & 0 deletions
23
resources/src/commonMain/kotlin/dev/icerock/moko/resources/desc/PluralFormattedStringDesc.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,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()) |
13 changes: 13 additions & 0 deletions
13
resources/src/commonMain/kotlin/dev/icerock/moko/resources/desc/PluralStringDesc.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,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) |
12 changes: 12 additions & 0 deletions
12
resources/src/commonMain/kotlin/dev/icerock/moko/resources/desc/RawStringDesc.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,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) |
Oops, something went wrong.