1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/gcal/widget.go
Konstantin Vorobyev e0b54d9315 gcal module improvements:
- Simplify Google OAuth client creation
- Read OAuth code via text dialog

Same approach can be re-used for gspreadsheets
2018-07-01 21:37:07 +02:00

71 lines
1.2 KiB
Go

package gcal
import (
"sync"
"time"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
calEvents []*CalEvent
ch chan struct{}
mutex sync.Mutex
app *tview.Application
}
func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Calendar ", "gcal", true),
app: app,
ch: make(chan struct{}),
}
go updateLoop(&widget)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Disable() {
close(widget.ch)
widget.TextWidget.Disable()
}
func (widget *Widget) Refresh() {
calEvents, err := Fetch(CreateCodeInputDialog(" Calendar ", widget))
if err != nil {
widget.calEvents = []*CalEvent{}
} else {
widget.calEvents = calEvents
}
widget.UpdateRefreshedAt()
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func updateLoop(widget *Widget) {
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
if interval == 0 {
return
}
tick := time.NewTicker(time.Duration(interval) * time.Second)
defer tick.Stop()
outer:
for {
select {
case <-tick.C:
widget.display()
case <-widget.ch:
break outer
}
}
}