diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/PackagesRepository.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/PackagesRepository.java index e736f86..32f8a37 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/PackagesRepository.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/PackagesRepository.java @@ -217,25 +217,18 @@ public void accept(Package aPackage) throws Exception { */ @Override public Observable refreshPackage(@NonNull final String packageId) { - return packagesRemoteDataSource - .refreshPackage(packageId) - .flatMap(new Function>() { - @Override - public ObservableSource apply(Package p) throws Exception { - return Observable - .just(p) - .doOnNext(new Consumer() { - @Override - public void accept(Package aPackage) throws Exception { - Package pkg = cachedPackages.get(aPackage.getNumber()); - if (pkg != null) { - pkg.setData(aPackage.getData()); - pkg.setReadable(true); - } - } - }); - } - }); + return packagesRemoteDataSource.refreshPackage(packageId) + .doOnNext(new Consumer() { + @Override + public void accept(Package aPackage) throws Exception { + Package pkg = cachedPackages.get(aPackage.getNumber()); + if (pkg != null) { + pkg.setData(aPackage.getData()); + pkg.setReadable(true); + } + } + + }); } /** diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java index b6d2b31..19428fd 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/CompaniesLocalDataSource.java @@ -20,11 +20,14 @@ import android.support.annotation.Nullable; import java.util.List; +import java.util.concurrent.Callable; import io.github.marktony.espresso.data.Company; import io.github.marktony.espresso.data.source.CompaniesDataSource; import io.github.marktony.espresso.realm.RealmHelper; import io.reactivex.Observable; +import io.reactivex.ObservableSource; +import io.reactivex.functions.Function; import io.realm.Case; import io.realm.Realm; import io.realm.Sort; @@ -52,18 +55,27 @@ public static CompaniesLocalDataSource getInstance() { @Override public Observable> getCompanies() { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable - .fromIterable(rlm.copyFromRealm(rlm.where(Company.class).findAllSorted("alphabet", Sort.ASCENDING))) - .toList() - .toObservable(); + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + return rlm.copyFromRealm(rlm.where(Company.class) + .findAllSorted("alphabet", Sort.ASCENDING)); + } + }); } @Override - public Observable getCompany(@NonNull String companyId) { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable - .just(rlm.copyFromRealm(rlm.where(Company.class).equalTo("id", companyId).findFirst())); + public Observable getCompany(@NonNull final String companyId) { + return Observable.fromCallable(new Callable() { + @Override + public Company call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + return rlm.copyFromRealm(rlm.where(Company.class) + .equalTo("id", companyId) + .findFirst()); + } + }); } @Override @@ -718,21 +730,25 @@ public void initData() { } @Override - public Observable> searchCompanies(@NonNull String keyWords) { - Realm rlm = RealmHelper.newRealmInstance(); - List results = rlm.copyFromRealm( - rlm.where(Company.class) - .like("name","*" + keyWords + "*", Case.INSENSITIVE) - .or() - .like("tel", "*" + keyWords + "*", Case.INSENSITIVE) - .or() - .like("website", "*" + keyWords + "*", Case.INSENSITIVE) - .or() - .like("alphabet", "*" + keyWords + "*", Case.INSENSITIVE) - .findAllSorted("alphabet", Sort.ASCENDING)); - return Observable.fromIterable(results) - .toList() - .toObservable(); + public Observable> searchCompanies(@NonNull final String keyWords) { + + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + return rlm.copyFromRealm(rlm.where(Company.class) + .like("name", "*" + keyWords + "*", Case.INSENSITIVE) + .or() + .like("tel", "*" + keyWords + "*", Case.INSENSITIVE) + .or() + .like("website", "*" + keyWords + "*", Case.INSENSITIVE) + .or() + .like("alphabet", + "*" + keyWords + "*", + Case.INSENSITIVE) + .findAllSorted("alphabet", Sort.ASCENDING)); + } + }); } } \ No newline at end of file diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java index e646803..47a14a0 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/local/PackagesLocalDataSource.java @@ -20,6 +20,7 @@ import android.support.annotation.Nullable; import java.util.List; +import java.util.concurrent.Callable; import io.github.marktony.espresso.data.Package; import io.github.marktony.espresso.data.source.PackagesDataSource; @@ -64,10 +65,14 @@ public static void destroyInstance() { */ @Override public Observable> getPackages() { - Realm rlm = RealmHelper.newRealmInstance(); - - return Observable.just(rlm.copyFromRealm(rlm.where(Package.class) - .findAllSorted("timestamp", Sort.DESCENDING))); + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + return rlm.copyFromRealm(rlm.where(Package.class) + .findAllSorted("timestamp", Sort.DESCENDING)); + } + }); } /** @@ -78,11 +83,16 @@ public Observable> getPackages() { * @return The observable package from database. */ @Override - public Observable getPackage(@NonNull String packNumber) { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable.just(rlm.copyFromRealm(rlm.where(Package.class) - .equalTo("number", packNumber) - .findFirst())); + public Observable getPackage(@NonNull final String packNumber) { + return Observable.fromCallable(new Callable() { + @Override + public Package call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + return rlm.copyFromRealm(rlm.where(Package.class) + .equalTo("number", packNumber) + .findFirst()); + } + }); } /** @@ -206,20 +216,26 @@ public void updatePackageName(@NonNull String packageId, @NonNull String name) { } @Override - public Observable> searchPackages(@NonNull String keyWords) { - Realm rlm = RealmHelper.newRealmInstance(); - return Observable.fromIterable(rlm.copyFromRealm( - rlm.where(Package.class) - .like("name", "*" + keyWords + "*", Case.INSENSITIVE) - .or() - .like("companyChineseName", "*" + keyWords + "*", Case.INSENSITIVE) - .or() - .like("company", "*" + keyWords + "*", Case.INSENSITIVE) - .or() - .like("number", "*" + keyWords + "*", Case.INSENSITIVE) - .findAll())) - .toList() - .toObservable(); + public Observable> searchPackages(@NonNull + final String keyWords) { + + return Observable.fromCallable(new Callable>() { + @Override + public List call() throws Exception { + Realm rlm = RealmHelper.newRealmInstance(); + return rlm.copyFromRealm(rlm.where(Package.class) + .like("name", "*" + keyWords + "*", Case.INSENSITIVE) + .or() + .like("companyChineseName", + "*" + keyWords + "*", + Case.INSENSITIVE) + .or() + .like("company", "*" + keyWords + "*", Case.INSENSITIVE) + .or() + .like("number", "*" + keyWords + "*", Case.INSENSITIVE) + .findAll()); + } + }); } }