~f4814n/frost

ref: 7c0d7579bbae138623265ccc19620874855e9135 frost/main.go -rw-r--r-- 3.4 KiB
7c0d7579Fabian Geiselhart Add room information to EphemeralEvent 1 year, 28 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main

import (
	"os"
	"encoding/json"
	"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"
	backend "git.sr.ht/~f4814n/frost/matrix/backend/sqlite"
	"git.sr.ht/~f4814n/frost/appdirs"
	log "github.com/sirupsen/logrus"
)

var (
	configFilePath = appdirs.UserDataDir("frost", "f4814n") + "/config.json"
)

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
}

func newApp() *App {
	return &App{
		rx: make(chan Event, 10),
		tx: make(chan Event, 10),
		cli: matrix.NewClient(matrix.ClientOpts{
			HTTPClient: http.DefaultClient,
			Backend:    backend.New(appdirs.UserDataDir("frost", "f4814n")),
		}),
		window: app.NewWindow(
			app.Size(unit.Dp(400), unit.Dp(800)),
			app.Title("Frost"),
		),
	}
}

func (a *App) run() error {
	for {
		select {
		case e := <-a.window.Events():
			switch e := e.(type) {
			case system.StageEvent:
				theme = material.NewTheme(gofont.Collection())
				a.loadConfig()
				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.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()}
					a.flushConfig()

					a.page.Stop()
					a.page = NewOverviewPage(a.cli)
					a.page.Start(a.tx, a.rx)
				}()
			case InvalidationEvent:
				a.window.Invalidate()
			default:
				log.Warnf("%#v", e)
			}
		}
	}
}

func (a *App) loadConfig() {
	f, err := os.Open(configFilePath)
	if err != nil {
		log.WithField("error", err).Error("Failed to open config file")
		return
	}

	err = json.NewDecoder(f).Decode(&a.config)
	if err != nil {
		// XXX Better error communication
		log.WithField("error", err).Error("Failed to parse config file")
	}
}

func (a *App) flushConfig() {
	f, err := os.Create(configFilePath)
	if err != nil {
		log.WithField("error", err).Error("Failed to flush config file")
	}

	err = json.NewEncoder(f).Encode(a.config)
	if err != nil {
		log.WithField("error", err).Error("Failed to encode configuration")
	}
}

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