Sveglia app is composed by two main model classes:
TimerTemplate, this post will explain some implementations details of
Timer class models a countdown timer. Countdown timers can be created, paused, resumed and destroyed.
To create a timer the minimal information needed is countdown target time: the precise time at which timer will end. This information is stored in
A mandatory requirement: timers must accumulate pause time durations to track the total pause time duration. Here are a couple ways to handle this task.
Using an accumulator:
paused_for. Its initial value is set to
nil and it's updated at every resume event adding the result of
paused_at to current value.
origin_target_at is set at creation time with original
target_at attribute's value. At the end of timer total pause time duration is the result of
This way to handle pause time durations accumulation is more efficient because it doesn't involve the n operations to add each ith pause duration used by
To summarize a timer's life cycle below are described steps involving in each timer event: creation, pause, resume and end.
When you create a new timer:
origin_target_atare set to timer's end time,
created_atis set to
paused_atis set to
When you pause a running timer:
paused_atattribute is updated with
When you resume a paused timer:
paused_atattribute is set to
target_atattribute is updated with result of
When a timer ends:
- total pause time duration dynamic attribute is the result of