Search in sources :

Example 1 with RCalculation

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

the class MetricEditor method getConfigField.

/**
 * Generates a configuration field for the layout with the specified settings.
 * The config field will detect the instance type of RMetric metric above and decide
 * what to update. Acceptable names:
 * -"title"
 * -"min"
 * -"max"
 * -"increment"
 * -"comma"
 */
private TextInputLayout getConfigField(final String name, RelativeLayout layout, int position) {
    TextInputLayout inputLayout = new TextInputLayout(this);
    inputLayout.setHint(name);
    inputLayout.setId(Utils.generateViewId());
    AppCompatEditText nameInput = new AppCompatEditText(this);
    Utils.setInputTextLayoutColor(rui.getAccent(), inputLayout, nameInput);
    nameInput.setTextColor(rui.getText());
    nameInput.setHighlightColor(rui.getAccent());
    nameInput.setId(Utils.generateViewId());
    if (name.equalsIgnoreCase("minimum")) {
        nameInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        if (metric instanceof RSlider)
            nameInput.setText(String.valueOf(((RSlider) metric).getMin()));
    } else if (name.equalsIgnoreCase("maximum")) {
        nameInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        if (metric instanceof RSlider)
            nameInput.setText(String.valueOf(((RSlider) metric).getMax()));
    } else if (name.equalsIgnoreCase("increment")) {
        nameInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        if (metric instanceof RCounter)
            nameInput.setText(String.valueOf(((RCounter) metric).getIncrement()));
    } else if (name.equalsIgnoreCase("calculation")) {
        if (metric instanceof RCalculation)
            nameInput.setText(((RCalculation) metric).getCalculation());
    } else if (name.startsWith("Comma separated list")) {
        if (metric instanceof RCheckbox) {
            StringBuilder text = new StringBuilder();
            RCheckbox checkbox = ((RCheckbox) metric);
            if (checkbox.getValues() != null) {
                for (Object o : checkbox.getValues().keySet()) {
                    text.append(o).append(",");
                }
            }
            if (text.toString().length() > 0)
                nameInput.setText(text.toString().substring(0, text.toString().length() - 1));
        } else if (metric instanceof RChooser) {
            StringBuilder text = new StringBuilder();
            RChooser chooser = ((RChooser) metric);
            if (chooser.getValues() != null) {
                for (String s : chooser.getValues()) text.append(s).append(",");
            }
            if (text.toString().length() > 0)
                nameInput.setText(text.toString().substring(0, text.toString().length() - 1));
        }
    } else
        nameInput.setText(metric.getTitle());
    nameInput.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (name.equalsIgnoreCase("title")) {
                metric.setTitle(charSequence.toString());
            } else if (name.equalsIgnoreCase("minimum") && metric instanceof RSlider) {
                ((RSlider) metric).setMin((int) processTextAsNumber(charSequence, 0));
            } else if (name.equalsIgnoreCase("maximum") && metric instanceof RSlider) {
                ((RSlider) metric).setMax((int) processTextAsNumber(charSequence, 100));
            } else if (name.equalsIgnoreCase("increment") && metric instanceof RCounter) {
                ((RCounter) metric).setIncrement(processTextAsNumber(charSequence, 1));
            } else if (name.startsWith("Comma")) {
                if (metric instanceof RCheckbox) {
                    String[] tokens = charSequence.toString().split(",");
                    LinkedHashMap<String, Boolean> hash = new LinkedHashMap<>();
                    for (String s : tokens) hash.put(s, false);
                    ((RCheckbox) metric).setValues(hash);
                } else if (metric instanceof RChooser) {
                    String[] tokens = charSequence.toString().split(",");
                    ((RChooser) metric).setValues(tokens);
                }
            } else if (name.equalsIgnoreCase("calculation")) {
                ((RCalculation) metric).setCalculation(charSequence.toString());
            }
            addMetricPreviewToToolbar();
        /*Toolbar tl = findViewById(R.id.toolbar);
                ViewGroup view = (ViewGroup) tl.getChildAt(0);
                RelativeLayout t = (RelativeLayout) view.getChildAt(0);
                TextView text = (TextView) t.getChildAt(0);
                text.setText(charSequence);
                metric.setTitle(charSequence.toString());*/
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    if (position > 0)
        params.addRule(RelativeLayout.BELOW, layout.getChildAt(position - 1).getId());
    inputLayout.setLayoutParams(params);
    inputLayout.addView(nameInput);
    inputLayout.requestFocus();
    return inputLayout;
}
Also used : RChooser(com.cpjd.roblu.models.metrics.RChooser) RCalculation(com.cpjd.roblu.models.metrics.RCalculation) LinkedHashMap(java.util.LinkedHashMap) AppCompatEditText(android.support.v7.widget.AppCompatEditText) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) RSlider(com.cpjd.roblu.models.metrics.RSlider) RelativeLayout(android.widget.RelativeLayout) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) RCounter(com.cpjd.roblu.models.metrics.RCounter) TextInputLayout(android.support.design.widget.TextInputLayout)

