-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (86 loc) · 2.17 KB
/
main.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
106
107
108
package main
import (
"database/sql"
"log"
"net/http"
savingsrepository "nsw-finance/repository/savings-repository"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/theme"
_ "github.com/glebarez/go-sqlite"
)
type UIComponents struct {
SavingsContainer *fyne.Container
PassableContainer *fyne.Container
}
type Utils struct {
InfoLog *log.Logger
ErrorLog *log.Logger
HTTPClient *http.Client
}
type App struct {
App fyne.App
MainWindow fyne.Window
UIComponents UIComponents
SavingsDB savingsrepository.Repository
Utils Utils
}
func main() {
var myApp App
// create a fyne app
fyneApp := app.NewWithID("am.gocode.nswfinance.preferences")
myApp.App = fyneApp
myApp.App.Settings().SetTheme(theme.LightTheme())
// create our loggers
myApp.Utils.InfoLog = log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
myApp.Utils.ErrorLog = log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
// open a connection to the database
sqlDB, err := myApp.connectSQL()
if err != nil {
log.Panic(err)
}
// setup the database
myApp.setupDB(sqlDB)
// create and size a fyne window
myApp.MainWindow = fyneApp.NewWindow("NSW Finance")
myApp.MainWindow.Resize(fyne.NewSize(770, 410))
myApp.MainWindow.SetFixedSize(true)
myApp.MainWindow.SetMaster()
// make the UI
myApp.makeUI()
// show and run the application
myApp.MainWindow.ShowAndRun()
}
func (app *App) connectSQL() (*sql.DB, error) {
path := "./sql.db"
if os.Getenv("DB_PATH") != "" {
path = os.Getenv("DB_PATH")
} else {
path = app.App.Storage().RootURI().Path() + "/sql.db"
app.Utils.InfoLog.Println("db in:", path)
}
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, err
}
return db, nil
}
func (app *App) setupDB(sqlDB *sql.DB) {
app.SavingsDB = savingsrepository.NewSQLiteRepository(sqlDB)
err := app.SavingsDB.MigrateSavings()
if err != nil {
app.Utils.ErrorLog.Println(err)
log.Panic(err)
}
err = app.SavingsDB.MigrateSpendingTables()
if err != nil {
app.Utils.ErrorLog.Println(err)
log.Panic(err)
}
err = app.SavingsDB.MigrateSpendings()
if err != nil {
app.Utils.ErrorLog.Println(err)
log.Panic(err)
}
}