Search in sources :

Example 6 with RForm

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);
        }
    }
}
Also used : RForm(com.cpjd.roblu.models.RForm) RTeam(com.cpjd.roblu.models.RTeam) TeamMetricProcessor(com.cpjd.roblu.ui.teamsSorting.TeamMetricProcessor)

Example 7 with RForm

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;
    }
}
Also used : RForm(com.cpjd.roblu.models.RForm) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException)

Example 8 with RForm

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();
}
Also used : RBoolean(com.cpjd.roblu.models.metrics.RBoolean) RForm(com.cpjd.roblu.models.RForm) UIHandler(com.cpjd.roblu.ui.UIHandler) IO(com.cpjd.roblu.io.IO) Toolbar(android.support.v7.widget.Toolbar)

Example 9 with RForm

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;
    }
}
Also used : RDivider(com.cpjd.roblu.models.metrics.RDivider) RBoolean(com.cpjd.roblu.models.metrics.RBoolean) RGallery(com.cpjd.roblu.models.metrics.RGallery) RTextfield(com.cpjd.roblu.models.metrics.RTextfield) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) LinkedHashMap(java.util.LinkedHashMap) RForm(com.cpjd.roblu.models.RForm) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) RSlider(com.cpjd.roblu.models.metrics.RSlider) RBoolean(com.cpjd.roblu.models.metrics.RBoolean) AssetManager(android.content.res.AssetManager) RChooser(com.cpjd.roblu.models.metrics.RChooser) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) BufferedReader(java.io.BufferedReader) RCounter(com.cpjd.roblu.models.metrics.RCounter)

Example 10 with RForm

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();
}
Also used : RForm(com.cpjd.roblu.models.RForm) IO(com.cpjd.roblu.io.IO) Intent(android.content.Intent)

Aggregations

RForm (com.cpjd.roblu.models.RForm)18 IO (com.cpjd.roblu.io.IO)10 RTeam (com.cpjd.roblu.models.RTeam)9 ArrayList (java.util.ArrayList)9 RMetric (com.cpjd.roblu.models.metrics.RMetric)7 RTab (com.cpjd.roblu.models.RTab)6 RGallery (com.cpjd.roblu.models.metrics.RGallery)4 RTextfield (com.cpjd.roblu.models.metrics.RTextfield)4 UIHandler (com.cpjd.roblu.ui.UIHandler)4 StrictMode (android.os.StrictMode)3 Toolbar (android.support.v7.widget.Toolbar)3 Request (com.cpjd.http.Request)3 CloudCheckoutRequest (com.cpjd.requests.CloudCheckoutRequest)3 RCheckout (com.cpjd.roblu.models.RCheckout)3 REvent (com.cpjd.roblu.models.REvent)3 RSettings (com.cpjd.roblu.models.RSettings)3 RSyncSettings (com.cpjd.roblu.models.RSyncSettings)3 RUI (com.cpjd.roblu.models.RUI)3 IOException (java.io.IOException)3 CloudCheckout (com.cpjd.models.CloudCheckout)2