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

Commit

Permalink
Refactor GuessActivity and using Starter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ezisazis committed Sep 2, 2017
1 parent 4d368a2 commit c3be54e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
Expand Up @@ -28,10 +28,8 @@ public void guessAnAnimal() {
// reverse-linked list traversal:
firstAnimal.noAnimal.prevAnimal = firstAnimal;
Game.firstAnimal = firstAnimal;
Intent intent = new Intent(this, GuessActivity.class);
intent.putExtra(Animal.ANIMAL, firstAnimal);
intent.putExtra(GuessActivity.FINAL_GUESS, false);
startActivity(intent);

GuessActivity.start(this, firstAnimal, false, false);
}

}
@@ -1,11 +1,13 @@
package org.sinisterstuf.guesstheanimal.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import org.sinisterstuf.guesstheanimal.Animal;
import org.sinisterstuf.guesstheanimal.Game;
import org.sinisterstuf.guesstheanimal.R;

import butterknife.BindView;
Expand All @@ -14,24 +16,38 @@

public class GuessActivity extends Activity {

public static final String FINAL_GUESS = "org.sinisterstuf.guesstheanimal.finalguess";

@BindView(R.id.guessText)
TextView guessText;

private Animal animal;
boolean finalGuess;
boolean prevReq;
public static final String FINAL_GUESS = "org.sinisterstuf.guesstheanimal.finalguess";
private boolean finalGuess;
private boolean prevReq;


/**
* Displays the GuessActivity screen
*/
public static void start(Context context, Animal animal, boolean isFinalGuess, boolean prevReq) {
// PREVREQ should be optional because it is used only in 1 place.
// Koltin -> https://kotlinlang.org/docs/reference/functions.html#default-arguments
Intent intent = new Intent(context, GuessActivity.class);
intent.putExtra(Animal.ANIMAL, animal);
intent.putExtra(FINAL_GUESS, isFinalGuess);
intent.putExtra(Animal.NEXT_REQ, prevReq);
context.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess);
ButterKnife.bind(this);

Intent intent = getIntent();
finalGuess = intent.getBooleanExtra(FINAL_GUESS, false);
prevReq = intent.getBooleanExtra(Animal.NEXT_REQ, false);
animal = (Animal) intent.getSerializableExtra(Animal.ANIMAL);
finalGuess = getIntent().getBooleanExtra(FINAL_GUESS, false);
prevReq = getIntent().getBooleanExtra(Animal.NEXT_REQ, false);
animal = (Animal) getIntent().getSerializableExtra(Animal.ANIMAL);

if (finalGuess) {
// write animal name
Expand All @@ -43,9 +59,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

/**
* Called when the user chooses Yes
*/
@OnClick(R.id.yesButton)
public void parseYes() {
if (finalGuess) {
Expand All @@ -56,50 +69,34 @@ public void parseYes() {
}
}

/**
* Called when the user chooses No
*/
@OnClick(R.id.noButton)
public void parseNo() {
if (finalGuess) {
learnNewAnimal(prevReq);
learnNewAnimal();
} else {
parseChoice(false, animal.noAnimal);
}
}

/**
* Compares the user's <code>choice</code> to the desired answer for this
* <code>Animal</code> from <code>Animal.answerWhenMe</code>
*
* @param choice the user's choice
*/
private void parseChoice(boolean choice, Animal next) {
if (choice == animal.answerWhenMe) {
Intent intent = new Intent(this, GuessActivity.class);
intent.putExtra(Animal.ANIMAL, animal);
intent.putExtra(FINAL_GUESS, true);
startActivity(intent);
GuessActivity.start(this, animal, true, false);
} else {
if (next == null) {
learnNewAnimal(choice);
learnNewAnimal();
} else {
Intent intent = new Intent(this, GuessActivity.class);
intent.putExtra(Animal.ANIMAL, next);
intent.putExtra(Animal.NEXT_REQ, choice);
intent.putExtra(FINAL_GUESS, false);
startActivity(intent);
GuessActivity.start(this, next, false, choice);
}
}
}

/**
* Start a new activity to learn an animal
*/
private void learnNewAnimal(Boolean nextReq) {
private void learnNewAnimal() {
Intent intent = new Intent(this, LearnNameActivity.class);
intent.putExtra(Animal.ANIMAL, animal);
intent.putExtra(Animal.NEXT_REQ, nextReq);
intent.putExtra(Animal.NEXT_REQ, prevReq);
startActivity(intent);
}

Expand Down
Expand Up @@ -22,10 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {

@OnClick(R.id.okButton)
public void startNewGame() {
Intent intent = new Intent(this, GuessActivity.class);
intent.putExtra(Animal.ANIMAL, Game.firstAnimal);
intent.putExtra(GuessActivity.FINAL_GUESS, false);
startActivity(intent);
GuessActivity.start(this, Game.firstAnimal, false, false);
}

}
Expand Up @@ -12,7 +12,7 @@
import butterknife.OnClick;

public class WinActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -22,10 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {

@OnClick(R.id.okButton)
public void startNewGame() {
Intent intent = new Intent(this, GuessActivity.class);
intent.putExtra(Animal.ANIMAL, Game.firstAnimal);
intent.putExtra(GuessActivity.FINAL_GUESS, false);
startActivity(intent);
GuessActivity.start(this, Game.firstAnimal, false, false);
}

}

0 comments on commit c3be54e

Please sign in to comment.