Skip to content

Commit

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.jorgecruces.metrometro.activities;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

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;
Expand All @@ -22,35 +18,25 @@

public class PlayGameActivity extends AppCompatActivity {

// Static Level Data
private String lineName;
private Line line;
private ArrayList<Station> stations;
private int stationsSize;

// Non-Static Level Data
private int position;
private String currentStationName;
private String lastStationName;
private Station correctStation;
private ArrayList<Station> alternatives;
private int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
this.setLineName();
this.initializeLevelData();
this.initializeViewsData();

position = 0;
TextView title = findViewById(R.id.textViewTitle);


if (extra != null) {
lineName = extra.getString("LINEA");
title.setText(lineName);
this.initializeData(lineName);
this.setStationQuestion(position);
}

}

private void initalizeActivity() {
this.initializeLevelViews();
}

/**
Expand All @@ -66,14 +52,31 @@ private void setLineName() {
}

/**
* Initialize the level data
* Initialize the level data:
* - Set stations list
* - Set position to 0
* - Set stations size
*/
private void initializeLevelData() {
this.setLevelStations();
this.setStationList();
this.position = 0;
this.stationsSize = this.stations.size();
}

/**
* Initialize static level views:
* - LevelTitleTextView
* - StationSizeTextView
*/
private void initializeLevelViews() {
TextView levelTitleTextView = findViewById(R.id.textViewTitle);
TextView stationSizeTextView = findViewById(R.id.textViewMaxPosition);

levelTitleTextView.setText(this.lineName);
stationSizeTextView.setText(String.valueOf(this.stationsSize));
}

private void setLevelStations() {
private void setStationList() {
MetroReaderXML metroReaderXML = new MetroReaderXML(this);
Metro metro = metroReaderXML.createMetro();
ArrayList<Line> lines = metro.getLines();
Expand All @@ -86,14 +89,6 @@ private void setLevelStations() {
throw new Error("Algo fallo");
}

private void initializeData(String lineName) {

// Set Level max Position
TextView textViewMaxPosition = findViewById(R.id.textViewMaxPosition);
String maxPositionStr = String.valueOf(this.stations.size());
textViewMaxPosition.setText(maxPositionStr);
}

private void setCurrentStation(int position) {
TextView currentStation = findViewById(R.id.textViewCurrentStation);
currentStation.setText(this.stations.get(position).getName());
Expand All @@ -103,7 +98,6 @@ private void setStationQuestion(int position) {
this.setCurrentStation(position);
this.setCorrectStation(position);
this.setPositionNumber(position);
this.setAlternatives(position);
}

private void setCorrectStation(int position) {
Expand All @@ -116,44 +110,15 @@ private void setPositionNumber(int position) {
currentPositionTextView.setText(currentPositionStr);
}

private void setAlternatives(int position) {

PickerStationsAlternative pickerStationsAlternative = new PickerStationsAlternative();
alternatives = pickerStationsAlternative.getAlternatives(this.line.getStations(), position);

ArrayList<Station> stationAlternatives = this.getStationAlternatives(position);
ArrayList<TextView> alternativesTextView = this.getAlternativesTextView();

for (int i = 0; i < stationAlternatives.size(); i++) {
Station currentAlternative = stationAlternatives.get(i);
alternativesTextView.get(i).setText(currentAlternative.getName());
}
this.setOnClickListenerAlternatives(alternativesTextView);
}

public ArrayList<Station> getStationAlternatives(int position) {

ArrayList<Station> stations = this.line.getStations();
Station correctAlternative = stations.get(position + 1);
Log.d("lol", correctAlternative.getName());


PickerStationsAlternative pickerStationsAlternative = new PickerStationsAlternative();
ArrayList<Station> stationAlternatives = pickerStationsAlternative.getAlternatives(stations, position);

stationAlternatives.add(correctAlternative);
return stationAlternatives;
private void setCurrentStationQuestion(int position) {
this.setCurrentStationData(position);
this.setCurrentStationViews();
}

private void setOnClickListenerAlternatives(ArrayList<TextView> alternatives) {
Station correctAlternative = this.stations.get(this.position + 1);
private void setCurrentStationData(int position) {
Station currentStation = this.stations.get(position);
this.currentStationName = currentStation.getName();

for (TextView alternative: alternatives) {
String lineName = alternative.getText().toString();
alternative.setOnClickListener(view -> {
this.checkAlternative(lineName);
});
}
}

private void checkAlternative(String alternativeString) {
Expand Down

0 comments on commit 95dbb3d

Please sign in to comment.