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

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

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
*/
package acr.browser.lightning.database;
import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
@ -15,11 +16,15 @@ import android.support.annotation.WorkerThread; @@ -15,11 +16,15 @@ import android.support.annotation.WorkerThread;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
@Singleton
@WorkerThread
class HistoryDatabase extends SQLiteOpenHelper {
public class HistoryDatabase extends SQLiteOpenHelper {
// All Static variables
// Database Version
@ -39,28 +44,18 @@ class HistoryDatabase extends SQLiteOpenHelper { @@ -39,28 +44,18 @@ class HistoryDatabase extends SQLiteOpenHelper {
@Nullable private SQLiteDatabase mDatabase;
@Nullable private static HistoryDatabase sInstance;
private HistoryDatabase() {
super(BrowserApp.getApplication(), DATABASE_NAME, null, DATABASE_VERSION);
@Inject
HistoryDatabase(@NonNull Application application) {
super(application, DATABASE_NAME, null, DATABASE_VERSION);
mDatabase = HistoryDatabase.this.getWritableDatabase();
}
@NonNull
public synchronized static HistoryDatabase getInstance() {
if (sInstance == null) {
sInstance = new HistoryDatabase();
}
return sInstance;
}
// Creating Tables
@Override
public void onCreate(@NonNull SQLiteDatabase db) {
String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + '(' + KEY_ID
+ " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT,"
+ KEY_TIME_VISITED + " INTEGER" + ')';
+ " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT,"
+ KEY_TIME_VISITED + " INTEGER" + ')';
db.execSQL(CREATE_HISTORY_TABLE);
}
@ -123,7 +118,7 @@ class HistoryDatabase extends SQLiteOpenHelper { @@ -123,7 +118,7 @@ class HistoryDatabase extends SQLiteOpenHelper {
values.put(KEY_TIME_VISITED, System.currentTimeMillis());
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) {
mDatabase.update(TABLE_HISTORY, values, KEY_URL + " = ?", new String[]{url});
@ -149,7 +144,7 @@ class HistoryDatabase extends SQLiteOpenHelper { @@ -149,7 +144,7 @@ class HistoryDatabase extends SQLiteOpenHelper {
synchronized String getHistoryItem(@NonNull String url) {
mDatabase = openIfNecessary();
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;
if (cursor != null) {
cursor.moveToFirst();
@ -172,7 +167,7 @@ class HistoryDatabase extends SQLiteOpenHelper { @@ -172,7 +167,7 @@ class HistoryDatabase extends SQLiteOpenHelper {
search = '%' + search + '%';
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()) {
itemList.add(fromCursor(cursor));

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

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

Loading…
Cancel
Save