use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.
the class TeamsView method showTeamCreateDialog.
/**
* Shows the team create dialog where the user can manually create a team
*/
private void showTeamCreateDialog() {
if (eventDrawerManager.getEvent() == null)
return;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final AppCompatEditText input = new AppCompatEditText(this);
Utils.setInputTextLayoutColor(settings.getRui().getAccent(), null, input);
input.setHighlightColor(settings.getRui().getAccent());
input.setHintTextColor(settings.getRui().getText());
input.setTextColor(settings.getRui().getText());
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setHint("Team name");
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(30);
input.setFilters(FilterArray);
layout.addView(input);
final AppCompatEditText input2 = new AppCompatEditText(this);
Utils.setInputTextLayoutColor(settings.getRui().getAccent(), null, input2);
input2.setHighlightColor(settings.getRui().getAccent());
input2.setHintTextColor(settings.getRui().getText());
input2.setTextColor(settings.getRui().getText());
input2.setInputType(InputType.TYPE_CLASS_NUMBER);
input2.setHint("Team number");
InputFilter[] FilterArray2 = new InputFilter[1];
FilterArray2[0] = new InputFilter.LengthFilter(6);
input2.setFilters(FilterArray2);
layout.addView(input2);
builder.setView(layout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (input2.getText().toString().equals(""))
input2.setText("0");
RTeam team = new RTeam(input.getText().toString(), Integer.parseInt(input2.getText().toString()), io.getNewTeamID(eventDrawerManager.getEvent().getID()));
/*
* Package for cloud
*/
if (eventDrawerManager.getEvent() != null && eventDrawerManager.getEvent().isCloudEnabled()) {
team.verify(io.loadForm(eventDrawerManager.getEvent().getID()));
RCheckout checkout = new RCheckout(team);
checkout.setStatus(0);
io.savePendingCheckout(checkout);
}
io.saveTeam(eventDrawerManager.getEvent().getID(), team);
executeLoadTeamsTask(lastFilter, true);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
TextView view = new TextView(this);
view.setTextSize(Utils.DPToPX(getApplicationContext(), 5));
view.setPadding(Utils.DPToPX(this, 18), Utils.DPToPX(this, 18), Utils.DPToPX(this, 18), Utils.DPToPX(this, 18));
view.setText(R.string.create_team);
view.setTextColor(settings.getRui().getText());
AlertDialog dialog = builder.create();
dialog.setCustomTitle(view);
if (dialog.getWindow() != null) {
dialog.getWindow().getAttributes().windowAnimations = settings.getRui().getAnimation();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(settings.getRui().getBackground()));
}
dialog.show();
dialog.getButton(Dialog.BUTTON_NEGATIVE).setTextColor(settings.getRui().getAccent());
dialog.getButton(Dialog.BUTTON_POSITIVE).setTextColor(settings.getRui().getAccent());
}
use of com.cpjd.roblu.models.RTeam 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.RTeam 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();
}
}
use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.
the class InitPacker method doInBackground.
@Override
protected Boolean doInBackground(Void... params) {
/*
* Make sure this thread has network permissions
*/
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
Log.d("RBS", "Executing InitPacker task...");
IO io = ioWeakReference.get();
RSettings settings = io.loadSettings();
RSyncSettings cloudSettings = io.loadCloudSettings();
cloudSettings.setPurgeRequested(false);
io.saveCloudSettings(cloudSettings);
io.saveSettings(settings);
Request r = new Request(settings.getServerIP());
if (!r.ping()) {
listener.statusUpdate("It appears as though the server is offline. Try again later.");
return false;
}
/*
* Load all teams from the event, also make sure that that the teams are verified
*/
REvent event = io.loadEvent(eventID);
event.setReadOnlyTeamNumber(-1);
RForm form = io.loadForm(eventID);
RTeam[] teams = io.loadTeams(eventID);
if (event == null || form == null || teams == null || teams.length == 0) {
Log.d("RBS", "Not enough data to warrant an event upload.");
listener.statusUpdate("This event doesn't contain any teams or sufficient data to upload to the server. Create some teams!");
return false;
}
// Generate the checkouts
SyncHelper syncHelper = new SyncHelper(io, event, SyncHelper.MODES.NETWORK);
ArrayList<RCheckout> checkouts = syncHelper.generateCheckoutsFromEvent(teams, -1);
// Remove field data
try {
for (RCheckout checkout : checkouts) {
for (RTab tab : checkout.getTeam().getTabs()) {
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RFieldData) {
((RFieldData) metric).setData(null);
}
}
}
}
} catch (Exception e) {
// Doesn't matter
}
/*
* Convert into JSON and upload
*/
ObjectMapper mapper = new ObjectMapper();
try {
// serialization all the checkouts and pack them in an json array, this will be processed by the server
String serializedCheckouts = syncHelper.packCheckouts(checkouts);
String serializedForm = mapper.writeValueAsString(form);
String serializedUI = mapper.writeValueAsString(settings.getRui());
String eventName = event.getName();
if (eventName == null)
eventName = "";
if (event.getKey() == null)
event.setKey("");
CloudCheckoutRequest ccr = new CloudCheckoutRequest(r, settings.getCode());
Log.d("RBS", "Initializing init packer upload...");
boolean success = ccr.init(settings.getTeamNumber(), eventName, serializedForm, serializedUI, serializedCheckouts, event.getKey());
/*
* Disable all other events with cloud syncing enabled
*/
if (success) {
REvent[] events = io.loadEvents();
for (int i = 0; events != null && i < events.length; i++) {
events[i].setCloudEnabled(events[i].getID() == eventID);
io.saveEvent(events[i]);
}
cloudSettings.getCheckoutSyncIDs().clear();
/*
* Add default sync ids
*/
for (RCheckout checkout : checkouts) {
cloudSettings.getCheckoutSyncIDs().put(checkout.getID(), 0L);
}
io.saveCloudSettings(cloudSettings);
io.saveSettings(settings);
} else
listener.statusUpdate("An error occurred. Event was not uploaded.");
return success;
} catch (Exception e) {
Log.d("RBS", "An error occurred in InitPacker: " + e.getMessage());
listener.statusUpdate("An error occurred. Event was not uploaded.");
return false;
} finally {
/*
* Set all images to null to return memory to normal
*/
for (RCheckout checkout : checkouts) {
for (RTab tab : checkout.getTeam().getTabs()) {
for (RMetric metric : tab.getMetrics()) {
if (metric instanceof RGallery) {
((RGallery) metric).setImages(null);
}
}
}
}
}
}
use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.
the class EventMergeTask method run.
@Override
public void run() {
try {
// Load teams for each event
RTeam[] localTeams = io.loadTeams(localEventID);
RTeam[] targetTeams = io.loadTeams(targetEventID);
RForm localForm = io.loadForm(localEventID);
RForm targetForm = io.loadForm(targetEventID);
for (RTeam team : localTeams) team.verify(localForm);
for (RTeam team : targetTeams) team.verify(targetForm);
// Start merging
for (RTeam local : localTeams) {
// Find the team
for (RTeam target : targetTeams) {
if (local.getNumber() == target.getNumber()) {
for (RTab localTab : local.getTabs()) {
// Find tab
for (RTab targetTab : target.getTabs()) {
if (targetTab.getTitle().equalsIgnoreCase(localTab.getTitle())) {
// Match found, start merging metrics
for (RMetric localMetric : localTab.getMetrics()) {
// Find metric
for (RMetric targetMetric : targetTab.getMetrics()) {
/*
* Okay, the forms might be different, so we need to run some checks before we override the localMetric:
* -Same instanceof type (obviously)
* -Same title (within reason, do a little trimming, and ignore caps)
* -ID - ID should not be considered, they might not be equal
*
* If we find a good candidate for the metric, override the local. Conditions
* -Target is not modified: do nothing
* -Target is modified: overwrite if local not modified
* -Both modified: Compare team last edit time stamp
*/
if ((localMetric.getClass().equals(targetMetric.getClass())) && localMetric.getTitle().toLowerCase().replaceAll(" ", "").equals(targetMetric.getTitle().toLowerCase().replaceAll(" ", ""))) {
if (localMetric instanceof RGallery) {
// Just add the images
if (((RGallery) localMetric).getPictureIDs() == null)
((RGallery) localMetric).setPictureIDs(new ArrayList<Integer>());
if (((RGallery) localMetric).getImages() != null) {
// Add images to the current gallery
for (int i = 0; i < ((RGallery) targetMetric).getImages().size(); i++) {
((RGallery) localMetric).getPictureIDs().add(io.savePicture(localEventID, ((RGallery) targetMetric).getImages().get(i)));
}
}
// Don't forget to clear the pictures from memory after they've been merged
((RGallery) targetMetric).setImages(null);
local.setLastEdit(target.getLastEdit());
} else // Alright, looks like we can do the checks now
if (targetMetric.isModified() && !localMetric.isModified()) {
int tabIndex = local.getTabs().indexOf(localTab);
int metricIndex = local.getTabs().get(tabIndex).getMetrics().indexOf(localMetric);
local.getTabs().get(tabIndex).getMetrics().set(metricIndex, targetMetric);
local.setLastEdit(target.getLastEdit());
} else if (targetMetric.isModified() && localMetric.isModified() && target.getLastEdit() > local.getLastEdit()) {
int tabIndex = local.getTabs().indexOf(localTab);
int metricIndex = local.getTabs().get(tabIndex).getMetrics().indexOf(localMetric);
local.getTabs().get(tabIndex).getMetrics().set(metricIndex, targetMetric);
local.setLastEdit(target.getLastEdit());
}
break;
}
}
}
break;
}
}
}
break;
}
}
io.saveTeam(localEventID, local);
}
listener.success();
} catch (Exception e) {
Log.d("RBS", "Error occurred in EventMergeTask: " + e.getMessage());
listener.error();
}
}
Aggregations