Skip to content

Commit

Permalink
refactor(PlayGameActivity): PlayGameActivity refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrucesdeveloper committed Aug 13, 2022
1 parent 95dbb3d commit 490752c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

import com.jorgecruces.metrometro.R;
import com.jorgecruces.metrometro.logic.MetroReaderXML;
import com.jorgecruces.metrometro.logic.PickerStationsAlternative;
import com.jorgecruces.metrometro.model.Line;
import com.jorgecruces.metrometro.model.Metro;
import com.jorgecruces.metrometro.model.Station;

import java.util.ArrayList;
import java.util.Collections;

public class PlayGameActivity extends AppCompatActivity {

Expand All @@ -27,7 +29,7 @@ public class PlayGameActivity extends AppCompatActivity {
private int position;
private String currentStationName;
private String lastStationName;
private Station correctStation;
private Station correctAlternative;
private ArrayList<Station> alternatives;

@Override
Expand All @@ -37,6 +39,8 @@ protected void onCreate(Bundle savedInstanceState) {
this.setLineName();
this.initializeLevelData();
this.initializeLevelViews();

this.setCurrentStationQuestion(this.position);
}

/**
Expand Down Expand Up @@ -89,42 +93,72 @@ private void setStationList() {
throw new Error("Algo fallo");
}

private void setCurrentStation(int position) {
TextView currentStation = findViewById(R.id.textViewCurrentStation);
currentStation.setText(this.stations.get(position).getName());
}
/**
* This method set the current Station Question:
* - Set current Station Data and View
* - Set current Alternatives Data and Views
* @param position position to set the data
*/
private void setCurrentStationQuestion(int position) {
// Current station
this.setCurrentStationData(position);
this.setCurrentStationView();

private void setStationQuestion(int position) {
this.setCurrentStation(position);
this.setCorrectStation(position);
this.setPositionNumber(position);
// Alternatives
this.setCurrentAlternativesData(position);
this.setCurrentAlternativesViews();
}

private void setCorrectStation(int position) {
this.correctStation = this.stations.get(position + 1);
private void setCurrentAlternativesData(int position) {
// Set correct alternativeData
this.correctAlternative = this.stations.get(position + 1);
// Set alternatives data
PickerStationsAlternative pickerStationsAlternative = new PickerStationsAlternative();

ArrayList<Station> alternatives = pickerStationsAlternative.getAlternatives(this.stations, position);
alternatives.add(this.correctAlternative);

this.alternatives = alternatives;
}

private void setPositionNumber(int position) {
TextView currentPositionTextView = findViewById(R.id.textViewCurrentPosition);
String currentPositionStr = String.valueOf(position);
currentPositionTextView.setText(currentPositionStr);
private void setCurrentAlternativesViews() {

ArrayList<TextView> alternativesTextView = new ArrayList<>();

TextView textViewAlternative1 = findViewById(R.id.textViewAlternative1);
TextView textViewAlternative2 = findViewById(R.id.textViewAlternative2);
TextView textViewAlternative3 = findViewById(R.id.textViewAlternative3);
TextView textViewAlternative4 = findViewById(R.id.textViewAlternative4);

alternativesTextView.add(textViewAlternative1);
alternativesTextView.add(textViewAlternative2);
alternativesTextView.add(textViewAlternative3);
alternativesTextView.add(textViewAlternative4);
// Shuffle!!!
Collections.shuffle(alternativesTextView);

for (int i = 0; i < alternativesTextView.size(); i++) {
TextView currentTextView = alternativesTextView.get(i);
Station currentStation = this.alternatives.get(i);
currentTextView.setText(currentStation.getName());
}
}

private void setCurrentStationQuestion(int position) {
this.setCurrentStationData(position);
this.setCurrentStationViews();

private void setCurrentStationView() {
TextView currentStationView = findViewById(R.id.textViewCurrentStation);
currentStationView.setText(this.currentStationName);
}

private void setCurrentStationData(int position) {
Station currentStation = this.stations.get(position);
this.currentStationName = currentStation.getName();

}

private void checkAlternative(String alternativeString) {

// Correct alternative
if (alternativeString.equals(this.correctStation.getName())) {
if (alternativeString.equals(this.correctAlternative.getName())) {
this.onCorrectAlternative();
} else {
// Incorrect alternative
Expand All @@ -135,27 +169,10 @@ private void checkAlternative(String alternativeString) {

private void onCorrectAlternative() {
this.position = this.position + 1;
this.setStationQuestion(this.position);
// this.setStationQuestion(this.position);
}


public ArrayList<TextView> getAlternativesTextView() {

ArrayList<TextView> alternativesTextView = new ArrayList<>();

TextView textViewAlternative1 = findViewById(R.id.textViewAlternative1);
TextView textViewAlternative2 = findViewById(R.id.textViewAlternative2);
TextView textViewAlternative3 = findViewById(R.id.textViewAlternative3);
TextView textViewAlternative4 = findViewById(R.id.textViewAlternative4);

alternativesTextView.add(textViewAlternative1);
alternativesTextView.add(textViewAlternative2);
alternativesTextView.add(textViewAlternative3);
alternativesTextView.add(textViewAlternative4);

return alternativesTextView;
}

public void goBackToMenu(View view) {
Intent intent = new Intent(this, MenuMetroActivity.class);
startActivity(intent);
Expand Down

0 comments on commit 490752c

Please sign in to comment.