Example 2 with RCalculation

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

the class MetricEditor method buildConfigLayout.

/**
 * Adds the config text fields below the metric preview
 */
private void buildConfigLayout() {
    // clear old config items (don't clear toolbar, preview metric, or metric type selector)
    for (int i = 3; i < this.layout.getChildCount(); i++) {
        this.layout.removeViewAt(i);
    }
    RelativeLayout layout = new RelativeLayout(this);
    layout.addView(getConfigField("Title", layout, 0));
    if (metric instanceof RCheckbox || metric instanceof RChooser) {
        layout.addView(getConfigField("Comma separated list", layout, 1));
    } else if (metric instanceof RCounter) {
        final TextInputLayout til = getConfigField("Increment", layout, 1);
        til.setId(Utils.generateViewId());
        layout.addView(til);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, til.getId());
        CheckBox checkBox = new CheckBox(getApplicationContext());
        checkBox.setId(Utils.generateViewId());
        checkBox.setLayoutParams(params);
        checkBox.setHighlightColor(rui.getAccent());
        checkBox.setTextColor(rui.getText());
        checkBox.setText("Verbose input");
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ((RCounter) metric).setVerboseInput(isChecked);
                addMetricPreviewToToolbar();
            }
        });
        ColorStateList colorStateList = new ColorStateList(new int[][] { // unchecked
        new int[] { -android.R.attr.state_checked }, // checked
        new int[] { android.R.attr.state_checked } }, new int[] { rui.getButtons(), rui.getAccent() });
        CompoundButtonCompat.setButtonTintList(checkBox, colorStateList);
        layout.addView(checkBox);
    } else if (metric instanceof RSlider) {
        layout.addView(getConfigField("Minimum", layout, 1));
        layout.addView(getConfigField("Maximum", layout, 2));
    } else if (metric instanceof RCalculation) {
        final TextInputLayout til = getConfigField("Calculation", layout, 1);
        layout.addView(til);
        // Also add a button for inputting metric names
        Button b = new Button(getApplicationContext());
        b.setText("Add metric");
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, til.getId());
        b.setLayoutParams(params);
        layout.addView(b);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final Dialog d = new Dialog(MetricEditor.this);
                d.setTitle("Pick metric:");
                d.setContentView(R.layout.metric_chooser);
                final Spinner spinner = d.findViewById(R.id.type);
                String[] values;
                final ArrayList<RMetric> metrics;
                if (tab == 0)
                    metrics = form.getPit();
                else
                    metrics = form.getMatch();
                // Remove all but counters, stopwatches, and sliders
                for (int i = 0; i < metrics.size(); i++) {
                    if (!(metrics.get(i) instanceof RCounter) && !(metrics.get(i) instanceof RStopwatch && !(metrics.get(i) instanceof RSlider)) && !(metrics.get(i) instanceof RCalculation)) {
                        metrics.remove(i);
                        i--;
                    }
                }
                values = new String[metrics.size()];
                for (int i = 0; i < metrics.size(); i++) {
                    values[i] = metrics.get(i).getTitle();
                }
                ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), 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.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            RMetric m = new ArrayList<>(metrics).get(spinner.getSelectedItemPosition());
                            til.getEditText().setText(til.getEditText().getText().toString() + " " + m.getTitle());
                        } catch (Exception e) {
                            Log.d("RBS", "Failed to select metric");
                        } finally {
                            d.dismiss();
                        }
                    }
                });
                if (d.getWindow() != null)
                    d.getWindow().getAttributes().windowAnimations = new IO(getApplicationContext()).loadSettings().getRui().getAnimation();
                d.show();
            }
        });
    }
    this.layout.addView(getCardView(layout));
}
Also used : Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) ColorStateList(android.content.res.ColorStateList) RMetric(com.cpjd.roblu.models.metrics.RMetric) RCalculation(com.cpjd.roblu.models.metrics.RCalculation) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) Button(android.widget.Button) CompoundButton(android.widget.CompoundButton) Dialog(android.app.Dialog) RSlider(com.cpjd.roblu.models.metrics.RSlider) TextInputLayout(android.support.design.widget.TextInputLayout) RChooser(com.cpjd.roblu.models.metrics.RChooser) IO(com.cpjd.roblu.io.IO) View(android.view.View) AdapterView(android.widget.AdapterView) CardView(android.support.v7.widget.CardView) TextView(android.widget.TextView) RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) CheckBox(android.widget.CheckBox) RelativeLayout(android.widget.RelativeLayout) RCounter(com.cpjd.roblu.models.metrics.RCounter) CompoundButton(android.widget.CompoundButton) ArrayAdapter(android.widget.ArrayAdapter)

