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. 33
      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();
} }

33
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,28 +44,18 @@ 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) {
String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + '(' + KEY_ID String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + '(' + KEY_ID
+ " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT," + " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT,"
+ KEY_TIME_VISITED + " INTEGER" + ')'; + KEY_TIME_VISITED + " INTEGER" + ')';
db.execSQL(CREATE_HISTORY_TABLE); db.execSQL(CREATE_HISTORY_TABLE);
} }
@ -123,7 +118,7 @@ class HistoryDatabase extends SQLiteOpenHelper {
values.put(KEY_TIME_VISITED, System.currentTimeMillis()); values.put(KEY_TIME_VISITED, System.currentTimeMillis());
Cursor cursor = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL}, Cursor cursor = mDatabase.query(false, TABLE_HISTORY, new String[]{KEY_URL},
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) {
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url}); mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url});
@ -149,7 +144,7 @@ class HistoryDatabase extends SQLiteOpenHelper {
synchronized String getHistoryItem(@NonNull String url) { synchronized String getHistoryItem(@NonNull String url) {
mDatabase = openIfNecessary(); mDatabase = openIfNecessary();
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE}, Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, KEY_TITLE},
KEY_URL + " = ?", new String[]{url}, null, null, null, "1"); KEY_URL + " = ?", new String[]{url}, null, null, null, "1");
String m = null; String m = null;
if (cursor != null) { if (cursor != null) {
cursor.moveToFirst(); cursor.moveToFirst();
@ -172,7 +167,7 @@ class HistoryDatabase extends SQLiteOpenHelper {
search = '%' + search + '%'; search = '%' + search + '%';
Cursor cursor = mDatabase.query(TABLE_HISTORY, null, KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?", Cursor cursor = mDatabase.query(TABLE_HISTORY, null, KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?",
new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5"); new String[]{search, search}, null, null, KEY_TIME_VISITED + " DESC", "5");
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
itemList.add(fromCursor(cursor)); itemList.add(fromCursor(cursor));

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