Skip to content

Commit

Permalink
Sort teams by number; Add Team Number Spinner to Match Scouting.
Browse files Browse the repository at this point in the history
The ability to select a team from TBA's database is now available in match scouting too. When pulled from TBA, team numbers are now sorted from smallest to largest.
  • Loading branch information
bqrichards committed Mar 14, 2019
1 parent 4f735b9 commit 8008812
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/frc63175985/csp/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Debug {
public static final boolean EMULATE_QR_SCAN = false;
public static final boolean LOG_GENERATED_QR_CODE = false;
public static final boolean LOG_DATABASE_SET = false;
public static final boolean CLEAR_FILES = false;

public static void log(@Nullable Object o) {
if (o == null) {
Expand Down
39 changes: 38 additions & 1 deletion app/src/main/java/com/frc63175985/csp/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ private FileManager() {
aggregateFolder = new File(rootFolder, "AGGREGATION");
eventsFolder = new File(rootFolder, "EVENTS");

if (Debug.CLEAR_FILES) {
for (File f : pitFolder.listFiles()) {
f.delete();
}
pitFolder.delete();

for (File f : matchFolder.listFiles()) {
f.delete();
}
matchFolder.delete();

for (File f : aggregateFolder.listFiles()) {
f.delete();
}
aggregateFolder.delete();

for (File f : eventsFolder.listFiles()) {
f.delete();
}
eventsFolder.delete();
}

if (!pitFolder.exists()) {
pitFolder.mkdirs();
}
Expand Down Expand Up @@ -156,6 +178,7 @@ public File saveEvent(String[] teams) {
writer.write(builder.toString());
writer.close();
Debug.log("Wrote event file to path " + newEventFile.getAbsolutePath());
Debug.log(builder.toString());

return newEventFile;
} catch (IOException e) {
Expand All @@ -170,9 +193,15 @@ public File saveEvent(String[] teams) {
if (eventFile.exists()) {
Debug.log("Event " + ScoutAuthState.shared.tournament + " is already saved! Loading...");

BufferedReader reader = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(eventFile));
reader = new BufferedReader(new FileReader(eventFile));
String rawData = reader.readLine();

if (rawData == null || rawData.isEmpty()) {
throw new FileNotFoundException("Stream is empty!");
}

String[] data = rawData.split(",");
reader.close();

Expand All @@ -181,6 +210,14 @@ public File saveEvent(String[] teams) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/com/frc63175985/csp/StartFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

// Match # listener
Match.GUI.bindEditText(view, R.id.start_match_num_editText, Match.MATCH_NUMBER);
Match.GUI.bindEditText(view, R.id.start_team_num_editText, Match.TEAM_NUMBER);

if (TbaCoordinator.shared.teams == null) {
Match.GUI.bindEditText(view, R.id.start_team_num_editText, Match.TEAM_NUMBER);
view.findViewById(R.id.start_team_num_editText).setVisibility(View.VISIBLE);
view.findViewById(R.id.start_team_num_spinner).setVisibility(View.GONE);
} else {
Match.GUI.bindTeamNumberSpinner(getContext(), view.findViewById(R.id.start_team_num_spinner));
view.findViewById(R.id.start_team_num_editText).setVisibility(View.GONE);
view.findViewById(R.id.start_team_num_spinner).setVisibility(View.VISIBLE);
}

// Alliance listener
((RadioGroup)view.findViewById(R.id.start_alliance_radioGroup)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/com/frc63175985/csp/TbaCoordinator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;

import javax.net.ssl.HttpsURLConnection;
Expand Down Expand Up @@ -119,15 +120,23 @@ protected String doInBackground(String... strings) {
protected void onPostExecute(String result) {
try {
JSONArray jsonResult = new JSONArray(result);
String[] postValue = new String[jsonResult.length()];
int[] values = new int[jsonResult.length()];

for (int i = 0; i < jsonResult.length(); i++) {
JSONObject teamObject = jsonResult.getJSONObject(i);
int teamNumber = teamObject.getInt("team_number");
postValue[i] = String.valueOf(teamNumber);
values[i] = teamNumber;
}

delegate.processFinished(postValue);
// Sort array
Arrays.sort(values);

String[] strings = new String[values.length];
for (int i = 0; i < values.length; i++) {
strings[i] = String.valueOf(values[i]);
}

delegate.processFinished(strings);
} catch (JSONException e) {
e.printStackTrace();
delegate.processFinished(null);
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/com/frc63175985/csp/auth/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.frc63175985.csp.Debug;
import com.frc63175985.csp.R;
import com.frc63175985.csp.TbaCoordinator;
import com.frc63175985.csp.enums.BaseScoutType;
import com.frc63175985.csp.enums.CargoShipSelection;
import com.frc63175985.csp.enums.LevelSelection;
Expand Down Expand Up @@ -487,6 +488,51 @@ public CharSequence filter(CharSequence source, int start, int end, Spanned dest
}});
}

/**
* Bind a specific type of spinner (one that specifies a team number)
* @param context
* @param view
*/
public static void bindTeamNumberSpinner(Context context, View view) {
if (TbaCoordinator.shared.teams == null) {
throw new IllegalArgumentException("Cannot be called when Tba teams is null");
}

// Create an array with ------ at the beginning
String[] modifiedArray = new String[TbaCoordinator.shared.teams.length + 1];
modifiedArray[0] = "--------";
System.arraycopy(TbaCoordinator.shared.teams, 0, modifiedArray, 1, TbaCoordinator.shared.teams.length);

// Create listener
ArrayAdapter<String> adapter = new ArrayAdapter<>(
context, R.layout.spinner_drop_down_item, modifiedArray);
final Spinner spinner = (Spinner)view;
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ScoutAuthState.shared.currentMatch.set(Match.TEAM_NUMBER, spinner.getSelectedItem());
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}
});

// Prefill
String teamNumber = ScoutAuthState.shared.currentMatch.str(Match.TEAM_NUMBER);
int positionInArray = -1;
for (int i = 0; i < TbaCoordinator.shared.teams.length; i++) {
if (TbaCoordinator.shared.teams[i].equals(teamNumber)) {
positionInArray = i+1; // +1 because array[0] == the placeholder, "-----"
break;
}
}

if (positionInArray != -1) {
spinner.setSelection(positionInArray);
}
}

public static void bindSpinner(Context context, View parentView, int id,
final String key, String[] options) {
// Create listener
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/fragment_start.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:textSize="26sp" />

<Spinner
android:id="@+id/start_team_num_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
Expand Down

0 comments on commit 8008812

Please sign in to comment.