Search in sources :

Example 21 with RTeam

use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.

the class MyMatches method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setup ui stuff
    setContentView(R.layout.mymatches);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null)
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // access the eventID, if no eventID is passed in to this class, the app will crash
    eventID = getIntent().getIntExtra("eventID", 0);
    // load the number from settings
    int number = new IO(getApplicationContext()).loadSettings().getTeamNumber();
    if (number == 0) {
        // the user hasn't changed their number yet, and since no team is #0, we have to stop the activity
        Toast.makeText(getApplicationContext(), "No team number found. Set it in settings.", Toast.LENGTH_LONG).show();
        setResult(Constants.CANCELLED);
        finish();
        return;
    }
    /*
         * we have a reference to the team's number but not the team'd ID (we need the team's ID to load).
         *
         * so the problem here is that there is a potential for the user to have created multiple teams with
         * their own team number (not likely, but possible). Currently, this code will just load the first
         * team that matches our number that it comes across, but you could modify this code to do a "smart select"
         * that looks of data contained within each team (num of matches, size, last edit, etc.). for now, the first
         * team we come across should be fine and work 99% of the time
         */
    RTeam[] local = new IO(getApplicationContext()).loadTeams(eventID);
    RTeam myTeam = null;
    for (RTeam team : local) {
        // search through locally stored teams until we find one that matches our number
        if (team.getNumber() == number) {
            myTeam = team;
            break;
        }
    }
    if (myTeam == null) {
        // team will be null if it was not contained in the event, if no team, force close this activity
        Toast.makeText(getApplicationContext(), "Your team is missing from event, please add it", Toast.LENGTH_LONG).show();
        setResult(Constants.CANCELLED);
        finish();
        return;
    }
    if (myTeam.getTabs() == null || myTeam.getTabs().size() <= 2) {
        // we found a team, but it doesn't contain any matches, so we can't load "my matches", force close
        Toast.makeText(getApplicationContext(), "Team does not contain any match data, please add some.", Toast.LENGTH_LONG).show();
        setResult(Constants.CANCELLED);
        finish();
        return;
    }
    // for more on verification, visit the RTeam class, basically, to be safe, we want to sync the form and the team before we play around with any of them
    myTeam.verify(new IO(getApplicationContext()).loadForm(eventID));
    // next, we need to split apart the RTab array within our team, we want one RCheckout model per match
    // we'll use this array for storing info, one RChecklist per match
    ArrayList<RCheckout> toSave = new ArrayList<>();
    for (int i = 0; i < myTeam.getTabs().size(); i++) {
        // we don't care about pit or predictions tabs, so skip them since they are always at index 0 & 1
        if (i < 2)
            continue;
        // create a new team with only one tab, wrap it in a checkout, and add it to our array
        RTeam team = new RTeam(myTeam.getName(), myTeam.getNumber(), myTeam.getID());
        team.addTab(myTeam.getTabs().get(i));
        toSave.add(new RCheckout(team));
    }
    // next, we need to look through our local teams list again and search for other teams in the same match as us, then we can add them to the either the teammates or opponents array
    for (RCheckout checkout : toSave) {
        ArrayList<RTeam> teammates = new ArrayList<>();
        ArrayList<RTeam> opponents = new ArrayList<>();
        for (RTeam team : local) {
            for (RTab tab : team.getTabs()) {
                if (tab.getTitle().equalsIgnoreCase(checkout.getTeam().getTabs().get(0).getTitle())) {
                    if (checkout.getTeam().getTabs().get(0).isRedAlliance() == tab.isRedAlliance())
                        teammates.add(team);
                    else
                        opponents.add(team);
                }
            }
        }
        checkout.getTeam().getTabs().get(0).setTeammates(teammates);
        checkout.getTeam().getTabs().get(0).setOpponents(opponents);
    }
    // we've got everything we need, let's load it into the UI
    rv = findViewById(R.id.recycler);
    // manages the layout loading
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setLayoutManager(linearLayoutManager);
    adapter = new CheckoutsViewAdapter(getApplicationContext(), new IO(getApplicationContext()).loadSettings());
    adapter.setCheckouts(toSave);
    adapter.setListener(this);
    rv.setAdapter(adapter);
    // prevents a weird rendering issues
    ((SimpleItemAnimator) rv.getItemAnimator()).setSupportsChangeAnimations(false);
    // don't forget to sync our activity ui with the RUI settings
    new UIHandler(this, toolbar).update();
}
Also used : SimpleItemAnimator(android.support.v7.widget.SimpleItemAnimator) RTeam(com.cpjd.roblu.models.RTeam) UIHandler(com.cpjd.roblu.ui.UIHandler) RTab(com.cpjd.roblu.models.RTab) IO(com.cpjd.roblu.io.IO) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RCheckout(com.cpjd.roblu.models.RCheckout) Toolbar(android.support.v7.widget.Toolbar)

