Skip to content

Commit

Permalink
Merge pull request #65 from PetJournal/feature/add-castration-field-o…
Browse files Browse the repository at this point in the history
…n-date-of-birth-screen

Add pet castration selectors to the birth date screen
  • Loading branch information
N0stalgiaUltra authored Jul 8, 2024
2 parents ba7f4c5 + fb3be63 commit 6939216
Show file tree
Hide file tree
Showing 20 changed files with 314 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ sealed class BirthDateFormEvent {
data class PetBirthDate(val petBirth: String) : BirthDateFormEvent()
object NextButton : BirthDateFormEvent()
data class IdPetInformation(val idPetInformation: Long) : BirthDateFormEvent()
data class PetCastration(val petCastration: Boolean?) : BirthDateFormEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ data class BirthDateFormState(
val name: String = "",
val gender: String = "",
val size: String = "",
val race: String = ""
val race: String = "",
val castration: Boolean? = null,
val castrationError: List<String>? = null
)

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract class BirthDateViewModel : ViewModel(){
abstract fun change(
petBirth: String? = null,
idPetInformation: Long? = null,
petCastration: Boolean? = null
)
abstract fun getPetInformation(id: Long)
abstract fun updatePetInformation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class BirthDateViewModelImpl(
gender = petInformationModel.gender ?: "",
size = petInformationModel.size ?: "",
race = petInformationModel.petRace ?: ""

)
viewModelScope.launch {
validationEventChannel.send(ValidationEvent.Success)
Expand All @@ -58,23 +57,34 @@ class BirthDateViewModelImpl(
is BirthDateFormEvent.PetBirthDate -> change(petBirth = event.petBirth)
is BirthDateFormEvent.IdPetInformation -> change(idPetInformation = event.idPetInformation)
is BirthDateFormEvent.NextButton -> {
change(petCastration = state.castration)
change(petBirth = state.birth)
}
is BirthDateFormEvent.PetCastration -> change(petCastration = event.petCastration)
}
}

override fun enableButton(): Boolean {
return state.birthError.isNullOrEmpty()
return state.birthError.isNullOrEmpty() && state.castrationError.isNullOrEmpty()
}

override fun change(petBirth: String?, idPetInformation: Long?) {
override fun change(petBirth: String?, idPetInformation: Long?, petCastration: Boolean?) {
when {
petBirth != null -> {
state = state.copy(birth = petBirth)
val result = validation.validateDate(state.birth)
state = if (result.success) state.copy(birthError = null)
else state.copy(birthError = result.errorMessage)
}
petCastration == null || petCastration == true || petCastration == false-> {
state = state.copy(castration = petCastration)
val result = validation.validatePetCastration(state.castration)
state = if (result.success) state.copy(castrationError = null)
else state.copy(castrationError = result.errorMessage)
}
idPetInformation != null -> {
state = state.copy(idPetInformation = idPetInformation)
}
}
}

Expand All @@ -99,7 +109,8 @@ class BirthDateViewModelImpl(
size = state.size,
petRace = state.race,
petAge = state.birth,
guardianId = 1
guardianId = 1,
castration = state.castration
)

val result = updatePetInformationUseCase.execute(petInformation)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.soujunior.petjournal.R
import com.soujunior.petjournal.ui.components.AlertText
import com.soujunior.petjournal.ui.components.RoundedSquare


@Composable
fun CastrationSelector(
selectedCastration: (Boolean?) -> Unit,
clearSelection: () -> Boolean,
textError: List<String>? = null,
textNamePet: String = "Bolinha"
) {
Column(modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
content = {
Row {
Text(
modifier = Modifier.fillMaxWidth(),
text = stringResource(id = R.string.pet_castration, textNamePet),
fontSize = 17.sp,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.primary,
textAlign = TextAlign.Center
)
Spacer(
modifier = Modifier
.fillMaxWidth()
.padding(25.dp)
)
}

CastrationButtons(selectedCastration, clearSelection, textError)
})
}

@Composable
private fun CastrationButtons(
selectedCastration: (Boolean?) -> Unit,
clearSelection: () -> Boolean,
textError: List<String>? = null,
) {

var selectedItem by remember { mutableStateOf("") }
if (clearSelection()) {
selectedItem = ""
selectedCastration(null)
}

Row(
modifier = Modifier
.padding(horizontal = 2.dp)
.testTag("genderButtons_test"),
horizontalArrangement = Arrangement.spacedBy(16.dp)
) {
RoundedSquare(
text = stringResource(id = R.string.text_yes),
colorText = if (selectedItem == "S") MaterialTheme.colorScheme.tertiary else MaterialTheme.colorScheme.onSurfaceVariant,
isSelected = selectedItem == "S",
size = 120.dp,
topLeftRadius = 32.dp,
topRightRadius = 32.dp,
bottomLeftRadius = 32.dp,
bottomRightRadius = 32.dp,
image = if (selectedItem == "S") painterResource(id = R.drawable.icon_solid_check_enable) else painterResource(
id = R.drawable.icon_check
),
selectedColor = if (selectedItem == "S") MaterialTheme.colorScheme.primary else Color.Transparent,


colorBackground = Color.Transparent,
onClick = {
selectedItem = "S"
selectedCastration(true)
}
)

RoundedSquare(
text = stringResource(id = R.string.text_no),
colorText = if (selectedItem == "N") MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant,
isSelected = selectedItem == "N",
size = 120.dp,
topLeftRadius = 32.dp,
topRightRadius = 32.dp,
bottomLeftRadius = 32.dp,
bottomRightRadius = 32.dp,
image = if (selectedItem == "N") painterResource(id = R.drawable.icon_solid_close_enable) else painterResource(
id = R.drawable.icon_close
),
selectedColor = if (selectedItem == "N") MaterialTheme.colorScheme.primary else Color.Transparent,
colorBackground = Color.Transparent,
onClick = {
selectedItem = "N"
selectedCastration(false)
}
)
}
Row(modifier = Modifier.fillMaxWidth()) {
if (textError != null) {
textError.forEach {
AlertText(textMessage = it, modifier = Modifier.padding(10.dp))
}
} else {
Text(
text = stringResource(id = R.string.required_field),
modifier = Modifier.padding(start = 2.dp, top = 10.dp),
fontSize = 15.sp,
textAlign = TextAlign.Start
)
}
}
}


@Preview(showBackground = true)
@Composable
fun CastrationSelectorPreview() {
CastrationSelector(
selectedCastration = {},
clearSelection = { true },
textError = null
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,32 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.soujunior.petjournal.R
import com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.BirthDateFormEvent
import com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.BirthDateViewModel
import com.soujunior.petjournal.ui.screens_app.screens_pets.petRaceAndSizeScreen.RaceSizeFormEvent
import com.soujunior.petjournal.ui.components.Breadcrumb
import com.soujunior.petjournal.ui.components.Button3
import com.soujunior.petjournal.ui.components.DateInputText
import com.soujunior.petjournal.ui.components.NavigationBar
import com.soujunior.petjournal.ui.components.ScaffoldCustom
import com.soujunior.petjournal.ui.components.mask.formatDate
import com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.BirthDateFormEvent
import com.soujunior.petjournal.ui.screens_app.screens_pets.petBirthDateScreen.BirthDateViewModel
import com.soujunior.petjournal.ui.screens_app.screens_pets.petRaceAndSizeScreen.RaceSizeFormEvent
import org.koin.androidx.compose.getViewModel

@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun Screen(idPetInformation: String?, navController: NavController) {
val viewModel: BirthDateViewModel = getViewModel()
var isClearCastration by remember { mutableStateOf(false) }
if (idPetInformation != null) {
viewModel.getPetInformation(idPetInformation.toLong())
RaceSizeFormEvent.IdPetInformation(idPetInformation = idPetInformation.toLong())
Expand Down Expand Up @@ -97,6 +102,25 @@ fun Screen(idPetInformation: String?, navController: NavController) {
visualTransformation = { formatDate(it) }
)
}
item {
CastrationSelector(
textNamePet = viewModel.state.name,
selectedCastration = { selectedCastration ->
if (selectedCastration != null) {
isClearCastration = false
}
viewModel.onEvent(
BirthDateFormEvent.PetCastration(
selectedCastration
)
)
},
clearSelection = {
isClearCastration
},
textError = viewModel.state.castrationError
)
}
item {
Row(
Modifier.padding(top = 80.dp)
Expand Down Expand Up @@ -126,9 +150,10 @@ fun Screen(idPetInformation: String?, navController: NavController) {
)

if (viewModel.enableButton() &&
viewModel.state.birth.isNotEmpty()
viewModel.state.birth.isNotEmpty() &&
viewModel.state.castration != null
) {
Log.i(TAG, viewModel.state.birth)
Log.i(TAG, viewModel.state.birth + viewModel.state.castration.toString())
viewModel.updatePetInformation()
//navController.navigate("pets/birth/$it")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class ViewModelNameGenderImpl(
}

is NameGenderFormEvent.NextButton -> {
change(petName = state.name, petGender = state.gender)
change(petGender = state.gender)
change(petName = state.name)
}

else -> {
Expand Down
10 changes: 10 additions & 0 deletions petJournal/app/src/main/res/drawable/icon_check.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#B2B2B2"
android:pathData="M422.46,563.69L329.62,470.85Q320,461.23 306.85,460.85Q293.69,460.46 283.31,470.85Q272.92,481.23 272.92,494Q272.92,506.77 283.31,517.15L390.85,624.69Q404.36,638.38 422.37,638.38Q440.38,638.38 454.08,624.69L677.15,401.62Q686.77,392 687.15,378.85Q687.54,365.69 677.15,355.31Q666.77,344.92 654,344.92Q641.23,344.92 630.85,355.31L422.46,563.69ZM480.13,872Q398.82,872 327.24,841.14Q255.67,810.28 202.72,757.38Q149.77,704.48 118.89,632.96Q88,561.45 88,480.13Q88,398.57 118.92,326.76Q149.84,254.96 202.84,201.85Q255.84,148.73 327.26,118.37Q398.67,88 479.87,88Q561.42,88 633.22,118.34Q705.01,148.68 758.14,201.76Q811.27,254.84 841.63,326.6Q872,398.36 872,479.95Q872,561.54 841.66,632.78Q811.32,704.01 758.25,757.06Q705.18,810.11 633.44,841.06Q561.7,872 480.13,872Z"/>
</vector>
13 changes: 13 additions & 0 deletions petJournal/app/src/main/res/drawable/icon_close.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="69dp"
android:height="68dp"
android:viewportWidth="69"
android:viewportHeight="68">
<group>
<clip-path
android:pathData="M3.947,4h60v60h-60z"/>
<path
android:pathData="M33.947,4C36.701,4 39.357,4.352 41.916,5.055C44.475,5.758 46.857,6.773 49.064,8.102C51.271,9.43 53.293,10.992 55.129,12.789C56.965,14.586 58.537,16.607 59.846,18.854C61.154,21.1 62.16,23.492 62.863,26.031C63.566,28.57 63.928,31.227 63.947,34C63.947,36.754 63.596,39.41 62.893,41.969C62.189,44.527 61.174,46.91 59.846,49.117C58.518,51.324 56.955,53.346 55.158,55.182C53.361,57.018 51.34,58.59 49.094,59.898C46.848,61.207 44.455,62.213 41.916,62.916C39.377,63.619 36.721,63.98 33.947,64C31.193,64 28.537,63.648 25.979,62.945C23.42,62.242 21.037,61.227 18.83,59.898C16.623,58.57 14.602,57.008 12.766,55.211C10.93,53.414 9.357,51.393 8.049,49.146C6.74,46.9 5.734,44.508 5.031,41.969C4.328,39.43 3.967,36.773 3.947,34C3.947,31.246 4.299,28.59 5.002,26.031C5.705,23.473 6.721,21.09 8.049,18.883C9.377,16.676 10.939,14.654 12.736,12.818C14.533,10.982 16.555,9.41 18.801,8.102C21.047,6.793 23.44,5.787 25.979,5.084C28.518,4.381 31.174,4.02 33.947,4ZM37.258,34L47.277,23.98L43.967,20.67L33.947,30.69L23.928,20.67L20.617,23.98L30.637,34L20.617,44.02L23.928,47.33L33.947,37.311L43.967,47.33L47.277,44.02L37.258,34Z"
android:fillColor="#B2B2B2"/>
</group>
</vector>
10 changes: 10 additions & 0 deletions petJournal/app/src/main/res/drawable/icon_solid_check_enable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="#A7EC97">
<path
android:fillColor="#B2B2B2"
android:pathData="M422.46,563.69L329.62,470.85Q320,461.23 306.85,460.85Q293.69,460.46 283.31,470.85Q272.92,481.23 272.92,494Q272.92,506.77 283.31,517.15L390.85,624.69Q404.36,638.38 422.37,638.38Q440.38,638.38 454.08,624.69L677.15,401.62Q686.77,392 687.15,378.85Q687.54,365.69 677.15,355.31Q666.77,344.92 654,344.92Q641.23,344.92 630.85,355.31L422.46,563.69ZM480.13,872Q398.82,872 327.24,841.14Q255.67,810.28 202.72,757.38Q149.77,704.48 118.89,632.96Q88,561.45 88,480.13Q88,398.57 118.92,326.76Q149.84,254.96 202.84,201.85Q255.84,148.73 327.26,118.37Q398.67,88 479.87,88Q561.42,88 633.22,118.34Q705.01,148.68 758.14,201.76Q811.27,254.84 841.63,326.6Q872,398.36 872,479.95Q872,561.54 841.66,632.78Q811.32,704.01 758.25,757.06Q705.18,810.11 633.44,841.06Q561.7,872 480.13,872Z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="69dp"
android:height="68dp"
android:viewportWidth="69"
android:viewportHeight="68">
<path
android:pathData="M33.947,4C36.701,4 39.357,4.352 41.916,5.055C44.475,5.758 46.857,6.773 49.064,8.102C51.271,9.43 53.293,10.992 55.129,12.789C56.965,14.586 58.537,16.607 59.846,18.854C61.154,21.1 62.16,23.492 62.863,26.031C63.566,28.57 63.928,31.227 63.947,34C63.947,36.754 63.596,39.41 62.893,41.969C62.189,44.527 61.174,46.91 59.846,49.117C58.518,51.324 56.955,53.346 55.158,55.182C53.361,57.018 51.34,58.59 49.094,59.898C46.848,61.207 44.455,62.213 41.916,62.916C39.377,63.619 36.721,63.98 33.947,64C31.193,64 28.537,63.648 25.979,62.945C23.42,62.242 21.037,61.227 18.83,59.898C16.623,58.57 14.602,57.008 12.766,55.211C10.93,53.414 9.357,51.393 8.049,49.146C6.74,46.9 5.734,44.508 5.031,41.969C4.328,39.43 3.967,36.773 3.947,34C3.947,31.246 4.299,28.59 5.002,26.031C5.705,23.473 6.721,21.09 8.049,18.883C9.377,16.676 10.939,14.654 12.736,12.818C14.533,10.982 16.555,9.41 18.801,8.102C21.047,6.793 23.44,5.787 25.979,5.084C28.518,4.381 31.174,4.02 33.947,4ZM37.258,34L47.277,23.98L43.967,20.67L33.947,30.69L23.928,20.67L20.617,23.98L30.637,34L20.617,44.02L23.928,47.33L33.947,37.311L43.967,47.33L47.277,44.02L37.258,34Z"
android:fillColor="#FF917A"/>
</vector>
3 changes: 3 additions & 0 deletions petJournal/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@
<string name="pet_birth_date">Data de nascimento</string>
<string name="placeholder_text_DD_MM_YYYY">DD/MM/AAAA</string>
<string name="pet_gender_letter_M">M</string>
<string name="pet_castration">Nos conte, %s é castrado?</string>
<string name="text_yes">Sim</string>
<string name="text_no">Não</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ val perInformation = PetInformationModel(
size = "Pequeno",
petRace = "Akita",
petAge = "12122012",
guardianId = 1
guardianId = 1,
castration = true
)

val listDogRaces = listOf(
Expand Down
Loading

0 comments on commit 6939216

Please sign in to comment.