Skip to content
This repository has been archived by the owner on May 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from wbinarytree/master
Browse files Browse the repository at this point in the history
Observable creation improve
  • Loading branch information
黎赵太郎 authored Apr 13, 2017
2 parents d648b0e + 147ee9e commit 69568f9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,18 @@ public void accept(Package aPackage) throws Exception {
*/
@Override
public Observable<Package> refreshPackage(@NonNull final String packageId) {
return packagesRemoteDataSource
.refreshPackage(packageId)
.flatMap(new Function<Package, ObservableSource<Package>>() {
@Override
public ObservableSource<Package> apply(Package p) throws Exception {
return Observable
.just(p)
.doOnNext(new Consumer<Package>() {
@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<Package>() {
@Override
public void accept(Package aPackage) throws Exception {
Package pkg = cachedPackages.get(aPackage.getNumber());
if (pkg != null) {
pkg.setData(aPackage.getData());
pkg.setReadable(true);
}
}

});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,18 +55,27 @@ public static CompaniesLocalDataSource getInstance() {

@Override
public Observable<List<Company>> 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<List<Company>>() {
@Override
public List<Company> call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
return rlm.copyFromRealm(rlm.where(Company.class)
.findAllSorted("alphabet", Sort.ASCENDING));
}
});
}

@Override
public Observable<Company> getCompany(@NonNull String companyId) {
Realm rlm = RealmHelper.newRealmInstance();
return Observable
.just(rlm.copyFromRealm(rlm.where(Company.class).equalTo("id", companyId).findFirst()));
public Observable<Company> getCompany(@NonNull final String companyId) {
return Observable.fromCallable(new Callable<Company>() {
@Override
public Company call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
return rlm.copyFromRealm(rlm.where(Company.class)
.equalTo("id", companyId)
.findFirst());
}
});
}

@Override
Expand Down Expand Up @@ -718,21 +730,25 @@ public void initData() {
}

@Override
public Observable<List<Company>> searchCompanies(@NonNull String keyWords) {
Realm rlm = RealmHelper.newRealmInstance();
List<Company> 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<List<Company>> searchCompanies(@NonNull final String keyWords) {

return Observable.fromCallable(new Callable<List<Company>>() {
@Override
public List<Company> 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));
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,10 +65,14 @@ public static void destroyInstance() {
*/
@Override
public Observable<List<Package>> getPackages() {
Realm rlm = RealmHelper.newRealmInstance();

return Observable.just(rlm.copyFromRealm(rlm.where(Package.class)
.findAllSorted("timestamp", Sort.DESCENDING)));
return Observable.fromCallable(new Callable<List<Package>>() {
@Override
public List<Package> call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
return rlm.copyFromRealm(rlm.where(Package.class)
.findAllSorted("timestamp", Sort.DESCENDING));
}
});
}

/**
Expand All @@ -78,11 +83,16 @@ public Observable<List<Package>> getPackages() {
* @return The observable package from database.
*/
@Override
public Observable<Package> getPackage(@NonNull String packNumber) {
Realm rlm = RealmHelper.newRealmInstance();
return Observable.just(rlm.copyFromRealm(rlm.where(Package.class)
.equalTo("number", packNumber)
.findFirst()));
public Observable<Package> getPackage(@NonNull final String packNumber) {
return Observable.fromCallable(new Callable<Package>() {
@Override
public Package call() throws Exception {
Realm rlm = RealmHelper.newRealmInstance();
return rlm.copyFromRealm(rlm.where(Package.class)
.equalTo("number", packNumber)
.findFirst());
}
});
}

/**
Expand Down Expand Up @@ -206,20 +216,26 @@ public void updatePackageName(@NonNull String packageId, @NonNull String name) {
}

@Override
public Observable<List<Package>> 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<List<Package>> searchPackages(@NonNull
final String keyWords) {

return Observable.fromCallable(new Callable<List<Package>>() {
@Override
public List<Package> 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());
}
});
}

}

0 comments on commit 69568f9

Please sign in to comment.