Implemented secondary buttons
This commit is contained in:
parent
ac56357b4b
commit
180a1c0af8
@ -98,8 +98,9 @@ public abstract class Timer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addOneMinute() {
|
public void addOneMinute() {
|
||||||
if (!isRunning())
|
// Allow extending even if paused.
|
||||||
throw new IllegalStateException("Cannot extend a timer that is not running");
|
// if (!isRunning())
|
||||||
|
// throw new IllegalStateException("Cannot extend a timer that is not running");
|
||||||
if (expired()) {
|
if (expired()) {
|
||||||
endTime = SystemClock.elapsedRealtime() + MINUTE;
|
endTime = SystemClock.elapsedRealtime() + MINUTE;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
package com.philliphsu.clock2.timers;
|
package com.philliphsu.clock2.timers;
|
||||||
|
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ImageButton;
|
||||||
|
|
||||||
import com.philliphsu.clock2.Timer;
|
import com.philliphsu.clock2.Timer;
|
||||||
|
|
||||||
@ -10,32 +12,45 @@ import com.philliphsu.clock2.Timer;
|
|||||||
public class TimerController {
|
public class TimerController {
|
||||||
private final Timer mTimer;
|
private final Timer mTimer;
|
||||||
private final CountdownChronometer mChronometer;
|
private final CountdownChronometer mChronometer;
|
||||||
|
private final ImageButton mAddOneMinute;
|
||||||
|
private final ImageButton mStartPause;
|
||||||
|
private final ImageButton mStop;
|
||||||
|
|
||||||
public TimerController(Timer timer, CountdownChronometer chronometer) {
|
public TimerController(Timer timer, CountdownChronometer chronometer, ImageButton addOneMinute,
|
||||||
|
ImageButton startPause, ImageButton stop) {
|
||||||
mTimer = timer;
|
mTimer = timer;
|
||||||
mChronometer = chronometer;
|
mChronometer = chronometer;
|
||||||
|
mAddOneMinute = addOneMinute;
|
||||||
|
mStartPause = startPause;
|
||||||
|
mStop = stop;
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
mChronometer.setBase(SystemClock.elapsedRealtime() + mTimer.duration());
|
mChronometer.setBase(SystemClock.elapsedRealtime() + mTimer.duration());
|
||||||
|
updateStartPauseIcon();
|
||||||
|
setSecondaryButtonsVisible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
mTimer.start();
|
mTimer.start();
|
||||||
mChronometer.setBase(mTimer.endTime());
|
mChronometer.setBase(mTimer.endTime());
|
||||||
mChronometer.start();
|
mChronometer.start();
|
||||||
|
updateStartPauseIcon();
|
||||||
|
setSecondaryButtonsVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
mTimer.pause();
|
mTimer.pause();
|
||||||
mChronometer.stop();
|
mChronometer.stop();
|
||||||
|
updateStartPauseIcon();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resume() {
|
public void resume() {
|
||||||
mTimer.resume();
|
mTimer.resume();
|
||||||
mChronometer.setBase(mTimer.endTime());
|
mChronometer.setBase(mTimer.endTime());
|
||||||
mChronometer.start();
|
mChronometer.start();
|
||||||
|
updateStartPauseIcon();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
@ -48,4 +63,15 @@ public class TimerController {
|
|||||||
mTimer.addOneMinute();
|
mTimer.addOneMinute();
|
||||||
mChronometer.setBase(mTimer.endTime());
|
mChronometer.setBase(mTimer.endTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateStartPauseIcon() {
|
||||||
|
// TODO: Pause and start icons, resp.
|
||||||
|
// mStartPause.setImageResource(mTimer.isRunning() ? 0 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecondaryButtonsVisible(boolean visible) {
|
||||||
|
int visibility = visible ? View.VISIBLE : View.INVISIBLE;
|
||||||
|
mAddOneMinute.setVisibility(visibility);
|
||||||
|
mStop.setVisibility(visibility);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,9 +37,10 @@ public class TimerViewHolder extends BaseViewHolder<Timer> {
|
|||||||
super.onBind(timer);
|
super.onBind(timer);
|
||||||
bindLabel(timer.label());
|
bindLabel(timer.label());
|
||||||
// We can't create the controller until this VH binds, because
|
// We can't create the controller until this VH binds, because
|
||||||
// the chronometer only exists after this point.
|
// the widgets only exist after this point.
|
||||||
mController = new TimerController(timer, mChronometer);
|
mController = new TimerController(timer, mChronometer, mAddOneMinute, mStartPause, mStop);
|
||||||
bindChronometer(timer);
|
bindChronometer(timer);
|
||||||
|
bindButtonControls(timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClick(R.id.start_pause)
|
@OnClick(R.id.start_pause)
|
||||||
@ -56,6 +57,16 @@ public class TimerViewHolder extends BaseViewHolder<Timer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.add_one_minute)
|
||||||
|
void addOneMinute() {
|
||||||
|
mController.addOneMinute();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClick(R.id.stop)
|
||||||
|
void stop() {
|
||||||
|
mController.stop();
|
||||||
|
}
|
||||||
|
|
||||||
private void bindLabel(String label) {
|
private void bindLabel(String label) {
|
||||||
if (!label.isEmpty()) {
|
if (!label.isEmpty()) {
|
||||||
mLabel.setText(label);
|
mLabel.setText(label);
|
||||||
@ -94,4 +105,9 @@ public class TimerViewHolder extends BaseViewHolder<Timer> {
|
|||||||
mChronometer.setDuration(timer.timeRemaining());
|
mChronometer.setDuration(timer.timeRemaining());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void bindButtonControls(Timer timer) {
|
||||||
|
mController.updateStartPauseIcon();
|
||||||
|
mController.setSecondaryButtonsVisible(timer.hasStarted());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user