Example 3 with RCalculation

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

the class MetricEditor method onItemSelected.

/**
 * Called when the user selects a metric type
 * @param adapterView the adapter containing all the choices
 * @param view the view that was tapped
 * @param i the position of the view
 * @param l id of the view
 */
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    ((TextView) adapterView.getChildAt(0)).setTextColor(rui.getText());
    /*
         * User selected a new metric, let's create it
         */
    String stringOfSelected = METRIC_TYPES[i];
    if (stringOfSelected.equals(METRIC_TYPES[0])) {
        metric = new RBoolean(0, "Boolean", false);
    } else if (stringOfSelected.equals(METRIC_TYPES[1])) {
        metric = new RCounter(0, "Counter", 1, 0);
    } else if (stringOfSelected.equals(METRIC_TYPES[2])) {
        metric = new RSlider(0, "Slider", 0, 100, 0);
    } else if (stringOfSelected.equals(METRIC_TYPES[3])) {
        metric = new RChooser(0, "Chooser", null, 0);
    } else if (stringOfSelected.equals(METRIC_TYPES[4])) {
        metric = new RCheckbox(0, "Checkbox", null);
    } else if (stringOfSelected.equals(METRIC_TYPES[5])) {
        metric = new RStopwatch(0, "Stopwatch", 0);
    } else if (stringOfSelected.equals(METRIC_TYPES[6])) {
        metric = new RTextfield(0, "Text field", "");
    } else if (stringOfSelected.equals(METRIC_TYPES[7])) {
        metric = new RGallery(0, "Gallery");
    } else if (stringOfSelected.equalsIgnoreCase(METRIC_TYPES[8])) {
        metric = new RDivider(0, "Divider");
    } else if (stringOfSelected.equals(METRIC_TYPES[9])) {
        metric = new RFieldDiagram(0, R.drawable.field2018, null);
    } else if (stringOfSelected.equals(METRIC_TYPES[10])) {
        metric = new RCalculation(0, "Custom calculation");
    } else if (stringOfSelected.equals(METRIC_TYPES[11])) {
        metric = new RFieldData(0, "Match data");
    }
    metric.setModified(true);
    addMetricPreviewToToolbar();
    buildConfigLayout();
}
Also used : RBoolean(com.cpjd.roblu.models.metrics.RBoolean) RDivider(com.cpjd.roblu.models.metrics.RDivider) RChooser(com.cpjd.roblu.models.metrics.RChooser) RGallery(com.cpjd.roblu.models.metrics.RGallery) RTextfield(com.cpjd.roblu.models.metrics.RTextfield) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RFieldDiagram(com.cpjd.roblu.models.metrics.RFieldDiagram) RCalculation(com.cpjd.roblu.models.metrics.RCalculation) RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) RSlider(com.cpjd.roblu.models.metrics.RSlider) TextView(android.widget.TextView) RCounter(com.cpjd.roblu.models.metrics.RCounter)

