Skip to content

Commit

Permalink
refactor greeting UI (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Sep 1, 2023
1 parent 02d236f commit 7869f27
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions ui/cmd/greeting/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const (
)

type Model struct {
ctx context.Context
base *base.Model
ctx context.Context
*base.Base
phase Phase
questionIndex int
qaList []*QA
Expand All @@ -66,7 +66,7 @@ type Model struct {
}

func NewModel(ctx context.Context, port string) (*Model, error) {
base, err := base.NewModel()
base, err := base.New()
if err != nil {
return nil, err
}
Expand All @@ -79,7 +79,7 @@ func NewModel(ctx context.Context, port string) (*Model, error) {

return &Model{
ctx: ctx,
base: base,
Base: base,
phase: questionPhase,
questionIndex: 0,
qaList: qaListTemplate,
Expand Down
22 changes: 11 additions & 11 deletions ui/cmd/greeting/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
switch m.phase {
case questionPhase:
m.qaList[m.questionIndex].answer = m.base.TextInput.Value()
m.base.Conversation = append(m.base.Conversation, &base.Message{
m.qaList[m.questionIndex].answer = m.TextInput.Value()
m.Conversation = append(m.Conversation, &base.Message{
Speaker: base.User,
Content: m.base.TextInput.Value(),
Content: m.TextInput.Value(),
})
return m, tea.Batch(inputCmd, m.setConfig)
case authPhase:
Expand All @@ -41,7 +41,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case questionCompMsg:
if msg.isDone {
m.phase = authPhase
m.base.Conversation = append(m.base.Conversation, &base.Message{
m.Conversation = append(m.Conversation, &base.Message{
Speaker: base.Bot,
Content: "Please press enter to authorize",
})
Expand All @@ -50,7 +50,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.user = msg.user
m.spotifyClient = msg.client
m.phase = devicePhase
m.base.Conversation = append(m.base.Conversation, &base.Message{
m.Conversation = append(m.Conversation, &base.Message{
Speaker: base.Bot,
Content: "Please open Spotify app to get device ID and press enter",
})
Expand All @@ -60,7 +60,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.err = msg.err
}

m.base.TextInput, inputCmd = m.base.TextInput.Update(msg)
m.TextInput, inputCmd = m.TextInput.Update(msg)

return m, inputCmd
}
Expand All @@ -70,18 +70,18 @@ type questionCompMsg struct {
}

func (m *Model) setConfig() tea.Msg {
if err := m.base.Cfg.Set(confKeyList[m.questionIndex], m.qaList[m.questionIndex].answer); err != nil {
if err := m.Cfg.Set(confKeyList[m.questionIndex], m.qaList[m.questionIndex].answer); err != nil {
return errMsg{err}
}

m.questionIndex++
m.base.TextInput.Reset()
m.TextInput.Reset()
if m.questionIndex == len(m.qaList) {
return questionCompMsg{
isDone: true,
}
}
m.base.Conversation = append(m.base.Conversation, &base.Message{
m.Conversation = append(m.Conversation, &base.Message{
Speaker: base.Bot,
Content: m.qaList[m.questionIndex].question,
})
Expand All @@ -96,7 +96,7 @@ type spotifyMsg struct {
}

func (m *Model) authorize() tea.Msg {
authClient := auth.NewClient(m.base.Cfg)
authClient := auth.NewClient(m.Cfg)

authClient.Authorize()

Expand Down Expand Up @@ -127,7 +127,7 @@ func (m *Model) getDevice() tea.Msg {
}

deviceID := devices[0].ID.String()
if err := m.base.Cfg.Set(config.DeviceID, deviceID); err != nil {
if err := m.Cfg.Set(config.DeviceID, deviceID); err != nil {
return errMsg{err}
}

Expand Down
12 changes: 6 additions & 6 deletions ui/cmd/greeting/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func (m *Model) View() string {
if m.err != nil {
return style.ErrorView(m.base.Window.Width, m.base.Window.Height)
return style.ErrorView(m.Window.Width, m.Window.Height)
}
return lipgloss.Place(m.base.Window.Width, m.base.Window.Height, lipgloss.Center, lipgloss.Center,
return lipgloss.Place(m.Window.Width, m.Window.Height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Center,
style.AsciiArt().Render(asciiArtView),
Expand All @@ -22,12 +22,12 @@ func (m *Model) View() string {
}

func (m *Model) getViewWidth() int {
halfWidth := m.base.Window.Width / 2
halfWidth := m.Window.Width / 2
return halfWidth
}

func (m *Model) getViewHeight() int {
halfHeight := m.base.Window.Height * 2 / 3
halfHeight := m.Window.Height * 2 / 3
return halfHeight
}

Expand All @@ -50,7 +50,7 @@ func (m *Model) chatView() string {
s = lipgloss.Place(m.getViewWidth(), m.getViewHeight(), lipgloss.Center, lipgloss.Center, ss)
default:
var lastSpeaker base.Speaker
for _, message := range m.base.Conversation {
for _, message := range m.Conversation {
if message.Speaker == base.Bot {
s += style.BotChat(m.getViewWidth()).Render(message.Content) + "\n\n"
lastSpeaker = base.Bot
Expand All @@ -60,7 +60,7 @@ func (m *Model) chatView() string {
}
}
if s != "" && lastSpeaker == base.Bot && m.phase == questionPhase {
s += style.TextInput().Render(m.base.TextInput.View())
s += style.TextInput().Render(m.TextInput.View())
}
}
return s
Expand Down

0 comments on commit 7869f27

Please sign in to comment.