-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add in-app and system notifications (#540)
- add toast notification implementation to notification page - move toast layout code to toast.go - add toast to load and common structs - remove previous toast implementation * implement system notification * stack toast layout on modals layout * add delay speeds to toast notification - add NotifyError method - add comments to Notify methods - call NotifyError for error messages in layout code and Notify for success messages.
- Loading branch information
1 parent
269ab3f
commit 8652943
Showing
16 changed files
with
208 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package notification | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/gen2brain/beeep" | ||
) | ||
|
||
const ( | ||
icon = "ui/assets/decredicons/qrcodeSymbol.png" | ||
title = "Godcr" | ||
) | ||
|
||
type SystemNotification struct { | ||
iconPath string | ||
message string | ||
} | ||
|
||
func NewSystemNotification() (*SystemNotification, error) { | ||
absolutePath, err := getAbsolutePath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SystemNotification{ | ||
iconPath: filepath.Join(absolutePath, icon), | ||
}, nil | ||
} | ||
|
||
func (s *SystemNotification) Notify(message string) error { | ||
err := beeep.Notify(title, message, s.iconPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getAbsolutePath() (string, error) { | ||
ex, err := os.Executable() | ||
if err != nil { | ||
return "", fmt.Errorf("error getting executable path: %s", err.Error()) | ||
} | ||
|
||
exSym, err := filepath.EvalSymlinks(ex) | ||
if err != nil { | ||
return "", fmt.Errorf("error getting filepath after evaluating sym links") | ||
} | ||
|
||
return path.Dir(exSym), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package notification | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"gioui.org/layout" | ||
"gioui.org/op" | ||
|
||
"github.com/planetdecred/godcr/ui/decredmaterial" | ||
"github.com/planetdecred/godcr/ui/values" | ||
) | ||
|
||
type ( | ||
C = layout.Context | ||
D = layout.Dimensions | ||
) | ||
|
||
type Toast struct { | ||
sync.Mutex | ||
theme *decredmaterial.Theme | ||
success bool | ||
message string | ||
timer *time.Timer | ||
} | ||
|
||
type duration int32 | ||
|
||
const ( | ||
Short duration = iota | ||
Long | ||
) | ||
|
||
func NewToast(th *decredmaterial.Theme) *Toast { | ||
return &Toast{ | ||
theme: th, | ||
} | ||
} | ||
|
||
func getDurationFromDelay(d duration) time.Duration { | ||
if d == Long { | ||
return 5 * time.Second | ||
} | ||
return 2 * time.Second | ||
} | ||
|
||
// Notify is called to display a message indicating a successful action. It updates the toast object with the toast message | ||
// and duration. The duration parameter is optional. | ||
func (t *Toast) Notify(message string, d ...duration) { | ||
t.notify(message, true, d...) | ||
} | ||
|
||
// Notify is called to display a message indicating a failed action. It updates the toast object with the toast message | ||
// and duration. The duration parameter is optional. | ||
func (t *Toast) NotifyError(message string, d ...duration) { | ||
t.notify(message, false, d...) | ||
} | ||
|
||
// notify updates notification parameters on the toast object. It takes the message, success and duration | ||
// parameters. | ||
func (t *Toast) notify(message string, success bool, d ...duration) { | ||
var notificationDelay duration | ||
if len(d) > 0 { | ||
notificationDelay = d[0] | ||
} | ||
|
||
t.Lock() | ||
t.message = message | ||
t.success = success | ||
t.timer = time.NewTimer(getDurationFromDelay(notificationDelay)) | ||
t.Unlock() | ||
} | ||
|
||
func (t *Toast) Layout(gtx layout.Context) layout.Dimensions { | ||
t.handleToastDisplay(gtx) | ||
if t.timer == nil { | ||
return layout.Dimensions{} | ||
} | ||
|
||
color := t.theme.Color.Success | ||
if !t.success { | ||
color = t.theme.Color.Danger | ||
} | ||
|
||
card := t.theme.Card() | ||
card.Color = color | ||
return layout.Center.Layout(gtx, func(gtx C) D { | ||
return layout.Inset{Top: values.MarginPadding65}.Layout(gtx, func(gtx C) D { | ||
return card.Layout(gtx, func(gtx C) D { | ||
return layout.Inset{ | ||
Top: values.MarginPadding7, Bottom: values.MarginPadding7, | ||
Left: values.MarginPadding15, Right: values.MarginPadding15, | ||
}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { | ||
msg := t.theme.Body1(t.message) | ||
msg.Color = t.theme.Color.Surface | ||
return msg.Layout(gtx) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func (t *Toast) handleToastDisplay(gtx layout.Context) { | ||
if t.timer == nil { | ||
return | ||
} | ||
|
||
select { | ||
case <-t.timer.C: | ||
t.timer = nil | ||
op.InvalidateOp{}.Add(gtx.Ops) | ||
default: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.