Issues with Dagger and Android Annotations, broken

ugly-unit-test
DjBushido 2014-05-08 23:47:07 -04:00
parent a046e9fa0a
commit e88c68b1e3
7 changed files with 55 additions and 84 deletions

View File

@ -18,11 +18,11 @@ dependencies {
compile project(path: ':jsword-minimalbible', configuration: 'buildJSword')
compile project(':appcompat_v7')
// apt "org.androidannotations:androidannotations:3.0+"
// compile "org.androidannotations:androidannotations-api:3.0+"
apt "org.androidannotations:androidannotations:3.0+"
compile "org.androidannotations:androidannotations-api:3.0+"
apt 'com.squareup.dagger:dagger-compiler:1.2.1+'
compile 'com.squareup.dagger:dagger:1.2.1+'
apt 'com.squareup.dagger:dagger-compiler:1.2.0'
compile 'com.squareup.dagger:dagger:1.2.0'
apt 'com.jakewharton:butterknife:5.0.1'
compile 'com.jakewharton:butterknife:5.0.1'

View File

@ -19,6 +19,14 @@ public class MinimalBible extends Application {
return instance;
}
public static MinimalBible getApplication(Context ctx) {
return (MinimalBible)ctx.getApplicationContext();
}
public static MinimalBible getApplication() {
return (MinimalBible)getAppContext();
}
@Override
public void onCreate() {
graph = ObjectGraph.create(new MinimalBibleModules());
@ -28,12 +36,4 @@ public class MinimalBible extends Application {
public void inject(Object o) {
graph.inject(o);
}
public static MinimalBible getApplication(Context ctx) {
return (MinimalBible)ctx.getApplicationContext();
}
public static MinimalBible getApplication() {
return (MinimalBible)getAppContext();
}
}

View File

@ -25,8 +25,6 @@ import de.greenrobot.event.EventBus;
)
public class ActivityDownloaderModule {
private final Context ctx = MinimalBible.getAppContext();
/**
* Provide a Singleton DownloadManager for injection
* Note that we need to annotate Singleton here, only annotating on the
@ -43,8 +41,8 @@ public class ActivityDownloaderModule {
return new EventBus();
}
@Provides @Singleton
DownloadPrefsManager providePrefsManager() {
return new DownloadPrefsManager(ctx);
@Provides //@Singleton
DownloadPrefs_ provideDownloadPrefs() {
return new DownloadPrefs_(MinimalBible.getApplication());
}
}

View File

@ -16,6 +16,7 @@ import android.widget.Toast;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
@ -48,13 +49,14 @@ public class BookListFragment extends Fragment {
ListView downloadsAvailable;
@Inject DownloadManager downloadManager;
@Inject DownloadPrefsManager prefsManager;
@Inject DownloadPrefs_ downloadPrefs;
private ProgressDialog refreshDialog;
/**
* Returns a new instance of this fragment for the given section number.
* TODO: This will need to be switched to an @Provides class
* TODO: This will need to be switched to an @Provides class maybe?
*/
public static BookListFragment newInstance(BookCategory c) {
BookListFragment fragment = new BookListFragment();
@ -67,8 +69,9 @@ public class BookListFragment extends Fragment {
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
MinimalBible app = MinimalBible.getApplication(getActivity());
app.inject(this);
//TODO: Figure out why this doesn't work. Best guess is because the context from
//getApplication(getActivity()) isn't actually MinimalBible.getAppContext()
MinimalBible.getApplication().inject(this);
}
@Override
@ -89,7 +92,7 @@ public class BookListFragment extends Fragment {
}
public void displayModules() {
boolean dialogDisplayed = prefsManager.getShowedDownloadDialog();
boolean dialogDisplayed = downloadPrefs.showedDownloadDialog().get();
if (!dialogDisplayed) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
@ -154,12 +157,12 @@ public class BookListFragment extends Fragment {
DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
prefsManager.setShowedDownloadDialog(true);
downloadPrefs.showedDownloadDialog().put(true);
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// Clicked ready to continue - allow downloading in the future
prefsManager.setDownloadEnabled(true);
downloadPrefs.hasEnabledDownload().put(true);
// And warn them that it has been enabled in the future.
Toast.makeText(getActivity(),
@ -170,7 +173,7 @@ public class BookListFragment extends Fragment {
case DialogInterface.BUTTON_NEGATIVE:
// Clicked to not download - Permanently disable downloading
prefsManager.setDownloadEnabled(false);
downloadPrefs.hasEnabledDownload().put(false);
Toast.makeText(getActivity(),
"Disabling downloading. Re-enable it in settings.",
Toast.LENGTH_SHORT).show();

View File

@ -0,0 +1,21 @@
package org.bspeice.minimalbible.activities.downloader;
import org.androidannotations.annotations.sharedpreferences.DefaultBoolean;
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
/**
* Created by Bradlee Speice on 5/8/2014.
*/
@SharedPref(value= SharedPref.Scope.UNIQUE)
public interface DownloadPrefs {
@DefaultBoolean(false)
boolean hasEnabledDownload();
@DefaultBoolean(false)
boolean showedDownloadDialog();
@DefaultLong(0)
long downloadRefreshedOn();
}

View File

@ -1,51 +0,0 @@
package org.bspeice.minimalbible.activities.downloader;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.Date;
import javax.inject.Singleton;
/**
* Created by Bradlee Speice on 5/8/2014.
*/
@Singleton
public class DownloadPrefsManager {
private final SharedPreferences prefs;
public static final String DOWNLOAD_PREFS_FILE = "DOWNLOADER_PREFERENCES";
public static final String KEY_DOWNLOAD_ENABLED = "HAS_ENABLED_DOWNLOAD";
public static final String KEY_SHOWED_DOWNLOAD_DIALOG = "SHOWED_DOWNLOAD_DIALOG";
public static final String KEY_DOWNLOAD_REFRESHED_ON = "DOWNLOAD_REFRESHED_ON";
public DownloadPrefsManager(Context ctx) {
prefs = ctx.getSharedPreferences(DOWNLOAD_PREFS_FILE, Context.MODE_PRIVATE);
}
public boolean getDownloadEnabled() {
return prefs.getBoolean(KEY_DOWNLOAD_ENABLED, false);
}
public void setDownloadEnabled(boolean val) {
prefs.edit().putBoolean(KEY_DOWNLOAD_ENABLED, val).commit();
}
public boolean getShowedDownloadDialog() {
return prefs.getBoolean(KEY_SHOWED_DOWNLOAD_DIALOG, false);
}
public void setShowedDownloadDialog(boolean val) {
prefs.edit().putBoolean(KEY_SHOWED_DOWNLOAD_DIALOG, val).commit();
}
public Date getDownloadRefreshedOn() {
return new Date(prefs.getLong(KEY_DOWNLOAD_REFRESHED_ON, 0));
}
public void setDownloadRefreshedOn(Date d) {
prefs.edit().putLong(KEY_DOWNLOAD_REFRESHED_ON, d.getTime()).commit();
}
}

View File

@ -7,13 +7,12 @@ import android.os.AsyncTask;
import android.util.Log;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.activities.downloader.DownloadPrefsManager;
import org.bspeice.minimalbible.activities.downloader.DownloadPrefs_;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookFilter;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@ -24,10 +23,11 @@ import de.greenrobot.event.EventBus;
public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
private static final String TAG = "EventBookRefreshTask";
// Refresh if last refresh date is after time below
private final Date refreshBefore = new Date(System.currentTimeMillis() - 604800000L); // 1 Week in millis
// If last refresh was before the below, force an internet refresh
private final Long refreshAfter = System.currentTimeMillis() - 604800000L; // 1 Week in millis
@Inject protected DownloadPrefsManager prefsManager;
@Inject
DownloadPrefs_ downloadPrefs;
private EventBus downloadBus;
private BookFilter filter;
@ -52,7 +52,7 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
if (doRefresh()) {
try {
i.reloadBookList();
prefsManager.setDownloadRefreshedOn(new Date(System.currentTimeMillis()));
downloadPrefs.downloadRefreshedOn().put(System.currentTimeMillis());
} catch (InstallException e) {
Log.e(TAG,
"Error downloading books from installer: "
@ -88,10 +88,10 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
}
private boolean downloadEnabled() {
return prefsManager.getDownloadEnabled();
return downloadPrefs.hasEnabledDownload().get();
}
private boolean needsRefresh() {
return (prefsManager.getDownloadRefreshedOn().before(refreshBefore));
return (downloadPrefs.downloadRefreshedOn().get() > refreshAfter);
}
}