Add proper comments to ui_main

This commit is contained in:
Marcel Otte 2022-08-14 21:40:37 +02:00
parent 62350fe8b7
commit 17d379aa9f
1 changed files with 11 additions and 20 deletions

View File

@ -19,7 +19,7 @@ import (
var mockOsWriteFile = os.WriteFile var mockOsWriteFile = os.WriteFile
var mockOsReadFile = os.ReadFile var mockOsReadFile = os.ReadFile
type UISettings struct { type uiSettings struct {
hideUntil time.Time hideUntil time.Time
globalLevel int globalLevel int
} }
@ -30,11 +30,12 @@ var (
config backive.Configuration config backive.Configuration
doNotShowUntil time.Time = time.Unix(0, 0) doNotShowUntil time.Time = time.Unix(0, 0)
c net.Conn c net.Conn
uisettings UISettings uisettings uiSettings
messageLevel int messageLevel int
apphomedir string apphomedir string
) )
// Init the fyne application
func Init(a fyne.App, w fyne.Window, conf backive.Configuration) { func Init(a fyne.App, w fyne.Window, conf backive.Configuration) {
app = a app = a
a.SetIcon(theme.FyneLogo()) a.SetIcon(theme.FyneLogo())
@ -45,6 +46,7 @@ func Init(a fyne.App, w fyne.Window, conf backive.Configuration) {
go PollConnection() go PollConnection()
} }
// PollConnection polls in an endless loop the connection
func PollConnection() { func PollConnection() {
var err error var err error
for { for {
@ -62,24 +64,7 @@ func PollConnection() {
} }
} }
func NotificationRun() { // ShowNotification shows a single notification
if c != nil {
b := make([]byte, 2048)
i, err := c.Read(b)
if err == nil && i > 0 {
var data map[string]string
err = json.Unmarshal(b, &data)
if err == nil {
ShowNotification(data)
}
// else ignore and try to read again
err = nil
}
// we just try again and discard the error
err = nil
}
}
func ShowNotification(data map[string]string) { func ShowNotification(data map[string]string) {
if ShallShow(data) { if ShallShow(data) {
app.SendNotification( app.SendNotification(
@ -91,6 +76,7 @@ func ShowNotification(data map[string]string) {
} }
} }
// ShallShow checks if a message should be shown by level
func ShallShow(data map[string]string) bool { func ShallShow(data map[string]string) bool {
level, err := strconv.ParseUint(data["level"], 10, 64) level, err := strconv.ParseUint(data["level"], 10, 64)
if err != nil { if err != nil {
@ -108,10 +94,12 @@ func ShallShow(data map[string]string) bool {
return false return false
} }
// SetHideUntil sets the time until messages should be hidden
func SetHideUntil(until time.Time) { func SetHideUntil(until time.Time) {
uisettings.hideUntil = until uisettings.hideUntil = until
} }
// SetMessageLevel does exactly that.
func SetMessageLevel(level int) { func SetMessageLevel(level int) {
if level <= 10 { if level <= 10 {
messageLevel = level messageLevel = level
@ -122,6 +110,7 @@ func SetMessageLevel(level int) {
} }
} }
// SaveSettings stores the settings in $HOME/.config/backive/ui.json
func SaveSettings() { func SaveSettings() {
// save internal settings to file in homedir // save internal settings to file in homedir
jsonstr, merr := json.Marshal(uisettings) jsonstr, merr := json.Marshal(uisettings)
@ -137,6 +126,7 @@ func SaveSettings() {
} }
} }
// LoadSettings loads the settings from the place where SaveSettings stored them.
func LoadSettings() { func LoadSettings() {
// load settings // load settings
if _, err := os.Stat(apphomedir); err == nil { if _, err := os.Stat(apphomedir); err == nil {
@ -150,6 +140,7 @@ func LoadSettings() {
}*/ }*/
} }
// makeTray creates the tray menus needed.
func makeTray(app fyne.App) { func makeTray(app fyne.App) {
if desk, ok := app.(desktop.App); ok { if desk, ok := app.(desktop.App); ok {
hideReminders := fyne.NewMenuItem( hideReminders := fyne.NewMenuItem(