Skip to content

Commit

Permalink
feat(sharedPref): sharedPreferences working, with level stars. Branch…
Browse files Browse the repository at this point in the history
… done
  • Loading branch information
jcrucesdeveloper committed Feb 1, 2023
1 parent c4a75a9 commit dcbbc96
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
6 changes: 6 additions & 0 deletions app/src/main/assets/metro_santiago.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,11 @@
<station>Lo valledor</station>
<station>Cerrillos</station>
</line>
<line name="TEST" color="#A53CEB">
<station>Los Leones</station>
<station>Cerrillos</station>
<station>Cerrillos</station>
<station>Cerrillos</station>
</line>

</metro>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand All @@ -49,20 +54,24 @@ public void initializeMetroMenuData() {
ArrayList<Line> 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);
this.scoreStarView.setText(scoreString);
}

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Station> alternatives;
private ArrayList<StationView> stationViews;
Expand Down Expand Up @@ -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();
}

Expand Down
26 changes: 24 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
tools:context=".activities.MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
Expand All @@ -19,9 +20,30 @@
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="272dp"
android:onClick="goNextActivity"
android:text="Button"
tools:layout_editor_absoluteX="168dp"
tools:layout_editor_absoluteY="321dp" />
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="16dp"
android:onClick="resetButton"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit dcbbc96

Please sign in to comment.