summaryrefslogtreecommitdiff
path: root/internal/client/ui/core/core.go
blob: 140d5f3825a74f0b99a46dbd87aa7777d6ab68d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package core

import (
	"crypto/ed25519"
	"fmt"
	"log"
	"slices"
	"time"

	"github.com/charmbracelet/bubbles/spinner"
	"github.com/charmbracelet/bubbles/timer"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"github.com/kyren223/eko/internal/client/gateway"
	"github.com/kyren223/eko/internal/client/ui"
	"github.com/kyren223/eko/internal/client/ui/core/networkcreation"
	"github.com/kyren223/eko/internal/client/ui/core/networklist"
	"github.com/kyren223/eko/internal/client/ui/core/state"
	"github.com/kyren223/eko/internal/client/ui/loadscreen"
	"github.com/kyren223/eko/internal/packet"
	"github.com/kyren223/eko/pkg/assert"
	"github.com/kyren223/eko/pkg/snowflake"
)

var (
	connectingToServer = "Connecting to server.."
	connectionFailed   = "Connection failed - retrying in %d sec..."
	connectionTimeout  = 5 * time.Second
	initialTimeout     = 3750 * time.Millisecond
	timerInterval      = 50 * time.Millisecond
)

const (
	FocusNetworkList = iota
	FocusFrequencies
	FocusChat
	FocusUsers
	FocusMax
)

type Model struct {
	name    string
	privKey ed25519.PrivateKey
	id      snowflake.ID

	loading   loadscreen.Model
	timer     timer.Model
	timeout   time.Duration
	connected bool

	networkCreationPopup *networkcreation.Model
	networkList          networklist.Model
	focus                int
}

func New(privKey ed25519.PrivateKey, name string) Model {
	m := Model{
		name:        name,
		privKey:     privKey,
		networkList: networklist.New(),
		loading:     loadscreen.New(connectingToServer),
		timer:       newTimer(initialTimeout),
		timeout:     initialTimeout,
		connected:   false,
		focus:       FocusNetworkList,
	}
	m.move(0) // Update focus

	return m
}

func (m Model) Init() tea.Cmd {
	return tea.Batch(gateway.Connect(m.privKey, connectionTimeout), m.loading.Init())
}

func (m Model) View() string {
	if !m.connected {
		return m.loading.View()
	}

	result := m.networkList.View()

	result = lipgloss.Place(
		ui.Width, ui.Height,
		lipgloss.Left, lipgloss.Top,
		result,
	)

	if m.networkCreationPopup != nil {
		popup := m.networkCreationPopup.View()
		x := (ui.Width - lipgloss.Width(popup)) / 2
		y := (ui.Height - lipgloss.Height(popup)) / 2
		result = ui.PlaceOverlay(x, y, popup, result)
	}

	return result
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	if m.connected {
		cmd := m.updateConnected(msg)
		return m, cmd
	} else {
		cmd := m.updateNotConnected(msg)
		return m, cmd
	}
}

func (m *Model) updateNotConnected(msg tea.Msg) tea.Cmd {
	switch msg := msg.(type) {
	case gateway.ConnectionEstablished:
		m.id = snowflake.ID(msg)
		m.connected = true
		m.timeout = initialTimeout
		return m.timer.Stop()

	case gateway.ConnectionFailed:
		m.timer = newTimer(m.timeout)
		m.updateLoadScreenContent()
		return m.timer.Start()

	case timer.TimeoutMsg:
		m.timeout = min(m.timeout*2, time.Minute)
		m.loading.SetContent(connectingToServer)
		return gateway.Connect(m.privKey, connectionTimeout)

	case timer.StartStopMsg:
		var cmd tea.Cmd
		m.timer, cmd = m.timer.Update(msg)
		return cmd

	case timer.TickMsg:
		m.updateLoadScreenContent()
		var cmd tea.Cmd
		m.timer, cmd = m.timer.Update(msg)
		return cmd

	case spinner.TickMsg:
		var loadscreenCmd tea.Cmd
		m.loading, loadscreenCmd = m.loading.Update(msg)
		return loadscreenCmd

	default:
		return nil
	}
}

func (m *Model) updateConnected(msg tea.Msg) tea.Cmd {
	switch msg := msg.(type) {
	case gateway.ConnectionLost:
		m.connected = false
		m.timeout = initialTimeout
		return tea.Batch(gateway.Connect(m.privKey, connectionTimeout), m.loading.Init())

	case *packet.NetworksInfo:
		if msg.Set {
			state.State.Networks = msg.Networks
		} else {
			// state.State.Networks = append(state.State.Networks, msg.Networks...)
			networks := state.State.Networks
			networks = append(networks, msg.Networks...)
			networks = slices.DeleteFunc(networks, func(network packet.FullNetwork) bool {
				return slices.Contains(msg.RemoveNetworks, network.ID)
			})
			slices.SortFunc(networks, func(a, b packet.FullNetwork) int {
				return a.Position - b.Position
			})
			log.Println(networks)
			state.State.Networks = networks
		}

	case ui.QuitMsg:
		gateway.Disconnect()

	case tea.KeyMsg:
		key := msg.Type
		switch key {
		case tea.KeyCtrlN:
			if m.networkCreationPopup == nil {
				popup := networkcreation.New()
				m.networkCreationPopup = &popup
			}

		case tea.KeyEscape:
			if m.networkCreationPopup != nil {
				m.networkCreationPopup = nil
			}

		case tea.KeyEnter:
			if m.networkCreationPopup != nil {
				cmd := m.networkCreationPopup.Select()
				if cmd != nil {
					m.networkCreationPopup = nil
				}
				return cmd
			}

		default:
			if m.networkCreationPopup != nil {
				popup, cmd := m.networkCreationPopup.Update(msg)
				m.networkCreationPopup = &popup
				return cmd
			}
		}
	}

	return nil
}

func (m *Model) updateLoadScreenContent() {
	seconds := m.timer.Timeout.Round(time.Second) / time.Second
	m.loading.SetContent(fmt.Sprintf(connectionFailed, seconds))
}

func newTimer(timeout time.Duration) timer.Model {
	return timer.NewWithInterval(timeout.Truncate(time.Second)+(time.Second/2), timerInterval)
}

func (m *Model) move(direction int) {
	focus := m.focus + direction
	m.focus = max(0, min(FocusMax-1, focus))

	m.networkList.Blur()
	switch m.focus {
	case FocusNetworkList:
		m.networkList.Focus()
	default:
		assert.Never("missing switch statement field in move", "focus", m.focus)
	}
}