From 61ff44a6ed60e4d41cdff7685cff0c4226bf959d Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 26 Jun 2014 15:01:32 -0500 Subject: [PATCH] Add SyncThrottle --- .../astrid/gtasks/GtasksListFragment.java | 4 ++- .../todoroo/astrid/service/SyncThrottle.java | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 astrid/src/main/java/com/todoroo/astrid/service/SyncThrottle.java diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksListFragment.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksListFragment.java index 567702170..028449109 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksListFragment.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksListFragment.java @@ -23,6 +23,7 @@ import com.todoroo.astrid.dao.TaskAttachmentDao; import com.todoroo.astrid.data.StoreObject; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.service.MetadataService; +import com.todoroo.astrid.service.SyncThrottle; import com.todoroo.astrid.service.SyncV2Service; import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.subtasks.OrderedListFragmentHelperInterface; @@ -49,6 +50,7 @@ public class GtasksListFragment extends SubtasksListFragment { @Inject @ForActivity Context context; @Inject TaskAttachmentDao taskAttachmentDao; @Inject ActivityPreferences preferences; + @Inject SyncThrottle syncThrottle; private StoreObject list; @@ -80,7 +82,7 @@ public class GtasksListFragment extends SubtasksListFragment { if (!isCurrentTaskListFragment()) { return; } - if (list != null && DateUtilities.now() - list.getValue(GtasksList.LAST_SYNC) > DateUtilities.ONE_MINUTE * 10) { + if (list != null && syncThrottle.canSync(list.getId())) { refreshData(false); } } diff --git a/astrid/src/main/java/com/todoroo/astrid/service/SyncThrottle.java b/astrid/src/main/java/com/todoroo/astrid/service/SyncThrottle.java new file mode 100644 index 000000000..75d6ffab1 --- /dev/null +++ b/astrid/src/main/java/com/todoroo/astrid/service/SyncThrottle.java @@ -0,0 +1,26 @@ +package com.todoroo.astrid.service; + +import org.joda.time.DateTime; + +import java.util.HashMap; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +@Singleton +public class SyncThrottle { + + private final Map lastSync = new HashMap<>(); + + @Inject + public SyncThrottle() { + } + + public synchronized boolean canSync(long listId) { + DateTime now = new DateTime(); + DateTime last = lastSync.get(listId); + lastSync.put(listId, now); + return last == null || last.isBefore(now.minusMinutes(10)); + } +}