Example 22 with RTeam

use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.

the class SyncHelper method generateCheckoutsFromEvent.

public ArrayList<RCheckout> generateCheckoutsFromEvent(RTeam[] teams, long time) {
    /*
         * Verify everything
         */
    for (RTeam team : teams) {
        team.verify(form);
        io.saveTeam(activeEvent.getID(), team);
        // Remove all these, since the scouter won't use them
        team.setImage(null);
        team.setTbaInfo(null);
        team.setWebsite(null);
    }
    /*
         * Start packaging
         * Important note: We have to clone each team so that they don't have to be re-loaded
         */
    ArrayList<RCheckout> checkouts = new ArrayList<>();
    int id = 0;
    // Package PIT & Predictions checkouts first
    for (RTeam team : teams) {
        RTeam temp = team.clone();
        temp.removeAllTabsButPIT();
        RCheckout newCheckout = new RCheckout(temp);
        newCheckout.setID(id);
        newCheckout.setStatus(HandoffStatus.AVAILABLE);
        if (mode == MODES.BLUETOOTH && newCheckout.getTeam().getLastEdit() >= time)
            checkouts.add(newCheckout);
        else if (mode != MODES.BLUETOOTH)
            checkouts.add(newCheckout);
        id++;
    }
    /*
         * Next, add an assignment for every match, for every team
         */
    for (RTeam team : teams) {
        if (team.getTabs() == null || team.getTabs().size() == 0)
            continue;
        for (int i = 2; i < team.getTabs().size(); i++) {
            RTeam temp = team.clone();
            temp.setPage(0);
            temp.removeAllTabsBut(i);
            RCheckout check = new RCheckout(temp);
            check.setID(id);
            check.setStatus(HandoffStatus.AVAILABLE);
            if (mode == MODES.BLUETOOTH && check.getTeam().getLastEdit() >= time)
                checkouts.add(check);
            else if (mode != MODES.BLUETOOTH)
                checkouts.add(check);
            id++;
        }
    }
    Log.d("RBS", "Created: " + checkouts.size() + " checkouts");
    return checkouts;
}
Also used : RTeam(com.cpjd.roblu.models.RTeam) ArrayList(java.util.ArrayList) RCheckout(com.cpjd.roblu.models.RCheckout)

Example 23 with RTeam

use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.

the class SyncHelper method mergeCheckout.

/**
 * Merges the checkout with the active event.
 * Creates new team / match if they don't already exist
 * @param checkout the checkout to merge
 */
