Search in sources :

Example 11 with Delta

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;
        }
    }
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta)

Example 12 with Delta

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;
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta)

Example 13 with Delta

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();
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta) RecordedChange(com.nolanlawson.keepscore.data.RecordedChange)

Example 14 with Delta

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;
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Delta(com.nolanlawson.keepscore.db.Delta) Spannable(android.text.Spannable)

Example 15 with Delta

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());
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta) RecordedChange(com.nolanlawson.keepscore.data.RecordedChange)

Aggregations

Delta (com.nolanlawson.keepscore.db.Delta)15 PlayerScore (com.nolanlawson.keepscore.db.PlayerScore)5 RecordedChange (com.nolanlawson.keepscore.data.RecordedChange)3 ArrayList (java.util.ArrayList)3 Spannable (android.text.Spannable)2 Game (com.nolanlawson.keepscore.db.Game)2 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 SparseArray (android.util.SparseArray)1 Button (android.widget.Button)1 GameDBHelper (com.nolanlawson.keepscore.db.GameDBHelper)1 LineChartLine (com.nolanlawson.keepscore.widget.chart.LineChartLine)1