From 80088129c6df3a2fb28fa07bf1d65e5739d4f9e7 Mon Sep 17 00:00:00 2001 From: Brandon Richards Date: Thu, 14 Mar 2019 16:48:12 -0500 Subject: [PATCH] Sort teams by number; Add Team Number Spinner to Match Scouting. 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. --- .../main/java/com/frc63175985/csp/Debug.java | 1 + .../java/com/frc63175985/csp/FileManager.java | 39 +++++++++++++++- .../com/frc63175985/csp/StartFragment.java | 11 ++++- .../com/frc63175985/csp/TbaCoordinator.java | 15 ++++-- .../java/com/frc63175985/csp/auth/Match.java | 46 +++++++++++++++++++ app/src/main/res/layout/fragment_start.xml | 5 ++ 6 files changed, 112 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/frc63175985/csp/Debug.java b/app/src/main/java/com/frc63175985/csp/Debug.java index 3bee987..c5cf02d 100644 --- a/app/src/main/java/com/frc63175985/csp/Debug.java +++ b/app/src/main/java/com/frc63175985/csp/Debug.java @@ -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) { diff --git a/app/src/main/java/com/frc63175985/csp/FileManager.java b/app/src/main/java/com/frc63175985/csp/FileManager.java index 7bcf15e..aa40653 100644 --- a/app/src/main/java/com/frc63175985/csp/FileManager.java +++ b/app/src/main/java/com/frc63175985/csp/FileManager.java @@ -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(); } @@ -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) { @@ -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(); @@ -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(); + } + } } } diff --git a/app/src/main/java/com/frc63175985/csp/StartFragment.java b/app/src/main/java/com/frc63175985/csp/StartFragment.java index 7381827..6a09025 100644 --- a/app/src/main/java/com/frc63175985/csp/StartFragment.java +++ b/app/src/main/java/com/frc63175985/csp/StartFragment.java @@ -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() { diff --git a/app/src/main/java/com/frc63175985/csp/TbaCoordinator.java b/app/src/main/java/com/frc63175985/csp/TbaCoordinator.java index 89a04e7..71991b2 100644 --- a/app/src/main/java/com/frc63175985/csp/TbaCoordinator.java +++ b/app/src/main/java/com/frc63175985/csp/TbaCoordinator.java @@ -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; @@ -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); diff --git a/app/src/main/java/com/frc63175985/csp/auth/Match.java b/app/src/main/java/com/frc63175985/csp/auth/Match.java index b13021e..452421f 100644 --- a/app/src/main/java/com/frc63175985/csp/auth/Match.java +++ b/app/src/main/java/com/frc63175985/csp/auth/Match.java @@ -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; @@ -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 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 diff --git a/app/src/main/res/layout/fragment_start.xml b/app/src/main/res/layout/fragment_start.xml index f3835c0..d9d5c9f 100644 --- a/app/src/main/res/layout/fragment_start.xml +++ b/app/src/main/res/layout/fragment_start.xml @@ -42,6 +42,11 @@ android:layout_height="wrap_content" android:inputType="numberSigned" android:textSize="26sp" /> + +