91 lines
1.9 KiB
Go
Raw Normal View History

2017-12-01 23:52:25 +01:00
package views
import (
"github.com/erroneousboat/termui"
"github.com/erroneousboat/slack-term/components"
2017-12-16 22:54:00 +01:00
"github.com/erroneousboat/slack-term/config"
2017-12-01 23:52:25 +01:00
"github.com/erroneousboat/slack-term/service"
)
type View struct {
2017-12-16 22:54:00 +01:00
Config *config.Config
2017-12-01 23:52:25 +01:00
Input *components.Input
Chat *components.Chat
Channels *components.Channels
2019-03-16 14:42:27 +01:00
Threads *components.Threads
2017-12-01 23:52:25 +01:00
Mode *components.Mode
Debug *components.Debug
}
2018-10-27 14:44:20 +02:00
func CreateView(config *config.Config, svc *service.SlackService) (*View, error) {
2017-12-01 23:52:25 +01:00
// Create Input component
input := components.CreateInputComponent()
// Channels: create the component
2019-03-16 14:42:27 +01:00
sideBarHeight := termui.TermHeight() - input.Par.Height
channels := components.CreateChannelsComponent(sideBarHeight)
2017-12-01 23:52:25 +01:00
// Channels: fill the component
2018-10-27 14:44:20 +02:00
slackChans, err := svc.GetChannels()
if err != nil {
return nil, err
}
// Channels: set channels in component
2017-12-01 23:52:25 +01:00
channels.SetChannels(slackChans)
2019-03-16 14:42:27 +01:00
// Threads: create component
threads := components.CreateThreadsComponent(sideBarHeight)
2017-12-01 23:52:25 +01:00
// Chat: create the component
chat := components.CreateChatComponent(input.Par.Height)
// Chat: fill the component
msgs, thr, err := svc.GetMessages(
channels.ChannelItems[channels.SelectedChannel].ID,
2017-12-01 23:52:25 +01:00
chat.GetMaxItems(),
)
2018-10-27 14:44:20 +02:00
if err != nil {
return nil, err
}
2017-12-03 21:43:33 +01:00
2018-10-27 14:44:20 +02:00
// Chat: set messages in component
chat.SetMessages(msgs)
2018-10-27 14:44:20 +02:00
chat.SetBorderLabel(
channels.ChannelItems[channels.SelectedChannel].GetChannelName(),
)
2017-12-01 23:52:25 +01:00
2019-03-16 14:42:27 +01:00
// Threads: set threads in component
threads.SetChannels(thr)
2019-03-16 14:42:27 +01:00
2017-12-01 23:52:25 +01:00
// Debug: create the component
2017-12-02 11:09:01 +01:00
debug := components.CreateDebugComponent(input.Par.Height)
2017-12-01 23:52:25 +01:00
// Mode: create the component
mode := components.CreateModeComponent()
view := &View{
2017-12-16 22:54:00 +01:00
Config: config,
2017-12-01 23:52:25 +01:00
Input: input,
Channels: channels,
2019-03-16 14:42:27 +01:00
Threads: threads,
2017-12-01 23:52:25 +01:00
Chat: chat,
Mode: mode,
Debug: debug,
}
2018-10-27 14:44:20 +02:00
return view, nil
2017-12-01 23:52:25 +01:00
}
func (v *View) Refresh() {
termui.Render(
v.Input,
v.Chat,
v.Channels,
2019-03-16 14:42:27 +01:00
v.Threads,
2017-12-01 23:52:25 +01:00
v.Mode,
)
}