Skip to content

Commit

Permalink
Merge pull request #14 from CodericLatam/data_preferences
Browse files Browse the repository at this point in the history
Guardado de datos usando SharedPreferences.
  • Loading branch information
NeftaliYagua committed Apr 4, 2024
2 parents 2b70d41 + f9e9b5d commit c1bf967
Show file tree
Hide file tree
Showing 114 changed files with 565 additions and 46 deletions.
Empty file modified .github/dependabot.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/gradle.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .idea/.gitignore
100644 → 100755
Empty file.
Empty file modified .idea/.name
100644 → 100755
Empty file.
Empty file modified .idea/appInsightsSettings.xml
100644 → 100755
Empty file.
123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified .idea/compiler.xml
100644 → 100755
Empty file.
Empty file modified .idea/deploymentTargetDropDown.xml
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion .idea/gradle.xml
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified .idea/inspectionProfiles/Project_Default.xml
100644 → 100755
Empty file.
Empty file modified .idea/kotlinc.xml
100644 → 100755
Empty file.
Empty file modified .idea/migrations.xml
100644 → 100755
Empty file.
Empty file modified .idea/misc.xml
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion .idea/vcs.xml
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified .settings/org.eclipse.buildship.core.prefs
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified app/.gitignore
100644 → 100755
Empty file.
Empty file modified app/.settings/org.eclipse.buildship.core.prefs
100644 → 100755
Empty file.
Empty file modified app/.settings/org.eclipse.jdt.core.prefs
100644 → 100755
Empty file.
Empty file modified app/build.gradle.kts
100644 → 100755
Empty file.
Empty file modified app/proguard-rules.pro
100644 → 100755
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.coderic.protective.mobile.MainActivity" android:exported="false" />
<activity android:name="org.coderic.protective.mobile.MainActivity" android:exported="true" />
</application>

</manifest>
Empty file modified app/src/main/ic_launcher-playstore.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions app/src/main/java/org/coderic/protective/mobile/MainActivity.kt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ 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.pet.PetScreen
import org.coderic.protective.mobile.presentation.UpdatePetScreen
import org.coderic.protective.mobile.presentation.components.MyBottomBar
import org.coderic.protective.mobile.presentation.pet.PetViewModel
import org.coderic.protective.mobile.presentation.components.BottomBar
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 {
Expand All @@ -33,7 +35,7 @@ class MainActivity : ComponentActivity() {
.fillMaxSize(),
contentColor = MaterialTheme.colorScheme.background,
bottomBar = {
BottomBar( navController )
MyBottomBar( navController )
}
) { paddingValues ->
NavHost(
Expand All @@ -44,7 +46,11 @@ class MainActivity : ComponentActivity() {
DashBoardScreen(paddingValues = paddingValues, navController )
}
composable( Routes.MyPetScreen.route) {
PetScreen( paddingValues = paddingValues, PetViewModel() )
// PetScreen( paddingValues = paddingValues, petViewModel )
UpdatePetScreen( petViewModel, paddingValues )
}
composable(Routes.MyUpdatePetScreen.route ) {
UpdatePetScreen( petViewModel, paddingValues )
}
composable( Routes.ExploreScreen.route ) {
LoadingScreen(paddingValues = paddingValues )
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.coderic.protective.mobile.data

import org.coderic.protective.mobile.model.datos.Device
import org.coderic.protective.mobile.model.datos.Pet

interface Almacenamiento {
fun savePet( pet: Pet )
fun saveDevice( device: Device )
fun getPet( id: Long ) : Pet?
fun getDevice() : Device
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.coderic.protective.mobile.data

import android.content.Context
import android.content.SharedPreferences
import org.coderic.protective.mobile.model.datos.Device
import org.coderic.protective.mobile.model.datos.Gender
import org.coderic.protective.mobile.model.datos.Pet
import org.json.JSONObject

const val ARCHIVE_PREFERENCE = "almacenamiento"
class MacronutrientPreferences (context: Context ) : Almacenamiento {

private val preferences: SharedPreferences = context.getSharedPreferences( ARCHIVE_PREFERENCE, Context.MODE_PRIVATE )
override fun savePet( pet: Pet ) {
val editor = preferences.edit()
val petJson = JSONObject()
.put("name", pet.name)
.put("typePet", pet.typePet )
.put("age", pet.age )
.put("weight", pet.weight )
.put("height", pet.height )
.put("color", pet.color )
.put("description", pet.description )
.put("image", pet.image )
.put("id", pet.id )
.put("gender", pet.gender.type )
editor.putString("pet${pet.id}", petJson.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)

return Pet(
name = petJSON.getString("name"),
gender = if (petJSON.getInt("gender") == 0) Gender.MAN else Gender.FEMALE,
typePet = petJSON.getString("typePet"),
age = petJSON.getInt("age"),
weight = petJSON.getDouble("weight"),
height = petJSON.getInt("height"),
color = petJSON.getString("color"),
description = petJSON.getString("description"),
image = petJSON.getString("image"),
id = petJSON.getLong("id")
)
}

override fun getDevice(): Device {
return Device(
preferences.getString("device_name", "")!!,
false,
0
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.coderic.protective.mobile.domain

import android.content.Context
import org.coderic.protective.mobile.data.Almacenamiento
import org.coderic.protective.mobile.data.MacronutrientPreferences
import org.coderic.protective.mobile.model.datos.Gender
import org.coderic.protective.mobile.model.datos.Pet

class CasosUsoMascota( context: Context ) {
val almacenamiento : Almacenamiento = MacronutrientPreferences( context )
fun guardarMascota( pet: Pet ) {
almacenamiento.savePet( pet )
}
fun getMascota( id: Long ) : Pet? {
val pet = almacenamiento.getPet( id )
return pet
/*if( pet == null ) {
return Pet(
name = "Bella",
typePet = "Border Collie",
gender = Gender.MAN,
age = 11,
weight = 7.5,
height = 54,
color = "Black",
description = "My first dog which was gifted by my mother for my 20th birthday."
)
}
else {
return pet
}*/
}

}
17 changes: 9 additions & 8 deletions app/src/main/java/org/coderic/protective/mobile/model/Routes.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.coderic.protective.mobile.model

sealed class Routes( val route : String ) {
// Rutas de Start Activity
object InicioSesion: Routes("inicio/inicio_sesion")
object GetStarted: Routes("inicio/get_started")
// Rutas de MainActivity
object MainScreen: Routes("main/home")
object MyPetScreen: Routes("main/profile")
object ExploreScreen: Routes("main/explore")
object ManageScreen: Routes("main/manage")
// Ruts de Start Activity
data object InicioSesion: Routes("inicio/inicio_sesion")
data object GetStarted: Routes("inicio/get_started")
// Ruts de MainActivity
data object MainScreen: Routes("main/home")
data object MyPetScreen: Routes("main/profile")
data object MyUpdatePetScreen : Routes("main/profile/update")
data object ExploreScreen: Routes("main/explore")
data object ManageScreen: Routes("main/manage")
}
Empty file.
Empty file.
5 changes: 3 additions & 2 deletions app/src/main/java/org/coderic/protective/mobile/model/datos/Pet.kt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ data class Pet(
val gender: Gender = Gender.FEMALE,
val typePet: String = "",
var age : Int = 0,
var weight: Float = 0f,
var weight: Double = 0.0,
var height: Int = 0,
val color: String = "",
var description: String = "",
var image: String = ""
var image: String = "",
val id: Long = System.currentTimeMillis()
)
/** Gender.
* @author José Ricardo
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Loading

0 comments on commit c1bf967

Please sign in to comment.