fix sync
This commit is contained in:
parent
5bbea05305
commit
72d2ceab98
|
@ -1,7 +1,6 @@
|
|||
package component
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
|
@ -66,26 +65,27 @@ func (t *Tasks) Today() (map[string]string, error) {
|
|||
return tasks, nil
|
||||
}
|
||||
|
||||
func (t *Tasks) Sync(logger *Logger) (int, int, error) {
|
||||
func (t *Tasks) Sync() (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)) {
|
||||
// use unix timestamp for time comparison, because time.Before and
|
||||
// time.After depend on a monotonic clock and in Android the
|
||||
// monotonic clock stops ticking if the phone is in suspended sleep
|
||||
if latestFetch.Add(15*time.Minute).Unix() > time.Now().Unix() {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,6 @@ func (r *Runner) Run() {
|
|||
|
||||
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..."
|
||||
|
@ -81,33 +80,28 @@ func (r *Runner) processRequest() {
|
|||
for k, val := range v.Fields {
|
||||
r.conf.Set(k, val)
|
||||
}
|
||||
r.logger.Log("new config saved")
|
||||
r.status = "ready"
|
||||
r.logger.Log("config saved")
|
||||
case screen.SyncTasksRequest:
|
||||
r.logger.Log("starting sync request")
|
||||
r.status = "syncing..."
|
||||
r.refresh <- true
|
||||
countDisp, countFetch, err := r.tasks.Sync(r.logger)
|
||||
countDisp, countFetch, err := r.tasks.Sync()
|
||||
if err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
}
|
||||
//if countDisp > 0 || countFetch > 0 {
|
||||
r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch))
|
||||
//}
|
||||
if countDisp > 0 || countFetch > 0 {
|
||||
r.logger.Log(fmt.Sprintf("task sync: dispatched: %d, fetched: %d", countDisp, countFetch))
|
||||
}
|
||||
r.status = "ready"
|
||||
|
||||
r.logger.Log("fetching all")
|
||||
all, err := r.tasks.All()
|
||||
if err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
break
|
||||
}
|
||||
r.logger.Log("saving all")
|
||||
if err := save(r.fileURI, all); err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
break
|
||||
}
|
||||
r.logger.Log("sync request done")
|
||||
case screen.MarkTaskDoneRequest:
|
||||
if err := r.tasks.MarkDone(v.ID); err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
|
@ -117,19 +111,15 @@ func (r *Runner) processRequest() {
|
|||
r.logger.Log("request unknown")
|
||||
}
|
||||
r.refresh <- true
|
||||
r.logger.Log("processing request done")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (r *Runner) refresher() {
|
||||
for <-r.refresh {
|
||||
r.logger.Log("start refresh")
|
||||
tasks, err := r.tasks.Today()
|
||||
if err != nil {
|
||||
r.logger.Log(err.Error())
|
||||
}
|
||||
r.logger.Log(fmt.Sprintf("found %d tasks", len(tasks)))
|
||||
sTasks := []screen.Task{}
|
||||
for id, action := range tasks {
|
||||
sTasks = append(sTasks, screen.Task{
|
||||
|
|
Loading…
Reference in New Issue