summaryrefslogtreecommitdiff
path: root/internal/client/ui/core/core.go
blob: bbc8576e864535a497a24ec473d0b05e19fce41d (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
package core

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

	"github.com/charmbracelet/bubbles/spinner"
	"github.com/charmbracelet/bubbles/timer"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/kyren223/eko/internal/client/gateway"
	"github.com/kyren223/eko/internal/client/ui"
	"github.com/kyren223/eko/internal/client/ui/loadscreen"
	"github.com/kyren223/eko/internal/client/ui/networks"
	"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
)

type Model struct {
	name    string
	privKey ed25519.PrivateKey

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

	id snowflake.ID

	networks networks.Model
}

func New(privKey ed25519.PrivateKey, name string) Model {
	return Model{
		name:      name,
		privKey:   privKey,
		loading:   loadscreen.New(connectingToServer),
		timeout:   initialTimeout,
		timer:     newTimer(initialTimeout),
		connected: false,
		networks:  networks.New(),
	}
}

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()
	}

	return m.networks.View()
}

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

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

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

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

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

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

		default:
			return m, nil
		}
	}

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

	case ui.QuitMsg:
		gateway.Disconnect()
	}

	return m, 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)
}