2018-01-20 13:03:23 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-01-21 11:35:03 +01:00
|
|
|
type TaskRunner struct {
|
2018-01-27 16:58:56 +01:00
|
|
|
count int
|
|
|
|
taskID int
|
|
|
|
taskMessage string
|
|
|
|
nPomodoros int
|
|
|
|
origDuration time.Duration
|
|
|
|
state State
|
|
|
|
store *Store
|
|
|
|
started time.Time
|
|
|
|
pause chan bool
|
|
|
|
toggle chan bool
|
|
|
|
notifier Notifier
|
|
|
|
duration time.Duration
|
2018-01-21 11:35:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 17:42:13 +01:00
|
|
|
func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) {
|
2018-01-21 11:35:03 +01:00
|
|
|
taskID, err := store.CreateTask(*task)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr := &TaskRunner{
|
2018-01-27 16:58:56 +01:00
|
|
|
taskID: taskID,
|
|
|
|
taskMessage: task.Message,
|
|
|
|
nPomodoros: task.NPomodoros,
|
|
|
|
origDuration: task.Duration,
|
|
|
|
store: store,
|
|
|
|
state: State(0),
|
|
|
|
pause: make(chan bool),
|
|
|
|
toggle: make(chan bool),
|
2018-01-27 17:42:13 +01:00
|
|
|
notifier: notifier,
|
2018-01-27 16:58:56 +01:00
|
|
|
duration: task.Duration,
|
2018-01-21 11:35:03 +01:00
|
|
|
}
|
|
|
|
return tr, nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:07:38 +01:00
|
|
|
func (t *TaskRunner) Start() {
|
|
|
|
go t.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskRunner) TimeRemaining() time.Duration {
|
|
|
|
return (t.duration - time.Since(t.started)).Truncate(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskRunner) run() error {
|
2018-01-27 16:58:56 +01:00
|
|
|
for t.count < t.nPomodoros {
|
2018-01-26 16:07:38 +01:00
|
|
|
// Create a new pomodoro where we
|
|
|
|
// track the start / end time of
|
|
|
|
// of this session.
|
|
|
|
pomodoro := &Pomodoro{}
|
|
|
|
// Start this pomodoro
|
|
|
|
pomodoro.Start = time.Now()
|
|
|
|
// Set state to RUNNING
|
|
|
|
t.state = RUNNING
|
|
|
|
// Create a new timer
|
|
|
|
timer := time.NewTimer(t.duration)
|
|
|
|
// Record our started time
|
|
|
|
t.started = pomodoro.Start
|
|
|
|
loop:
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
t.state = BREAKING
|
2018-01-27 16:58:56 +01:00
|
|
|
t.count++
|
2018-01-26 16:07:38 +01:00
|
|
|
case <-t.toggle:
|
|
|
|
// Catch any toggles when we
|
|
|
|
// are not expecting them
|
|
|
|
goto loop
|
|
|
|
case <-t.pause:
|
|
|
|
timer.Stop()
|
2018-01-27 16:58:56 +01:00
|
|
|
// Record the remaining time of the current pomodoro
|
2018-01-26 16:07:38 +01:00
|
|
|
remaining := t.TimeRemaining()
|
|
|
|
// Change state to PAUSED
|
|
|
|
t.state = PAUSED
|
|
|
|
// Wait for the user to press [p]
|
|
|
|
<-t.pause
|
2018-01-27 16:58:56 +01:00
|
|
|
// Resume the timer with previous
|
2018-01-26 16:07:38 +01:00
|
|
|
// remaining time
|
|
|
|
timer.Reset(remaining)
|
2018-01-27 16:58:56 +01:00
|
|
|
// Change duration
|
2018-01-26 16:07:38 +01:00
|
|
|
t.started = time.Now()
|
|
|
|
t.duration = remaining
|
|
|
|
// Restore state to RUNNING
|
|
|
|
t.state = RUNNING
|
|
|
|
goto loop
|
|
|
|
}
|
|
|
|
pomodoro.End = time.Now()
|
2018-01-27 16:58:56 +01:00
|
|
|
err := t.store.CreatePomodoro(t.taskID, *pomodoro)
|
2018-01-26 16:07:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-27 16:58:56 +01:00
|
|
|
// All pomodoros completed
|
|
|
|
if t.count == t.nPomodoros {
|
|
|
|
break
|
|
|
|
}
|
2018-01-27 17:42:13 +01:00
|
|
|
|
|
|
|
t.notifier.Notify("Pomo", "It is time to take a break!")
|
2018-01-27 16:58:56 +01:00
|
|
|
// Reset the duration incase it
|
|
|
|
// was paused.
|
|
|
|
t.duration = t.origDuration
|
|
|
|
// User concludes the break
|
|
|
|
<-t.toggle
|
|
|
|
|
2018-01-26 16:07:38 +01:00
|
|
|
}
|
|
|
|
t.state = COMPLETE
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskRunner) Toggle() {
|
|
|
|
t.toggle <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskRunner) Pause() {
|
|
|
|
t.pause <- true
|
|
|
|
}
|