Search in sources :

Example 16 with RMetric

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();
    }
}
Also used : RBackup(com.cpjd.roblu.models.RBackup) RTeam(com.cpjd.roblu.models.RTeam) RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) IO(com.cpjd.roblu.io.IO) Bundle(android.os.Bundle) REvent(com.cpjd.roblu.models.REvent) Intent(android.content.Intent) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 17 with RMetric

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();
}
Also used : RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 18 with RMetric

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);
    }
}
Also used : Bundle(android.os.Bundle) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RMetric(com.cpjd.roblu.models.metrics.RMetric)

Example 19 with RMetric

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();
}
Also used : SimpleItemAnimator(android.support.v7.widget.SimpleItemAnimator) UIHandler(com.cpjd.roblu.ui.UIHandler) IO(com.cpjd.roblu.io.IO) RTextfield(com.cpjd.roblu.models.metrics.RTextfield) RUI(com.cpjd.roblu.models.RUI) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) BottomBar(com.roughike.bottombar.BottomBar) RForm(com.cpjd.roblu.models.RForm) BottomBarTab(com.roughike.bottombar.BottomBarTab) FloatingActionButton(android.support.design.widget.FloatingActionButton) RecyclerView(android.support.v7.widget.RecyclerView) Toolbar(android.support.v7.widget.Toolbar)

Example 20 with RMetric

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);
                    }
                }
            }
        }
    }
}
Also used : RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RMetric(com.cpjd.roblu.models.metrics.RMetric) StrictMode(android.os.StrictMode) RForm(com.cpjd.roblu.models.RForm) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper) RCheckout(com.cpjd.roblu.models.RCheckout) RSyncSettings(com.cpjd.roblu.models.RSyncSettings) RSettings(com.cpjd.roblu.models.RSettings) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest) RTeam(com.cpjd.roblu.models.RTeam) IO(com.cpjd.roblu.io.IO) Request(com.cpjd.http.Request) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest)

Aggregations

RMetric (com.cpjd.roblu.models.metrics.RMetric)26 RTab (com.cpjd.roblu.models.RTab)12 RTeam (com.cpjd.roblu.models.RTeam)10 RStopwatch (com.cpjd.roblu.models.metrics.RStopwatch)10 RTextfield (com.cpjd.roblu.models.metrics.RTextfield)10 ArrayList (java.util.ArrayList)10 RCounter (com.cpjd.roblu.models.metrics.RCounter)9 RGallery (com.cpjd.roblu.models.metrics.RGallery)9 IO (com.cpjd.roblu.io.IO)8 RCheckbox (com.cpjd.roblu.models.metrics.RCheckbox)8 RChooser (com.cpjd.roblu.models.metrics.RChooser)8 RSlider (com.cpjd.roblu.models.metrics.RSlider)8 RForm (com.cpjd.roblu.models.RForm)7 RBoolean (com.cpjd.roblu.models.metrics.RBoolean)7 RFieldData (com.cpjd.roblu.models.metrics.RFieldData)7 RCalculation (com.cpjd.roblu.models.metrics.RCalculation)6 RCheckout (com.cpjd.roblu.models.RCheckout)5 LinkedHashMap (java.util.LinkedHashMap)5 Bundle (android.os.Bundle)4 View (android.view.View)4