Example 4 with RCalculation

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

the class Overview method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.overview_tab, container, false);
    Bundle bundle = this.getArguments();
    layout = view.findViewById(R.id.overview_layout);
    REvent event = (REvent) getArguments().getSerializable("event");
    RTeam team = new IO(getActivity()).loadTeam(event.getID(), TeamViewer.team.getID());
    rMetricToUI = new RMetricToUI(getActivity(), new IO(getActivity()).loadSettings().getRui(), true);
    try {
        /*
         * Do statistics generation, this will generate graphs for certain metrics
         */
        // Stores pie chart values, with the sub linked hash map <item,occurrences>, this will need to be processed later into a percent
        LinkedHashMap<String, LinkedHashMap<String, Double>> pieValues = new LinkedHashMap<>();
        // Stores line chart values, with the sub linked hash map <matchName,value>
        LinkedHashMap<String, LinkedHashMap<String, Double>> lineValues = new LinkedHashMap<>();
        // This isn't directly related, more of a side project
        ArrayList<RGallery> galleries = new ArrayList<>();
        for (RTab tab : team.getTabs()) {
            // Rule out disallowed tabs
            if (tab.getTitle().equalsIgnoreCase("PIT"))
                continue;
            // Start processing metrics
            for (RMetric metric : tab.getMetrics()) {
                if (metric instanceof RGallery) {
                    galleries.add((RGallery) metric);
                }
                if (!metric.isModified())
                    continue;
                // Pie graph metrics, scan these here
                if (metric instanceof RBoolean) {
                    LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
                    if (temp == null)
                        temp = new LinkedHashMap<>();
                    String key = ((RBoolean) metric).isValue() ? "Yes" : "No";
                    if (temp.get(key) == null)
                        temp.put(key, 1.0);
                    else
                        temp.put(key, temp.get(key) + 1);
                    pieValues.put(metric.getTitle(), temp);
                } else if (metric instanceof RCheckbox) {
                    if (((RCheckbox) metric).getValues() != null) {
                        for (Object key : ((RCheckbox) metric).getValues().keySet()) {
                            LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
                            if (temp == null)
                                temp = new LinkedHashMap<>();
                            if (temp.get(key.toString()) == null)
                                temp.put(key.toString(), 1.0);
                            else
                                temp.put(key.toString(), temp.get(key.toString()) + 1);
                            pieValues.put(metric.getTitle(), temp);
                        }
                    }
                } else if (metric instanceof RChooser) {
                    LinkedHashMap<String, Double> temp = pieValues.get(metric.getTitle());
                    if (temp == null)
                        temp = new LinkedHashMap<>();
                    if (temp.get(metric.toString()) == null)
                        temp.put(metric.toString(), 1.0);
                    else
                        temp.put(metric.toString(), temp.get(metric.toString()) + 1);
                    pieValues.put(metric.getTitle(), temp);
                } else // Line chart metrics
                if (metric instanceof RCounter || metric instanceof RSlider || metric instanceof RStopwatch || metric instanceof RCalculation) {
                    LinkedHashMap<String, Double> temp = lineValues.get(metric.getTitle());
                    if (temp == null)
                        temp = new LinkedHashMap<>();
                    temp.put(tab.getTitle(), Double.parseDouble(metric.toString()));
                    lineValues.put(metric.getTitle(), temp);
                }
            }
        }
        // Add the divider metrics by position, -1 if no metric after it, or at the end
        ArrayList<RDivider> addedDividers = new ArrayList<>();
        /*
         * Add the charts!
         */
        for (Object key : lineValues.keySet()) {
            if (lineValues.get(key.toString()).size() >= 2) {
                loop: for (RTab tab : team.getTabs()) {
                    for (int i = 0; i < tab.getMetrics().size(); i++) {
                        if (tab.getMetrics().get(i).getTitle().equals(key.toString())) {
                            // See if there is a RDivider hiding above this metric
                            for (int j = i; j >= 0; j--) {
                                if (tab.getMetrics().get(j) instanceof RDivider && !addedDividers.contains(tab.getMetrics().get(j))) {
                                    layout.addView(rMetricToUI.getDivider((RDivider) tab.getMetrics().get(j)));
                                    addedDividers.add((RDivider) tab.getMetrics().get(j));
                                    break loop;
                                }
                            }
                        }
                    }
                }
                layout.addView(rMetricToUI.generateLineChart(key.toString(), lineValues.get(key.toString())));
            }
        }
        // Process the pie charts
        for (Object key : pieValues.keySet()) {
            if (pieValues.get(key.toString()).size() <= 1)
                continue;
            int metricID = 0;
            loop: for (RTab tab : team.getTabs()) {
                for (int i = 0; i < tab.getMetrics().size(); i++) {
                    if (tab.getMetrics().get(i).getTitle().equals(key.toString())) {
                        metricID = tab.getMetrics().get(i).getID();
                        // See if there is a RDivider hiding above this metric
                        for (int j = i; j >= 0; j--) {
                            if (tab.getMetrics().get(j) instanceof RDivider && !addedDividers.contains(tab.getMetrics().get(j))) {
                                layout.addView(rMetricToUI.getDivider((RDivider) tab.getMetrics().get(j)));
                                addedDividers.add((RDivider) tab.getMetrics().get(j));
                                break loop;
                            }
                        }
                    }
                }
            }
            for (Object key2 : pieValues.get(key.toString()).keySet()) {
                if (numModified(team.getTabs(), metricID) != 0)
                    pieValues.get(key.toString()).put(key2.toString(), pieValues.get(key.toString()).get(key2.toString()) / (double) numModified(team.getTabs(), metricID));
            }
            layout.addView(rMetricToUI.generatePieChart(key.toString(), pieValues.get(key.toString())));
        }
        /*
         * Find the image with the most entropy, and add
         * it as the "featured" image
         */
        galleryLoop: for (int j = galleries.size() - 1; j >= 0; j--) {
            if (galleries.get(j).getImages() != null && galleries.get(j).getImages().size() > 0) {
                for (int i = galleries.get(j).getImages().size() - 1; i >= 0; i--) {
                    try {
                        layout.addView(rMetricToUI.getImageView("Featured image", BitmapFactory.decodeByteArray(galleries.get(j).getImages().get(i), 0, galleries.get(j).getImages().get(i).length)));
                        break galleryLoop;
                    } catch (Exception e) {
                        Log.d("RBS", "Failed to load featured image: " + e.getMessage());
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.d("RBS", "Failed to generate graphs for this team profile.");
    }
    /*
         * Attempt to download TBA info for this team
         */
    if (!team.hasTBAInfo()) {
        if (event.getKey() != null && event.getKey().length() >= 4)
            new TBATeamInfoTask(view.getContext(), team.getNumber(), event.getKey().substring(0, 4), this);
    } else {
        // TBA info card
        layout.addView(rMetricToUI.getInfoField("TBA.com information", TeamViewer.team.getTbaInfo(), TeamViewer.team.getWebsite(), TeamViewer.team.getNumber()), 0);
        if (TeamViewer.team.getImage() != null) {
            // Image view
            Bitmap bitmap = BitmapFactory.decodeByteArray(TeamViewer.team.getImage(), 0, TeamViewer.team.getImage().length);
            layout.addView(rMetricToUI.getImageView("Robot", bitmap));
        }
    }
    /*
         * Add UI cards to the layout
         */
    // "Other" card
    layout.addView(rMetricToUI.getInfoField("Other", "Last edited: " + Utils.convertTime(team.getLastEdit()) + "\nSize on disk: " + new IO(view.getContext()).getTeamSize(bundle.getInt("eventID"), team.getID()) + " KB", "", 0));
    return view;
}
Also used : RBoolean(com.cpjd.roblu.models.metrics.RBoolean) RDivider(com.cpjd.roblu.models.metrics.RDivider) RGallery(com.cpjd.roblu.models.metrics.RGallery) RTab(com.cpjd.roblu.models.RTab) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) RCalculation(com.cpjd.roblu.models.metrics.RCalculation) LinkedHashMap(java.util.LinkedHashMap) Bitmap(android.graphics.Bitmap) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) RSlider(com.cpjd.roblu.models.metrics.RSlider) REvent(com.cpjd.roblu.models.REvent) TBATeamInfoTask(com.cpjd.roblu.ui.team.TBATeamInfoTask) RChooser(com.cpjd.roblu.models.metrics.RChooser) RTeam(com.cpjd.roblu.models.RTeam) Bundle(android.os.Bundle) IO(com.cpjd.roblu.io.IO) View(android.view.View) RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) RCounter(com.cpjd.roblu.models.metrics.RCounter) RMetricToUI(com.cpjd.roblu.ui.forms.RMetricToUI)

