-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.go
84 lines (72 loc) · 2.06 KB
/
notify.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
package main
import (
"os"
"strconv"
"time"
notif "github.com/deckarep/gosx-notifier"
"github.com/getlantern/systray"
)
// checkIfExists checks if a file/folder exists
func checkIfExists(filePath string) bool {
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
return false
}
return true
}
// pushBatteryNotifyMessage() will trigger notify() when time remaining equals the specified minutesRemaining variable
func pushBatteryNotifyMessage(notifier *reminder) {
notifier.notifier = true
wg.Add(1)
go func() {
stop := systray.AddMenuItem("Stop Notifier (at "+getTitle(convMinToSpec(notifier.MinutesRemaining))+")", "")
wg.Add(1)
go checkIfClick(stop, stopNotification, notifier)
Y:
for {
for i := 0; i < conf.UpdateInterval*1000; i++ {
if checkIfShutdown() {
stop.ClickedCh <- struct{}{}
break Y
}
time.Sleep(1 * time.Millisecond)
}
info, err := getBatteryInfo()
logError("", err)
if notifier.notifier {
stop.Hide()
break
}
if convTimeSpecToMin(info.timeRemaining) <= notifier.MinutesRemaining {
stop.ClickedCh <- struct{}{}
stop.Hide()
if !(info.timeRemaining.hours == 0 && info.timeRemaining.mins == 0) {
message := "You have " + strconv.Itoa(info.timeRemaining.hours) + "h and " + strconv.Itoa(info.timeRemaining.mins) + "min of battery life remaining"
logError("There was a problem while sending the notification", notify(message, "", ""))
}
break
}
}
defer wg.Done()
}()
}
// notify() sends a message
func notify(msg, tit, iconPath string) error {
note := notif.NewNotification(msg)
note.Title = tit
note.ContentImage = iconPath
note.Sound = "'default'"
if checkIfExists(conf.AppIcon) {
note.AppIcon = conf.AppIcon
}
return note.Push()
}
// stopNotification() changes the notifications struct so that pushBatteryNotification() will break out of the for loop
func stopNotification(notifier *reminder) {
for k, v := range conf.Reminders {
if v.MinutesRemaining == notifier.MinutesRemaining {
enable(conf.Reminders[k].item)
conf.Reminders[k].notifier = false
}
}
}