github.com/wtfutil/wtf@v0.43.0/modules/gcal/widget.go (about) 1 package gcal 2 3 import ( 4 "github.com/rivo/tview" 5 "github.com/wtfutil/wtf/view" 6 ) 7 8 type Widget struct { 9 view.TextWidget 10 11 calEvents []*CalEvent 12 err error 13 settings *Settings 14 tviewApp *tview.Application 15 } 16 17 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget { 18 widget := Widget{ 19 TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common), 20 21 tviewApp: tviewApp, 22 settings: settings, 23 } 24 25 return &widget 26 } 27 28 /* -------------------- Exported Functions -------------------- */ 29 30 func (widget *Widget) Disable() { 31 widget.TextWidget.Disable() 32 } 33 34 func (widget *Widget) Refresh() { 35 if isAuthenticated(widget.settings.email) { 36 widget.fetchAndDisplayEvents() 37 return 38 } 39 40 widget.tviewApp.Suspend(widget.authenticate) 41 widget.Refresh() 42 } 43 44 /* -------------------- Unexported Functions -------------------- */ 45 46 func (widget *Widget) fetchAndDisplayEvents() { 47 calEvents, err := widget.Fetch() 48 if err != nil { 49 widget.err = err 50 widget.calEvents = []*CalEvent{} 51 } else { 52 widget.err = nil 53 widget.calEvents = calEvents 54 } 55 56 widget.display() 57 }