Skip to content

Commit

Permalink
refactoring/#31-state-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Neomer committed Apr 15, 2019
1 parent 0db0cb6 commit f1ecdb5
Show file tree
Hide file tree
Showing 16 changed files with 443 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import butterknife.OnClick;
import my.neomer.sixtyseconds.gamemodes.IGameMode;
import my.neomer.sixtyseconds.gamemodes.SinglePlayerWithRatesGameMode;
import my.neomer.sixtyseconds.helpers.ApplicationResources;

public class GameModeSelectionActivity extends AppCompatActivity {

Expand All @@ -21,6 +22,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode_selection);

ApplicationResources.getInstance().loadSounds(this, null);

ButterKnife.bind(this);
}

Expand Down
82 changes: 11 additions & 71 deletions app/src/main/java/my/neomer/sixtyseconds/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import my.neomer.sixtyseconds.gamemodes.IGameMode;
import my.neomer.sixtyseconds.helpers.ActivityHelper;
import my.neomer.sixtyseconds.helpers.AppMetricaHelper;
import my.neomer.sixtyseconds.model.Answer;
import my.neomer.sixtyseconds.model.Question;
import my.neomer.sixtyseconds.transport.TransportConfiguration;

public class MainActivity
extends AppCompatActivity
implements ICountdownListener, View.OnClickListener, Observer<Question>, SoundPool.OnLoadCompleteListener {
implements ICountdownListener, Observer<Question>, SoundPool.OnLoadCompleteListener {

private static final int MAX_STREAMS = 2;
private static final String TAG = "MainActivity";
Expand Down Expand Up @@ -151,23 +152,17 @@ protected void onCreate(Bundle savedInstanceState) {

txtCountdown = findViewById(R.id.txtTime);

btnStart = findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);

mainLayout = findViewById(R.id.mainLayout);
voteLayout = findViewById(R.id.layoutVote);

btnLike = findViewById(R.id.btnLike);
btnLike.setOnClickListener(this);

btnDislike = findViewById(R.id.btnDislike);
btnDislike.setOnClickListener(this);

cardGuess = findViewById(R.id.cardGuess);
progressBarGuess = findViewById(R.id.progressBarGuess);
progressBarGuess.setMax(GUESS_MAX_TIME);
btnSendGuess = findViewById(R.id.btnSendGuess);
btnSendGuess.setOnClickListener(this);
txtGuess = findViewById(R.id.txtGuess);

cardCorrect = findViewById(R.id.cardCorrect);
Expand All @@ -194,14 +189,20 @@ public void run() {
loadPreferences();
}

@Override
protected void onStart() {
super.onStart();

gameMode.run();
}

private void loadGameMode() {
gameMode = getIntent().getParcelableExtra(GameModeSelectionActivity.GAMEMODE_TAG);
if (gameMode == null) {
Log.e(TAG, "GameMode not set!");
finish();
}
gameMode.getGameContext().setActivity(this);
gameMode.run();
}

private void showCardCorrect(boolean correct) {
Expand Down Expand Up @@ -239,19 +240,6 @@ public void onAdClosed() {
* Подготовтиь библиотеку звуков
*/
private void loadSounds() {
soundPool = new SoundPool.Builder()
.setMaxStreams(MAX_STREAMS)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
.build();
soundPool.setOnLoadCompleteListener(this);

timeIsUpSoundId = soundPool.load(this, R.raw.time_is_up, 1);
clickSoundId = soundPool.load(this, R.raw.click, 1);
countDownSoundId = soundPool.load(this, R.raw.countdown, 1);
}

/**
Expand Down Expand Up @@ -313,9 +301,6 @@ public void updateTime(AsyncTask<Void, Integer, Void> timer, int value) {
public void countFinish(AsyncTask<Void, Integer, Void> timer) {
if (timer instanceof Countdown) {
displayGuessCard();
txtCountdown.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.title_font_size));
txtCountdown.setText(R.string.finish_message);
} else if (timer instanceof GuessTypingCountdown) {
displayAnswer();
}
Expand All @@ -328,10 +313,6 @@ public void countFinish(AsyncTask<Void, Integer, Void> timer) {
public void countCancel(AsyncTask<Void, Integer, Void> timer) {
if (timer instanceof Countdown) {
displayGuessCard();
soundPool.play(timeIsUpSoundId, 1, 1, 0, 0, 1);
txtCountdown.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.title_font_size));
txtCountdown.setText(R.string.cancel_message);
} else if (timer instanceof GuessTypingCountdown) {
displayAnswer();
}
Expand Down Expand Up @@ -396,48 +377,7 @@ private void displayAds() {

//endregion

@Override
public void onClick(View v) {
soundPool.play(clickSoundId, 1, 1,0,0,1);
if (v == btnStart) {

switch (state) {
case Updating:
break;

case Idle:
if (mViewModel != null && mViewModel.hasValue()) {
startTimer();
}
break;

case Counting:
mainCountdown.cancel(true);
break;

case TypingGuess:
break;

case Result:
displayAds();
break;

case DisplayAds:
updateQuestion();
break;
}
} else if (v == btnLike) {
mViewModel.like();
hideVoting();
} else if (v == btnDislike) {
mViewModel.dislike();
hideVoting();
} else if (v == btnSendGuess) {
guessCountdown.cancel(true);
}
}

@Override
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_privacypolicy:
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/my/neomer/sixtyseconds/MyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.yandex.metrica.YandexMetrica;
import com.yandex.metrica.YandexMetricaConfig;

import my.neomer.sixtyseconds.helpers.AppMetricaHelper;

public class MyApp extends Application {

public static final String Version = BuildConfig.VERSION_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import java.security.PrivateKey;
import java.util.Observable;

import my.neomer.sixtyseconds.gamemodes.BaseGameContext;
import my.neomer.sixtyseconds.model.Answer;
import my.neomer.sixtyseconds.model.Question;

public class QuestionFragment extends Fragment {

private QuestionFragmentViewModel mViewModel;
private BaseGameContext mViewModel;
private TextView txtQuestion;
private ProgressBar progressBar;

Expand All @@ -50,7 +51,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

progressBar = getView().findViewById(R.id.updateProgressBar);

mViewModel = ViewModelProviders.of(getActivity()).get(QuestionFragmentViewModel.class);
mViewModel = ViewModelProviders.of(getActivity()).get(BaseGameContext.class);
mViewModel.getQuestion().observe(this, new Observer<Question>() {
@Override
public void onChanged(@Nullable Question question) {
Expand Down Expand Up @@ -125,7 +126,7 @@ public void clear() {
}

public void displayAnswer(String guess) {
mViewModel.checkAnswer(guess);

}
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package my.neomer.sixtyseconds.core;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;

public class InfinitePipeline<T extends Parcelable> extends ForwardPipeline<T> {

public static final Parcelable.Creator<InfinitePipeline> CREATOR = new Parcelable.Creator<InfinitePipeline>() {
public InfinitePipeline createFromParcel(Parcel in) {
return new InfinitePipeline(in);
}

public InfinitePipeline[] newArray(int size) {
return new InfinitePipeline[size];
}
};

private InfinitePipeline(Parcel in) {
super(in);
}

public InfinitePipeline() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package my.neomer.sixtyseconds.gamemodes;

import android.annotation.SuppressLint;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.os.Parcel;
Expand Down Expand Up @@ -49,7 +50,9 @@ public BaseGameContext() {

private final MutableLiveData<Question> question = new MutableLiveData<>();
private final MutableLiveData<Answer> answer = new MutableLiveData<>();
@SuppressLint("StaticFieldLeak")
private FragmentActivity activity;
private String guess;

public MutableLiveData<Question> getQuestion() {
return question;
Expand All @@ -63,4 +66,10 @@ public FragmentActivity getActivity() {
public void setActivity(FragmentActivity activity) {
this.activity = activity;
}
public String getGuess() {
return guess;
}
public void setGuess(String guess) {
this.guess = guess;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Parcelable;

import my.neomer.sixtyseconds.core.InfinitePipeline;
import my.neomer.sixtyseconds.states.AnswerReceivingState;
import my.neomer.sixtyseconds.states.GuessInputState;
import my.neomer.sixtyseconds.states.IState;
import my.neomer.sixtyseconds.states.QuestionReceivingState;
import my.neomer.sixtyseconds.states.QuestionState;
Expand Down Expand Up @@ -40,7 +42,9 @@ public SinglePlayerWithRatesGameMode() {
super(new SinglePlayerWithRatesContext(),
new InfinitePipeline<IState>(
new QuestionReceivingState(),
new QuestionState()
new QuestionState(),
new GuessInputState(),
new AnswerReceivingState()

));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package my.neomer.sixtyseconds;
package my.neomer.sixtyseconds.helpers;

import android.app.Activity;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package my.neomer.sixtyseconds;
package my.neomer.sixtyseconds.helpers;

public final class AppMetricaHelper {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package my.neomer.sixtyseconds.helpers;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import my.neomer.sixtyseconds.R;
import my.neomer.sixtyseconds.transport.FakeQuestionProvider;
import my.neomer.sixtyseconds.transport.IQuestionProvider;

public class ApplicationResources {
//region Singleton

private static final ApplicationResources ourInstance = new ApplicationResources();
private static final int MAX_STREAMS = 5;

public static ApplicationResources getInstance() {
return ourInstance;
}

private ApplicationResources() {
}

//endregion

private IQuestionProvider questionProvider = new FakeQuestionProvider();

private SoundPool soundPool;
private int timeIsUpSoundId;
private int clickSoundId;
private int countDownSoundId;

public void loadSounds(@NonNull Context context, @Nullable SoundPool.OnLoadCompleteListener completeListener) {
soundPool = new SoundPool.Builder()
.setMaxStreams(MAX_STREAMS)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
.build();

if (completeListener != null) {
soundPool.setOnLoadCompleteListener(completeListener);
}

timeIsUpSoundId = soundPool.load(context, R.raw.time_is_up, 1);
countDownSoundId = soundPool.load(context, R.raw.countdown, 1);
clickSoundId = soundPool.load(context, R.raw.click, 1);
}

public void playClickSound() {
soundPool.play(clickSoundId, 1, 1, 0, 0, 1);
}

public void playCountDownSound() {
soundPool.play(countDownSoundId, 1, 1, 0,0,1);
}

public void playTimeIsUpSound() {
soundPool.play(timeIsUpSoundId, 1, 1, 0,0,1);
}

public IQuestionProvider getQuestionProvider() {
return questionProvider;
}
}
Loading

0 comments on commit f1ecdb5

Please sign in to comment.