-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
105 lines (89 loc) · 2.39 KB
/
app.go
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
package app
import (
"fmt"
"muscurdig/conf"
c "muscurdig/context"
"muscurdig/db"
"muscurdig/ui"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
)
type App struct {
isLogEnabled bool
ctx *c.AppContext
views map[c.AppRoute]func() *fyne.Container
application fyne.App
window *fyne.Window
}
func NewApp() App {
a := app.NewWithID(conf.AppId)
w := a.NewWindow(conf.WindowTitle)
w.Resize(fyne.NewSize(
conf.WindowWidth,
conf.WindowHeight,
))
w.SetFixedSize(conf.WindowFixed)
isLogEnabled := conf.EnableConsoleLog
db := db.NewDb(conf.DbFiles)
ctx := setupContext(db, w)
ctx.Version = conf.Version
a.Settings().SetTheme(&ui.MuscurdigTheme{})
return App{
ctx: &ctx,
isLogEnabled: isLogEnabled,
application: a,
window: &w,
views: map[c.AppRoute]func() *fyne.Container{
c.Setup: func() *fyne.Container { return ui.GetSetupView(&ctx) },
c.Login: func() *fyne.Container { return ui.GetLoginView(&ctx) },
c.List: func() *fyne.Container { return ui.GetPasswordListView(&ctx) },
c.Details: func() *fyne.Container { return ui.GetPasswordDetailsView(&ctx) },
c.AddUpdate: func() *fyne.Container { return ui.GetPasswordAddUpdateView(&ctx) },
c.Settings: func() *fyne.Container { return ui.GetSettingsView(&ctx) },
c.About: func() *fyne.Container { return ui.GetAboutView(&ctx) },
},
}
}
// TODO: for the next project this might be better as a Container
// or Factory with Cache and a stack to simulate pop push routes
func (a *App) getView() *fyne.Container {
key := a.ctx.CurrentRoute()
if content, ok := a.views[key]; ok {
return content()
}
return a.views[c.Login]()
}
func (a *App) setView() {
(*a.window).SetContent(a.getView())
}
func (a *App) log(msg string) {
if a.isLogEnabled {
fmt.Printf("%s - %s\n", time.Now().Format("15:04:05"), msg)
}
}
func (a *App) Run() {
a.ctx.OnRouteChange(func() {
val := a.ctx.CurrentRoute()
a.log(fmt.Sprintf("route state changed %s", val))
if val == c.Quit {
a.application.Quit()
}
a.setView()
})
a.setView()
(*a.window).ShowAndRun()
a.log("exiting...")
}
func (a *App) Cleanup() {
a.log("Running cleanup")
a.ctx.Db.Close()
a.log("cleanup finished")
}
func setupContext(db *db.Db, w fyne.Window) c.AppContext {
initialRoute := c.Login
if _, err := db.GetMasterPassword(); err != nil {
initialRoute = c.Setup
}
return c.NewAppContext(initialRoute, db, w)
}