Expensive bee

This commit is contained in:
2026-02-03 16:49:05 +02:00
parent d3f29e3927
commit cdcdf2c644
10 changed files with 376 additions and 50 deletions

View File

@@ -29,12 +29,15 @@ type alarmsLoadedMsg []db.Alarm
type snoozeFireMsg db.Alarm
type autoTimeoutMsg struct{}
type AlarmsChangedMsg struct{}
type pollTickMsg struct{}
// Model is the main bubbletea model.
type Model struct {
store *db.Store
scheduler *scheduler.Scheduler
player *player.Player
store *db.Store // For settings (always local)
alarmStore db.AlarmStore // For alarm CRUD (local or remote)
scheduler *scheduler.Scheduler
player *player.Player
clientMode bool
// State
alarms []db.Alarm
@@ -63,13 +66,15 @@ type Model struct {
statusMsg string
}
func NewModel(store *db.Store, sched *scheduler.Scheduler, pl *player.Player) Model {
func NewModel(store *db.Store, alarmStore db.AlarmStore, sched *scheduler.Scheduler, pl *player.Player, clientMode bool) Model {
m := Model{
store: store,
scheduler: sched,
player: pl,
now: time.Now(),
settings: store.LoadSettings(),
store: store,
alarmStore: alarmStore,
scheduler: sched,
player: pl,
clientMode: clientMode,
now: time.Now(),
settings: store.LoadSettings(),
}
m.applySettings()
return m
@@ -107,11 +112,15 @@ func (m *Model) applySettings() {
}
func (m Model) Init() tea.Cmd {
return tea.Batch(
cmds := []tea.Cmd{
tick(),
listenForAlarms(m.scheduler),
loadAlarmsCmd(m.store),
)
loadAlarmsCmd(m.alarmStore),
}
if m.clientMode {
cmds = append(cmds, pollTick(m.settings.PollSeconds))
}
return tea.Batch(cmds...)
}
func tick() tea.Cmd {
@@ -127,7 +136,7 @@ func listenForAlarms(sched *scheduler.Scheduler) tea.Cmd {
}
}
func loadAlarmsCmd(store *db.Store) tea.Cmd {
func loadAlarmsCmd(store db.AlarmStore) tea.Cmd {
return func() tea.Msg {
alarms, err := store.ListAlarms()
if err != nil {
@@ -137,6 +146,12 @@ func loadAlarmsCmd(store *db.Store) tea.Cmd {
}
}
func pollTick(seconds int) tea.Cmd {
return tea.Tick(time.Duration(seconds)*time.Second, func(t time.Time) tea.Msg {
return pollTickMsg{}
})
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
@@ -154,6 +169,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.refreshAlarms()
return m, nil
case pollTickMsg:
m.refreshAlarms()
return m, pollTick(m.settings.PollSeconds)
case tickMsg:
m.now = time.Time(msg)
if m.firingAlarm != nil {
@@ -244,7 +263,7 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch key {
case "y", "Y":
if m.cursor < len(m.alarms) {
_ = m.store.DeleteAlarm(m.alarms[m.cursor].ID)
_ = m.alarmStore.DeleteAlarm(m.alarms[m.cursor].ID)
m.refreshAlarms()
if m.cursor >= len(m.alarms) && m.cursor > 0 {
m.cursor--
@@ -280,7 +299,7 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
case " ":
// Toggle enabled
if m.cursor < len(m.alarms) {
_ = m.store.ToggleAlarm(m.alarms[m.cursor].ID)
_ = m.alarmStore.ToggleAlarm(m.alarms[m.cursor].ID)
m.refreshAlarms()
}
case "a":
@@ -341,7 +360,7 @@ func autoTimeoutCmd(dur time.Duration) tea.Cmd {
}
func (m *Model) refreshAlarms() {
alarms, err := m.store.ListAlarms()
alarms, err := m.alarmStore.ListAlarms()
if err == nil {
m.alarms = alarms
}
@@ -372,6 +391,17 @@ func (m Model) View() string {
dateLine = lipgloss.PlaceHorizontal(m.width, lipgloss.Center, dateLine)
sections = append(sections, dateLine)
// Connection status
var connStatus string
if m.clientMode {
connStatus = fmt.Sprintf("[Client: %s]", m.settings.ServerURL)
} else {
connStatus = "[Local]"
}
connLine := HelpStyle.Render(connStatus)
connLine = lipgloss.PlaceHorizontal(m.width, lipgloss.Center, connLine)
sections = append(sections, connLine)
// Snooze indicator
if !m.snoozeUntil.IsZero() && m.now.Before(m.snoozeUntil) {
snoozeText := fmt.Sprintf("[Snoozing %s until %s]", m.snoozeName, m.snoozeUntil.Format("15:04:05"))