~f4814n/frost

ref: 8db62c04508c238d307cee6c40f2a8891ee6ea56 frost/main.go -rw-r--r-- 3.0 KiB
8db62c04Fabian Geiselhart Create linux Platform 1 year, 23 days ago
                                                                                
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
package main

import (
	"fmt"
	"net/http"

	"gioui.org/app"
	"gioui.org/font/gofont"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/unit"
	"gioui.org/widget/material"
	"git.sr.ht/~f4814n/frost/matrix"
	log "github.com/sirupsen/logrus"
)

var theme *material.Theme

type Gtx = layout.Context
type Dims = layout.Dimensions
type Event interface{}

type Page interface {
	Start(rx chan Event, tx chan Event)
	Layout(Gtx) Dims
	Stop()
}

type AppConfig struct {
	Session *SessionConfig `json:"session"`
}

type SessionConfig struct {
	MxID string `json:"mxid"`
	DeviceID string `json:"device_id"`
}

type App struct {
	window *app.Window
	cli *matrix.Client
	rx, tx chan Event
	page   Page
	config AppConfig
	platform Platform
}

func newApp() *App {
	platform := InitPlatform()
	return &App{
		rx: make(chan Event, 10),
		tx: make(chan Event, 10),
		cli: matrix.NewClient(matrix.ClientOpts{
			HTTPClient: http.DefaultClient,
			Backend: platform.NewBackend(),
		}),
		window: app.NewWindow(
			app.Size(unit.Dp(400), unit.Dp(800)),
			app.Title("Frost"),
		),
		platform: platform,
	}
}

func (a *App) run() error {
	for {
		select {
		case e := <-a.window.Events():
			switch e := e.(type) {
			case system.StageEvent:
				theme = material.NewTheme(gofont.Collection())
				var err error
				a.config, err = a.platform.LoadConfig()
				if err != nil {
					log.WithError(err).Warn("platform: Failed to load config")
				}
				page := a.initialPage()
				a.page = page
				page.Start(a.tx, a.rx)
			case system.FrameEvent:
				var ops op.Ops
				gtx := layout.NewContext(&ops, e)
				a.Layout(gtx)
				e.Frame(gtx.Ops)
			case system.DestroyEvent:
				return e.Err
			default:
				log.Tracef("%#v", e)
			}
			// case app.pageEvents:
		case e := <-a.rx:
			switch e := e.(type) {
			case LoginStartEvent:
				go func() {
					log.Info("Logging in")
					err := a.cli.Login(e.Username, e.Password)
					if err != nil {
						log.WithError(err).Info("Failed to log in")
						a.tx <- LoginErrorEvent{Err: err}
						return
					}
					log.Info("Successfully authenticated")

					a.config.Session = &SessionConfig{
						MxID: a.cli.MxID(),
						DeviceID: a.cli.DeviceID(),
					}
					err = a.platform.FlushConfig(a.config)
					if err != nil {
						log.WithError(err).Warn("platform: Failed to flush config")
					}

					a.page.Stop()
					a.page = NewOverviewPage(a.cli)
					a.page.Start(a.tx, a.rx)
				}()
			case InvalidationEvent:
				a.window.Invalidate()
			default:
				log.WithField("event", fmt.Sprintf("%#v", e)).Warn("Unhandled event")
			}
		}
	}
}

func (a *App) initialPage() Page {
	if a.config.Session != nil {
		err := a.cli.LoadToken(a.config.Session.MxID, a.config.Session.DeviceID)
		if err != nil {
			log.WithField("error", err).Error("Failed to reuse session")
		}
		return NewOverviewPage(a.cli)
	}

	return NewLoginPage()
}

func (a *App) Layout(gtx Gtx) {
	a.page.Layout(gtx)
}

func main() {
	log.SetLevel(log.DebugLevel)
	go func() {
		a := newApp()
		if err := a.run(); err != nil {
			log.Fatal(err)
		}
	}()
	app.Main()
}