Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.androidfactory.network.models.domain.CharacterStatus
import com.androidfactory.simplerick.ui.text.displayNameResource
import com.androidfactory.simplerick.ui.theme.RickTextPrimary
import com.androidfactory.simplerick.ui.theme.SimpleRickTheme

Expand All @@ -28,7 +30,7 @@ fun CharacterStatusComponent(characterStatus: CharacterStatus) {
.padding(horizontal = 12.dp, vertical = 4.dp)
) {
Text(
text = "Status: ${characterStatus.displayName}",
text = "Status: ${stringResource(characterStatus.displayNameResource())}",
fontSize = 20.sp,
color = RickTextPrimary
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package com.androidfactory.simplerick.components.common
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.androidfactory.simplerick.ui.text.UiText
import com.androidfactory.simplerick.ui.theme.RickAction
import com.androidfactory.simplerick.ui.theme.RickTextPrimary

data class DataPoint(
val title: String,
val description: String
val description: UiText
)

@Composable
fun DataPointComponent(dataPoint: DataPoint) {
val context = LocalContext.current
Column {
Text(
text = dataPoint.title,
Expand All @@ -24,7 +27,7 @@ fun DataPointComponent(dataPoint: DataPoint) {
color = RickAction
)
Text(
text = dataPoint.description,
text = dataPoint.description.asString(context),
fontSize = 24.sp,
color = RickTextPrimary
)
Expand All @@ -34,6 +37,9 @@ fun DataPointComponent(dataPoint: DataPoint) {
@Preview
@Composable
fun DataPointComponentPreview() {
val data = DataPoint(title = "Last known location", description = "Citadel of Ricks")
val data = DataPoint(
title = "Last known location",
description = UiText.DynamicText("Citadel of Ricks")
)
DataPointComponent(dataPoint = data)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.androidfactory.simplerick.ui.text

import com.androidfactory.network.models.domain.CharacterGender
import com.androidfactory.simplerick.R

fun CharacterGender.displayNameResource(): Int {
return when (this) {
CharacterGender.Male -> R.string.character_gender_male
CharacterGender.Female -> R.string.character_gender_female
CharacterGender.Genderless -> R.string.character_gender_unspecified
CharacterGender.Unknown -> R.string.character_gender_unknown
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.androidfactory.simplerick.ui.text

import com.androidfactory.network.models.domain.CharacterStatus
import com.androidfactory.simplerick.R

fun CharacterStatus.displayNameResource(): Int {
return when (this) {
CharacterStatus.Alive -> R.string.character_status_alive
CharacterStatus.Dead -> R.string.character_status_dead
CharacterStatus.Unknown -> R.string.character_status_unknown
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/androidfactory/simplerick/ui/text/UiText.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.androidfactory.simplerick.ui.text

import android.content.Context
import androidx.annotation.StringRes

sealed interface UiText {
data class StringResource(@StringRes val resId: Int) : UiText
data class DynamicText(val value: String) : UiText

fun asString(context: Context): String {
return when (this) {
is StringResource -> context.getString(resId)
is DynamicText -> value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.viewModelScope
import com.androidfactory.simplerick.components.common.DataPoint
import com.androidfactory.simplerick.repositories.CharacterRepository
import com.androidfactory.simplerick.screens.CharacterDetailsViewState
import com.androidfactory.simplerick.ui.text.UiText
import com.androidfactory.simplerick.ui.text.displayNameResource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -25,14 +27,14 @@ class CharacterDetailsViewModel @Inject constructor(
_internalStorageFlow.update { return@update CharacterDetailsViewState.Loading }
characterRepository.fetchCharacter(characterId).onSuccess { character ->
val dataPoints = buildList {
add(DataPoint("Last known location", character.location.name))
add(DataPoint("Species", character.species))
add(DataPoint("Gender", character.gender.displayName))
add(DataPoint("Last known location", UiText.DynamicText(character.location.name)))
add(DataPoint("Species", UiText.DynamicText(character.species)))
add(DataPoint("Gender", UiText.StringResource(character.gender.displayNameResource())))
character.type.takeIf { it.isNotEmpty() }?.let { type ->
add(DataPoint("Type", type))
add(DataPoint("Type", UiText.DynamicText(type)))
}
add(DataPoint("Origin", character.origin.name))
add(DataPoint("Episode count", character.episodeIds.size.toString()))
add(DataPoint("Origin", UiText.DynamicText(character.origin.name)))
add(DataPoint("Episode count", UiText.DynamicText(character.episodeIds.size.toString())))
}
_internalStorageFlow.update {
return@update CharacterDetailsViewState.Success(
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<resources>
<string name="app_name">Simple Rick</string>
<string name="character_status_dead">Dead</string>
<string name="character_status_alive">Alive</string>
<string name="character_status_unknown">Unknown</string>
<string name="character_gender_male">Male</string>
<string name="character_gender_female">Female</string>
<string name="character_gender_unspecified">No Gender</string>
<string name="character_gender_unknown">Unknown</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.androidfactory.network.models.domain

sealed class CharacterGender(val displayName: String) {
object Male: CharacterGender("Male")
object Female: CharacterGender("Female")
object Genderless: CharacterGender("No gender")
object Unknown: CharacterGender("Not specified")
sealed class CharacterGender() {
object Male: CharacterGender()
object Female: CharacterGender()
object Genderless: CharacterGender()
object Unknown: CharacterGender()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.androidfactory.network.models.domain

import androidx.compose.ui.graphics.Color

sealed class CharacterStatus(val displayName: String, val color: Color) {
object Alive: CharacterStatus("Alive", Color.Green)
object Dead: CharacterStatus("Dead", Color.Red)
object Unknown: CharacterStatus("Unknown", Color.Yellow)
sealed class CharacterStatus(val color: Color) {
object Alive: CharacterStatus(Color.Green)
object Dead: CharacterStatus(Color.Red)
object Unknown: CharacterStatus(Color.Yellow)
}