This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

The counter data is new saved through the use of a static ArrayList

This is accessible from anywhere and will be where data to be saved is read from
  • Loading branch information...
Ryan Zoeller
Ryan Zoeller committed Oct 16, 2013
1 parent 8bdc751 commit 61c017b857b4914ccad96846a42b9d3d5a1f55f0
@@ -0,0 +1,33 @@
package com.kingtec2169.scouting;

import android.view.View;

import java.util.ArrayList;

/**
* Created by rtzoeller on 10/14/13.
*/
public class AutonomousData {
// Stores the references to the counters
public static ArrayList<Counter> goals;

public static void updateReferences(View layout) {
// Used to store data across fragments
// This method updates the references to the Views holding the data
// It should be called upon inflating a new layout in order to re-bind to the views
// State needs to be saved externally and then set to the refreshed views

// Wipe/Create a list of our views
if (goals == null) {
goals = new ArrayList<Counter>();
} else {
goals.clear();
}

// Populate the list with updated references
goals.add(((Counter)layout.findViewById(R.id.high_goal)));
goals.add(((Counter)layout.findViewById(R.id.medium_goal)));
goals.add(((Counter)layout.findViewById(R.id.low_goal)));
}

}
@@ -7,22 +7,41 @@
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
* Created by rtzoeller on 10/13/13.
*/
public class AutonomousFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_autonomous, container, false);

((Counter)layout.findViewById(R.id.high_goal)).setFloor(0);
((Counter)layout.findViewById(R.id.medium_goal)).setFloor(0);
((Counter)layout.findViewById(R.id.low_goal)).setFloor(0);
// List to contain the states of the counters that we might want to save
ArrayList<Counter.State> counterState = new ArrayList<Counter.State>();

if(AutonomousData.goals == null) {
// If there aren't references to any views (first run)
// then configure them
((Counter)layout.findViewById(R.id.high_goal)).setFloor(0);
((Counter)layout.findViewById(R.id.medium_goal)).setFloor(0);
((Counter)layout.findViewById(R.id.low_goal)).setFloor(0);
} else {
// If references exist, save their state
for (Counter counter : AutonomousData.goals) {
counterState.add(counter.getState());
}
}

// Refresh the references of the counters so that they match the current layout
// This is necessary so we can bind the saved state to the layout
AutonomousData.updateReferences(layout);

// Iterate through and set the state of each Counter to it's saved state
for (int i = 0; i < counterState.size() && i < AutonomousData.goals.size(); i++) {
AutonomousData.goals.get(i).setState(counterState.get(i));
}

return layout;
}
@@ -1,7 +1,6 @@
package com.kingtec2169.scouting;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
@@ -16,16 +15,16 @@
private Button button_subtract;
private Button button_add;
private TextView display;
private int step_size = 1;
private int count = 0;
private Integer floor = null;
private State state;

public Counter(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.counter, this);

state = new State();

button_subtract = (Button)findViewById(R.id.button_subtract);
button_add = (Button)findViewById(R.id.button_add);
display = (TextView)findViewById(R.id.display);
@@ -37,35 +36,54 @@ public Counter(Context context, AttributeSet attrs){
@Override
public void onClick(View view) {
if (view == button_subtract) {
if (floor == null || count > floor) {
count -= step_size;
// Make sure we don't go below the floor, if one exists
if (state.floor == null || state.count > state.floor) {
state.count -= state.step_size;
refresh();
}
} else if (view == button_add) {
count += step_size;
state.count += state.step_size;
refresh();
}
}

private void refresh() {
display.setText(Integer.toString(count));
// Update the display to show the new count
display.setText(Integer.toString(state.count));
}

public void setFloor(Integer x) {
// Set the floor
floor = x;
state.floor = x;
// Check if a floor exists and force our count to be above the floor if one does.
if (floor != null && count < floor) {
count = floor;
if (state.floor != null && state.count < state.floor) {
state.count = state.floor;
}
}

public int getCount() {
return count;
public State getState() {
return state;
}

public void setState(State state) {
// Set the state of the counter to the passed parameter, then
// refresh the display to match the new data
this.state = state;
refresh();
}

public void setCount(int x) {
count = x;
public class State {
private int count = 0;
private Integer floor = null;
private int step_size = 1;

public int save() {
// Return the data that we need to save for the match
// This isn't the entire state, it is only the count.
// We don't care about the step size used or the floor value
// when we are analyzing the match results, so we won't return it
return count;
}
}

}

0 comments on commit 61c017b

Please sign in to comment.