Switch to Esperandro for SharedPreferences

This commit is contained in:
Bradlee Speice 2014-05-19 22:52:00 -04:00
parent d664f12d08
commit d10626efe6
6 changed files with 33 additions and 108 deletions

View File

@ -8,6 +8,7 @@ import javax.inject.Singleton;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import de.devland.esperandro.Esperandro;
import de.greenrobot.event.EventBus; import de.greenrobot.event.EventBus;
/** /**
@ -40,7 +41,7 @@ public class ActivityDownloaderModule {
@Provides //@Singleton @Provides //@Singleton
DownloadPrefs_ provideDownloadPrefs() { DownloadPrefs provideDownloadPrefs() {
return new DownloadPrefs_(MinimalBible.getApplication()); return Esperandro.getPreferences(DownloadPrefs.class, MinimalBible.getAppContext());
} }
} }

View File

@ -12,8 +12,6 @@ import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import com.f2prateek.dart.InjectExtra;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.BaseFragment; import org.bspeice.minimalbible.activities.BaseFragment;
@ -51,7 +49,7 @@ public class BookListFragment extends BaseFragment {
@Inject DownloadManager downloadManager; @Inject DownloadManager downloadManager;
@Inject DownloadPrefs_ downloadPrefs; @Inject DownloadPrefs downloadPrefs;
private ProgressDialog refreshDialog; private ProgressDialog refreshDialog;
private LayoutInflater inflater; private LayoutInflater inflater;
@ -97,7 +95,7 @@ public class BookListFragment extends BaseFragment {
* from the internet is allowable. * from the internet is allowable.
*/ */
public void displayModules() { public void displayModules() {
boolean dialogDisplayed = downloadPrefs.showedDownloadDialog().get(); boolean dialogDisplayed = downloadPrefs.hasShownDownloadDialog();
if (!dialogDisplayed) { if (!dialogDisplayed) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
@ -168,12 +166,12 @@ public class BookListFragment extends BaseFragment {
DialogInterface.OnClickListener { DialogInterface.OnClickListener {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
downloadPrefs.showedDownloadDialog().put(true); downloadPrefs.hasShownDownloadDialog(true);
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
// Clicked ready to continue - allow downloading in the future // Clicked ready to continue - allow downloading in the future
downloadPrefs.hasEnabledDownload().put(true); downloadPrefs.hasEnabledDownload(true);
// And warn them that it has been enabled in the future. // And warn them that it has been enabled in the future.
Toast.makeText(getActivity(), Toast.makeText(getActivity(),
@ -184,7 +182,7 @@ public class BookListFragment extends BaseFragment {
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE:
// Clicked to not download - Permanently disable downloading // Clicked to not download - Permanently disable downloading
downloadPrefs.hasEnabledDownload().put(false); downloadPrefs.hasEnabledDownload(false);
Toast.makeText(getActivity(), Toast.makeText(getActivity(),
"Disabling downloading. Re-enable it in settings.", "Disabling downloading. Re-enable it in settings.",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();

View File

@ -0,0 +1,20 @@
package org.bspeice.minimalbible.activities.downloader;
import de.devland.esperandro.annotations.SharedPreferences;
/**
* Created by bspeice on 5/19/14.
*/
@SharedPreferences(name="DownloadPrefs")
public interface DownloadPrefs {
boolean hasEnabledDownload();
void hasEnabledDownload(boolean hasEnabledDownload);
boolean hasShownDownloadDialog();
void hasShownDownloadDialog(boolean hasShownDownloadDialog);
long downloadRefreshedOn();
void downloadRefreshedOn(long downloadRefreshedOn);
}

View File

@ -1,72 +0,0 @@
/*
This is brutally ugly, but until https://github.com/square/dagger/issues/410 is resolved,
this is the best I can do while making sure that I can easily switch the API later
*/
//
// DO NOT EDIT THIS FILE, IT HAS BEEN GENERATED USING AndroidAnnotations 3.0.1.
//
package org.bspeice.minimalbible.activities.downloader;
import android.content.Context;
import android.content.SharedPreferences;
import org.androidannotations.api.sharedpreferences.BooleanPrefEditorField;
import org.androidannotations.api.sharedpreferences.BooleanPrefField;
import org.androidannotations.api.sharedpreferences.EditorHelper;
import org.androidannotations.api.sharedpreferences.LongPrefEditorField;
import org.androidannotations.api.sharedpreferences.LongPrefField;
import org.androidannotations.api.sharedpreferences.SharedPreferencesHelper;
public final class DownloadPrefs_
extends SharedPreferencesHelper
{
private Context context_;
public DownloadPrefs_(Context context) {
super(context.getSharedPreferences("DownloadPrefs", 0));
this.context_ = context;
}
public DownloadPrefs_.DownloadPrefsEditor_ edit() {
return new DownloadPrefs_.DownloadPrefsEditor_(getSharedPreferences());
}
public BooleanPrefField hasEnabledDownload() {
return booleanField("hasEnabledDownload", false);
}
public BooleanPrefField showedDownloadDialog() {
return booleanField("showedDownloadDialog", false);
}
public LongPrefField downloadRefreshedOn() {
return longField("downloadRefreshedOn", 0L);
}
public final static class DownloadPrefsEditor_
extends EditorHelper<DownloadPrefs_.DownloadPrefsEditor_>
{
DownloadPrefsEditor_(SharedPreferences sharedPreferences) {
super(sharedPreferences);
}
public BooleanPrefEditorField<DownloadPrefs_.DownloadPrefsEditor_> hasEnabledDownload() {
return booleanField("hasEnabledDownload");
}
public BooleanPrefEditorField<DownloadPrefs_.DownloadPrefsEditor_> showedDownloadDialog() {
return booleanField("showedDownloadDialog");
}
public LongPrefEditorField<DownloadPrefs_.DownloadPrefsEditor_> downloadRefreshedOn() {
return longField("downloadRefreshedOn");
}
}
}

View File

@ -1,22 +0,0 @@
package org.bspeice.minimalbible.activities.downloader;
import org.androidannotations.annotations.sharedpreferences.DefaultBoolean;
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
/**
* Renamed while waiting for https://github.com/square/dagger/issues/410 to get resolved.
* Once the issue is fixed, this should go back to being DownloadPrefs
*/
@SharedPref(value= SharedPref.Scope.UNIQUE)
public interface _DownloadPrefs {
@DefaultBoolean(false)
boolean hasEnabledDownload();
@DefaultBoolean(false)
boolean showedDownloadDialog();
@DefaultLong(0)
long downloadRefreshedOn();
}

View File

@ -7,7 +7,7 @@ import android.os.AsyncTask;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activities.downloader.DownloadPrefs_; import org.bspeice.minimalbible.activities.downloader.DownloadPrefs;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.InstallException; import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
@ -26,7 +26,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis
@Inject @Inject
DownloadPrefs_ downloadPrefs; DownloadPrefs downloadPrefs;
private EventBus downloadBus; private EventBus downloadBus;
@ -44,7 +44,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
if (doRefresh()) { if (doRefresh()) {
try { try {
i.reloadBookList(); i.reloadBookList();
downloadPrefs.downloadRefreshedOn().put(System.currentTimeMillis()); downloadPrefs.downloadRefreshedOn(System.currentTimeMillis());
} catch (InstallException e) { } catch (InstallException e) {
Log.e(TAG, Log.e(TAG,
"Error downloading books from installer: " "Error downloading books from installer: "
@ -76,10 +76,10 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
} }
private boolean downloadEnabled() { private boolean downloadEnabled() {
return downloadPrefs.hasEnabledDownload().get(); return downloadPrefs.hasEnabledDownload();
} }
private boolean needsRefresh() { private boolean needsRefresh() {
return (downloadPrefs.downloadRefreshedOn().get() > refreshAfter); return (downloadPrefs.downloadRefreshedOn() > refreshAfter);
} }
} }