Skip to content

Commit

Permalink
Merge pull request #53 from steni510/feature/GAMES_012
Browse files Browse the repository at this point in the history
Feature/games 012
  • Loading branch information
pkohout authored Jun 3, 2020
2 parents 4c3d2ef + 5c95395 commit 720a3d9
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

Expand Down Expand Up @@ -70,6 +73,38 @@ public void clickOnBlackButton() {
Assert.assertEquals(oldScore + 1, st.getScore(Game.TILES));
}

@Test
public void testTimerDisabled() {
onView(withId(R.id.whiteTilesButton)).perform(click());
//swipeLeft toggles the switch, only do it if it is enabled!
if (((Switch) (activityRule.getActivity()
.findViewById(R.id.whiteTilesTimerSwitch))).isChecked()) {
onView(withId(R.id.whiteTilesTimerSwitch)).perform(ViewActions.swipeLeft());
}
onView(withId(R.id.tiles_menu_button)).perform(click());
TextView timeText = activityRule.getActivity().findViewById(R.id.whiteTilesTimeText);
TextView remainingTimeText = activityRule.getActivity()
.findViewById(R.id.whiteTilesRemainingTime);
Assert.assertEquals("", timeText.getText().toString());
Assert.assertEquals("", remainingTimeText.getText().toString());
}

@Test
public void testTimerEnabled() {
onView(withId(R.id.whiteTilesButton)).perform(click());
if (!((Switch) (activityRule.getActivity()
.findViewById(R.id.whiteTilesTimerSwitch))).isChecked()) {
onView(withId(R.id.whiteTilesTimerSwitch)).perform(ViewActions.swipeRight());
}
onView(withId(R.id.tiles_menu_button)).perform(click());
TextView timeText = activityRule.getActivity().findViewById(R.id.whiteTilesTimeText);
TextView remainingTimeText = activityRule.getActivity()
.findViewById(R.id.whiteTilesRemainingTime);
Assert.assertTrue(Integer.parseInt(remainingTimeText.getText().toString()) >= 1);
Assert.assertEquals("Remaining Time", timeText.getText().toString());

}