Example 5 with RCalculation

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

the class RTeam method verify.

/**
 * verify() makes sure that the form and team are synchronized. Here's what it does:
 * <p>
 * PIT:
 * -If the user modified the form and ADDED elements, then we'll make sure to add them to this team's form copy
 * -If the user modified the form and REMOVED elements, then we'll make sure to remove them from this team's form copy
 * -If the user changed any item titles, change them right away
 * -If the user changed any default values, reset all the values on all elements that have NOT been modified
 * -If the user changed the order of any elements, change the order
 * <p>
 * MATCH:
 * -If the user modified the match form and ADDED elements, then we'll add those to EVERY match profile
 * -If the user modified the match form and REMOVED elements, then we'll remove those from EVERY match profile
 * -If the user changed any item titles, change them on ALL match profiles
 * -If the user changed any default values, reset all the values of EVERY match that have NOT been modified
 * -If the user changed the order of any elements, change the order
 * <p>
 * PREMISE:
 * -PIT and MATCH form arrays may NOT be null, only empty
 * <p>
 * NULLS to check for:
 * -If the team has never been opened before, set the PIT values, matches don't need to be set until creation.
 */
public void verify(RForm form) {
    // Check for null or missing Pit & Predictions tabs
    if (this.tabs == null || this.tabs.size() == 0) {
        this.tabs = new ArrayList<>();
        addTab(new RTab(number, "Pit", Utils.duplicateRMetricArray(form.getPit()), false, false, 0));
        addTab(new RTab(number, "Predictions", Utils.duplicateRMetricArray(form.getMatch()), false, false, 0));
        // Check to make sure the team name and number have been inserted into the form
        for (RMetric m : this.getTabs().get(0).getMetrics()) {
            if (// team name
            m.getID() == 0)
                // team name
                ((RTextfield) m).setText(name);
            else // team number
            if (m.getID() == 1)
                ((RTextfield) m).setText(String.valueOf(number));
        }
        return;
    }
    // Remove elements that aren't on the form
    // less if statements, just switches between PIT or MATCH depending on what needs to be verified
    ArrayList<RMetric> temp = form.getPit();
    for (int i = 0; i < tabs.size(); i++) {
        if (!tabs.get(i).getTitle().equalsIgnoreCase("Pit"))
            temp = form.getMatch();
        for (int j = 0; j < tabs.get(i).getMetrics().size(); j++) {
            boolean found = false;
            if (temp.size() == 0) {
                tabs.get(i).getMetrics().clear();
                break;
            }
            for (int k = 0; k < temp.size(); k++) {
                if (tabs.get(i).getMetrics().get(j).getID() == temp.get(k).getID())
                    found = true;
                if (k == temp.size() - 1 && !found) {
                    tabs.get(i).getMetrics().remove(j);
                    j = 0;
                    break;
                }
            }
        }
    }
    /*
         * Alright, so we removed old elements, but we're still very suspicious. For example,
         * let's say the user edited the form (by removing an element), didn't re-verify any teams (by not opening them),
         * and then added a new element in the position of the old one. What will happen is that the above method will
         * NOT remove the old metric instance, and instead below the metrics name will not get updated. So, here we will
         * make sure that both the ID and INSTANCE TYPE of each metric (in the form and team) match, otherwise, the metric
         * gets booted.
         */
    temp = form.getPit();
    for (int i = 0; i < tabs.size(); i++) {
        if (!tabs.get(i).getTitle().equalsIgnoreCase("Pit"))
            temp = form.getMatch();
        for (int j = 0; j < temp.size(); j++) {
            for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
                if (tabs.get(i).getMetrics().get(k).getID() == temp.get(j).getID()) {
                    if (!temp.get(j).getClass().equals(tabs.get(i).getMetrics().get(k).getClass())) {
                        tabs.get(i).getMetrics().remove(k);
                        j = 0;
                        k = 0;
                    }
                }
            }
        }
    }
    // Add elements that are on the form, but not in this team
    temp = form.getPit();
    for (int i = 0; i < tabs.size(); i++) {
        if (!tabs.get(i).getTitle().equalsIgnoreCase("Pit"))
            temp = form.getMatch();
        for (int j = 0; j < temp.size(); j++) {
            boolean found = false;
            if (tabs.get(i).getMetrics().size() == 0) {
                tabs.get(i).getMetrics().add(temp.get(j).clone());
                continue;
            }
            for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
                if (tabs.get(i).getMetrics().get(k).getID() == temp.get(j).getID())
                    found = true;
                if (k == tabs.get(i).getMetrics().size() - 1 && !found) {
                    tabs.get(i).getMetrics().add(temp.get(j).clone());
                    j = 0;
                    break;
                }
            }
        }
    }
    // Update item names
    temp = form.getPit();
    for (int i = 0; i < tabs.size(); i++) {
        if (!tabs.get(i).getTitle().equalsIgnoreCase("PIT"))
            temp = form.getMatch();
        for (int j = 0; j < temp.size(); j++) {
            for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
                if (temp.get(j).getID() == tabs.get(i).getMetrics().get(k).getID()) {
                    tabs.get(i).getMetrics().get(k).setTitle(temp.get(j).getTitle());
                    break;
                }
            }
        }
    }
    // Update default values for non-modified values, also check for some weird scenario
    temp = form.getPit();
    for (int i = 0; i < tabs.size(); i++) {
        if (!tabs.get(i).getTitle().equalsIgnoreCase("PIT"))
            temp = form.getMatch();
        for (int j = 0; j < temp.size(); j++) {
            for (int k = 0; k < tabs.get(i).getMetrics().size(); k++) {
                if (temp.get(j).getID() == tabs.get(i).getMetrics().get(k).getID()) {
                    RMetric e = temp.get(j);
                    RMetric s = tabs.get(i).getMetrics().get(k);
                    if (e instanceof RBoolean && !s.isModified() && s instanceof RBoolean)
                        ((RBoolean) s).setValue(((RBoolean) e).isValue());
                    else if (e instanceof RCounter && !s.isModified() && s instanceof RCounter) {
                        ((RCounter) s).setValue(((RCounter) e).getValue());
                    } else if (e instanceof RCalculation && s instanceof RCalculation) {
                        ((RCalculation) s).setCalculation(((RCalculation) e).getCalculation());
                    } else if (e instanceof RCheckbox && s instanceof RCheckbox) {
                        // update the titles always, rely on position for this one
                        LinkedHashMap<String, Boolean> newHash = new LinkedHashMap<>();
                        // get old values
                        ArrayList<Boolean> values = new ArrayList<>();
                        // put values from team checkbox into this array
                        for (String oKey : ((RCheckbox) s).getValues().keySet()) {
                            values.add(((RCheckbox) s).getValues().get(oKey));
                        }
                        // copy values to new linked hash map, replacing the new keys
                        int index = 0;
                        for (String oKey : ((RCheckbox) e).getValues().keySet()) {
                            newHash.put(oKey, values.get(index));
                            index++;
                        }
                        // set new array back to team metric
                        ((RCheckbox) s).setValues(newHash);
                        // if the checkbox is not modified, reset the boolean values
                        if (!s.isModified()) {
                            for (String key : ((RCheckbox) e).getValues().keySet()) ((RCheckbox) s).getValues().put(key, ((RCheckbox) e).getValues().get(key));
                        }
                    } else // if one line is true, it means its the team name or number metric and its value shouldn't be overrided
                    if (e instanceof RTextfield && !s.isModified() && !((RTextfield) e).isOneLine())
                        ((RTextfield) s).setText(((RTextfield) e).getText());
                    else if (e instanceof RChooser && s instanceof RChooser) {
                        // Always update the title
                        if (!Arrays.equals(((RChooser) s).getValues(), ((RChooser) e).getValues())) {
                            ((RChooser) s).setValues(((RChooser) e).getValues());
                        }
                        // if the chooser is not modified, reset the chooser values
                        if (!s.isModified())
                            ((RChooser) s).setSelectedIndex(((RChooser) e).getSelectedIndex());
                    } else if (e instanceof RStopwatch && !s.isModified() && s instanceof RStopwatch)
                        ((RStopwatch) s).setTime(((RStopwatch) e).getTime());
                    else if (e instanceof RSlider && !s.isModified() && s instanceof RSlider) {
                        ((RSlider) s).setMax(((RSlider) e).getMax());
                        ((RSlider) s).setMin(((RSlider) e).getMin());
                        ((RSlider) s).setValue(((RSlider) e).getValue());
                    } else if (e instanceof RCounter && s instanceof RCounter) {
                        ((RCounter) s).setIncrement(((RCounter) e).getIncrement());
                        ((RCounter) s).setVerboseInput(((RCounter) e).isVerboseInput());
                        if (!s.isModified())
                            ((RCounter) s).setValue(((RCounter) e).getValue());
                    }
                    break;
                }
            }
        }
    }
}
Also used : RBoolean(com.cpjd.roblu.models.metrics.RBoolean) RChooser(com.cpjd.roblu.models.metrics.RChooser) RTextfield(com.cpjd.roblu.models.metrics.RTextfield) ArrayList(java.util.ArrayList) RMetric(com.cpjd.roblu.models.metrics.RMetric) RCalculation(com.cpjd.roblu.models.metrics.RCalculation) LinkedHashMap(java.util.LinkedHashMap) RStopwatch(com.cpjd.roblu.models.metrics.RStopwatch) RCheckbox(com.cpjd.roblu.models.metrics.RCheckbox) RSlider(com.cpjd.roblu.models.metrics.RSlider) RCounter(com.cpjd.roblu.models.metrics.RCounter)

