Filthy aphid
This commit is contained in:
72
ui/model.go
72
ui/model.go
@@ -25,6 +25,8 @@ const (
|
||||
type tickMsg time.Time
|
||||
type alarmFiredMsg scheduler.AlarmEvent
|
||||
type alarmsLoadedMsg []db.Alarm
|
||||
type snoozeFireMsg db.Alarm
|
||||
type autoTimeoutMsg struct{}
|
||||
|
||||
// Model is the main bubbletea model.
|
||||
type Model struct {
|
||||
@@ -41,8 +43,10 @@ type Model struct {
|
||||
now time.Time
|
||||
|
||||
// Alarm firing state
|
||||
firingAlarm *db.Alarm
|
||||
firingBlink bool
|
||||
firingAlarm *db.Alarm
|
||||
firingBlink bool
|
||||
firingStart time.Time
|
||||
snoozeCount int
|
||||
|
||||
// Form state
|
||||
form *formModel
|
||||
@@ -113,11 +117,27 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
case alarmFiredMsg:
|
||||
alarm := msg.Alarm
|
||||
m.firingAlarm = &alarm
|
||||
m.firingBlink = true
|
||||
m.player.Play(alarm.SoundPath)
|
||||
m.startFiring(&alarm)
|
||||
m.refreshAlarms()
|
||||
return m, listenForAlarms(m.scheduler)
|
||||
return m, tea.Batch(
|
||||
listenForAlarms(m.scheduler),
|
||||
autoTimeoutCmd(),
|
||||
)
|
||||
|
||||
case snoozeFireMsg:
|
||||
alarm := db.Alarm(msg)
|
||||
m.snoozeCount++
|
||||
m.startFiring(&alarm)
|
||||
return m, autoTimeoutCmd()
|
||||
|
||||
case autoTimeoutMsg:
|
||||
if m.firingAlarm != nil {
|
||||
m.player.Stop()
|
||||
m.statusMsg = "Alarm auto-dismissed after 5 minutes"
|
||||
m.firingAlarm = nil
|
||||
m.snoozeCount = 0
|
||||
}
|
||||
return m, nil
|
||||
|
||||
case tea.KeyMsg:
|
||||
return m.handleKey(msg)
|
||||
@@ -144,13 +164,16 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
switch key {
|
||||
case "enter", " ", "d":
|
||||
m.player.Stop()
|
||||
alarm := m.firingAlarm
|
||||
m.firingAlarm = nil
|
||||
m.statusMsg = "Alarm dismissed"
|
||||
m.snoozeCount = 0
|
||||
m.statusMsg = fmt.Sprintf("Alarm '%s' dismissed", alarm.Name)
|
||||
case "s":
|
||||
m.player.Stop()
|
||||
alarm := *m.firingAlarm
|
||||
m.firingAlarm = nil
|
||||
m.statusMsg = "Snoozed for 5 minutes (not yet implemented)"
|
||||
// TODO: implement snooze by creating a one-shot alarm 5min from now
|
||||
m.statusMsg = fmt.Sprintf("Snoozed '%s' for 5 minutes", alarm.Name)
|
||||
return m, snoozeCmd(alarm, snoozeDuration)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -224,6 +247,30 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
const snoozeDuration = 5 * time.Minute
|
||||
const autoTimeoutDuration = 5 * time.Minute
|
||||
|
||||
func (m *Model) startFiring(alarm *db.Alarm) {
|
||||
m.firingAlarm = alarm
|
||||
m.firingBlink = true
|
||||
m.firingStart = time.Now()
|
||||
m.player.PlayLoop(alarm.SoundPath)
|
||||
}
|
||||
|
||||
func snoozeCmd(alarm db.Alarm, after time.Duration) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
time.Sleep(after)
|
||||
return snoozeFireMsg(alarm)
|
||||
}
|
||||
}
|
||||
|
||||
func autoTimeoutCmd() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
time.Sleep(autoTimeoutDuration)
|
||||
return autoTimeoutMsg{}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) refreshAlarms() {
|
||||
alarms, err := m.store.ListAlarms()
|
||||
if err == nil {
|
||||
@@ -255,11 +302,14 @@ func (m Model) View() string {
|
||||
|
||||
// Firing alarm overlay
|
||||
if m.firingAlarm != nil {
|
||||
firingText := fmt.Sprintf("🔔 ALARM: %s 🔔", m.firingAlarm.Name)
|
||||
firingText := fmt.Sprintf("ALARM: %s", m.firingAlarm.Name)
|
||||
if m.firingAlarm.Description != "" {
|
||||
firingText += "\n" + m.firingAlarm.Description
|
||||
}
|
||||
firingText += "\n\n[Enter/Space/d] Dismiss [s] Snooze"
|
||||
if m.snoozeCount > 0 {
|
||||
firingText += fmt.Sprintf("\n(snoozed %d time(s))", m.snoozeCount)
|
||||
}
|
||||
firingText += "\n\n[Enter/Space/d] Dismiss [s] Snooze 5min"
|
||||
styled := AlarmFiringStyle.Render(firingText)
|
||||
styled = lipgloss.PlaceHorizontal(m.width, lipgloss.Center, styled)
|
||||
sections = append(sections, "", styled)
|
||||
|
||||
Reference in New Issue
Block a user