public View getButtonWithColor(Integer color) {
View button = null;
for (Integer integer : WhiteTilesGameLogic.tileButtonIds) {
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/java/com/swt20/swt_morning2/WhiteTilesFragment.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.swt20.swt_morning2;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand All @@ -15,6 +20,9 @@
public class WhiteTilesFragment extends Fragment {

private WhiteTilesGameLogic logic;
private boolean timerEnabled;
private int timerSetting;
CountDownTimer timer;

@Override
public View onCreateView(
Expand All @@ -29,6 +37,8 @@ public void onViewCreated(@NonNull final View view, Bundle savedInstanceState) {
logic = new WhiteTilesGameLogic();
super.onViewCreated(view, savedInstanceState);

timerSetup(view);

logic.scrambleButtons();
colorButtons(view);
logic.getTilesButtons().keySet().forEach((buttonId) -> {
Expand Down Expand Up @@ -60,6 +70,7 @@ private void blackButtonClicked(View view) {
tracker.addScore(Game.TILES, 1);
logic.scrambleButtons();
colorButtons(view);
resetTimer();
}

private void colorButtons(View view) {
Expand All @@ -68,5 +79,48 @@ private void colorButtons(View view) {
});
}

private TextView getRemainingTimeTextView(View view) {
return (TextView) view.findViewById(R.id.whiteTilesRemainingTime);

}

private void timerSetup(View view) {
WhiteTilesSettings settings = new WhiteTilesSettings(getContext().getApplicationContext());
timerEnabled = settings.getTimerEnabled();
timerSetting = settings.getTimerSetting();

timer = new CountDownTimer(timerSetting * 1000, 1000) {

public void onTick(long millisUntilFinished) {
String remainingTime = String.valueOf(Math.round(millisUntilFinished / 1000.0));
getRemainingTimeTextView(view).setText(remainingTime);

if (millisUntilFinished < 1200) {
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
}
}

public void onFinish() {
whiteButtonClicked();
}

};

if (timerEnabled) {
timer.start();
} else {
TextView remainingTimeTextView = view.findViewById(R.id.whiteTilesTimeText);
remainingTimeTextView.setText("");
}
}

private void resetTimer() {
if (timerEnabled) {
timer.cancel();
timer.start();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
Expand All @@ -24,14 +26,42 @@ public View onCreateView(
public void onViewCreated(@NonNull final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

WhiteTilesSettings settings =
new WhiteTilesSettings(getContext().getApplicationContext());

boolean timerEnabled = settings.getTimerEnabled();
Integer timeSetting = settings.getTimerSetting();

getTimerSwitch(view).setChecked(timerEnabled);
getTimerSetting(view).setText(String.format(timeSetting.toString()));


view.findViewById(R.id.tiles_menu_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
public void onClick(View buttonView) {
boolean timerEnabled = getTimerSwitch(view).isChecked();
int timeSetting = Integer.parseInt(getTimerSetting(view).getText().toString());

WhiteTilesSettings settings =
new WhiteTilesSettings(getContext().getApplicationContext());

settings.setTimerEnabled(timerEnabled);
settings.setTimerSetting(timeSetting);

NavHostFragment.findNavController(WhiteTilesMenuFragment.this)
.navigate(R.id.action_Menu_to_Game);
}
});
}

private Switch getTimerSwitch(View view) {
return (Switch) view.findViewById(R.id.whiteTilesTimerSwitch);
}

private EditText getTimerSetting(View view) {
return (EditText) view.findViewById(R.id.whiteTilesTimeSetting);

}


}
38 changes: 38 additions & 0 deletions app/src/main/java/com/swt20/swt_morning2/WhiteTilesSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.swt20.swt_morning2;

import android.content.Context;
import android.content.SharedPreferences;

public class WhiteTilesSettings {
private static final String TIMER_ENABLED = "WHITE_TILES_TIMER_ENABLED";
private static final String TIMER_SETTING = "WHITE_TILES_TIMER_SETTING";
private SharedPreferences pref;

public WhiteTilesSettings(Context context) {
setSharedPreferences(context.getSharedPreferences("WHITE_TILES_SETTINGS", 0));
}

public void setSharedPreferences(SharedPreferences pref) {
this.pref = pref;
}

public boolean getTimerEnabled() {
return pref.getBoolean(TIMER_ENABLED, false);
}

public int getTimerSetting() {
return pref.getInt(TIMER_SETTING, 0);
}

public void setTimerEnabled(boolean timerEnabled) {
SharedPreferences.Editor editor = this.pref.edit();
editor.putBoolean(TIMER_ENABLED, timerEnabled);
editor.apply();
}

public void setTimerSetting(int timerSetting) {
SharedPreferences.Editor editor = this.pref.edit();
editor.putInt(TIMER_SETTING, timerSetting);
editor.apply();
}
}
9 changes: 8 additions & 1 deletion app/src/main/res/layout/tiles_game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
android:orientation="horizontal">

<TextView
android:id="@+id/textView2"
android:id="@+id/whiteTilesTimeText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/white_tiles_remaining_time" />

<TextView
android:id="@+id/whiteTilesRemainingTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
Expand Down
67 changes: 54 additions & 13 deletions app/src/main/res/layout/tiles_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,59 @@
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=".HangmanMenuFragment">

<Button
android:id="@+id/tiles_menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="492dp"
android:text="@string/start_game"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:context=".WhiteTilesMenuFragment">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
android:id="@+id/tiles_menu_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start_game"
android:textSize="30sp" />

<Switch
android:id="@+id/whiteTilesTimerSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/white_tiles_timer"
android:textSize="18sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/white_tiles_time" />

<EditText
android:id="@+id/whiteTilesTimeSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:importantForAutofill="no"
android:inputType="number"
android:hint="@string/white_tiles_time_hint"/>
</LinearLayout>

</LinearLayout>
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values-de-rAT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@
<string name="tiles_menu_fragment_label">Nicht die weißen Felder berühren</string>
<string name="hangman_lose">Gratuliere, du hast das Wort nicht erraten!</string>
<string name="imageViewDescription">Dies ist das Feedback Bild für den user</string>
<string name="white_tiles_timer">Timer</string>
<string name="white_tiles_time">Zeit in Sekunden</string>
<string name="white_tiles_time_hint">Zeit eingeben</string>
<string name="white_tiles_remaining_time">Verbleibende Zeit</string>

</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@
<string name="tiles_menu_fragment_label">Dont touch the white tiles</string>
<string name="hangman_lose">Congratulations. You lost!</string>
<string name="imageViewDescription">This is the ImageView for the user feedback</string>
<string name="white_tiles_timer">Timer</string>
<string name="white_tiles_time">Time in seconds</string>
<string name="white_tiles_time_hint">Set Timer</string>
<string name="white_tiles_remaining_time">Remaining Time</string>

</resources>

0 comments on commit 720a3d9

Please sign in to comment.