Fix broken logic in original "views" example

This commit is contained in:
Christian Rocha 2020-06-05 14:12:02 -04:00
parent b10f6d6791
commit 4ce9b4ea83
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 17 additions and 11 deletions

View File

@ -44,19 +44,21 @@ type Model struct {
// INIT // INIT
func initialize() (tea.Model, tea.Cmd) { func initialize() (tea.Model, tea.Cmd) {
return Model{0, false, 10, 0, 0, false}, tick return Model{0, false, 10, 0, 0, false}, tick()
} }
// CMDS // CMDS
func tick() tea.Msg { func tick() tea.Cmd {
time.Sleep(time.Second) return tea.Tick(time.Second, func(time.Time) tea.Msg {
return tickMsg{} return tickMsg{}
})
} }
func frame() tea.Msg { func frame() tea.Cmd {
time.Sleep(time.Second / 60) return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
return frameMsg{} return frameMsg{}
})
} }
// UPDATE // UPDATE
@ -91,7 +93,7 @@ func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
} }
case "enter": case "enter":
m.Chosen = true m.Chosen = true
return m, nil return m, frame()
case "q": case "q":
fallthrough fallthrough
case "esc": case "esc":
@ -105,9 +107,10 @@ func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
return m, tea.Quit return m, tea.Quit
} }
m.Ticks -= 1 m.Ticks -= 1
return m, tick()
} }
return m, tick return m, nil
} }
func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) { func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
@ -126,12 +129,14 @@ func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
case frameMsg: case frameMsg:
if !m.Loaded { if !m.Loaded {
m.Frames += 1 m.Frames += 1
m.Progress = ease.OutBounce(float64(m.Frames) / float64(160)) m.Progress = ease.OutBounce(float64(m.Frames) / float64(100))
if m.Progress >= 1 { if m.Progress >= 1 {
m.Progress = 1 m.Progress = 1
m.Loaded = true m.Loaded = true
m.Ticks = 3 m.Ticks = 3
return m, tick()
} }
return m, frame()
} }
case tickMsg: case tickMsg:
@ -140,10 +145,11 @@ func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
return m, tea.Quit return m, tea.Quit
} }
m.Ticks -= 1 m.Ticks -= 1
return m, tick()
} }
} }
return m, frame return m, nil
} }
// VIEW // VIEW