This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature logging the high score.
The highest score is stored persistently. A new menu entry allows to display the current high score. The design allows to extend the score information with low effort. Fixes #4.
- Loading branch information
Showing
10 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/src/main/java/info/hebbeker/david/memorex/DisplayHighScore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package info.hebbeker.david.memorex; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.text.Html; | ||
import android.widget.TextView; | ||
|
||
import java.io.Serializable; | ||
|
||
public class DisplayHighScore extends AppCompatActivity | ||
{ | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) | ||
{ | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_display_high_score); | ||
final Serializable currentHighScore = getIntent().getSerializableExtra(MainActivity.HIGH_SCORE_DATA); | ||
final String aboutText = getResources().getString(R.string.high_score_display, currentHighScore.toString()); | ||
final TextView textView = findViewById(R.id.textViewHighScore); | ||
textView.setText(Html.fromHtml(aboutText)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/info/hebbeker/david/memorex/HighScoreContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package info.hebbeker.david.memorex; | ||
|
||
import android.content.SharedPreferences; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
import java.io.IOException; | ||
|
||
class HighScoreContainer | ||
{ | ||
final private SharedPreferences sharedPref; | ||
final private String highScorePreferenceKey = "HighScoreStorage"; | ||
final private Gson gson = new Gson(); | ||
private Score currentHighScore; | ||
|
||
HighScoreContainer(final SharedPreferences sharedPref) | ||
{ | ||
this.sharedPref = sharedPref; | ||
loadHighScore(); | ||
} | ||
|
||
private void loadHighScore() | ||
{ | ||
try | ||
{ | ||
String highScoreSerializedObject = this.sharedPref.getString(highScorePreferenceKey, ""); | ||
currentHighScore = gson.fromJson(highScoreSerializedObject, Score.class); | ||
if (currentHighScore == null) // this may be overcautious | ||
{ | ||
throw new IOException("Getting stored high score failed!"); | ||
} | ||
} | ||
catch (JsonSyntaxException|IOException e) | ||
{ | ||
e.printStackTrace(); | ||
currentHighScore = new Score(0); | ||
saveHighScore(); | ||
} | ||
} | ||
|
||
boolean setNewHighScore(final Score newScore) | ||
{ | ||
final boolean isNewHighScore = newScore.isGreaterThan(currentHighScore); | ||
if (isNewHighScore) | ||
{ | ||
currentHighScore = newScore; | ||
saveHighScore(); | ||
} | ||
return isNewHighScore; | ||
} | ||
|
||
private void saveHighScore() | ||
{ | ||
SharedPreferences.Editor editor = sharedPref.edit(); | ||
String highScoreSerializedObject = gson.toJson(currentHighScore); | ||
editor.putString(highScorePreferenceKey, highScoreSerializedObject); | ||
editor.apply(); | ||
} | ||
|
||
public Score getCurrentHighScore() | ||
{ | ||
return currentHighScore; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package info.hebbeker.david.memorex; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Does contain information about a score. | ||
* \todo Add the following attributes: username, speed, date, points | ||
*/ | ||
class Score implements Serializable | ||
{ | ||
/** | ||
* Completed level. | ||
* <p> | ||
* If the player fails at level 2, the completed level is 1. If he fails at level 1, the | ||
* completed level is 0. | ||
*/ | ||
private final int level; | ||
|
||
Score(final int level) | ||
{ | ||
this.level = level; | ||
} | ||
|
||
/** | ||
* @return true if this score is greater than other score | ||
*/ | ||
boolean isGreaterThan(final Score otherScore) | ||
{ | ||
return this.level > otherScore.level; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "Level=" + level; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="info.hebbeker.david.memorex.DisplayHighScore"> | ||
|
||
<TextView | ||
android:id="@+id/textViewHighScore" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginLeft="11dp" | ||
android:layout_marginRight="11dp" | ||
android:layout_marginTop="8dp" | ||
android:autoLink="web|email|map" | ||
android:text="@string/high_score_display" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters