Skip to content

Commit

Permalink
Eases delay calculation. Removes variable delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trundicho committed Oct 14, 2018
1 parent d59de55 commit 846a13d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ public PlayState getPlayState() {

@Override
public void setPlayState(PlayState playState) {
PlayState old = this.playState;
this.playState = playState;
for (Listener listener : listeners) {
listener.playStateChanged(playState);
if (!old.equals(playState)) {
for (Listener listener : listeners) {
listener.playStateChanged(playState);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,13 @@
public final class SpeedCalculator {

private final DelayModel speedModel;
private final SpeedWeightModel speedWeightModel;

public SpeedCalculator(DelayModel speedModel, SpeedWeightModel speedWeightModel) {
this.speedModel = speedModel;
this.speedWeightModel = speedWeightModel;
}

public int computeNextSpeed(final String nextWord) {
final double defaultSpeed = speedModel.getDefaultDelay();

double nextSpeed = 1;
final double sentenceSeparation = speedWeightModel.getPunctuationMarkSpeedWeight();
if (nextWord.contains(".") || nextWord.contains("!") || nextWord.contains("?")) {
nextSpeed = sentenceSeparation;
} else if (nextWord.contains(",")) {
nextSpeed = sentenceSeparation;
}

final int length = nextWord.length();
final double wordLengthSpeedWeight = speedWeightModel.getWordLengthSpeedWeight();
nextSpeed = nextSpeed + (length * wordLengthSpeedWeight - length);
final int newSpeed = new Double(defaultSpeed * nextSpeed).intValue();
return newSpeed;
final double defaultDelay = speedModel.getDefaultDelay();
return Double.valueOf(defaultDelay).intValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,58 @@
* Created by angeloromito on 11.04.14.
*/
public final class DelayModelImpl implements DelayModel {
private final Collection<SpeedUpdateListener> listeners;
private double defaultDelay;
private double delay;
private final Collection<SpeedUpdateListener> listeners;
private double defaultDelay;
private double delay;

public DelayModelImpl(final double defaultDelay) {
this.delay = defaultDelay;
this.defaultDelay = defaultDelay;
listeners = new HashSet<SpeedUpdateListener>();
}
public DelayModelImpl(final double defaultDelay) {
this.delay = defaultDelay;
this.defaultDelay = defaultDelay;
listeners = new HashSet<SpeedUpdateListener>();
}

@Override
public double getVariableDelay() {
return delay;
}
@Override
public double getVariableDelay() {
return delay;
}

@Override
public double getDefaultDelay() {
return defaultDelay;
}
@Override
public double getDefaultDelay() {
return defaultDelay;
}

@Override
public void setVariableDelay(double speed) {
if (this.delay != speed) {
double oldSpeed = this.delay;
this.delay = speed;
for (SpeedUpdateListener listener : listeners) {
listener.speedChanged(oldSpeed, speed);
}
}
}
@Override
public void setVariableDelay(double speed) {
double oldSpeed = this.delay;
this.delay = speed;
for (SpeedUpdateListener listener : listeners) {
listener.speedChanged(oldSpeed, speed);
}
}

@Override
public void setDefaultDelay(double speed) {
double oldDefaultSpeed = defaultDelay;
double oldSpeed = delay;
this.defaultDelay = speed;
for (SpeedUpdateListener listener : listeners) {
listener.defaultSpeedChanged(oldDefaultSpeed, defaultDelay);
listener.speedChanged(oldSpeed, speed);
}
}
@Override
public void setDefaultDelay(double speed) {
double oldDefaultSpeed = defaultDelay;
double oldSpeed = delay;
this.defaultDelay = speed;
for (SpeedUpdateListener listener : listeners) {
listener.defaultSpeedChanged(oldDefaultSpeed, defaultDelay);
listener.speedChanged(oldSpeed, speed);
}
}

@Override
public void addListener(SpeedUpdateListener listener) {
listeners.add(listener);
}
@Override
public void addListener(SpeedUpdateListener listener) {
listeners.add(listener);
}

@Override
public void removeListener(SpeedUpdateListener listener) {
listeners.remove(listener);
}
@Override
public void removeListener(SpeedUpdateListener listener) {
listeners.remove(listener);
}

@Override
public void dispose() {
listeners.clear();
}
@Override
public void dispose() {
listeners.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import de.trundicho.warp.reader.core.model.playmode.impl.PlayModel;
import de.trundicho.warp.reader.core.model.speed.DelayModel;
import de.trundicho.warp.reader.core.model.speed.SpeedWeightModel;
import de.trundicho.warp.reader.core.model.speed.WpmSpeedExchanger;
import de.trundicho.warp.reader.core.model.speed.impl.DelayModelImpl;
import de.trundicho.warp.reader.core.model.speed.impl.SpeedWeightModelImpl;
import de.trundicho.warp.reader.core.model.speed.impl.WpmSpeedExchangerImpl;
import de.trundicho.warp.reader.core.model.warpword.TextSplitter;
import de.trundicho.warp.reader.core.model.warpword.impl.WordLengthModelImpl;
import de.trundicho.warp.reader.core.view.api.timer.WarpTimerFactory;
Expand All @@ -21,19 +23,21 @@

public class WarpInitializerTest {
private static final int DEFAULT_NUMBER_OF_CHARS_TO_DISPLAY = 15;
private static final int DEFAULT_DELAY = 50;
private static final int UPDATES_PER_MINUTE = 250;
private WarpInitializer warpInitializer;
private WarpTextWidgetForTest warpTextLabelUpdater;
private static final int TEST_WARP_DURATION_IN_MS = 500;
private static final int TEST_WARP_DURATION_IN_MS = 3500;
private PlayModel playModel;
private PlayModeModel playModeModel;
private static final String INITIAL_TEXT = "WarpReader Copy and paste your text or URL into the text field and start reading. As you get faster just increase the tempo as much as you like. Have fun reading at warp speed with WarpReader";

@BeforeEach
void setUp() {
this.playModel = new PlayModel();
DelayModel speedModel = new DelayModelImpl(DEFAULT_DELAY);
playModeModel = new PlayModeModelImpl(PlayState.PLAYING);
WpmSpeedExchanger wpmSpeedExchanger = new WpmSpeedExchangerImpl();
double DELAY = wpmSpeedExchanger.exchangeToSpeed(UPDATES_PER_MINUTE);
DelayModel speedModel = new DelayModelImpl(DELAY);
playModeModel = new PlayModeModelImpl(PlayState.PAUSE);
SpeedWeightModel speedWeightModel = new SpeedWeightModelImpl();
TextSplitter textSplitter = new TextSplitter(new WordLengthModelImpl(DEFAULT_NUMBER_OF_CHARS_TO_DISPLAY));
warpTextLabelUpdater = new WarpTextWidgetForTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class WarpTextWidgetForTest implements WarpTextWidget {

@Override
public void setWarpText(String leftPart, String centerPart, String rightPart) {
warpedTextLines.add(leftPart + centerPart + rightPart);
final String text = leftPart + centerPart + rightPart;
warpedTextLines.add(text);
System.err.println(text);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import de.trundicho.warp.reader.core.controller.WarpUpdater;
import de.trundicho.warp.reader.core.view.api.timer.WarpTimer;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.Timer;
import java.util.TimerTask;

class WarpTimerForTest implements WarpTimer {
private final WarpUpdater warpUpdater;
private final ScheduledExecutorService scheduledExecutorService;
private ScheduledFuture<?> schedule;
private Timer timer;
private final Runnable runnable;

WarpTimerForTest(WarpUpdater warpUpdater) {
this.warpUpdater = warpUpdater;
scheduledExecutorService = Executors.newScheduledThreadPool(1);
timer = new Timer("Timer");
runnable = new Runnable() {

@Override
public void run() {
warpUpdater.doNextWarp();
}
};

}

@Override
Expand All @@ -25,17 +29,17 @@ public void doNextWarp(WarpUpdater warpUpdater) {

@Override
public void cancel() {
schedule.cancel(false);
timer.cancel();
}

@Override
public void scheduleRepeating(int periodMillis) {
Runnable task = new Runnable() {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
doNextWarp(warpUpdater);
runnable.run();
}
};
schedule = scheduledExecutorService.schedule(task, periodMillis, TimeUnit.MILLISECONDS);
timer.schedule(timerTask, periodMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
public class WarpReaderWeb implements EntryPoint {

private static final int DEFAULT_NUMBER_OF_CHARS_TO_DISPLAY = 15;
private static final int DEFAULT_WORDS_PER_MINUTE = 240;
private static final int DEFAULT_WORDS_PER_MINUTE = 200;
private static final int TEXT_AREA_PARSER_DELAY = 500;

public void onModuleLoad() {
PlayModeModel playModeModel = new PlayModeModelImpl(PlayState.PLAYING);
PlayModeModel playModeModel = new PlayModeModelImpl(PlayState.PAUSE);
WordLengthModelMutable wordLengthModel = new WordLengthModelImpl(DEFAULT_NUMBER_OF_CHARS_TO_DISPLAY);
TextSplitter textSplitter = new TextSplitter(wordLengthModel);
SpeedWeightModel speedWeightModel = new SpeedWeightModelImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public class WarpReaderView extends UI {

private static final int DEFAULT_NUMBER_OF_CHARS_TO_DISPLAY = 15;
private static final int DEFAULT_WORDS_PER_MINUTE = 260;
private static final int DEFAULT_WORDS_PER_MINUTE = 200;
private static final int TEXT_AREA_PARSER_DELAY = 500;
private I18nLocalizer i18nLocalizer;
private Disposer disposer;
Expand All @@ -51,7 +51,7 @@ public class WarpReaderView extends UI {
protected void init(VaadinRequest request) {
disposer = new Disposer();
i18nLocalizer = new I18nLocalizer(Locale.ENGLISH);
PlayModeModel playModeModel = new PlayModeModelImpl(PlayState.PLAYING);
PlayModeModel playModeModel = new PlayModeModelImpl(PlayState.PAUSE);
WordLengthModelMutable wordLengthModel = new WordLengthModelImpl(DEFAULT_NUMBER_OF_CHARS_TO_DISPLAY);
TextSplitter textSplitter = new TextSplitter(wordLengthModel);
SpeedWeightModel speedWeightModel = new SpeedWeightModelImpl();
Expand Down

0 comments on commit 846a13d

Please sign in to comment.