use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.
the class PlayerView method revertChange.
public void revertChange(RecordedChange recordedChange) {
synchronized (lock) {
switch(recordedChange.getType()) {
case AddNew:
playerScore.getHistory().remove(playerScore.getHistory().size() - 1);
playerScore.setScore(playerScore.getScore() - recordedChange.getDelta().getValue());
break;
case DeleteLast:
case DeleteLastZero:
playerScore.getHistory().add(recordedChange.getDelta());
playerScore.setScore(playerScore.getScore() + recordedChange.getDelta().getValue());
break;
case ModifyLast:
default:
int lastIdx = playerScore.getHistory().size() - 1;
Delta lastDelta = playerScore.getHistory().get(lastIdx);
lastDelta.setValue(lastDelta.getValue() - recordedChange.getDelta().getValue());
playerScore.setScore(playerScore.getScore() - recordedChange.getDelta().getValue());
break;
}
}
}
use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.
the class PlayerView method historyHash.
/**
* Do a quick hash of the showable history, as an optimization to check to
* see if we need to redraw the history or not
*
* @param history
* @param currentTime
* @return
*/
private int historyHash(List<Delta> history, long currentTime) {
List<Delta> historyToShow = historyToShow(history, currentTime);
// hash code implementation copied from java.util.Arrays (just the values, not the timestamps)
if (historyToShow.isEmpty()) {
return 0;
}
int result = 1;
for (Delta delta : historyToShow) {
result = 31 * result + delta.getValue();
}
return result;
}
use of com.nolanlawson.keepscore.db.Delta 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.db.Delta in project KeepScore by nolanlawson.
the class PlayerView method historyToSpan.
private Function<Delta, Spannable> historyToSpan(final int maxChars) {
Function<Delta, Spannable> function = historyToSpanLookup.get(maxChars);
if (function == null) {
function = new Function<Delta, Spannable>() {
@Override
public Spannable apply(Delta delta) {
int value = delta.getValue();
int colorResId = (value >= 0) ? positiveTextColor : negativeTextColor;
ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(colorResId));
CharSequence str = IntegerUtil.toCharSequenceWithSign(value);
str = StringUtil.padLeft(str, ' ', maxChars);
Spannable spannable = new SpannableString(str);
SpannableUtil.setWholeSpan(spannable, colorSpan);
return spannable;
}
};
historyToSpanLookup.put(maxChars, function);
}
return function;
}
use of com.nolanlawson.keepscore.db.Delta 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