backive/ui/ui_main.go

109 lines
2.1 KiB
Go
Raw Normal View History

2022-03-07 23:24:02 +01:00
package ui
import (
2022-06-07 21:20:16 +02:00
"encoding/json"
2022-03-12 23:08:57 +01:00
"fmt"
2022-06-07 21:20:16 +02:00
"net"
"time"
2022-03-12 23:08:57 +01:00
2022-03-07 23:24:02 +01:00
"fyne.io/fyne/v2"
2022-03-17 20:56:24 +01:00
"fyne.io/fyne/v2/driver/desktop"
2022-03-17 21:01:26 +01:00
"fyne.io/fyne/v2/theme"
2022-03-07 23:24:02 +01:00
"github.com/qwc/backive"
)
var (
app fyne.App
window fyne.Window
config backive.Configuration
db backive.Database
doNotShowUntil time.Time = time.Unix(0, 0)
2022-06-07 21:20:16 +02:00
c net.Conn
2022-03-07 23:24:02 +01:00
)
2022-06-07 21:20:16 +02:00
func Init(a fyne.App, w fyne.Window, conf backive.Configuration, d backive.Database) {
2022-03-07 23:24:02 +01:00
app = a
2022-03-17 20:56:24 +01:00
a.SetIcon(theme.FyneLogo())
makeTray(app)
2022-06-07 21:20:16 +02:00
config = conf
2022-03-07 23:24:02 +01:00
db = d
2022-06-07 21:20:16 +02:00
go PollConnection()
2022-03-07 23:24:02 +01:00
}
2022-06-07 21:20:16 +02:00
func PollConnection() {
var err error
for {
if c == nil {
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
} else {
err = fmt.Errorf("Connection already established")
}
if err != nil {
// ignore
err = nil
// sleep a while and then retry
time.Sleep(10 * time.Second)
}
}
}
func NotificationRun() {
2022-06-07 21:20:16 +02:00
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
}
/*
if doNotShowUntil == time.Unix(0, 0) || time.Now().After(doNotShowUntil) {
ShowNotification()
if doNotShowUntil != time.Unix(0, 0) {
doNotShowUntil = time.Unix(0, 0)
}
}
h, _ := time.ParseDuration("15m")
time.Sleep(h)
2022-06-07 21:20:16 +02:00
//*/
}
2022-06-07 21:20:16 +02:00
func ShowNotification(data map[string]string) {
if ShallShow(data) {
app.SendNotification(
fyne.NewNotification(
2022-06-07 21:20:16 +02:00
data["header"],
data["message"],
),
)
}
}
2022-06-07 21:20:16 +02:00
func ShallShow(data map[string]string) bool {
return true
}
2022-03-17 20:56:24 +01:00
func makeTray(app fyne.App) {
if desk, ok := app.(desktop.App); ok {
menu := fyne.NewMenu(
"backive",
fyne.NewMenuItem("Show notifications again", func() {
}),
fyne.NewMenuItem("Hide notifications for today", func() {
doNotShowUntil = time.Now().AddDate(0, 0, 1)
}),
fyne.NewMenuItem("Hide notifications for a hour", func() {
doNotShowUntil = time.Now().Add(time.Hour)
}),
)
2022-03-17 20:56:24 +01:00
desk.SetSystemTrayMenu(menu)
}
}