Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
Integration of HighScoreContainer with DisplayHighScore.
Browse files Browse the repository at this point in the history
* DisplayHighScore will print the string of the current high score
* HighScoreContainer returns current high score
* MainActivity passes high score as intent (through serialization)
* Score is serializable (again)
* Score can be written as a String

Fixes #4.
  • Loading branch information
dhebbeker committed Mar 18, 2018
1 parent 15e80c6 commit 517369b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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
{
Expand All @@ -11,5 +15,9 @@ 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ private void saveHighScore()
editor.putString(highScorePreferenceKey, highScoreSerializedObject);
editor.apply();
}

public Score getCurrentHighScore()
{
return currentHighScore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

public class MainActivity extends AppCompatActivity implements GameBoardInterface, View.OnClickListener
{
static final String HIGH_SCORE_DATA = MainActivity.class.getPackage().getName() + "HIGH_SCORE_DATA";
private final SymbolButton[] symbols = new SymbolButton[4];
private final Game game = new Game(this, symbols);
private View startGameButton = null;
Expand Down Expand Up @@ -160,6 +161,7 @@ public void run()
public void showHighScore(@SuppressWarnings("unused") final MenuItem menuItem)
{
Intent intent = new Intent(this, DisplayHighScore.class);
intent.putExtra(HIGH_SCORE_DATA, highScoreContainer.getCurrentHighScore());
startActivity(intent);
}

Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/info/hebbeker/david/memorex/Score.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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
class Score implements Serializable
{
/**
* Completed level.
Expand All @@ -26,4 +28,10 @@ boolean isGreaterThan(final Score otherScore)
{
return this.level > otherScore.level;
}

@Override
public String toString()
{
return "Level=" + level;
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_display_high_score.xml
Original file line number Diff line number Diff line change
@@ -1,8 +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>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="preference_speed_list" translatable="false">speed_list</string>
<string name="preference_switch_sound" translatable="false">switch_preference_sound</string>
<string name="show_high_score">Show high score</string>
<string name="high_score_display">Current high score is: %1$s</string>
<string-array name="pref_speed_titles">
<item>Slow</item>
<item>Medium</item>
Expand Down

0 comments on commit 517369b

Please sign in to comment.