use of com.cpjd.roblu.models.RForm in project Roblu by wdavies973.
the class LoadTeamsTask method run.
/**
* Performs loading, sorting, searching, etc.
*/
@Override
public void run() {
/*
* Verify that the event actually exists
*/
if (ioWeakReference.get() == null || !ioWeakReference.get().doesEventExist(eventID)) {
quit();
return;
}
/*
* Next, take care of "reload", if reload is true, teams must be reloaded from the file system
*/
if (teams == null || teams.size() == 0) {
RTeam[] local = ioWeakReference.get().loadTeams(eventID);
// no teams were found locally
if (local == null || local.length == 0) {
Log.d("RBS", "LoadTeamsTask found no teams locally stored.");
teams = null;
listener.teamsListLoaded(null, false);
quit();
return;
}
// teams were found locally, so attach them to the teams array
teams = new ArrayList<>(Arrays.asList(local));
Log.d("RBS", "LoadTeamsTask loaded " + teams.size() + " teams");
}
// threads) finish.
for (RTeam team : teams) {
if (team == null) {
quit();
return;
}
}
/*
* Next, each RTeam contains a variable named filter, it's a sort of "helper" variable.
* Each RTeam has a compareTo method that will allow it to easily be sorted, the filter variable within
* the team will let the team now how it's to behave when compareTo() is called in it
*/
for (RTeam team : teams) team.setFilter(filter);
/*
* If filter is ANYTHING but SORT_TYPE.SEARCH or SORT_TYPE.CUSTOM_SORT, just run a standard sort.
* And actually, that also means that we're done with this method, return the teams!
*/
if (filter == TeamsView.SORT_TYPE.LAST_EDIT || filter == TeamsView.SORT_TYPE.NUMERICAL || filter == TeamsView.SORT_TYPE.ALPHABETICAL) {
// reset the filter tag
for (RTeam team : this.teams) team.setFilterTag("");
try {
Collections.sort(teams);
if (filter == TeamsView.SORT_TYPE.LAST_EDIT)
Collections.reverse(teams);
listener.teamsListLoaded(this.teams, false);
quit();
} catch (Exception e) {
Log.d("RBS", "A problem occurred while attempting to sort teams with SORT_TYPE = " + filter);
}
} else /*
* Okay, the user actually wants to search, possibly.
* Let's handle PURE searching next (no custom sort)
*/
if (filter == TeamsView.SORT_TYPE.SEARCH) {
for (RTeam team : teams) {
team.setCustomRelevance(0);
team.setFilterTag("");
// assign search relevance to the team
processTeamForSearch(team);
}
try {
Collections.sort(this.teams);
Collections.reverse(this.teams);
listener.teamsListLoaded(this.teams, true);
quit();
} catch (Exception e) {
Log.d("RBS", "A problem occurred while attempting to sort teams with SORT_TYPE = " + filter);
}
} else /*
* Alright, the user actually wants to custom sort, so, let's custom sort, also
* check to see if query is not null, because searching is also allowing during custom sorting
*/
if (filter == TeamsView.SORT_TYPE.CUSTOM_SORT) {
/*
* Elements Processor is a powerful helper utility. How it works is the following:
* -Elements Processor will be passed an input (int method defined by TeamMetricProcessor.PROCESS_METHOD, and ID of the metric to analyze
* -Elements Processor will attach a string to filterTag and a relevance to customRelevance and return the team object, then just sort
*/
TeamMetricProcessor teamMetricProcessor = new TeamMetricProcessor();
/*
* TeamMetricProcessor requires all inputted teams to be synced with the form, so let's do that
*/
RForm form = ioWeakReference.get().loadForm(eventID);
for (RTeam team : teams) {
team.verify(form);
ioWeakReference.get().saveTeam(eventID, team);
}
/*
* Decode the method process and metric ID from the customSortToken string
*/
int methodID = Integer.parseInt(customSortToken.split(":")[0]);
int metricID = Integer.parseInt(customSortToken.split(":")[1]);
Log.d("RBS", "Custom sorting with methodID: " + methodID + ", metricID: " + metricID);
/*
* Make sure to set the extra "matchTitle" parameter if processMethod==PROCESS_METHOD.IN_MATCH
*/
// for the "In match" option, zero relevance items should be hidden
boolean shouldHideZeroRelevance = false;
if (methodID == TeamMetricProcessor.PROCESS_METHOD.OTHER && metricID == TeamMetricProcessor.PROCESS_METHOD.OTHER_METHOD.IN_MATCH && customSortToken.split(":").length == 3) {
teamMetricProcessor.setInMatchTitle(customSortToken.split(":")[2]);
Log.d("RBS", "In match title: " + teamMetricProcessor.getInMatchTitle());
shouldHideZeroRelevance = true;
} else if (methodID == TeamMetricProcessor.PROCESS_METHOD.MATCHES && customSortToken.split(":").length == 3) {
teamMetricProcessor.setInMatchTitle(customSortToken.split(":")[2]);
Log.d("RBS", "In match title: " + teamMetricProcessor.getInMatchTitle());
}
/*
* Next, perform the operation
*/
for (RTeam team : teams) {
team.setFilter(filter);
// reset old filter tags
team.setFilterTag("");
// reset any old relevance
team.setCustomRelevance(0);
teamMetricProcessor.process(team, methodID, metricID);
}
/*
* Finally, check to see if the user also wants to sort through the array
*/
for (RTeam team : teams) processTeamForSearch(team);
try {
Collections.sort(teams);
// don't reverse array if sorting by "In matches" since in matches uses numerical sub sort
if (!(methodID == TeamMetricProcessor.PROCESS_METHOD.OTHER && metricID == TeamMetricProcessor.PROCESS_METHOD.OTHER_METHOD.IN_MATCH))
Collections.reverse(teams);
listener.teamsListLoaded(this.teams, shouldHideZeroRelevance);
quit();
} catch (Exception e) {
Log.d("RBS", "A problem occurred while attempting to sort teams with SORT_TYPE = " + filter);
}
}
}
use of com.cpjd.roblu.models.RForm in project Roblu by wdavies973.
the class IO method convertFormFile.
public RForm convertFormFile(Uri toCopy) {
File file = new File(context.getFilesDir(), PREFIX + File.separator + "master_form.ser");
if (file.mkdirs())
Log.d("RBS", "Successfully created form backup parent dirs");
if (file.exists()) {
if (!file.delete())
Log.d("RBS", "Failed to delete old cached backup file.");
}
try {
InputStream is = context.getContentResolver().openInputStream(toCopy);
FileOutputStream out = new FileOutputStream(file);
if (is != null) {
IOUtils.copy(is, out);
RForm backup = (RForm) deserializeObject(file);
is.close();
out.flush();
out.close();
}
return (RForm) deserializeObject(file);
} catch (Exception e) {
Log.d("RBS", "Exception: " + e.getMessage());
return null;
}
}
use of com.cpjd.roblu.models.RForm in project Roblu by wdavies973.
the class MetricEditor method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_element);
/*
* Load dependencies
*/
rui = new IO(getApplicationContext()).loadSettings().getRui();
if (getIntent().getSerializableExtra("form") != null)
form = (RForm) getIntent().getSerializableExtra("form");
tab = getIntent().getIntExtra("tab", 0);
/*
* Setup UI
*/
// Toolbar
Toolbar toolbar = findViewById(R.id.support_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeAsUpIndicator(R.drawable.clear);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Add metric");
}
// layout
layout = findViewById(R.id.add_element_layout);
// RMetric to UI
rMetricToUI = new RMetricToUI(this, rui, true);
rMetricToUI.setListener(this);
/*
* Check if the user actually wants to edit
*/
if (getIntent().getSerializableExtra("metric") != null) {
metric = (RMetric) getIntent().getSerializableExtra("metric");
if (getSupportActionBar() != null)
getSupportActionBar().setTitle("Edit metric");
/*
* The modified variable DOES NOT matter in the form, so always, always make sure it's true
* so N.O. tags don't show up
*/
metric.setModified(true);
addMetricPreviewToToolbar();
buildConfigLayout();
} else {
// add a RBoolean type, it's the default loaded type until the user selects a different one
metric = new RBoolean(0, "Boolean", false);
/*
* The modified variable DOES NOT matter in the form, so always, always make sure it's true
* so N.O. tags don't show up
*/
metric.setModified(true);
layout.addView(initMetricSelector());
}
// Sync UI with color preferences
new UIHandler(this, toolbar).update();
}
use of com.cpjd.roblu.models.RForm in project Roblu by wdavies973.
the class PredefinedFormSelector method processForm.
/**
* Converst the form into an RForm reference
* @param name the file name
* @return an RForm instance
*/
private RForm processForm(int index, String name) {
RForm form = new RForm(null, null);
ArrayList<RMetric> metrics = new ArrayList<>();
try {
AssetManager am = getAssets();
InputStream is = am.open("predefinedForms" + File.separator + name);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
int ID = 0;
while ((line = br.readLine()) != null) {
if (line.startsWith("Title")) {
items[index] = line.split(":")[1];
continue;
} else if (line.startsWith("Description")) {
sub_items[index] = line.split(":")[1];
continue;
}
switch(line) {
case "PIT":
continue;
case "MATCH":
form.setPit(new ArrayList<>(metrics));
metrics.clear();
continue;
case "DEFAULTS":
metrics.add(new RTextfield(0, "Team name", false, true, ""));
metrics.add(new RTextfield(1, "Team number", true, true, ""));
ID = 2;
break;
}
/*
* Process file
*/
String regex = "(?<!\\\\)" + Pattern.quote(",");
String[] tokens = line.split(regex);
for (int i = 0; i < tokens.length; i++) {
tokens[i] = tokens[i].replaceAll("\\\\,", ",");
}
switch(tokens[0]) {
case "counter":
metrics.add(new RCounter(ID, tokens[1], Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3])));
ID++;
break;
case "divider":
metrics.add(new RDivider(ID, tokens[1]));
ID++;
break;
case "chooser":
metrics.add(new RChooser(ID, tokens[1], tokens[2].split(":"), Integer.parseInt(tokens[3])));
ID++;
break;
case "slider":
metrics.add(new RSlider(ID, tokens[1], Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4])));
ID++;
break;
case "checkbox":
LinkedHashMap<String, Boolean> temp = new LinkedHashMap<>();
for (String s : tokens[2].split(":")) temp.put(s, false);
metrics.add(new RCheckbox(ID, tokens[1], temp));
ID++;
break;
case "textfield":
metrics.add(new RTextfield(ID, tokens[1], ""));
ID++;
break;
case "stopwatch":
metrics.add(new RStopwatch(ID, tokens[1], Double.parseDouble(tokens[2])));
ID++;
break;
case "boolean":
metrics.add(new RBoolean(ID, tokens[1], Boolean.parseBoolean(tokens[2])));
ID++;
break;
case "gallery":
metrics.add(new RGallery(ID, tokens[1]));
ID++;
break;
}
}
form.setMatch(metrics);
Log.d("RBS", "Form created successfully with " + form.getPit().size() + " pit metrics and " + form.getMatch().size() + " match metrics");
return form;
} catch (IOException e) {
Log.d("RBS", "Failed to process form: " + e.getMessage());
return null;
}
}
use of com.cpjd.roblu.models.RForm in project Roblu by wdavies973.
the class PredefinedFormSelector method onItemClick.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RForm form = forms.get(position);
if (form == null)
return;
if (previewModeEnabled) {
new IO(getApplicationContext()).createPreview(form);
Intent intent = new Intent(this, TeamViewer.class);
intent.putExtra("teamID", -1);
intent.putExtra("eventID", -1);
intent.putExtra("editable", false);
startActivity(intent);
return;
}
Intent intent = new Intent();
intent.putExtra("form", form);
setResult(Constants.PREDEFINED_FORM_SELECTED, intent);
finish();
}
Aggregations