use of com.nolanlawson.keepscore.data.RecordedChange in project KeepScore by nolanlawson.
the class PlayerView method addZero.
private void addZero() {
// with no points for a particular player
synchronized (lock) {
Delta delta = new Delta(System.currentTimeMillis(), 0);
changeRecorder.onCallback(new RecordedChange(playerScore.getPlayerNumber(), Type.AddNew, delta));
playerScore.getHistory().add(delta);
}
// reset last incremented
lastIncremented.set(0);
shouldAutosave.set(true);
updateViews();
}
use of com.nolanlawson.keepscore.data.RecordedChange in project KeepScore by nolanlawson.
the class GameActivity method undoOrRedo.
private void undoOrRedo(boolean undo) {
DataExpiringStack<RecordedChange> stackToPoll = undo ? undoStack : redoStack;
DataExpiringStack<RecordedChange> stackToPop = !undo ? undoStack : redoStack;
RecordedChange recordedChange = null;
int lastPlayerNumber = -1;
RecordedChange.Type lastType = null;
while ((recordedChange = stackToPoll.peek()) != null && (lastPlayerNumber == -1 || lastPlayerNumber == recordedChange.getPlayerNumber()) && // only apply multiple changes to the same PlayerScore
(lastType == null || isAcceptableUndoOrRedoTransition(undo, lastType, recordedChange.getType()))) {
recordedChange = stackToPoll.poll();
PlayerView playerView = playerViews.get(recordedChange.getPlayerNumber());
if (undo) {
playerView.revertChange(recordedChange);
} else {
playerView.reexecuteChange(recordedChange);
}
stackToPop.pop(recordedChange);
lastPlayerNumber = recordedChange.getPlayerNumber();
lastType = recordedChange.getType();
}
if (lastPlayerNumber != -1) {
PlayerView playerView = playerViews.get(lastPlayerNumber);
// keeps the badge from showing
playerView.resetLastIncremented();
playerView.updateViews();
}
}
use of com.nolanlawson.keepscore.data.RecordedChange in project KeepScore by nolanlawson.
the class GameActivity method setUpWidgets.
private void setUpWidgets() {
rootLayout = (LinearLayout) findViewById(R.id.game_root_layout);
// add top and bottom spacing on the two-player game. it looks nicer
rootPadding1 = findViewById(R.id.game_root_padding_1);
rootPadding2 = findViewById(R.id.game_root_padding_2);
rootPadding1.setVisibility(playerScores.size() <= 2 ? View.VISIBLE : View.GONE);
rootPadding2.setVisibility(playerScores.size() <= 2 ? View.VISIBLE : View.GONE);
// if the round totals are enabled
try {
roundTotalViewStub = (ViewStub) findViewById(R.id.round_totals);
int versionInt = VersionHelper.getVersionSdkIntCompat();
if (versionInt > VersionHelper.VERSION_DONUT && versionInt < VersionHelper.VERSION_FROYO) {
roundTotalTextView = (TextView) roundTotalViewStub.inflate();
}
} catch (ClassCastException ignore) {
// view stub already inflated
}
playerViews = new ArrayList<PlayerView>();
boolean showOnscreenDeltaButtons = getShouldShowOnscreenDeltaButtons();
for (int i = 0; i < playerScores.size(); i++) {
PlayerScore playerScore = playerScores.get(i);
int resId = getPlayerViewResId(i);
View view = getPlayerScoreView(resId);
if (view == null) {
log.e("Null pointer exception; view for player %s is null", i);
}
PlayerView playerView = new PlayerView(this, view, playerScore, handler, showOnscreenDeltaButtons);
playerView.setChangeRecorder(new Callback<RecordedChange>() {
@Override
public void onCallback(RecordedChange recordedChange) {
undoStack.pop(recordedChange);
redoStack.clear();
}
});
playerView.setOnChangeListener(new Runnable() {
@Override
public void run() {
updateRoundTotalViewText();
updateHighlightedPlayer();
}
});
// worth saving. This only applies for newly created games.
if (game.getId() == -1 && !TextUtils.isEmpty(playerScore.getName())) {
playerView.getShouldAutosave().set(true);
}
playerViews.add(playerView);
}
hideAbsentPlayers();
}
use of com.nolanlawson.keepscore.data.RecordedChange in project KeepScore by nolanlawson.
the class PlayerView method deleteLast.
private void deleteLast() {
synchronized (lock) {
List<Delta> history = playerScore.getHistory();
// undo the last history items
if (history != null && !history.isEmpty()) {
Delta removed = history.remove((int) (history.size() - 1));
playerScore.setScore(playerScore.getScore() - removed.getValue());
changeRecorder.onCallback(new RecordedChange(playerScore.getPlayerNumber(), Type.DeleteLast, removed));
}
}
// reset lastIncremented
lastIncremented.set(0);
shouldAutosave.set(true);
updateViews();
}
use of com.nolanlawson.keepscore.data.RecordedChange in project KeepScore by nolanlawson.
the class PlayerView method incrementInBackground.
private void incrementInBackground(int value) {
log.d("incrementInBackground()");
long currentTime = System.currentTimeMillis();
long lastIncrementedTime = lastIncremented.getAndSet(currentTime);
if (currentTime - lastIncrementedTime > getUpdateDelayInMs() || playerScore.getHistory().isEmpty()) {
log.d("it's been awhile");
Delta delta = new Delta(currentTime, value);
// if it's been awhile since the last time we incremented
changeRecorder.onCallback(new RecordedChange(playerScore.getPlayerNumber(), Type.AddNew, delta));
playerScore.getHistory().add(delta);
} else {
log.d("it hasn't been awhile");
// else just update the most recent history item
int lastIndex = playerScore.getHistory().size() - 1;
int newValue = playerScore.getHistory().get(lastIndex).getValue() + value;
if (newValue == 0) {
// don't add "0" to the list; just delete the
// last history item
Delta deletedDelta = playerScore.getHistory().remove(lastIndex);
changeRecorder.onCallback(new RecordedChange(playerScore.getPlayerNumber(), Type.DeleteLastZero, deletedDelta));
// reset the lastIncremented time so we
lastIncremented.set(0);
// don't update the
// previous value later
} else {
playerScore.getHistory().set(lastIndex, new Delta(currentTime, newValue));
changeRecorder.onCallback(new RecordedChange(playerScore.getPlayerNumber(), Type.ModifyLast, new Delta(currentTime, value)));
}
}
playerScore.setScore(playerScore.getScore() + value);
shouldAutosave.set(true);
// this runnable updates the history after 10 seconds and makes the
// blibbet disappear
createDelayedHistoryUpdateTask();
// this runnable updates the history text view and the total score text
// view
handler.post(getUpdateViewsRunnable());
}
Aggregations