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