Remove SyncProviderUtilities

pull/189/head
Alex Baker 10 years ago
parent d7b6ebbc58
commit 7ab8d3a811

@ -5,7 +5,7 @@
*/ */
package com.todoroo.astrid.gtasks; package com.todoroo.astrid.gtasks;
import com.todoroo.astrid.sync.SyncProviderUtilities; import com.todoroo.andlib.utility.DateUtilities;
import org.tasks.R; import org.tasks.R;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
@ -20,7 +20,9 @@ import javax.inject.Singleton;
* *
*/ */
@Singleton @Singleton
public class GtasksPreferenceService extends SyncProviderUtilities { public class GtasksPreferenceService {
private final Preferences preferences;
public static final String IDENTIFIER = "gtasks"; //$NON-NLS-1$ public static final String IDENTIFIER = "gtasks"; //$NON-NLS-1$
@ -29,15 +31,13 @@ public class GtasksPreferenceService extends SyncProviderUtilities {
@Inject @Inject
public GtasksPreferenceService(Preferences preferences) { public GtasksPreferenceService(Preferences preferences) {
super(preferences); this.preferences = preferences;
} }
@Override
public String getIdentifier() { public String getIdentifier() {
return IDENTIFIER; return IDENTIFIER;
} }
@Override
public int getSyncIntervalKey() { public int getSyncIntervalKey() {
return R.string.gtasks_GPr_interval_key; return R.string.gtasks_GPr_interval_key;
} }
@ -57,4 +57,79 @@ public class GtasksPreferenceService extends SyncProviderUtilities {
public void setUserName(String userName) { public void setUserName(String userName) {
preferences.setString(PREF_USER_NAME, userName); preferences.setString(PREF_USER_NAME, userName);
} }
protected static final String PREF_TOKEN = "_token"; //$NON-NLS-1$
protected static final String PREF_LAST_SYNC = "_last_sync"; //$NON-NLS-1$
protected static final String PREF_LAST_ATTEMPTED_SYNC = "_last_attempted"; //$NON-NLS-1$
protected static final String PREF_LAST_ERROR = "_last_err"; //$NON-NLS-1$
protected static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$
/**
* @return true if we have a token for this user, false otherwise
*/
public boolean isLoggedIn() {
return preferences.getStringValue(getIdentifier() + PREF_TOKEN) != null;
}
/** authentication token, or null if doesn't exist */
public String getToken() {
return preferences.getStringValue(getIdentifier() + PREF_TOKEN);
}
/** Sets the authentication token. Set to null to clear. */
public void setToken(String setting) {
preferences.setString(getIdentifier() + PREF_TOKEN, setting);
}
/** @return Last Successful Sync Date, or 0 */
public long getLastSyncDate() {
return preferences.getLong(getIdentifier() + PREF_LAST_SYNC, 0);
}
/** @return Last Attempted Sync Date, or 0 if it was successful */
public long getLastAttemptedSyncDate() {
return preferences.getLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0);
}
/** @return Last Error, or null if no last error */
public String getLastError() {
return preferences.getStringValue(getIdentifier() + PREF_LAST_ERROR);
}
/** @return Last Error, or null if no last error */
public boolean isOngoing() {
return preferences.getBoolean(getIdentifier() + PREF_ONGOING, false);
}
/** Deletes Last Successful Sync Date */
public void clearLastSyncDate() {
preferences.clear(getIdentifier() + PREF_LAST_SYNC);
}
/** Set Last Error */
public void setLastError(String error) {
preferences.setString(getIdentifier() + PREF_LAST_ERROR, error);
}
/** Set Ongoing */
public void stopOngoing() {
preferences.setBoolean(getIdentifier() + PREF_ONGOING, false);
}
/** Set Last Successful Sync Date */
public void recordSuccessfulSync() {
preferences.setLong(getIdentifier() + PREF_LAST_SYNC, DateUtilities.now() + 1000);
preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0);
}
/** Set Last Attempted Sync Date */
public void recordSyncStart() {
preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, DateUtilities.now());
preferences.clear(getIdentifier() + PREF_LAST_ERROR);
preferences.setBoolean(getIdentifier() + PREF_ONGOING, true);
}
} }

