Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RxJvav2 support #669

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DaoCore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
compileOnly 'com.google.android:support-v4:r7'

compileOnly 'io.reactivex:rxjava:1.1.8'
compileOnly 'io.reactivex.rxjava2:rxjava:2.0.9'

compileOnly files('libs/sqlcipher.jar')
}
Expand Down
32 changes: 32 additions & 0 deletions DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.rx.RxDao;
import org.greenrobot.greendao.rx2.Rx2Dao;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -73,6 +74,9 @@ public abstract class AbstractDao<T, K> {
private volatile RxDao<T, K> rxDao;
private volatile RxDao<T, K> rxDaoPlain;

private volatile Rx2Dao<T, K> rx2Dao;
private volatile Rx2Dao<T, K> rx2DaoPlain;

public AbstractDao(DaoConfig config) {
this(config, null);
}
Expand Down Expand Up @@ -964,6 +968,34 @@ public RxDao<T, K> rx() {
return rxDao;
}

/**
* The returned Rx2Dao is a special DAO that let's you interact with Rx Observables without any Scheduler set
* for subscribeOn.
*
* @see #rx2()
*/
@Experimental
public Rx2Dao<T, K> rx2Plain() {
if (rx2DaoPlain == null) {
rx2DaoPlain = new Rx2Dao<>(this);
}
return rx2DaoPlain;
}

/**
* The returned Rx2Dao is a special DAO that let's you interact with Rx Observables using RX's IO scheduler for
* subscribeOn.
*
* @see #rx2Plain()
*/
@Experimental
public Rx2Dao<T, K> rx2() {
if (rx2Dao == null) {
rx2Dao = new Rx2Dao<>(this, io.reactivex.schedulers.Schedulers.io());
}
return rx2Dao;
}

/** Gets the SQLiteDatabase for custom database access. Not needed for greenDAO entities. */
public Database getDatabase() {
return db;
Expand Down
35 changes: 35 additions & 0 deletions DaoCore/src/main/java/org/greenrobot/greendao/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.greenrobot.greendao.annotation.apihint.Internal;
import org.greenrobot.greendao.rx.RxQuery;
import org.greenrobot.greendao.rx.RxTransaction;
import org.greenrobot.greendao.rx2.Rx2Query;
import org.greenrobot.greendao.rx2.Rx2Transaction;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -69,6 +71,9 @@ static <T2> Query<T2> create(AbstractDao<T2, ?> dao, String sql, Object[] initia
private volatile RxQuery rxTxPlain;
private volatile RxQuery rxTxIo;

private volatile Rx2Query rx2TxPlain;
private volatile Rx2Query rx2TxIo;

private Query(QueryData<T> queryData, AbstractDao<T, ?> dao, String sql, String[] initialValues, int limitPosition,
int offsetPosition) {
super(dao, sql, initialValues, limitPosition, offsetPosition);
Expand Down Expand Up @@ -188,4 +193,34 @@ public RxQuery __InternalRx() {
}
return rxTxIo;
}

/**
* DO NOT USE.
* The returned {@link Rx2Transaction} allows getting query results using Rx Observables without any Scheduler set
* for subscribeOn.
*
* @see #__InternalRx()
*/
@Internal
public Rx2Query __internalRx2Plain() {
if (rx2TxPlain == null) {
rx2TxPlain = new Rx2Query(this);
}
return rx2TxPlain;
}

/**
* DO NOT USE.
* The returned {@link Rx2Transaction} allows getting query results using Rx Observables using RX's IO scheduler for
* subscribeOn.
*
* @see #__internalRxPlain()
*/
@Internal
public Rx2Query __InternalRx2() {
if (rx2TxIo == null) {
rx2TxIo = new Rx2Query(this, io.reactivex.schedulers.Schedulers.io());
}
return rx2TxIo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.greenrobot.greendao.annotation.apihint.Experimental;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.rx.RxQuery;
import org.greenrobot.greendao.rx2.Rx2Query;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -427,6 +428,22 @@ public List<T> list() {
return build().list();
}

/**
* Shorthand for {@link QueryBuilder#build() build()}.{@link Query#__InternalRx2()}.
*/
@Experimental
public Rx2Query<T> rx2() {
return build().__InternalRx2();
}

/**
* Shorthand for {@link QueryBuilder#build() build()}.{@link Query#__internalRx2Plain()}.
*/
@Experimental
public Rx2Query<T> rx2Plain() {
return build().__internalRx2Plain();
}

/**
* Shorthand for {@link QueryBuilder#build() build()}.{@link Query#__InternalRx()}.
*/
Expand Down
57 changes: 57 additions & 0 deletions DaoCore/src/main/java/org/greenrobot/greendao/rx2/Rx2Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.greenrobot.greendao.rx2;

import org.greenrobot.greendao.annotation.apihint.Experimental;
import org.greenrobot.greendao.annotation.apihint.Internal;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.Scheduler;

/**
* Created by Zhang Tingkuo.
* Date: 2017-04-28
* Time: 14:14
*/
@Internal
class Rx2Base {

protected final Scheduler mScheduler;

/**
* No default scheduler.
*/
public Rx2Base() {
mScheduler = null;
}

/**
* Sets the default scheduler, which is used to configure returned observables with
* {@link Observable#subscribeOn(Scheduler)}.
*/
@Experimental
Rx2Base(Scheduler scheduler) {
mScheduler = scheduler;
}

/**
* The default scheduler (or null) used for wrapping.
*/
@Experimental
public Scheduler getScheduler() {
return mScheduler;
}

protected <R> Observable<R> wrap(Callable<R> callable) {
return wrap(Rx2Utils.fromCallable(callable));
}

protected <R> Observable<R> wrap(Observable<R> observable) {
if (mScheduler != null) {
return observable.subscribeOn(mScheduler);
} else {
return observable;
}
}

}
Loading