use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class ManualScheduleImporter method run.
@Override
public void run() {
RForm form = io.loadForm(eventID);
RTeam[] teams = io.loadTeams(eventID);
// Load the schedule
ArrayList<String> lines = new ArrayList<>();
try {
InputStream fis = context.getContentResolver().openInputStream(uri);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = br.readLine()) != null) {
Log.d("RBS", "Line: " + line);
lines.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
listener.error("File was not found on the system.");
return;
} catch (IOException e) {
e.printStackTrace();
listener.error("File contains syntax errors. Please double check it for accurate syntax.");
return;
}
if (lines.size() == 0) {
listener.error("File contains no readable data. Please double check syntax.");
return;
}
for (int i = 0; i < lines.size(); i++) {
try {
String[] tokens = lines.get(i).split(",");
RTeam team = new RTeam(tokens[0], Integer.parseInt(tokens[1]), io.getNewTeamID(eventID));
/*
* Only add the team if it hasn't been found already
*/
if (teams != null) {
for (RTeam local : teams) {
// Compare name and number, since IDs will be different
if (local.getName().equalsIgnoreCase(team.getName()) && local.getNumber() == team.getNumber()) {
team = local;
break;
}
}
}
// Verify the team against the form
team.verify(form);
// The team has been added (or found locally), start processing matches
for (int j = 2; j < tokens.length; j++) {
// use j = 2 to ignore name and number
String name = expandMatchName(tokens[j]);
boolean isRedAlliance = name.contains("R");
name = name.replaceAll("B", "").replaceAll("R", "");
RTab tab = new RTab(team.getNumber(), name, form.getMatch(), isRedAlliance, false, 0);
// Search for it
if (team.getTabs() != null) {
boolean found = false;
for (RTab local : team.getTabs()) {
if (local.getTitle().equalsIgnoreCase(tab.getTitle())) {
tab = local;
found = true;
break;
}
}
if (!found) {
team.addTab(tab);
Collections.sort(team.getTabs());
}
}
}
io.saveTeam(eventID, team);
} catch (Exception e) {
listener.error("A syntax error occurred one line #" + (i + 1) + ". Please double check syntax.");
return;
}
}
listener.success();
}
use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class Utils method getMatchTitlesWithinEvent.
/**
* Returns the match titles within an REvent
* @return a String[] containing ALL match titles within an REvent, may be null
*/
public static String[] getMatchTitlesWithinEvent(Context context, int eventID) {
RTeam[] local = new IO(context).loadTeams(eventID);
// no teams found
if (local == null || local.length == 0)
return null;
ArrayList<RTab> tabs = new ArrayList<>();
RForm form = new IO(context).loadForm(eventID);
for (RTeam team : local) {
team.verify(form);
// check if the match already exists
if (team.getTabs() == null || team.getTabs().size() == 0)
continue;
for (RTab tab : team.getTabs()) {
if (tab.getTitle().equalsIgnoreCase("pit") || tab.getTitle().equalsIgnoreCase("predictions"))
continue;
boolean found = false;
for (RTab temp : tabs) {
if (temp.getTitle().equalsIgnoreCase(tab.getTitle())) {
found = true;
break;
}
}
if (!found)
tabs.add(tab);
}
}
if (tabs.size() == 0)
return null;
Collections.sort(tabs);
// Convert to String[]
String[] values = new String[tabs.size()];
for (int i = 0; i < tabs.size(); i++) {
values[i] = tabs.get(i).getTitle();
}
return values;
}
use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class Utils method randomizeTeamMetrics.
/**
* This code will randomize an event with random metric data for the purposes of show casing an app
* or testing
*/
public static void randomizeTeamMetrics(ArrayList<RTab> tabs) {
Random r = new Random();
if (tabs != null) {
for (RTab tab : tabs) {
for (RMetric metric : tab.getMetrics()) {
metric.setModified(true);
if (metric instanceof RSlider) {
((RSlider) metric).setMax(100);
((RSlider) metric).setValue(r.nextInt(100));
} else if (metric instanceof RCounter) {
((RCounter) metric).setValue(r.nextDouble() * 100);
} else if (metric instanceof RStopwatch) {
((RStopwatch) metric).setTime(r.nextDouble() * 10);
((RStopwatch) metric).setTimes(new ArrayList<Double>());
for (int i = 0; i < r.nextInt(5); i++) {
((RStopwatch) metric).getTimes().add(Utils.round(r.nextDouble() * 8.2, 2));
}
} else if (metric instanceof RBoolean) {
((RBoolean) metric).setValue(r.nextDouble() <= 0.5);
} else if (metric instanceof RCheckbox) {
for (Object o : ((RCheckbox) metric).getValues().keySet()) {
((RCheckbox) metric).getValues().put(o.toString(), r.nextDouble() <= 0.50);
}
} else if (metric instanceof RChooser) {
((RChooser) metric).setSelectedIndex(r.nextInt(((RChooser) metric).getValues().length - 1));
} else if (metric instanceof RTextfield) {
if (!((RTextfield) metric).isOneLine())
((RTextfield) metric).setText("RTextfield has been randomized to: " + getSaltString());
}
}
}
}
}
use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class MetricSortFragment method metricSelected.
/**
* This method is called when the user taps on a metric
*
* @param v the View that the user tapped on (used for inferring the RMetric object)
*/
@Override
public void metricSelected(View v) {
final int position = rv.getChildLayoutPosition(v);
if (metrics.get(position) instanceof RFieldData) {
final Dialog d = new Dialog(getActivity());
d.setTitle("Select sub metric");
d.setContentView(R.layout.submetric_import);
final Spinner spinner = d.findViewById(R.id.type);
// Attempt to load a team to get a list of values
int id = 0;
IO io = new IO(getActivity());
RTeam team;
do {
team = io.loadTeam(eventID, id);
id++;
} while (team == null && id < 100);
RFieldData fieldData = null;
try {
mainLoop: for (RTab tab : team.getTabs()) {
if (tab.getTitle().equalsIgnoreCase("PIT") || tab.getTitle().equalsIgnoreCase("PREDICTIONS"))
continue;
for (RMetric metric2 : tab.getMetrics()) {
if (metric2 instanceof RFieldData && metrics.get(position).getID() == metric2.getID() && ((RFieldData) metric2).getData() != null && ((RFieldData) metric2).getData().size() >= 1) {
fieldData = (RFieldData) metric2;
break mainLoop;
}
}
}
} catch (Exception e) {
// }
}
if (fieldData == null)
return;
final String[] values = Utils.depackFieldData(fieldData);
if (values == null) {
Toast.makeText(getActivity(), "Error occurred while loading metrics.", Toast.LENGTH_LONG).show();
return;
}
ArrayAdapter<String> adp = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, values);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
Button button = d.findViewById(R.id.button7);
button.setText(R.string.select);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra("sortToken", TeamMetricProcessor.PROCESS_METHOD.MATCHES + ":" + metrics.get(position).getID() + ":" + values[spinner.getSelectedItemPosition()]);
getActivity().setResult(Constants.CUSTOM_SORT_CONFIRMED, result);
getActivity().finish();
d.dismiss();
}
});
if (d.getWindow() != null)
d.getWindow().getAttributes().windowAnimations = new IO(getActivity()).loadSettings().getRui().getAnimation();
d.show();
return;
} else // User selected the "In Match" option, now we have to display a list of all the matches within the event
if (processMethod == TeamMetricProcessor.PROCESS_METHOD.OTHER && metrics.get(position).getID() == TeamMetricProcessor.PROCESS_METHOD.OTHER_METHOD.IN_MATCH) {
final Dialog d = new Dialog(getActivity());
d.setTitle("Select match");
d.setContentView(R.layout.event_import_dialog);
final Spinner spinner = d.findViewById(R.id.type);
TextView t = d.findViewById(R.id.spinner_tip);
t.setText(R.string.match);
final String[] values = Utils.getMatchTitlesWithinEvent(getContext(), eventID);
if (values == null) {
Toast.makeText(getActivity(), "Error occurred while loading matches. Do any matches exist?", Toast.LENGTH_LONG).show();
return;
}
ArrayAdapter<String> adp = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, values);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
Button button = d.findViewById(R.id.button7);
button.setText(R.string.select);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra("sortToken", TeamMetricProcessor.PROCESS_METHOD.OTHER + ":" + TeamMetricProcessor.PROCESS_METHOD.OTHER_METHOD.IN_MATCH + ":" + values[spinner.getSelectedItemPosition()]);
getActivity().setResult(Constants.CUSTOM_SORT_CONFIRMED, result);
getActivity().finish();
d.dismiss();
}
});
if (d.getWindow() != null)
d.getWindow().getAttributes().windowAnimations = new IO(getActivity()).loadSettings().getRui().getAnimation();
d.show();
return;
}
String sortToken = processMethod + ":" + metrics.get(position).getID();
Intent result = new Intent();
result.putExtra("sortToken", sortToken);
if (getActivity() != null) {
getActivity().setResult(Constants.CUSTOM_SORT_CONFIRMED, result);
getActivity().finish();
}
}
use of com.cpjd.roblu.models.RTab in project Roblu by wdavies973.
the class EventCreateMethodPicker method onActivityResult.
/**
* Receives result data from child activities
* @param requestCode the request code of the child activities
* @param resultCode the result code of the child activity
* @param data any result data returned from the activity
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/*
* The user selected a backup file, let's attempt to import it here
*/
if (requestCode == Constants.FILE_CHOOSER) {
// this means the user didn't select a file, no point in returning an error message
if (data == null)
return;
try {
IO io = new IO(getApplicationContext());
RBackup backup = io.convertBackupFile(data.getData());
if (!backup.getFileVersion().equals(IO.PREFIX)) {
Utils.showSnackbar(findViewById(R.id.activity_create_event_picker), getApplicationContext(), "Invalid backup file. Backup was created with an older version of Roblu.", true, rui.getPrimaryColor());
return;
}
/*
* Create the event, we're not gonna use an AsyncTask because the user is just
* watching the event import anyway, and it will only freeze the UI for a couple hundred
* milliseconds.
*/
REvent event = backup.getEvent();
event.setCloudEnabled(false);
event.setID(io.getNewEventID());
io.saveEvent(event);
io.saveForm(event.getID(), backup.getForm());
if (backup.getTeams() != null) {
for (RTeam team : backup.getTeams()) {
for (RTab tab : team.getTabs()) {
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RGallery && ((RGallery) metric).getImages() != null) {
// Add images to the current gallery
for (int i = 0; i < ((RGallery) metric).getImages().size(); i++) {
((RGallery) metric).getPictureIDs().add(io.savePicture(event.getID(), ((RGallery) metric).getImages().get(i)));
}
((RGallery) metric).setImages(null);
}
}
}
io.saveTeam(event.getID(), team);
}
}
Utils.showSnackbar(findViewById(R.id.activity_create_event_picker), getApplicationContext(), "Successfully imported event from backup", false, rui.getPrimaryColor());
Intent intent = new Intent();
intent.putExtra("eventID", event.getID());
setResult(Constants.NEW_EVENT_CREATED, intent);
finish();
} catch (Exception e) {
Utils.showSnackbar(findViewById(R.id.activity_create_event_picker), getApplicationContext(), "Invalid backup file", true, rui.getPrimaryColor());
}
} else /*
* The user created an event manually with EventEditor, we actually don't need to do anything but auto-finish our class
* with a result code letting the TeamsView class now to refresh the event list
*/
if (resultCode == Constants.NEW_EVENT_CREATED) {
Bundle b = data.getExtras();
Intent intent = new Intent();
if (b != null)
intent.putExtras(b);
setResult(Constants.NEW_EVENT_CREATED, intent);
finish();
}
}
Aggregations