@ -13,6 +13,13 @@ import android.support.annotation.NonNull;
@@ -13,6 +13,13 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable ;
import android.support.annotation.WorkerThread ;
import com.anthonycr.bonsai.Completable ;
import com.anthonycr.bonsai.CompletableAction ;
import com.anthonycr.bonsai.CompletableSubscriber ;
import com.anthonycr.bonsai.Single ;
import com.anthonycr.bonsai.SingleAction ;
import com.anthonycr.bonsai.SingleSubscriber ;
import java.util.ArrayList ;
import java.util.List ;
@ -22,9 +29,15 @@ import javax.inject.Singleton;
@@ -22,9 +29,15 @@ import javax.inject.Singleton;
import acr.browser.lightning.R ;
import acr.browser.lightning.database.HistoryItem ;
/ * *
* The disk backed download database .
* See { @link HistoryModel } for method
* documentation .
* /
@Singleton
@WorkerThread
public class HistoryDatabase extends SQLiteOpenHelper {
public class HistoryDatabase extends SQLiteOpenHelper implements HistoryModel {
// All Static variables
// Database Version
@ -45,9 +58,8 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@@ -45,9 +58,8 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@Nullable private SQLiteDatabase mDatabase ;
@Inject
HistoryDatabase ( @NonNull Application application ) {
public HistoryDatabase ( @NonNull Application application ) {
super ( application , DATABASE_NAME , null , DATABASE_VERSION ) ;
mDatabase = HistoryDatabase . this . getWritableDatabase ( ) ;
}
// Creating Tables
@ -87,19 +99,39 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@@ -87,19 +99,39 @@ public class HistoryDatabase extends SQLiteOpenHelper {
return mDatabase ;
}
@WorkerThread
synchronized void deleteHistory ( ) {
@NonNull
@Override
public Completable deleteHistory ( ) {
return Completable . create ( new CompletableAction ( ) {
@Override
public void onSubscribe ( @NonNull CompletableSubscriber subscriber ) {
lazyDatabase ( ) . delete ( TABLE_HISTORY , null , null ) ;
lazyDatabase ( ) . close ( ) ;
subscriber . onComplete ( ) ;
}
} ) ;
}
@WorkerThread
synchronized void deleteHistoryItem ( @NonNull String url ) {
@NonNull
@Override
public Completable deleteHistoryItem ( @NonNull final String url ) {
return Completable . create ( new CompletableAction ( ) {
@Override
public void onSubscribe ( @NonNull CompletableSubscriber subscriber ) {
lazyDatabase ( ) . delete ( TABLE_HISTORY , KEY_URL + " = ?" , new String [ ] { url } ) ;
subscriber . onComplete ( ) ;
}
} ) ;
}
@WorkerThread
synchronized void visitHistoryItem ( @NonNull String url , @Nullable String title ) {
@NonNull
@Override
public Completable visitHistoryItem ( @NonNull final String url , @Nullable final String title ) {
return Completable . create ( new CompletableAction ( ) {
@Override
public void onSubscribe ( @NonNull CompletableSubscriber subscriber ) {
ContentValues values = new ContentValues ( ) ;
values . put ( KEY_TITLE , title = = null ? "" : title ) ;
values . put ( KEY_TIME_VISITED , System . currentTimeMillis ( ) ) ;
@ -115,40 +147,18 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@@ -115,40 +147,18 @@ public class HistoryDatabase extends SQLiteOpenHelper {
cursor . close ( ) ;
}
@WorkerThread
private synchronized void addHistoryItem ( @NonNull HistoryItem item ) {
ContentValues values = new ContentValues ( ) ;
values . put ( KEY_URL , item . getUrl ( ) ) ;
values . put ( KEY_TITLE , item . getTitle ( ) ) ;
values . put ( KEY_TIME_VISITED , System . currentTimeMillis ( ) ) ;
lazyDatabase ( ) . insert ( TABLE_HISTORY , null , values ) ;
}
@WorkerThread
@Nullable
synchronized String getHistoryItem ( @NonNull String url ) {
Cursor cursor = lazyDatabase ( ) . query ( TABLE_HISTORY , new String [ ] { KEY_ID , KEY_URL , KEY_TITLE } ,
KEY_URL + " = ?" , new String [ ] { url } , null , null , null , "1" ) ;
String m = null ;
if ( cursor ! = null ) {
cursor . moveToFirst ( ) ;
m = cursor . getString ( 0 ) ;
cursor . close ( ) ;
}
return m ;
} ) ;
}
@WorkerThread
@NonNull
synchronized List < HistoryItem > findItemsContaining ( @Nullable String search ) {
@Override
public Single < List < HistoryItem > > findHistoryItemsContaining ( @NonNull final String query ) {
return Single . create ( new SingleAction < List < HistoryItem > > ( ) {
@Override
public void onSubscribe ( @NonNull SingleSubscriber < List < HistoryItem > > subscriber ) {
List < HistoryItem > itemList = new ArrayList < > ( 5 ) ;
if ( search = = null ) {
return itemList ;
}
search = '%' + search + '%' ;
String search = '%' + query + '%' ;
Cursor cursor = lazyDatabase ( ) . query ( TABLE_HISTORY , null , KEY_TITLE + " LIKE ? OR " + KEY_URL + " LIKE ?" ,
new String [ ] { search , search } , null , null , KEY_TIME_VISITED + " DESC" , "5" ) ;
@ -159,12 +169,18 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@@ -159,12 +169,18 @@ public class HistoryDatabase extends SQLiteOpenHelper {
cursor . close ( ) ;
return itemList ;
subscriber . onItem ( itemList ) ;
subscriber . onComplete ( ) ;
}
} ) ;
}
@WorkerThread
@NonNull
synchronized List < HistoryItem > getLastHundredItems ( ) {
@Override
public Single < List < HistoryItem > > lastHundredVisitedHistoryItems ( ) {
return Single . create ( new SingleAction < List < HistoryItem > > ( ) {
@Override
public void onSubscribe ( @NonNull SingleSubscriber < List < HistoryItem > > subscriber ) {
List < HistoryItem > itemList = new ArrayList < > ( 100 ) ;
Cursor cursor = lazyDatabase ( ) . query ( TABLE_HISTORY , null , null , null , null , null , KEY_TIME_VISITED + " DESC" , "100" ) ;
@ -174,7 +190,34 @@ public class HistoryDatabase extends SQLiteOpenHelper {
@@ -174,7 +190,34 @@ public class HistoryDatabase extends SQLiteOpenHelper {
cursor . close ( ) ;
return itemList ;
subscriber . onItem ( itemList ) ;
subscriber . onComplete ( ) ;
}
} ) ;
}
@WorkerThread
private synchronized void addHistoryItem ( @NonNull HistoryItem item ) {
ContentValues values = new ContentValues ( ) ;
values . put ( KEY_URL , item . getUrl ( ) ) ;
values . put ( KEY_TITLE , item . getTitle ( ) ) ;
values . put ( KEY_TIME_VISITED , System . currentTimeMillis ( ) ) ;
lazyDatabase ( ) . insert ( TABLE_HISTORY , null , values ) ;
}
@WorkerThread
@Nullable
synchronized String getHistoryItem ( @NonNull String url ) {
Cursor cursor = lazyDatabase ( ) . query ( TABLE_HISTORY , new String [ ] { KEY_ID , KEY_URL , KEY_TITLE } ,
KEY_URL + " = ?" , new String [ ] { url } , null , null , null , "1" ) ;
String m = null ;
if ( cursor ! = null ) {
cursor . moveToFirst ( ) ;
m = cursor . getString ( 0 ) ;
cursor . close ( ) ;
}
return m ;
}
@WorkerThread