-
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.
Merge pull request #26 from Q42/feature/viewstatestring
ADD ViewStateString
- Loading branch information
Showing
10 changed files
with
139 additions
and
22 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
28 changes: 28 additions & 0 deletions
28
core/ui/src/main/kotlin/nl/q42/template/ui/compose/ViewStateStringResolver.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,28 @@ | ||
package nl.q42.template.ui.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.pluralStringResource | ||
import androidx.compose.ui.res.stringResource | ||
import nl.q42.template.ui.presentation.ViewStateString | ||
|
||
/** | ||
* Resolve a ViewStateString to a string from a Compose context. | ||
*/ | ||
@Composable | ||
fun ViewStateString.get(): String { | ||
return when (this) { | ||
is ViewStateString.Res -> { | ||
// Map any nested ViewStateStrings to their resolved values. | ||
val resolvedArguments = | ||
this.formatArgs.map { if (it is ViewStateString) it.get() else it } | ||
.toTypedArray() | ||
stringResource(id = this.stringRes, formatArgs = resolvedArguments) | ||
} | ||
|
||
is ViewStateString.PluralRes -> { | ||
pluralStringResource(id = pluralRes, count = count, count) | ||
} | ||
|
||
is ViewStateString.Basic -> this.value | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
core/ui/src/main/kotlin/nl/q42/template/ui/presentation/ViewStateString.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,41 @@ | ||
package nl.q42.template.ui.presentation | ||
|
||
import androidx.annotation.PluralsRes | ||
import androidx.annotation.StringRes | ||
|
||
/** | ||
* This class enables you to use string logic in the ViewModel, especially plurals or replacement parameter logic. | ||
* | ||
* All strings will be refreshed on view recreation (i.e. after locale or other config changes). | ||
* | ||
* There is one edge case: the formatArgs are not refreshed on config changes, so if you use a string with a | ||
* replacement parameter that is f.e. a localized date string, move your logic to the view instead of using [ViewStateString]. | ||
*/ | ||
sealed class ViewStateString { | ||
data class Res( | ||
@StringRes val stringRes: Int, | ||
val formatArgs: List<Any> = listOf() | ||
) : ViewStateString() { | ||
// Allow constructing ViewStateString.Res with varargs instead of passing a list | ||
@Suppress("unused") | ||
constructor(@StringRes stringRes: Int, vararg formatArgs: Any) : this(stringRes, formatArgs.toList()) | ||
} | ||
|
||
data class PluralRes( | ||
@PluralsRes val pluralRes: Int, | ||
val count: Int, | ||
val formatArgs: List<Any> = listOf(count) | ||
) : ViewStateString() { | ||
// Allow constructing ViewStateString.PluralRes with varargs instead of passing a list | ||
@Suppress("unused") | ||
constructor(@PluralsRes pluralRes: Int, count: Int, vararg formatArgs: Any) : this( | ||
pluralRes, | ||
count, | ||
formatArgs.toList() | ||
) | ||
} | ||
|
||
data class Basic(val value: String) : ViewStateString() | ||
} | ||
|
||
fun String.toViewStateString(): ViewStateString.Basic = ViewStateString.Basic(this) |
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
6 changes: 4 additions & 2 deletions
6
feature/home/src/main/kotlin/nl/q42/template/presentation/home/HomeViewState.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,8 +1,10 @@ | ||
package nl.q42.template.presentation.home | ||
|
||
import nl.q42.template.ui.presentation.ViewStateString | ||
|
||
sealed class HomeViewState { | ||
data class Data(val userEmail: String? = null) : HomeViewState() | ||
data class Data(val userEmailTitle: ViewStateString? = null) : HomeViewState() | ||
object Loading : HomeViewState() | ||
object Error : HomeViewState() | ||
object Empty : HomeViewState() | ||
} | ||
} |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="emailTitle">Email adres %s</string> | ||
</resources> |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="emailTitle">Email address %s</string> | ||
</resources> |
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