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

import (
	"crypto/ed25519"
	"time"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/kyren223/eko/internal/client/gateway"
	"github.com/kyren223/eko/internal/client/ui/loadscreen"
)

var (
	connectingToServer = "Connecting to server.."
	connectionFailed   = "Connection failed - retrying in %d sec..."
)

type Model struct {
	privKey ed25519.PrivateKey
	name    string

	loading   loadscreen.Model
	connected bool
}

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

func (m Model) Init() tea.Cmd {
	return tea.Batch(gateway.Connect(m.privKey, 5*time.Second), m.loading.Init())
}

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

	return ""
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	if !m.connected {
		var loadscreenCmd tea.Cmd
		m.loading, loadscreenCmd = m.loading.Update(msg)
		return m, loadscreenCmd
	}
	return m, nil
}