internal refactor of notifications
This commit is contained in:
+55
-32
@@ -1,63 +1,86 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"github.com/leonardotrapani/hyprvoice/internal/config"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Notifier interface {
|
||||
Error(msg string)
|
||||
Notify(title, message string)
|
||||
Send(mt MessageType)
|
||||
Error(msg string) // for dynamic errors (e.g., pipeline errors)
|
||||
}
|
||||
|
||||
type Desktop struct{}
|
||||
|
||||
func (d Desktop) RecordingStarted() {
|
||||
d.Notify("Hyprvoice", "Recording Started")
|
||||
// NewNotifier creates a notifier based on type with resolved messages
|
||||
func NewNotifier(notifType string, messages map[MessageType]Message) Notifier {
|
||||
switch notifType {
|
||||
case "desktop":
|
||||
return NewDesktop(messages)
|
||||
case "log":
|
||||
return NewLog(messages)
|
||||
default:
|
||||
return &Nop{}
|
||||
}
|
||||
}
|
||||
|
||||
func (d Desktop) Transcribing() {
|
||||
d.Notify("Hyprvoice", "Transcribing...")
|
||||
type Desktop struct {
|
||||
messages map[MessageType]Message
|
||||
}
|
||||
|
||||
func (Desktop) Error(msg string) {
|
||||
func NewDesktop(messages map[MessageType]Message) *Desktop {
|
||||
return &Desktop{messages: messages}
|
||||
}
|
||||
|
||||
func (d *Desktop) Send(mt MessageType) {
|
||||
msg, ok := d.messages[mt]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if msg.IsError {
|
||||
d.Error(msg.Body)
|
||||
return
|
||||
}
|
||||
d.notify(msg.Title, msg.Body)
|
||||
}
|
||||
|
||||
func (d *Desktop) Error(msg string) {
|
||||
cmd := exec.Command("notify-send", "-a", "Hyprvoice", "-u", "critical", "Hyprvoice Error", msg)
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Printf("Failed to send error notification: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (Desktop) Notify(title, message string) {
|
||||
cmd := exec.Command("notify-send", "-a", "Hyprvoice", title, message)
|
||||
func (d *Desktop) notify(title, body string) {
|
||||
cmd := exec.Command("notify-send", "-a", "Hyprvoice", title, body)
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Printf("Failed to send notification: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type Log struct{}
|
||||
|
||||
func (l Log) Error(msg string) {
|
||||
l.Notify("Hyprvoice Error", msg)
|
||||
type Log struct {
|
||||
messages map[MessageType]Message
|
||||
}
|
||||
|
||||
func (Log) Notify(title, message string) {
|
||||
log.Printf("%s: %s", title, message)
|
||||
func NewLog(messages map[MessageType]Message) *Log {
|
||||
return &Log{messages: messages}
|
||||
}
|
||||
|
||||
func (l *Log) Send(mt MessageType) {
|
||||
msg, ok := l.messages[mt]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if msg.IsError {
|
||||
l.Error(msg.Body)
|
||||
return
|
||||
}
|
||||
log.Printf("%s: %s", msg.Title, msg.Body)
|
||||
}
|
||||
|
||||
func (l *Log) Error(msg string) {
|
||||
log.Printf("Hyprvoice Error: %s", msg)
|
||||
}
|
||||
|
||||
type Nop struct{}
|
||||
|
||||
func (Nop) Error(msg string) {}
|
||||
func (Nop) Notify(title, message string) {}
|
||||
|
||||
func GetNotifierBasedOnConfig(c *config.Config) Notifier {
|
||||
switch c.Notifications.Type {
|
||||
case "desktop":
|
||||
return Desktop{}
|
||||
case "log":
|
||||
return Log{}
|
||||
case "none":
|
||||
return Nop{}
|
||||
}
|
||||
return Nop{}
|
||||
}
|
||||
func (Nop) Send(mt MessageType) {}
|
||||
func (Nop) Error(msg string) {}
|
||||
|
||||
Reference in New Issue
Block a user