public void mergeCheckout(RCheckout checkout) {
    // Verify the target checkout, to make sure it's in sync with the local form
    checkout.getTeam().verify(form);
    // Check to see if there is a local team matching this checkout
    RTeam team = io.loadTeam(activeEvent.getID(), checkout.getTeam().getID());
    // The team was found, so do a merge
    if (team != null) {
        team.verify(form);
        boolean shouldOverrideLastEdited = false;
        for (RTab downloadedTab : checkout.getTeam().getTabs()) {
            boolean matchLocated = false;
            for (RTab localTab : team.getTabs()) {
                localTab.setWon(downloadedTab.isWon());
                // Found the match, start merging
                if (localTab.getTitle().equalsIgnoreCase(downloadedTab.getTitle())) {
                    /*
                         * Copy over the edit tabs
                         */
                    if (downloadedTab.getEdits() != null)
                        localTab.setEdits(downloadedTab.getEdits());
                    for (RMetric downloadedMetric : downloadedTab.getMetrics()) {
                        if (!(downloadedMetric instanceof RCalculation) && !(downloadedMetric instanceof RFieldData) && downloadedMetric.isModified())
                            shouldOverrideLastEdited = true;
                        for (RMetric localMetric : localTab.getMetrics()) {
                            // Found the metric, determine if a merge needs to occur
                            if (downloadedMetric.getID() == localMetric.getID()) {
                                // Ignore imports from this metric
                                if (downloadedMetric instanceof RFieldData)
                                    break;
                                /*
                                     * We have to deal with one special case scenario - the gallery.
                                     * The gallery should never be overrided, just added to
                                     */
                                if (downloadedMetric instanceof RGallery && localMetric instanceof RGallery) {
                                    if (((RGallery) localMetric).getPictureIDs() == null)
                                        ((RGallery) localMetric).setPictureIDs(new ArrayList<Integer>());
                                    if (((RGallery) downloadedMetric).getImages() != null) {
                                        // Add images to the current gallery
                                        for (int i = 0; i < ((RGallery) downloadedMetric).getImages().size(); i++) {
                                            ((RGallery) localMetric).getPictureIDs().add(io.savePicture(activeEvent.getID(), ((RGallery) downloadedMetric).getImages().get(i)));
                                        }
                                    }
                                    // Don't forget to clear the pictures from memory after they've been merged
                                    ((RGallery) downloadedMetric).setImages(null);
                                    break;
                                }
                                // If the local metric is already edited, keep whichever data is newest
                                if (localMetric.isModified()) {
                                    if (checkout.getTeam().getLastEdit() >= team.getLastEdit()) {
                                        int replaceIndex = localTab.getMetrics().indexOf(localMetric);
                                        localTab.getMetrics().set(replaceIndex, downloadedMetric);
                                    }
                                } else // Otherwise, just do a straight override
                                {
                                    int replaceIndex = localTab.getMetrics().indexOf(localMetric);
                                    localTab.getMetrics().set(replaceIndex, downloadedMetric);
                                }
                                break;
                            }
                        }
                    }
                    matchLocated = true;
                    break;
                }
            }
            if (!matchLocated) {
                // Add as a new match if a merge wasn't performed
                team.addTab(checkout.getTeam().getTabs().get(0));
                Collections.sort(team.getTabs());
            }
        }
        if (shouldOverrideLastEdited)
            team.setLastEdit(checkout.getTeam().getLastEdit());
    } else // The team was not found locally, create a new one
    {
        Log.d("RBS", "Team was not found, creating a new one.");
        team = new RTeam(checkout.getTeam().getName(), checkout.getTeam().getNumber(), checkout.getTeam().getID());
        team.setLastEdit(checkout.getTeam().getLastEdit());
        team.verify(form);
        if (checkout.getTeam().getTabs().size() > 1) {
            // this means the downloaded team was a PIT tab, so override the new team's tabs
            team.setTabs(checkout.getTeam().getTabs());
        } else {
            // otherwise just add them
            team.addTab(checkout.getTeam().getTabs().get(0));
        }
    }
    // Save the team
    io.saveTeam(activeEvent.getID(), team);
    // Request a UI refresh
    Utils.requestTeamViewerRefresh(context, team.getID());
    Log.d("RBS-Service", "Merged the team: " + checkout.getTeam().getName());
}
Also used : RTeam(com.cpjd.roblu.models.RTeam) RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) RCalculation(com.cpjd.roblu.models.metrics.RCalculation)

Example 24 with RTeam

use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.

the class ExportCSVTask method run.

