Skip to content

Commit

Permalink
A small fix with dialog QuantityForm
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGnk committed Jun 11, 2024
1 parent 4435123 commit b882acc
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ class AdminHomeModel(val repository: GreenRepository) : ViewModel() {

init {
setRandomTip()
if (!greetingAnimationHasPlayedOnce) {
//if (!greetingAnimationHasPlayedOnce) {

viewModelScope.launch {
delay(400)
getGreetingTextFromTime()
}
viewModelScope.launch {
delay(2300)
state.update {
it.copy(greetingText = R.string.app_name)
}
greetingAnimationHasPlayedOnce = true
viewModelScope.launch {
delay(300)
getGreetingTextFromTime()
}
viewModelScope.launch {
delay(2300)
state.update {
it.copy(greetingText = R.string.app_name)
}
//greetingAnimationHasPlayedOnce = true
}
//}
cityLevelInit()
initializeRankChart()
initializeQuantityChart()
Expand Down Expand Up @@ -198,10 +198,11 @@ class AdminHomeModel(val repository: GreenRepository) : ViewModel() {
}
}

var greetingAnimationHasPlayedOnce: Boolean = false
//var greetingAnimationHasPlayedOnce: Boolean = hide for now

data class AdminHomeState(
val greetingText: Int = if (greetingAnimationHasPlayedOnce) R.string.app_name else R.string.empty,
// val greetingText: Int = if (greetingAnimationHasPlayedOnce) R.string.app_name else R.string.empty,
val greetingText: Int = R.string.empty,
)

data class PieState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ class UserFormModel(
state.update {
it.copy(
selectedMaterial = material,
dialogDestination = FormDialogDestination.QUANTITY
dialogDestination = FormDialogDestination.QUANTITY,
isGramsSelected = when (material.type) {
is Both -> QuantityType.BOTH_SHOW_GRAMS
is Grams -> QuantityType.ONLY_GRAMS
is Pieces -> QuantityType.ONLY_PIECES
}
)
}
}
Expand All @@ -107,10 +112,11 @@ class UserFormModel(
val idOfTrack = form.value.formId
val material = state.value.selectedMaterial
val selectedMaterial = state.value.selectedMaterial
val points = when (selectedMaterial.type) {
is Both -> if (state.value.isGramsSelected) selectedMaterial.type.pointsPerGram else selectedMaterial.type.pointsPerPiece
is Grams -> selectedMaterial.type.pointsPerGram
is Pieces -> selectedMaterial.type.pointsPerPiece
val points = when (state.value.isGramsSelected) {
QuantityType.ONLY_GRAMS -> selectedMaterial.type.pointsPerGram
QuantityType.ONLY_PIECES -> selectedMaterial.type.pointsPerPiece
QuantityType.BOTH_SHOW_GRAMS -> selectedMaterial.type.pointsPerGram
QuantityType.BOTH_SHOW_PIECES -> selectedMaterial.type.pointsPerPiece
}

val givenQuantityClean = state.value.query.replace(',', '.')
Expand Down Expand Up @@ -144,9 +150,9 @@ class UserFormModel(
repository.delete(mutableEntry.first, viewModelScope)
}

fun onDialogQuantityChangeSelection(index: Int) {
fun onDialogQuantityChangeSelection(type: QuantityType) {
state.update {
it.copy(isGramsSelected = index == 0)
it.copy(isGramsSelected = type)
}
}

Expand Down Expand Up @@ -181,7 +187,7 @@ data class UserFormState(
val trackToAdd: Track? = null,
val trackMaterialsMap: List<Pair<Track, Material>> = listOf(),
val selectedMaterial: Material = Material(0, RecyclingCategory.OTHER, ""),
val isGramsSelected: Boolean = true,
val isGramsSelected: QuantityType = QuantityType.ONLY_GRAMS,
val query: String = "",
val recyclingCategories: EnumEntries<RecyclingCategory> = RecyclingCategory.entries,
val selectedCategory: RecyclingCategory = RecyclingCategory.PLASTIC,
Expand All @@ -205,4 +211,8 @@ enum class FormDialogDestination(val title: Int) {
CATEGORY(R.string.user_form_dialog_category_title),
MATERIAL(R.string.user_form_dialog_material_title),
QUANTITY(R.string.user_form_dialog_quantity_title)
}

enum class QuantityType {
ONLY_GRAMS, ONLY_PIECES, BOTH_SHOW_GRAMS, BOTH_SHOW_PIECES
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -37,23 +38,23 @@ import com.example.greenifyme.compose_utilities.theme.ComposeTheme
import com.example.greenifyme.data.Both
import com.example.greenifyme.data.Grams
import com.example.greenifyme.data.OptionsType
import com.example.greenifyme.data.Pieces
import com.example.greenifyme.ui.user.form.QuantityType

@Composable
@Preview(showBackground = true)
fun QuantityFormPreview() {
ComposeTheme {
QuantityForm(
Grams(23f), true, {}, {}, "", {}
Grams(23f), QuantityType.BOTH_SHOW_GRAMS, {}, {}, "", {}
)
}
}

@Composable
fun QuantityForm(
options: OptionsType,
isGramsSelected: Boolean,
onDialogQuantityChangeSelection: (Int) -> Unit,
isGramsSelected: QuantityType,
onDialogQuantityChangeSelection: (QuantityType) -> Unit,
onDialogQuantityQueryChange: (String) -> Unit,
query: String,
onEnter: () -> Unit
Expand Down Expand Up @@ -97,13 +98,13 @@ fun QuantityForm(
selectedOption = isGramsSelected,
gramsContent = {
Text(
text = if (options is Grams) "Enter grams" else "Enter pieces",
text = getString(R.string.user_dialog_quantity_grams),
style = MaterialTheme.typography.bodyMedium
)
},
piecesContent = {
Text(
text = "Enter pieces",
text = getString(R.string.user_dialog_quantity_pieces),
style = MaterialTheme.typography.bodyMedium
)
}
Expand All @@ -121,60 +122,55 @@ fun QuantityForm(
)


when (options) {
is Grams -> {
Text(
text = "*For every gram you get ${options.pointsPerGram} point",
style = MaterialTheme.typography.bodySmall,
Text(
text = when (isGramsSelected) {
QuantityType.ONLY_GRAMS -> stringResource(
R.string.user_dialog_quantity_grams_dis,
options.pointsPerGram
)
}

is Pieces -> {
Text(
text = "*For every piece you get ${options.pointsPerPiece} point",
style = MaterialTheme.typography.bodySmall
QuantityType.ONLY_PIECES -> stringResource(
R.string.user_dialog_quantity_pieces_dis,
options.pointsPerPiece
)
}

is Both -> {
if (isGramsSelected) {
Text(
text = "*For every gram you get ${options.pointsPerGram} point",
style = MaterialTheme.typography.bodySmall,
)
} else {
Text(
text = "*For every piece you get ${options.pointsPerPiece} point",
style = MaterialTheme.typography.bodySmall
)
}
}
}
QuantityType.BOTH_SHOW_GRAMS -> stringResource(
R.string.user_dialog_quantity_grams_dis,
options.pointsPerGram
)

QuantityType.BOTH_SHOW_PIECES -> stringResource(
R.string.user_dialog_quantity_pieces_dis,
options.pointsPerPiece
)
},
style = MaterialTheme.typography.bodySmall
)
}
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun SegmentedButtons(
isGramsSelected: Boolean,
onDialogQuantityChangeSelection: (Int) -> Unit
isGramsSelected: QuantityType,
onDialogQuantityChangeSelection: (QuantityType) -> Unit
) {
SingleChoiceSegmentedButtonRow(
modifier = Modifier.fillMaxWidth()
) {
SegmentedButton(
shape = SegmentedButtonDefaults.itemShape(index = 0, count = 2),
onClick = { onDialogQuantityChangeSelection(0) },
selected = isGramsSelected,
onClick = { onDialogQuantityChangeSelection(QuantityType.BOTH_SHOW_GRAMS) },
selected = (isGramsSelected == QuantityType.BOTH_SHOW_GRAMS),
modifier = Modifier.height(50.dp),
) {
Text(getString(stringValue = R.string.user_form_dialog_quantity_option_grams))
}
SegmentedButton(
shape = SegmentedButtonDefaults.itemShape(index = 1, count = 2),
onClick = { onDialogQuantityChangeSelection(1) },
selected = !isGramsSelected,
onClick = { onDialogQuantityChangeSelection(QuantityType.BOTH_SHOW_PIECES) },
selected = isGramsSelected == QuantityType.BOTH_SHOW_PIECES,
modifier = Modifier.height(50.dp),
) {
Text(getString(stringValue = R.string.user_form_dialog_quantity_option_pieces))
Expand All @@ -184,7 +180,7 @@ private fun SegmentedButtons(

@Composable
private fun AnimatedSwitcher(
selectedOption: Boolean,
selectedOption: QuantityType,
gramsContent: @Composable (() -> Unit),
piecesContent: @Composable (() -> Unit),
modifier: Modifier = Modifier
Expand All @@ -203,8 +199,10 @@ private fun AnimatedSwitcher(
modifier = modifier
) { targetDestination ->
when (targetDestination) {
true -> gramsContent()
false -> piecesContent()
QuantityType.ONLY_GRAMS -> gramsContent()
QuantityType.ONLY_PIECES -> piecesContent()
QuantityType.BOTH_SHOW_GRAMS -> gramsContent()
QuantityType.BOTH_SHOW_PIECES -> piecesContent()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ fun UserFormDialogMain(model: UserFormModel, state: UserFormState) {
},
text = {

// Box(
// modifier = if (state.dialogDestination != FormDialogDestination.QUANTITY) Modifier.height(
// 314.dp
// ) else Modifier
// ) {
AnimatedDialogSwitcher(
state.dialogDestination,
{
Expand All @@ -66,9 +61,8 @@ fun UserFormDialogMain(model: UserFormModel, state: UserFormState) {
onEnter = { model.addTrack() }
)
},
modifier = Modifier.height(314.dp) // Adjust height as needed,
modifier = Modifier.height(314.dp)
)
//}
},
dismissButton = {
TextButton(onClick = { model.onDismissButton() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ class UserHomeModel(
tipState.update {
it.copy(selectedTip = tipList[tipList.indices.random()])
}
if (!greetingAnimationPlayedUser) {
viewModelScope.launch {
delay(400)
getGreetingTextFromTime()
}
viewModelScope.launch {
delay(2300)
state.update {
it.copy(greetingText = R.string.app_name)
}
greetingAnimationPlayedUser = true
//if (!greetingAnimationPlayedUser) {
viewModelScope.launch {
delay(300)
getGreetingTextFromTime()
}
viewModelScope.launch {
delay(2300)
state.update {
it.copy(greetingText = R.string.app_name)
}
//greetingAnimationPlayedUser = true
}
// }
viewModelScope.launch {
delay(400) // Adjust this delay as needed
animatedState.update { pointState.value.percentInLevel }
Expand Down Expand Up @@ -77,9 +77,11 @@ class UserHomeModel(
}
}

var greetingAnimationPlayedUser: Boolean = false
//var greetingAnimationPlayedUser: Boolean = false

data class UserHomeState(
val hasShowedOnce: Boolean = false,
val greetingText: Int = if (greetingAnimationPlayedUser) R.string.app_name else R.string.empty,
)
// val greetingText: Int = if (greetingAnimationPlayedUser) R.string.app_name else R.string.empty,
val greetingText: Int = R.string.empty,

)
4 changes: 4 additions & 0 deletions app/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
<string name="user_form_dialog_quantity_option_pieces">Κομμάτια</string>
<string name="user_points_label">Πόντοι</string>
<string name="user_home_welcome_dialog_message">Κερδίστε πόντους μέσα από την ανακύκλωση\n\nΔημιουργήστε την πρώτη σας φόρμα πατώντας "Φόρμα".</string>
<string name="user_dialog_quantity_pieces">Εισάγεται τεμάχια</string>
<string name="user_dialog_quantity_grams">Εισάγεται γραμμάρια</string>
<string name="user_dialog_quantity_pieces_dis">*Για κάθε τεμάχιο παίρνετε %1$s πόντους</string>
<string name="user_dialog_quantity_grams_dis">*Για κάθε γραμμάριο παίρνετε %1$s πόντους</string>

<!-- Admin Pages -->
<string name="admin_city_level_1">Eco-Starter</string>
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
<string name="user_form_dialog_quantity_option_grams">Grams</string>
<string name="user_form_dialog_quantity_option_pieces">Pieces</string>
<string name="user_home_welcome_dialog_message">Earn rewards for recycling and track your progress\n\nCreate your first recycling form with a tap in the "Form" section.</string>
<string name="user_dialog_quantity_pieces">Enter pieces</string>
<string name="user_dialog_quantity_grams">Enter grams</string>
<string name="user_dialog_quantity_pieces_dis">*For every piece you get %1$s points</string>
<string name="user_dialog_quantity_grams_dis">*For every gram you get %1$s points</string>


<!-- Admin Pages -->
Expand Down Expand Up @@ -248,7 +252,8 @@
<string name="admin_notification_form_tracks">Tracks</string>
<string name="admin_notification_form_information">Information</string>
<string name="admin_edit_materials_dialog_name">Enter name</string>
<string name="admin_edit_materials_dialog_pieces">Enter pieces</string>
<string name="admin_edit_materials_dialog_grams">Enter grams</string>
<string name="admin_edit_materials_dialog_pieces">Enter points per piece</string>
<string name="admin_edit_materials_dialog_grams">Enter points per gram</string>


</resources>

0 comments on commit b882acc

Please sign in to comment.