moved routines, detailed logging removed refresh button
This commit is contained in:
parent
712b39ee0f
commit
8bed9a0d9b
|
@ -16,7 +16,7 @@ func NewLogger() *Logger {
|
|||
}
|
||||
|
||||
func (l *Logger) Log(line string) {
|
||||
l.lines = append(l.lines, fmt.Sprintf("%s: %s", time.Now().Format("15:04:05"), line))
|
||||
l.lines = append(l.lines, fmt.Sprintf("%s: %s", time.Now().Format(time.Stamp), line))
|
||||
if len(l.lines) > 50 {
|
||||
l.lines = l.lines[1:]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package component
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
|
@ -49,24 +50,26 @@ func (t *Tasks) Today() (map[string]string, error) {
|
|||
return tasks, nil
|
||||
}
|
||||
|
||||
func (t *Tasks) Sync() (int, int, error) {
|
||||
func (t *Tasks) Sync(logger *Logger) (int, int, error) {
|
||||
countDisp, err := process.NewSend(t.local, t.disp).Process()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
logger.Log("finished dispatch")
|
||||
|
||||
latestFetch, err := t.local.LatestSync()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
logger.Log(fmt.Sprintf("latest fetch time, was at %s", latestFetch.Format(time.Stamp)))
|
||||
if time.Now().Before(latestFetch.Add(15 * time.Minute)) {
|
||||
return countDisp, 0, nil
|
||||
}
|
||||
|
||||
res, err := process.NewFetch(t.remote, t.local).Process()
|
||||
if err != nil {
|
||||
return countDisp, 0, err
|
||||
}
|
||||
logger.Log("fininshed actual fetch")
|
||||
return countDisp, res.Count, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,22 @@
|
|||
package main
|
||||
|
||||
import "ewintr.nl/gte/cmd/android-app/runner"
|
||||
import (
|
||||
"ewintr.nl/gte/cmd/android-app/component"
|
||||
"ewintr.nl/gte/cmd/android-app/runner"
|
||||
"fyne.io/fyne/v2/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
runner := runner.NewRunner()
|
||||
runner.Init()
|
||||
runner.Run()
|
||||
fyneApp := app.NewWithID("nl.ewintr.gte")
|
||||
w := fyneApp.NewWindow("gte - getting things email")
|
||||
conf := component.NewConfigurationFromPreferences(fyneApp.Preferences())
|
||||
conf.Load()
|
||||
logger := component.NewLogger()
|
||||
|
||||
runner := runner.NewRunner(conf, logger)
|
||||
tabs := runner.Init()
|
||||
w.SetContent(tabs)
|
||||
go runner.Run()
|
||||
|
||||
w.ShowAndRun()
|
||||
}
|
||||
|
|
|
@ -2,15 +2,17 @@ package runner
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"ewintr.nl/gte/cmd/android-app/component"
|
||||
"ewintr.nl/gte/cmd/android-app/screen"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/app"
|
||||
"fyne.io/fyne/v2/container"
|
||||
)
|
||||
|
||||
var runnerLock = sync.Mutex{}
|
||||
|
||||
type Runner struct {
|
||||
fyneApp fyne.App
|
||||
fyneWindow fyne.Window
|
||||
|
@ -23,24 +25,22 @@ type Runner struct {
|
|||
refresh chan bool
|
||||
}
|
||||
|
||||
func NewRunner() *Runner {
|
||||
func NewRunner(conf *component.Configuration, logger *component.Logger) *Runner {
|
||||
return &Runner{
|
||||
requests: make(chan interface{}, 3),
|
||||
status: "init",
|
||||
conf: conf,
|
||||
logger: logger,
|
||||
requests: make(chan interface{}),
|
||||
refresh: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Runner) Init() {
|
||||
fyneApp := app.NewWithID("nl.ewintr.gte")
|
||||
w := fyneApp.NewWindow("gte - getting things email")
|
||||
func (r *Runner) Init() fyne.CanvasObject {
|
||||
|
||||
r.logger = component.NewLogger()
|
||||
logScreen := screen.NewLog()
|
||||
logTab := container.NewTabItem("log", logScreen.Content())
|
||||
r.screens = append(r.screens, logScreen)
|
||||
|
||||
r.conf = component.NewConfigurationFromPreferences(fyneApp.Preferences())
|
||||
r.conf.Load()
|
||||
configScreen := screen.NewConfig(r.requests)
|
||||
configTab := container.NewTabItem("config", configScreen.Content())
|
||||
r.screens = append(r.screens, configScreen)
|
||||
|
@ -55,24 +55,18 @@ func (r *Runner) Init() {
|
|||
r.screens = append(r.screens, taskScreen)
|
||||
tabs := container.NewAppTabs(taskTab, configTab, logTab)
|
||||
|
||||
w.SetContent(tabs)
|
||||
|
||||
r.fyneApp = fyneApp
|
||||
r.fyneWindow = w
|
||||
r.logger.Log("app started")
|
||||
|
||||
r.requests <- screen.SyncTasksRequest{}
|
||||
return tabs
|
||||
}
|
||||
|
||||
func (r *Runner) Run() {
|
||||
go r.refresher()
|
||||
go r.processRequest()
|
||||
go r.backgroundSync()
|
||||
r.fyneWindow.ShowAndRun()
|
||||
r.backgroundSync()
|
||||
}
|
||||
|
||||
func (r *Runner) processRequest() {
|
||||
for req := range r.requests {
|
||||
r.logger.Log(fmt.Sprintf("processing request %T", req))
|
||||
switch v := req.(type) {
|
||||
case screen.SaveConfigRequest:
|
||||
r.status = "saving..."
|
||||
|
@ -82,33 +76,37 @@ func (r *Runner) processRequest() {
|
|||
}
|
||||
r.logger.Log("new config saved")
|
||||
r.status = "ready"
|
||||
r.refresh <- true
|
||||
case screen.SyncTasksRequest:
|
||||
r.logger.Log("starting sync request")
|
||||
r.status = "syncing..."
|
||||
r.refresh <- true
|
||||
countDisp, countFetch, err := r.tasks.Sync()
|
||||
countDisp, countFetch, err := r.tasks.Sync(r.logger)
|
||||
if err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
}
|
||||
if countDisp > 0 || countFetch > 0 {
|
||||
//if countDisp > 0 || countFetch > 0 {
|
||||
r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch))
|
||||
}
|
||||
//}
|
||||
r.status = "ready"
|
||||
r.refresh <- true
|
||||
r.logger.Log("sync request done")
|
||||
|
||||
case screen.MarkTaskDoneRequest:
|
||||
if err := r.tasks.MarkDone(v.ID); err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
}
|
||||
r.logger.Log(fmt.Sprintf("marked task %q done", v.ID))
|
||||
r.refresh <- true
|
||||
default:
|
||||
r.logger.Log("request unknown")
|
||||
}
|
||||
r.refresh <- true
|
||||
r.logger.Log("processing request done")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (r *Runner) refresher() {
|
||||
for range r.refresh {
|
||||
for <-r.refresh {
|
||||
r.logger.Log("start refresh")
|
||||
tasks, err := r.tasks.Today()
|
||||
if err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
|
@ -127,6 +125,7 @@ func (r *Runner) refresher() {
|
|||
Config: r.conf.Fields(),
|
||||
Logs: r.logger.Lines(),
|
||||
}
|
||||
|
||||
for _, s := range r.screens {
|
||||
s.Refresh(state)
|
||||
}
|
||||
|
@ -134,9 +133,9 @@ func (r *Runner) refresher() {
|
|||
}
|
||||
|
||||
func (r *Runner) backgroundSync() {
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
ticker := time.NewTicker(15 * time.Second)
|
||||
for {
|
||||
<-ticker.C
|
||||
r.requests <- screen.SyncTasksRequest{}
|
||||
<-ticker.C
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package screen
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/data/binding"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
|
@ -19,11 +17,7 @@ func NewLog() *Log {
|
|||
}
|
||||
|
||||
func (l *Log) Refresh(state State) {
|
||||
lines := state.Logs
|
||||
sort.Slice(lines, func(i, j int) bool {
|
||||
return lines[i] > lines[j]
|
||||
})
|
||||
l.lines.Set(lines)
|
||||
l.lines.Set(state.Logs)
|
||||
}
|
||||
|
||||
func (l *Log) Content() fyne.CanvasObject {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package screen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
|
@ -33,7 +34,7 @@ func NewTasks(out chan interface{}) *Tasks {
|
|||
}
|
||||
|
||||
func (t *Tasks) Refresh(state State) {
|
||||
t.status.Set(state.Status)
|
||||
t.status.Set(fmt.Sprintf("> %s", state.Status))
|
||||
t.tasks = state.Tasks
|
||||
sort.Slice(t.tasks, func(i, j int) bool {
|
||||
return t.tasks[i].Action < t.tasks[j].Action
|
||||
|
@ -46,10 +47,9 @@ func (t *Tasks) Refresh(state State) {
|
|||
}
|
||||
|
||||
func (t *Tasks) Content() fyne.CanvasObject {
|
||||
statusLabel := widget.NewLabelWithData(t.status)
|
||||
refreshButton := widget.NewButton("refresh", func() {
|
||||
t.out <- SyncTasksRequest{}
|
||||
})
|
||||
statusLabel := widget.NewLabel("> init...")
|
||||
statusLabel.Bind(t.status)
|
||||
statusLabel.TextStyle.Italic = true
|
||||
doneButton := widget.NewButton("done", func() {
|
||||
t.markDone()
|
||||
})
|
||||
|
@ -65,7 +65,7 @@ func (t *Tasks) Content() fyne.CanvasObject {
|
|||
list.OnSelected = t.selectItem
|
||||
|
||||
return container.NewBorder(
|
||||
container.NewHBox(refreshButton, statusLabel),
|
||||
container.NewHBox(statusLabel),
|
||||
doneButton,
|
||||
nil,
|
||||
nil,
|
||||
|
|
Loading…
Reference in New Issue