Skip to content

Commit

Permalink
Merge pull request #18 from CodericLatam/data_preferences
Browse files Browse the repository at this point in the history
Flujo de Datos Completado
  • Loading branch information
Richi-Mi committed Apr 13, 2024
2 parents 61fa8ac + 11d6a6e commit 1d01a69
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 69 deletions.
27 changes: 20 additions & 7 deletions app/src/main/java/org/coderic/protective/mobile/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -14,50 +18,59 @@ import org.coderic.protective.mobile.model.Routes
import org.coderic.protective.mobile.presentation.DashBoardScreen
import org.coderic.protective.mobile.presentation.DeviceScreen
import org.coderic.protective.mobile.presentation.LoadingScreen
import org.coderic.protective.mobile.presentation.UpdatePetScreen
import org.coderic.protective.mobile.presentation.pet.UpdatePetScreen
import org.coderic.protective.mobile.presentation.components.MyBottomBar
import org.coderic.protective.mobile.presentation.pet.PetScreen
import org.coderic.protective.mobile.presentation.pet.PetViewModel
import org.coderic.protective.mobile.ui.theme.CocoAppTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val petViewModel = PetViewModel( this@MainActivity )

setContent {

CocoAppTheme {
// A surface container using the 'background' color from the theme
val navController = rememberNavController()
var showNav by remember { mutableStateOf( true ) }
val petViewModel = PetViewModel( this@MainActivity, navController )

Scaffold (
modifier = Modifier
.fillMaxSize(),
contentColor = MaterialTheme.colorScheme.background,
bottomBar = {
MyBottomBar( navController )
if( showNav ) MyBottomBar( navController )
}
) { paddingValues ->
NavHost(
navController = navController,
startDestination = Routes.MainScreen.route
) {
composable( Routes.MainScreen.route ) {
showNav = true
DashBoardScreen(paddingValues = paddingValues, navController )
}
composable( Routes.MyPetScreen.route) {
// PetScreen( paddingValues = paddingValues, petViewModel )
UpdatePetScreen( petViewModel, paddingValues )
showNav = true
PetScreen( paddingValues = paddingValues, petViewModel )
}
composable(Routes.MyUpdatePetScreen.route ) {
showNav = true
UpdatePetScreen( petViewModel, paddingValues )
}
composable( Routes.ExploreScreen.route ) {
showNav = true
LoadingScreen(paddingValues = paddingValues )
}
composable( Routes.ManageScreen.route ) {
showNav = true
DeviceScreen( paddingValues )
}
composable( Routes.MyUpdatePetScreen.route ) {
showNav = false
UpdatePetScreen( petViewModel, paddingValues )
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import org.coderic.protective.mobile.model.datos.Pet

interface Almacenamiento {
fun savePet( pet: Pet )
fun getPet() : Pet?
fun deletePet( pet: Pet )
fun saveDevice( device: Device )
fun getPet( id: Long ) : Pet?
fun getDevice() : Device
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ class MacronutrientPreferences (context: Context ) : Almacenamiento {
.put("id", pet.id )
.put("gender", pet.gender.type )
editor.putString("pet${pet.id}", petJson.toString() )
editor.putString( "id", pet.id.toString() )
editor.apply()
}

override fun saveDevice( device: Device ) {
TODO("Not yet implemented")
}

override fun getPet(id: Long): Pet? {
val petString = preferences.getString("pet$id", "")
if (petString.equals("")) return null;
val petJSON = JSONObject(petString)
override fun getPet(): Pet? {
val petId = preferences.getString("id", "")
if ( petId == "" ) return null;
val petString = preferences.getString("pet$petId", "")
val petJSON = JSONObject( petString!! )

return Pet(
name = petJSON.getString("name"),
Expand All @@ -51,6 +53,13 @@ class MacronutrientPreferences (context: Context ) : Almacenamiento {
)
}

override fun deletePet( pet: Pet ) {
val editor = preferences.edit()
editor.putString("pet${pet.id}", null )
editor.putString( "id", null )
editor.apply()
}

override fun getDevice(): Device {
return Device(
preferences.getString("device_name", "")!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class CasosUsoMascota( context: Context ) {
fun guardarMascota( pet: Pet ) {
almacenamiento.savePet( pet )
}
fun getMascota( id: Long ) : Pet? {
val pet = almacenamiento.getPet( id )
fun getMascota() : Pet? {
val pet = almacenamiento.getPet()
return pet
/*if( pet == null ) {
return Pet(
Expand All @@ -30,5 +30,8 @@ class CasosUsoMascota( context: Context ) {
return pet
}*/
}
fun deleteMascota( pet: Pet ) {
almacenamiento.deletePet( pet )
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ sealed class Routes( val route : String ) {
data object MyUpdatePetScreen : Routes("main/profile/update")
data object ExploreScreen: Routes("main/explore")
data object ManageScreen: Routes("main/manage")

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material3.Button
Expand All @@ -28,6 +29,9 @@ import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -38,6 +42,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import org.coderic.protective.mobile.R
import org.coderic.protective.mobile.model.datos.Pet
import org.coderic.protective.mobile.ui.theme.PetCareContentText
import org.coderic.protective.mobile.ui.theme.PetCareTitleText
import org.coderic.protective.mobile.ui.theme.rosa
Expand All @@ -46,13 +51,19 @@ import org.coderic.protective.mobile.ui.theme.text_desc_color

@Composable
fun PetScreen( paddingValues: PaddingValues, petViewModel: PetViewModel) {
val arePet by petViewModel.arePet.observeAsState( initial = false )

petViewModel.getPet()
Box(
Modifier
.fillMaxSize()
.padding(paddingValues)
) {
if( petViewModel.myPet != null ) {
if( arePet != false ) {
ImageBanner()
ButtonDelete( Modifier.align( Alignment.TopEnd ) ) {
petViewModel.deletePet( petViewModel.myPet.value!! )
}
BodyPetScreen(Modifier.align(Alignment.BottomCenter), petViewModel )
}
else {
Expand All @@ -61,6 +72,15 @@ fun PetScreen( paddingValues: PaddingValues, petViewModel: PetViewModel) {
}
}
@Composable
fun ButtonDelete( modifier: Modifier, onClick: () -> Unit ) {
Button(
modifier = modifier,
onClick = { onClick() }
) {
Icon(imageVector = Icons.Filled.Delete, contentDescription = "Delete")
}
}
@Composable
fun NoPetScreen(paddingValues: PaddingValues, petViewModel: PetViewModel) {
Column(
modifier = Modifier
Expand All @@ -75,7 +95,7 @@ fun NoPetScreen(paddingValues: PaddingValues, petViewModel: PetViewModel) {
Button(
modifier = Modifier
.fillMaxWidth(0.8f),
onClick = { petViewModel.btnAddPet() },
onClick = { petViewModel.btnToAddPet() },
shape = RoundedCornerShape(10.dp),
colors = ButtonDefaults.buttonColors(
containerColor = seed,
Expand Down Expand Up @@ -117,12 +137,12 @@ fun BodyPetScreen( modifier: Modifier, petViewModel: PetViewModel ) {
horizontalArrangement = Arrangement.SpaceBetween
) {
Column {
PetCareTitleText( text = petViewModel.myPet!!.name, 24 )
PetCareTitleText( text = petViewModel.myPet.value!!.name, 24 )
Spacer(modifier = Modifier.height(8.dp))
PetCareContentText (text = petViewModel.myPet.typePet, 16, text_desc_color )
PetCareContentText (text = petViewModel.myPet.value!!.typePet, 16, text_desc_color )
}
Icon(
painter = painterResource( id = petViewModel.myPet!!.gender.image ),
painter = painterResource( id = petViewModel.myPet.value!!.gender.image ),
contentDescription = "Gender",
tint = Color.White,
modifier = Modifier
Expand All @@ -141,20 +161,20 @@ fun BodyPetScreen( modifier: Modifier, petViewModel: PetViewModel ) {
tint = Color.Black
)
Spacer(modifier = Modifier.width(8.dp))
PetCareTitleText(text = "About ${petViewModel.myPet!!.name}", size = 20)
PetCareTitleText(text = "About ${petViewModel.myPet.value!!.name}", size = 20)
}
Spacer(modifier = Modifier.height(16.dp))
Row(
Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
PetData(stringResource(id = R.string.date_age), "${petViewModel.myPet!!.age.toString()} days" )
PetData(stringResource(id = R.string.date_weight), "${petViewModel.myPet!!.weight.toString()} Kg")
PetData(stringResource(id = R.string.date_height), "${petViewModel.myPet!!.height.toString()} cm" )
PetData(stringResource(id = R.string.date_color), petViewModel.myPet!!.color )
PetData(stringResource(id = R.string.date_age), "${petViewModel.myPet.value!!.age.toString()} days" )
PetData(stringResource(id = R.string.date_weight), "${petViewModel.myPet.value!!.weight.toString()} Kg")
PetData(stringResource(id = R.string.date_height), "${petViewModel.myPet.value!!.height.toString()} cm" )
PetData(stringResource(id = R.string.date_color), petViewModel.myPet.value!!.color )
}
Spacer(modifier = Modifier.height(16.dp))
PetCareContentText(text = petViewModel.myPet!!.description, size = 16, color = Color.Gray, align = TextAlign.Start )
PetCareContentText(text = petViewModel.myPet.value!!.description, size = 16, color = Color.Gray, align = TextAlign.Start )
Spacer(modifier = Modifier.height(24.dp))
Row {
Icon(
Expand All @@ -163,7 +183,7 @@ fun BodyPetScreen( modifier: Modifier, petViewModel: PetViewModel ) {
tint = Color.Black
)
Spacer(modifier = Modifier.width(8.dp))
PetCareTitleText(text = "${petViewModel.myPet.name}'s Status", size = 20)
PetCareTitleText(text = "${petViewModel.myPet.value!!.name}'s Status", size = 20)
}
Spacer(modifier = Modifier.height(16.dp))
PetStateData(title = "Location")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
package org.coderic.protective.mobile.presentation.pet

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.NavHostController
import org.coderic.protective.mobile.domain.CasosUsoMascota
import org.coderic.protective.mobile.model.datos.Gender
import org.coderic.protective.mobile.model.Routes
import org.coderic.protective.mobile.model.datos.Pet

class PetViewModel( context: Context) : ViewModel() {
val casosUsoMascota = CasosUsoMascota( context )
val myPet = casosUsoMascota.getMascota( 1231 )
class PetViewModel( context: Context, val navController: NavHostController ) : ViewModel() {
private val casosUsoMascota = CasosUsoMascota( context )

fun btnAddPet() {
private val _myPet = MutableLiveData<Pet?>( casosUsoMascota.getMascota() )
private val _arePet = MutableLiveData<Boolean>()

val myPet = _myPet
val arePet = _arePet
fun btnToAddPet() {
navController.navigate( Routes.MyUpdatePetScreen.route )
}
fun btnAddPet( pet: Pet ) {
casosUsoMascota.guardarMascota( pet )

_myPet.value = pet
arePet.value = true

navController.navigate( Routes.MyPetScreen.route )
}
fun getPet() {
_myPet.value = casosUsoMascota.getMascota()
if( _myPet.value == null ) {
_arePet.value = false
} else {
_arePet.value = true
}
}
fun deletePet( pet: Pet ) {
_arePet.value = false
_myPet.value = null
casosUsoMascota.deleteMascota( pet = pet )
}
}
Loading

0 comments on commit 1d01a69

Please sign in to comment.