Browse Source

Constructor injection for HistoryDatabase

master
anthony restaino 8 years ago
parent
commit
e7280be103
  1. 5
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  2. 23
      app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java
  3. 23
      app/src/main/java/acr/browser/lightning/database/HistoryModel.java

5
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -1,5 +1,7 @@
package acr.browser.lightning.app; package acr.browser.lightning.app;
import android.support.annotation.NonNull;
import javax.inject.Singleton; import javax.inject.Singleton;
import acr.browser.lightning.activity.BrowserActivity; import acr.browser.lightning.activity.BrowserActivity;
@ -11,6 +13,7 @@ import acr.browser.lightning.browser.BrowserPresenter;
import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener; import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.fragment.BookmarkSettingsFragment; import acr.browser.lightning.fragment.BookmarkSettingsFragment;
@ -74,4 +77,6 @@ public interface AppComponent {
void inject(SuggestionsAdapter suggestionsAdapter); void inject(SuggestionsAdapter suggestionsAdapter);
@NonNull HistoryDatabase historyDatabase();
} }

23
app/src/main/java/acr/browser/lightning/database/HistoryDatabase.java

@ -3,6 +3,7 @@
*/ */
package acr.browser.lightning.database; package acr.browser.lightning.database;
import android.app.Application;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils; import android.database.DatabaseUtils;
@ -15,11 +16,15 @@ import android.support.annotation.WorkerThread;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
@Singleton
@WorkerThread @WorkerThread
class HistoryDatabase extends SQLiteOpenHelper { public class HistoryDatabase extends SQLiteOpenHelper {
// All Static variables // All Static variables
// Database Version // Database Version
@ -39,22 +44,12 @@ class HistoryDatabase extends SQLiteOpenHelper {
@Nullable private SQLiteDatabase mDatabase; @Nullable private SQLiteDatabase mDatabase;
@Nullable private static HistoryDatabase sInstance; @Inject
HistoryDatabase(@NonNull Application application) {
private HistoryDatabase() { super(application, DATABASE_NAME, null, DATABASE_VERSION);
super(BrowserApp.getApplication(), DATABASE_NAME, null, DATABASE_VERSION);
mDatabase = HistoryDatabase.this.getWritableDatabase(); mDatabase = HistoryDatabase.this.getWritableDatabase();
} }
@NonNull
public synchronized static HistoryDatabase getInstance() {
if (sInstance == null) {
sInstance = new HistoryDatabase();
}
return sInstance;
}
// Creating Tables // Creating Tables
@Override @Override
public void onCreate(@NonNull SQLiteDatabase db) { public void onCreate(@NonNull SQLiteDatabase db) {

23
app/src/main/java/acr/browser/lightning/database/HistoryModel.java

@ -12,6 +12,8 @@ import com.anthonycr.bonsai.SingleSubscriber;
import java.util.List; import java.util.List;
import acr.browser.lightning.app.BrowserApp;
/** /**
* A model class providing reactive bindings * A model class providing reactive bindings
* with the underlying history database. * with the underlying history database.
@ -25,7 +27,9 @@ public final class HistoryModel {
return Completable.create(new CompletableAction() { return Completable.create(new CompletableAction() {
@Override @Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) { public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
HistoryDatabase.getInstance().deleteHistory(); BrowserApp.getAppComponent()
.historyDatabase()
.deleteHistory();
subscriber.onComplete(); subscriber.onComplete();
} }
@ -37,7 +41,9 @@ public final class HistoryModel {
return Completable.create(new CompletableAction() { return Completable.create(new CompletableAction() {
@Override @Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) { public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
HistoryDatabase.getInstance().deleteHistoryItem(url); BrowserApp.getAppComponent()
.historyDatabase()
.deleteHistoryItem(url);
subscriber.onComplete(); subscriber.onComplete();
} }
@ -49,7 +55,12 @@ public final class HistoryModel {
return Completable.create(new CompletableAction() { return Completable.create(new CompletableAction() {
@Override @Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) { public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
HistoryDatabase.getInstance().visitHistoryItem(url, title); BrowserApp.getAppComponent()
.historyDatabase()
.visitHistoryItem(url, title);
System.out.println("SHIT: " + BrowserApp.getAppComponent().historyDatabase().toString());
System.out.println("SHIT: " + BrowserApp.getAppComponent().historyDatabase().toString());
subscriber.onComplete(); subscriber.onComplete();
} }
@ -61,7 +72,8 @@ public final class HistoryModel {
return Single.create(new SingleAction<List<HistoryItem>>() { return Single.create(new SingleAction<List<HistoryItem>>() {
@Override @Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) { public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
List<HistoryItem> result = HistoryDatabase.getInstance().findItemsContaining(query); List<HistoryItem> result = BrowserApp.getAppComponent()
.historyDatabase().findItemsContaining(query);
subscriber.onItem(result); subscriber.onItem(result);
subscriber.onComplete(); subscriber.onComplete();
@ -74,7 +86,8 @@ public final class HistoryModel {
return Single.create(new SingleAction<List<HistoryItem>>() { return Single.create(new SingleAction<List<HistoryItem>>() {
@Override @Override
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) { public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
List<HistoryItem> result = HistoryDatabase.getInstance().getLastHundredItems(); List<HistoryItem> result = BrowserApp.getAppComponent()
.historyDatabase().getLastHundredItems();
subscriber.onItem(result); subscriber.onItem(result);
subscriber.onComplete(); subscriber.onComplete();

Loading…
Cancel
Save