@ -9,7 +9,6 @@ import android.content.Intent;
import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity; import com.todoroo.astrid.gtasks.auth.GtasksLoginActivity;
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider; import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
import com.todoroo.astrid.sync.SyncProviderUtilities;
import org.tasks.R; import org.tasks.R;
import org.tasks.injection.InjectingSyncProviderPreferences; import org.tasks.injection.InjectingSyncProviderPreferences;
@ -71,7 +70,7 @@ public class GtasksPreferences extends InjectingSyncProviderPreferences {
} }
@Override @Override
public SyncProviderUtilities getUtilities() { public GtasksPreferenceService getUtilities() {
return gtasksPreferenceService; return gtasksPreferenceService;
} }

@ -24,8 +24,8 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.api.SyncAction; import com.todoroo.astrid.api.SyncAction;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.service.SyncV2Service; import com.todoroo.astrid.service.SyncV2Service;
import com.todoroo.astrid.sync.SyncProviderUtilities;
import com.todoroo.astrid.sync.SyncResultCallback; import com.todoroo.astrid.sync.SyncResultCallback;
import com.todoroo.astrid.sync.SyncV2Provider; import com.todoroo.astrid.sync.SyncV2Provider;
@ -69,12 +69,12 @@ public class SyncActionHelper {
// --- boilerplate // --- boilerplate
public SyncActionHelper(SyncProviderUtilities syncProviderUtilities, SyncV2Service syncService, final FragmentActivity activity, Preferences preferences, Fragment fragment) { public SyncActionHelper(GtasksPreferenceService gtasksPreferenceService, SyncV2Service syncService, final FragmentActivity activity, Preferences preferences, Fragment fragment) {
this.syncService = syncService; this.syncService = syncService;
this.activity = activity; this.activity = activity;
this.preferences = preferences; this.preferences = preferences;
this.fragment = fragment; this.fragment = fragment;
syncResultCallback = new IndeterminateProgressBarSyncResultCallback(syncProviderUtilities, activity, new Runnable() { syncResultCallback = new IndeterminateProgressBarSyncResultCallback(gtasksPreferenceService, activity, new Runnable() {
@Override @Override
public void run() { public void run() {
activity.sendBroadcast( activity.sendBroadcast(

@ -21,6 +21,7 @@ import android.view.ViewGroup.OnHierarchyChangeListener;
import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.utility.TodorooPreferenceActivity; import com.todoroo.astrid.utility.TodorooPreferenceActivity;
import org.tasks.api.R; import org.tasks.api.R;
@ -62,7 +63,7 @@ abstract public class SyncProviderPreferences extends TodorooPreferenceActivity
/** /**
* @return get preference utilities * @return get preference utilities
*/ */
abstract public SyncProviderUtilities getUtilities(); abstract public GtasksPreferenceService getUtilities();
protected static final int REQUEST_LOGIN = 0; protected static final int REQUEST_LOGIN = 0;

@ -1,109 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.sync;
import com.todoroo.andlib.utility.DateUtilities;
import org.tasks.preferences.Preferences;
/**
* Sync Provider Utility class for accessing preferences
*/
abstract public class SyncProviderUtilities {
protected final Preferences preferences;
public SyncProviderUtilities(Preferences preferences) {
this.preferences = preferences;
}
/**
* @return your plugin identifier
*/
abstract public String getIdentifier();
/**
* @return key for sync interval
*/
abstract public int getSyncIntervalKey();
// --- implementation
protected static final String PREF_TOKEN = "_token"; //$NON-NLS-1$
protected static final String PREF_LAST_SYNC = "_last_sync"; //$NON-NLS-1$
protected static final String PREF_LAST_ATTEMPTED_SYNC = "_last_attempted"; //$NON-NLS-1$
protected static final String PREF_LAST_ERROR = "_last_err"; //$NON-NLS-1$
protected static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$
/**
* @return true if we have a token for this user, false otherwise
*/
public boolean isLoggedIn() {
return preferences.getStringValue(getIdentifier() + PREF_TOKEN) != null;
}
/** authentication token, or null if doesn't exist */
public String getToken() {
return preferences.getStringValue(getIdentifier() + PREF_TOKEN);
}
/** Sets the authentication token. Set to null to clear. */
public void setToken(String setting) {
preferences.setString(getIdentifier() + PREF_TOKEN, setting);
}
/** @return Last Successful Sync Date, or 0 */
public long getLastSyncDate() {
return preferences.getLong(getIdentifier() + PREF_LAST_SYNC, 0);
}
/** @return Last Attempted Sync Date, or 0 if it was successful */
public long getLastAttemptedSyncDate() {
return preferences.getLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0);
}
/** @return Last Error, or null if no last error */
public String getLastError() {
return preferences.getStringValue(getIdentifier() + PREF_LAST_ERROR);
}
/** @return Last Error, or null if no last error */
public boolean isOngoing() {
return preferences.getBoolean(getIdentifier() + PREF_ONGOING, false);
}
/** Deletes Last Successful Sync Date */
public void clearLastSyncDate() {
preferences.clear(getIdentifier() + PREF_LAST_SYNC);
}
/** Set Last Error */
public void setLastError(String error) {
preferences.setString(getIdentifier() + PREF_LAST_ERROR, error);
}
/** Set Ongoing */
public void stopOngoing() {
preferences.setBoolean(getIdentifier() + PREF_ONGOING, false);
}
/** Set Last Successful Sync Date */
public void recordSuccessfulSync() {
preferences.setLong(getIdentifier() + PREF_LAST_SYNC, DateUtilities.now() + 1000);
preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, 0);
}
/** Set Last Attempted Sync Date */
public void recordSyncStart() {
preferences.setLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, DateUtilities.now());
preferences.clear(getIdentifier() + PREF_LAST_ERROR);
preferences.setBoolean(getIdentifier() + PREF_ONGOING, true);
}
}

@ -5,6 +5,8 @@
*/ */
package com.todoroo.astrid.sync; package com.todoroo.astrid.sync;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -48,7 +50,7 @@ abstract public class SyncV2Provider {
/** /**
* @return sync utility instance * @return sync utility instance
*/ */
abstract protected SyncProviderUtilities getUtilities(); abstract protected GtasksPreferenceService getUtilities();
protected void finishSync(SyncResultCallback callback) { protected void finishSync(SyncResultCallback callback) {
getUtilities().recordSuccessfulSync(); getUtilities().recordSuccessfulSync();

@ -2,7 +2,7 @@ package org.tasks.sync;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import com.todoroo.astrid.sync.SyncProviderUtilities; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -14,8 +14,8 @@ public class IndeterminateProgressBarSyncResultCallback extends RecordSyncStatus
private final FragmentActivity activity; private final FragmentActivity activity;
private Runnable onFinished; private Runnable onFinished;
public IndeterminateProgressBarSyncResultCallback(SyncProviderUtilities syncProviderUtilities, FragmentActivity activity, Runnable onFinished) { public IndeterminateProgressBarSyncResultCallback(GtasksPreferenceService gtasksPreferenceService, FragmentActivity activity, Runnable onFinished) {
super(syncProviderUtilities); super(gtasksPreferenceService);
this.activity = activity; this.activity = activity;
this.onFinished = onFinished; this.onFinished = onFinished;

@ -1,32 +1,32 @@
package org.tasks.sync; package org.tasks.sync;
import com.todoroo.astrid.sync.SyncProviderUtilities; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.sync.SyncResultCallback; import com.todoroo.astrid.sync.SyncResultCallback;
import org.tasks.Broadcaster; import org.tasks.Broadcaster;
public class RecordSyncStatusCallback implements SyncResultCallback { public class RecordSyncStatusCallback implements SyncResultCallback {
private SyncProviderUtilities syncProviderUtilities; private GtasksPreferenceService gtasksPreferenceService;
private Broadcaster broadcaster; private Broadcaster broadcaster;
public RecordSyncStatusCallback(SyncProviderUtilities syncProviderUtilities) { public RecordSyncStatusCallback(GtasksPreferenceService gtasksPreferenceService) {
this(syncProviderUtilities, null); this(gtasksPreferenceService, null);
} }
public RecordSyncStatusCallback(SyncProviderUtilities syncProviderUtilities, Broadcaster broadcaster) { public RecordSyncStatusCallback(GtasksPreferenceService gtasksPreferenceService, Broadcaster broadcaster) {
this.syncProviderUtilities = syncProviderUtilities; this.gtasksPreferenceService = gtasksPreferenceService;
this.broadcaster = broadcaster; this.broadcaster = broadcaster;
} }
@Override @Override
public void started() { public void started() {
syncProviderUtilities.recordSyncStart(); gtasksPreferenceService.recordSyncStart();
} }
@Override @Override
public void finished() { public void finished() {
syncProviderUtilities.stopOngoing(); gtasksPreferenceService.stopOngoing();
if (broadcaster != null) { if (broadcaster != null) {
broadcaster.eventRefresh(); broadcaster.eventRefresh();
} }

Loading…
Cancel
Save