55package dev.icerock.moko.resources.desc
66
77import android.content.Context
8+ import android.os.Parcel
9+ import android.os.Parcelable
810import dev.icerock.moko.resources.PluralsResource
911import dev.icerock.moko.resources.StringResource
12+ import kotlinx.android.parcel.Parcelize
1013
1114actual sealed class StringDesc {
12- actual data class Resource actual constructor(val stringRes : StringResource ) : StringDesc() {
15+ protected fun processArgs (args : List <Any >, context : Context ): Array <out Any > {
16+ return args.toList().map { (it as ? StringDesc )?.toString(context) ? : it }.toTypedArray()
17+ }
18+
19+ @Parcelize
20+ actual data class Resource actual constructor(val stringRes : StringResource ) : StringDesc(), Parcelable {
1321 override fun toString (context : Context ): String {
1422 return context.getString(stringRes.resourceId)
1523 }
@@ -20,7 +28,9 @@ actual sealed class StringDesc {
2028 val args : List <Any >
2129 ) : StringDesc() {
2230 override fun toString (context : Context ): String {
23- return context.getString(stringRes.resourceId, * (args.toTypedArray()))
31+ return context.getString(
32+ stringRes.resourceId, * processArgs(args, context)
33+ )
2434 }
2535
2636 actual constructor (stringRes: StringResource , vararg args: Any ) : this (
@@ -29,8 +39,11 @@ actual sealed class StringDesc {
2939 )
3040 }
3141
32- actual data class Plural actual constructor(val pluralsRes : PluralsResource , val number : Int ) :
33- StringDesc () {
42+ @Parcelize
43+ actual data class Plural actual constructor(
44+ val pluralsRes : PluralsResource ,
45+ val number : Int
46+ ) : StringDesc(), Parcelable {
3447 override fun toString (context : Context ): String {
3548 return context.resources.getQuantityString(pluralsRes.resourceId, number)
3649 }
@@ -45,7 +58,7 @@ actual sealed class StringDesc {
4558 return context.resources.getQuantityString(
4659 pluralsRes.resourceId,
4760 number,
48- * (args.toTypedArray() )
61+ * processArgs (args, context )
4962 )
5063 }
5164
@@ -56,17 +69,21 @@ actual sealed class StringDesc {
5669 )
5770 }
5871
59- actual data class Raw actual constructor(val string : String ) : StringDesc() {
72+ @Parcelize
73+ actual data class Raw actual constructor(
74+ val string : String
75+ ) : StringDesc(), Parcelable {
6076 override fun toString (context : Context ): String {
6177 return string
6278 }
6379 }
6480
65- actual data class Composition actual constructor(val args : List <StringDesc >, val separator : String? ) : StringDesc() {
81+ actual data class Composition actual constructor(val args : List <StringDesc >, val separator : String? ) :
82+ StringDesc () {
6683 override fun toString (context : Context ): String {
6784 return StringBuilder ().apply {
6885 args.forEachIndexed { index, stringDesc ->
69- if (index != 0 && separator != null ) {
86+ if (index != 0 && separator != null ) {
7087 append(separator)
7188 }
7289 append(stringDesc.toString(context))
0 commit comments