use of com.cpjd.roblu.models.metrics.RMetric 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.metrics.RMetric in project Roblu by wdavies973.
the class FormRecyclerAdapter method setMetrics.
/**
* Sets the metrics to the UI, also calculates the initID
* @param metrics the metrics to set to the adapter
*/
public void setMetrics(ArrayList<RMetric> metrics) {
if (metrics == null)
return;
initID = 0;
this.metrics = metrics;
for (RMetric metric : this.metrics) {
if (metric.getID() > initID)
initID = metric.getID();
}
notifyDataSetChanged();
}
use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class FormViewer method onActivityResult.
/**
* Receive information from a child activity
* @param requestCode the request code the child activity was launched with
* @param resultCode the result code of the child activity
* @param data any data returned with the child activity
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/*
* User created a new metric
*/
if (requestCode == Constants.NEW_METRIC_REQUEST && resultCode == Constants.METRIC_CONFIRMED) {
changesMade = true;
Bundle b = data.getExtras();
RMetric metric = (RMetric) b.getSerializable("metric");
if (metric instanceof RFieldData && currentTab == 0) {
Toast.makeText(getApplicationContext(), "You can't add the field data metric to the pit tab.", Toast.LENGTH_LONG).show();
return;
}
metricsAdapter.addMetric(metric);
} else /*
* User edited a metric
*/
if (requestCode == Constants.EDIT_METRIC_REQUEST && resultCode == Constants.METRIC_CONFIRMED) {
changesMade = true;
Bundle b = data.getExtras();
RMetric metric = (RMetric) b.getSerializable("metric");
metricsAdapter.reAdd(metric);
} else /*
* User discarded a metric edit
*/
if (requestCode == Constants.EDIT_METRIC_REQUEST && resultCode == Constants.CANCELLED) {
metricsAdapter.reAdd(metricEditHolder);
}
}
use of com.cpjd.roblu.models.metrics.RMetric in project Roblu by wdavies973.
the class FormViewer method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_form);
/*
* Load dependencies
*/
/*
Stores the user's UI preferences
*/
RUI rui = new IO(getApplicationContext()).loadSettings().getRui();
/*
* Setup UI
*/
// Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Form editor");
if (getIntent().getBooleanExtra("master", false))
getSupportActionBar().setSubtitle("Master form");
}
// Bottom bar - selector that lets the user switch between PIT and MATCH forms
BottomBar bBar = findViewById(R.id.bottomBar);
bBar.setOnTabSelectListener(this);
BottomBarTab tab = bBar.getTabAtPosition(0);
BottomBarTab tab2 = bBar.getTabAtPosition(1);
tab.setBarColorWhenSelected(rui.getPrimaryColor());
tab2.setBarColorWhenSelected(rui.getPrimaryColor());
bBar.selectTabAtPosition(0);
// Add the "New metric" button
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(this);
// Recycler view setup
RecyclerView rv = findViewById(R.id.movie_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(linearLayoutManager);
((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false);
metricsAdapter = new FormRecyclerAdapter(this, this);
rv.setAdapter(metricsAdapter);
// Gesture helper
ItemTouchHelper.Callback callback = new FormRecyclerTouchHelper(metricsAdapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(rv);
/*
* Check to see if we received a form from a different class or
* if we need to create a new one
*/
if (getIntent().getSerializableExtra("form") != null) {
form = (RForm) getIntent().getSerializableExtra("form");
} else {
RTextfield name = new RTextfield(0, "Team name", false, true, "");
RTextfield number = new RTextfield(1, "Team number", true, true, "");
ArrayList<RMetric> pit = new ArrayList<>();
pit.add(name);
pit.add(number);
form = new RForm(pit, new ArrayList<RMetric>());
}
loadViews(true, 0);
new UIHandler(this, toolbar, fab).update();
}
use of com.cpjd.roblu.models.metrics.RMetric 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);
}
}
}
}
}
}
Aggregations