flewkey
/
linger
Archived
1
0
Fork 0
This repository has been archived on 2021-02-24. You can view files and clone it, but cannot push or open issues or pull requests.
linger/model.go

94 lines
1.7 KiB
Go

package main
import (
tea "github.com/charmbracelet/bubbletea"
)
type viewType int
const (
viewNone viewType = 0
viewLogin viewType = 1
viewChannels viewType = 2
viewChat viewType = 3
)
type mainModel struct {
conf *config
conn *loungeConnection
view viewType
width int
height int
loginModel loginModel
channelsModel channelsModel
chatModel chatModel
}
func mainModelInit() mainModel {
conf := configLoad()
conn := &loungeConnection{User: conf.User, Token: conf.Token, Action: actionNone}
login := loginModelInit(conf)
channels := channelsModelInit()
chat := chatModelInit()
return mainModel{conf, conn, viewLogin, 0, 0, login, channels, chat}
}
func (m mainModel) Init() tea.Cmd {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
cmd = m.loginInit(m.conf)
cmds = append(cmds, cmd)
cmd = m.channelsInit()
cmds = append(cmds, cmd)
cmd = m.chatInit()
cmds = append(cmds, cmd)
return tea.Batch(cmds...)
}
func (m mainModel) View() string {
s := ""
switch m.view {
case viewLogin:
s = m.loginView()
case viewChannels:
s = m.channelsView()
case viewChat:
s = m.chatView()
}
return s
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var model tea.Model = m
var cmd tea.Cmd = nil
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if m.conn.Action != actionNone {
m = connActionHandle(m)
} else {
m.width = msg.Width
m.height = msg.Height
}
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
return m, tea.Quit
case "ctrl+s":
if m.view != viewLogin {
m.view = viewChannels
}
}
}
switch m.view {
case viewLogin:
model, cmd = m.loginUpdate(msg)
case viewChannels:
model, cmd = m.channelsUpdate(msg)
case viewChat:
model, cmd = m.chatUpdate(msg)
}
return model, cmd
}