Aggregations

RCalculation (com.cpjd.roblu.models.metrics.RCalculation)9 RCheckbox (com.cpjd.roblu.models.metrics.RCheckbox)7 RChooser (com.cpjd.roblu.models.metrics.RChooser)7 RCounter (com.cpjd.roblu.models.metrics.RCounter)7 RSlider (com.cpjd.roblu.models.metrics.RSlider)7 RStopwatch (com.cpjd.roblu.models.metrics.RStopwatch)7 RMetric (com.cpjd.roblu.models.metrics.RMetric)6 RBoolean (com.cpjd.roblu.models.metrics.RBoolean)5 RFieldData (com.cpjd.roblu.models.metrics.RFieldData)5 RGallery (com.cpjd.roblu.models.metrics.RGallery)5 RTextfield (com.cpjd.roblu.models.metrics.RTextfield)4 ArrayList (java.util.ArrayList)4 RDivider (com.cpjd.roblu.models.metrics.RDivider)3 LinkedHashMap (java.util.LinkedHashMap)3 TextInputLayout (android.support.design.widget.TextInputLayout)2 View (android.view.View)2 RelativeLayout (android.widget.RelativeLayout)2 TextView (android.widget.TextView)2 IO (com.cpjd.roblu.io.IO)2 RTab (com.cpjd.roblu.models.RTab)2