@Override
public void run() {
    if (Build.VERSION.SDK_INT < 21) {
        listener.errorOccurred("Your device does not support CSV exporting.");
        return;
    }
    /*
         * Check to see if the event is null, if it is, we must cancel this task
         */
    if (event == null) {
        listener.errorOccurred("Event could not be loaded.");
        return;
    }
    /*
         * Load teams
         */
    teams = new IO(contextWeakReference.get()).loadTeams(event.getID());
    form = new IO(contextWeakReference.get()).loadForm(event.getID());
    /*
         * Check to see if the teams or forms are null, if they are, cancel the task
         */
    if (teams == null || teams.length == 0 || form == null) {
        listener.errorOccurred("This event doesn't contain any teams");
        return;
    }
    /*
         * Verify all the teams
         */
    for (RTeam team : teams) {
        if (team != null) {
            // also sneak in a line here to tell each team to sort by numerical if a sort request is made
            team.setFilter(TeamsView.SORT_TYPE.NUMERICAL);
            team.verify(form);
            new IO(contextWeakReference.get()).saveTeam(event.getID(), team);
        }
    }
    /*
         * Sort the teams by number
         */
    Collections.sort(Arrays.asList(teams));
    /*
         * Build checkouts array, nice way to store data
         */
    final ArrayList<RCheckout> checkouts = new ArrayList<>();
    for (RTeam team : teams) {
        RTeam temp = team.clone();
        temp.removeAllTabsButPIT();
        RCheckout newCheckout = new RCheckout(temp);
        checkouts.add(newCheckout);
    }
    /*
         * Next, add an assignment for every match, for every team
         */
    for (RTeam team : teams) {
        if (team.getTabs() == null || team.getTabs().size() == 0)
            continue;
        for (int i = 2; i < team.getTabs().size(); i++) {
            RTeam temp = team.clone();
            temp.setPage(0);
            temp.removeAllTabsBut(i);
            RCheckout check = new RCheckout(temp);
            checkouts.add(check);
        }
    }
    Collections.sort(checkouts);
    // Create an IO reference
    final IO io = new IO(contextWeakReference.get());
    /*
         * Start executing all the different CSVSheets generate commands
         */
    for (final CSVSheet s : CSVSheets) {
        new Thread() {

            public void run() {
                if (s.isEnabled()) {
                    try {
                        s.setIo(io);
                        s.setVerboseness(verboseness);
                        s.setWorkbook(workbook);
                        Log.d("RBS", "ExportCSVTask: Generating sheet: " + s.getSheetName());
                        // sets the default, this may get overrided at any point in time by the user
                        s.setCellStyle(BorderStyle.THIN, IndexedColors.WHITE, IndexedColors.BLACK, false);
                        s.generateSheet(sheets.get(s.getSheetName()), event, form, teams, checkouts);
                        for (int i = 0; i < sheets.get(s.getSheetName()).getRow(0).getLastCellNum(); i++) sheets.get(s.getSheetName()).setColumnWidth(i, s.getColumnWidth());
                    } catch (Exception e) {
                        listener.errorOccurred("Failed to execute " + s.getSheetName() + " sheet generation.");
                        Log.d("RBS", "Failed to execute " + s.getSheetName() + " sheet generation. Err: " + e.getMessage());
                    }
                    threadCompleted(s.getSheetName());
                }
            }
        }.start();
    }
}
Also used : RTeam(com.cpjd.roblu.models.RTeam) IO(com.cpjd.roblu.io.IO) ArrayList(java.util.ArrayList) RCheckout(com.cpjd.roblu.models.RCheckout) CSVSheet(com.cpjd.roblu.csv.csvSheets.CSVSheet) IOException(java.io.IOException)

Example 25 with RTeam

use of com.cpjd.roblu.models.RTeam in project Roblu by wdavies973.

the class IO method duplicateEvent.

/**
 * Duplicates an event. The EVENT MUST BE RELOADED, never ever return the REvent
 * reference.
 * @param event the REvent to duplicate
 * @param keepScoutingData true if scouting data should be preserved
 * @return reference to the duplicated event
 */
public REvent duplicateEvent(REvent event, boolean keepScoutingData) {
    int newID = getNewEventID();
    // New name
    event.setName("Copy of " + event.getName());
    // Set teams
    RTeam[] teams = loadTeams(event.getID());
    // Set forms
    RForm form = loadForm(event.getID());
    event.setID(newID);
    saveEvent(event);
    saveForm(newID, form);
    RTeam temp;
    for (int i = 0; teams != null && i < teams.length; i++) {
        temp = teams[i];
        if (!keepScoutingData)
            temp.setTabs(null);
        temp.setPage(1);
        saveTeam(newID, temp);
    }
    return event;
}
Also used : RForm(com.cpjd.roblu.models.RForm) RTeam(com.cpjd.roblu.models.RTeam)

Aggregations

RTeam (com.cpjd.roblu.models.RTeam)25 RTab (com.cpjd.roblu.models.RTab)13 IO (com.cpjd.roblu.io.IO)10 RMetric (com.cpjd.roblu.models.metrics.RMetric)10 ArrayList (java.util.ArrayList)10 RForm (com.cpjd.roblu.models.RForm)9 RCheckout (com.cpjd.roblu.models.RCheckout)8 RGallery (com.cpjd.roblu.models.metrics.RGallery)6 REvent (com.cpjd.roblu.models.REvent)5 Bundle (android.os.Bundle)4 RFieldData (com.cpjd.roblu.models.metrics.RFieldData)4 Intent (android.content.Intent)3 RCounter (com.cpjd.roblu.models.metrics.RCounter)3 RStopwatch (com.cpjd.roblu.models.metrics.RStopwatch)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 StrictMode (android.os.StrictMode)2 View (android.view.View)2 TextView (android.widget.TextView)2 Request (com.cpjd.http.Request)2