From dcbbc969abc16eadcd383d50bca2836673b70a94 Mon Sep 17 00:00:00 2001 From: jaco Date: Wed, 1 Feb 2023 18:30:14 -0300 Subject: [PATCH] feat(sharedPref): sharedPreferences working, with level stars. Branch done --- app/src/main/assets/metro_santiago.xml | 6 +++++ .../metrometro/activities/MainActivity.java | 10 +++++++ .../activities/MenuMetroActivity.java | 17 +++++++++--- .../MenuMetroRecyclerViewAdapter.java | 10 +++---- .../activities/PlayGameActivity.java | 14 ++++++++-- app/src/main/res/layout/activity_main.xml | 26 +++++++++++++++++-- 6 files changed, 70 insertions(+), 13 deletions(-) diff --git a/app/src/main/assets/metro_santiago.xml b/app/src/main/assets/metro_santiago.xml index e3f0eab..c39715e 100644 --- a/app/src/main/assets/metro_santiago.xml +++ b/app/src/main/assets/metro_santiago.xml @@ -133,5 +133,11 @@ Lo valledor Cerrillos + + Los Leones + Cerrillos + Cerrillos + Cerrillos + \ No newline at end of file diff --git a/app/src/main/java/com/jorgecruces/metrometro/activities/MainActivity.java b/app/src/main/java/com/jorgecruces/metrometro/activities/MainActivity.java index deb8249..b1c2186 100644 --- a/app/src/main/java/com/jorgecruces/metrometro/activities/MainActivity.java +++ b/app/src/main/java/com/jorgecruces/metrometro/activities/MainActivity.java @@ -1,8 +1,11 @@ package com.jorgecruces.metrometro.activities; +import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; @@ -24,4 +27,11 @@ public void goNextActivity(View view) { Intent intent = new Intent(this, MenuMetroActivity.class); startActivity(intent); } + + public void resetButton(View view) { + SharedPreferences sharedPref = this.getSharedPreferences( + String.valueOf(R.string.app_name), Context.MODE_PRIVATE); + sharedPref.edit().clear().commit(); + Toast.makeText(this, "SharedPref Reset", Toast.LENGTH_SHORT).show(); + } } diff --git a/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroActivity.java b/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroActivity.java index a25f170..a1c5712 100644 --- a/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroActivity.java +++ b/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroActivity.java @@ -9,6 +9,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.TextView; @@ -34,12 +35,16 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_menu_metro); scoreStarView = findViewById(R.id.scoreStarView); - this.initializeMetroMenuData(); + } + @Override + protected void onResume() { + this.initializeMetroMenuData(); RecyclerView recyclerView = findViewById(R.id.menuMetroRecyclerView); MenuMetroRecyclerViewAdapter adapter = new MenuMetroRecyclerViewAdapter(this,this.metroMenu); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); + super.onResume(); } public void initializeMetroMenuData() { @@ -49,12 +54,15 @@ public void initializeMetroMenuData() { ArrayList lines = metro.getLines(); for (Line line : lines ) { MetroMenu item = new MetroMenu(line.getName(),line.getColor()); - boolean levelStar = this.getLevelStarSharedPreferences(line.getName()); + boolean levelStar = this.getLevelStarSharedPreferences("Prueba"); item.setLevelStar(levelStar); metroMenu.add(item); } + // Score Star - SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE); + SharedPreferences sharedPref = this.getSharedPreferences( + String.valueOf(R.string.app_name),Context.MODE_PRIVATE); + int scoreStar = sharedPref.getInt("score", 0); String scoreString = Integer.toString(scoreStar); @@ -62,7 +70,8 @@ public void initializeMetroMenuData() { } private boolean getLevelStarSharedPreferences(String lineName) { - SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE); + SharedPreferences sharedPref = this.getSharedPreferences( + String.valueOf(R.string.app_name),Context.MODE_PRIVATE); return sharedPref.getBoolean(lineName,false); } diff --git a/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroRecyclerViewAdapter.java b/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroRecyclerViewAdapter.java index 452cf3a..e53db3d 100644 --- a/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroRecyclerViewAdapter.java +++ b/app/src/main/java/com/jorgecruces/metrometro/activities/MenuMetroRecyclerViewAdapter.java @@ -3,6 +3,7 @@ import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Color; import android.util.Log; import android.view.LayoutInflater; @@ -41,6 +42,7 @@ public MenuMetroRecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull Vie @Override public void onBindViewHolder(@NonNull MenuMetroRecyclerViewAdapter.MyViewHolder holder, int position) { + Log.d("TEST", "test"); MetroMenu currentMetroMenu = metroMenuList.get(position); String lineName = currentMetroMenu.getMetroName(); @@ -55,13 +57,11 @@ public void onBindViewHolder(@NonNull MenuMetroRecyclerViewAdapter.MyViewHolder }); // Star Level - Log.d("DEBUG", lineName); -// SharedPreferences sharedPref = context.getApplicationContext().etActgetPreferences(Context.MODE_PRIVATE); -// boolean starLevel = sharedPref.getBoolean(lineaName,false); - if (currentMetroMenu.getLevelStar()) { + SharedPreferences sharedPref = this.context.getSharedPreferences( + String.valueOf(R.string.app_name),Context.MODE_PRIVATE); + if (sharedPref.getBoolean(lineName, false)) { holder.starView.setImageResource(R.drawable.ic_mediamodifier_design_2_); } - } @Override diff --git a/app/src/main/java/com/jorgecruces/metrometro/activities/PlayGameActivity.java b/app/src/main/java/com/jorgecruces/metrometro/activities/PlayGameActivity.java index d40ec65..33f6456 100644 --- a/app/src/main/java/com/jorgecruces/metrometro/activities/PlayGameActivity.java +++ b/app/src/main/java/com/jorgecruces/metrometro/activities/PlayGameActivity.java @@ -47,7 +47,6 @@ public class PlayGameActivity extends AppCompatActivity { // Non-Static Level Data private int position; private String currentStationName; - private String lastStationName; private Station correctAlternative; private ArrayList alternatives; private ArrayList stationViews; @@ -280,9 +279,20 @@ public void onWinLevel() { } private void updateProgressInfo() { - SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE); + + SharedPreferences sharedPref = this.getSharedPreferences( + String.valueOf(R.string.app_name),Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPref.edit(); + int lastScore = sharedPref.getInt("score", 0); + + // Score + if (lastScore < 6) { + editor.putInt("score",lastScore + 1); + } + // Star editor.putBoolean(this.lineName,true); + editor.commit(); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5cd2c3d..a24147e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,6 +7,7 @@ tools:context=".activities.MainActivity"> + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/textView" /> + +