From 449ae39f8531505ac6b53c149e98d129ee5cecd8 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Mon, 4 Apr 2022 22:19:15 +0200 Subject: [PATCH] Add reoccuring notifications for initiation of backups --- cmd/backive_ui/main.go | 4 +++ ui/ui_main.go | 63 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/cmd/backive_ui/main.go b/cmd/backive_ui/main.go index 01ef647..0670c22 100644 --- a/cmd/backive_ui/main.go +++ b/cmd/backive_ui/main.go @@ -16,9 +16,13 @@ func main() { database.Load() config.Load() + backive.Init(config, database) app := app.NewWithID("Backive UI") w := app.NewWindow("Backive UI") backiveui.Init(app, w, config, database) + go func() { + backiveui.NotificationRun() + }() //w.SetContent(widget.NewLabel("Hello World!")) w.ShowAndRun() diff --git a/ui/ui_main.go b/ui/ui_main.go index 4a718e1..17c8c63 100644 --- a/ui/ui_main.go +++ b/ui/ui_main.go @@ -1,10 +1,21 @@ package ui +/* +# TODO: +- [x] Show last backup date at frequency +- [x] Define reminder message/interval for backups + - [x] implement 15m interval of showing notifications +- Send notifications of events from service + - Define event protocol + - implement service side + - implement ui side + +*/ + import ( "fmt" - "io/ioutil" - "path" "strings" + "time" "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" @@ -26,8 +37,15 @@ var ( content *fyne.Container devBtnList []*widget.Button bacBtnList []*widget.Button + notifs notifications ) +type notifications struct { + TurnOffForToday bool + TurnOffTS time.Time + Interval int +} + func Init(a fyne.App, w fyne.Window, c backive.Configuration, d backive.Database) { app = a a.SetIcon(theme.FyneLogo()) @@ -42,6 +60,23 @@ func Init(a fyne.App, w fyne.Window, c backive.Configuration, d backive.Database }) } +func NotificationRun() { + fmt.Printf("Notification run\n") + for _, v := range config.Backups { + fmt.Printf("Notification run %s\n", v.Name) + if v.ShouldRun() && v.Frequency > 0 { + fmt.Printf("Notification for %s\n", v.Name) + app.SendNotification(fyne.NewNotification( + fmt.Sprintf("%s's frequency days reached", v.Name), + fmt.Sprintf("The '%s' frequency days have been reached.\n\nPlease insert the disk with the label '%s' to initiate the backup routine.", + v.Name, + v.TargetDevice))) + } + } + h, _ := time.ParseDuration("15m") + time.Sleep(h) +} + func makeTray(app fyne.App) { if desk, ok := app.(desktop.App); ok { menu := fyne.NewMenu( @@ -52,6 +87,10 @@ func makeTray(app fyne.App) { fyne.NewMenuItem("Hide app", func() { window.Hide() }), + fyne.NewMenuItem("Turn off notifications for today", func() { + notifs.TurnOffTS = time.Now() + notifs.TurnOffForToday = true + }), fyne.NewMenuItem("Send note", func() { app.SendNotification(fyne.NewNotification("Hi", "content stuff")) }), @@ -141,7 +180,16 @@ func DisplayBackup(bac string) { dataForm.Add(widget.NewLabel("Name")) dataForm.Add(widget.NewLabel(backup.Name)) dataForm.Add(widget.NewLabel("Frequency (days)")) - dataForm.Add(widget.NewLabel(fmt.Sprintf("%d", backup.Frequency))) + var runs backive.Runs + runs.Load(db) + lastBackup, err := runs.LastRun(backup) + if err == nil { + fmt.Printf("displaying run data") + dataForm.Add(widget.NewLabel(fmt.Sprintf("%d (%v)", backup.Frequency, lastBackup))) + } else { + fmt.Printf("no run data") + dataForm.Add(widget.NewLabel(fmt.Sprintf("%d (never run)", backup.Frequency))) + } dataForm.Add(widget.NewLabel("Target device")) dataForm.Add(widget.NewLabel(backup.TargetDevice)) dataForm.Add(widget.NewLabel("Target directory")) @@ -169,10 +217,11 @@ func DisplayBackup(bac string) { dataForm.Add(widget.NewLabel("Label")) dataForm.Add(widget.NewLabel(backup.Label)) logEntry := widget.NewMultiLineEntry() - content, err := ioutil.ReadFile(path.Join(config.Settings.LogLocation, bac+".log")) - if err != nil { - logEntry.SetText("Reading file failed") - } + //content, err := ioutil.ReadFile(path.Join(config.Settings.LogLocation, bac+".log")) + content := "" + //if err != nil { + // logEntry.SetText("Reading file failed") + //} logEntry.Disable() logEntry.SetText(string(content)) vbox := container.NewBorder(dataForm, nil, nil, nil, logEntry)