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

Commit

Permalink
Revert
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhaotailang committed Apr 14, 2017
1 parent ced794c commit 8c65cce
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 291 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.1'

// Realm database
classpath "io.realm:realm-gradle-plugin:3.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright(c) 2017 lizhaotailang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.marktony.espresso.data.source;

import android.support.annotation.NonNull;
Expand Down Expand Up @@ -68,4 +52,4 @@ public Observable<List<Company>> searchCompanies(@NonNull String keyWords) {
return localDataSource.searchCompanies(keyWords);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright(c) 2017 lizhaotailang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.marktony.espresso.data.source;

import android.support.annotation.NonNull;
Expand Down Expand Up @@ -56,6 +40,8 @@ public class PackagesRepository implements PackagesDataSource {

private Map<String, Package> cachedPackages;

private Package cachePackage = null;

// Prevent direct instantiation
private PackagesRepository(@NonNull PackagesDataSource packagesRemoteDataSource,
@NonNull PackagesDataSource packagesLocalDataSource) {
Expand Down Expand Up @@ -114,21 +100,19 @@ public int compare(Package o1, Package o2) {
// Return the cached packages.
return packagesLocalDataSource
.getPackages()
.publish(new Function<Observable<List<Package>>, ObservableSource<List<Package>>>() {
.flatMap(new Function<List<Package>, ObservableSource<List<Package>>>() {
@Override
public ObservableSource<List<Package>> apply(Observable<List<Package>> listObservable) throws Exception {
listObservable.flatMapIterable(new Function<List<Package>, Iterable<Package>>() {
@Override
public Iterable<Package> apply(List<Package> packages) throws Exception {
return packages;
}
}).subscribe(new Consumer<Package>() {
@Override
public void accept(Package aPackage) throws Exception {
cachedPackages.put(aPackage.getNumber(), aPackage);
}
});
return listObservable;
public ObservableSource<List<Package>> apply(List<Package> packages) throws Exception {
return Observable
.fromIterable(packages)
.doOnNext(new Consumer<Package>() {
@Override
public void accept(Package aPackage) throws Exception {
cachedPackages.put(aPackage.getNumber(), aPackage);
}
})
.toList()
.toObservable();
}
});
}
Expand Down Expand Up @@ -174,6 +158,7 @@ public void savePackage(@NonNull Package pack) {
*/
@Override
public void deletePackage(@NonNull String packageId) {
cachePackage = getPackageWithNumber(packageId);
packagesLocalDataSource.deletePackage(packageId);
cachedPackages.remove(packageId);
}
Expand All @@ -187,16 +172,13 @@ public void deletePackage(@NonNull String packageId) {
public Observable<List<Package>> refreshPackages() {
return packagesRemoteDataSource
.refreshPackages()
.publish(new Function<Observable<List<Package>>, ObservableSource<List<Package>>>() {
@Override
public ObservableSource<List<Package>> apply(Observable<List<Package>> listObservable) throws Exception {
listObservable.flatMapIterable(new Function<List<Package>, Iterable<Package>>() {
@Override
public Iterable<Package> apply(List<Package> packages) throws Exception {
return packages;
}
})
.subscribe(new Consumer<Package>() {
.flatMap(new Function<List<Package>, ObservableSource<List<Package>>>() {
@Override
public ObservableSource<List<Package>> apply(List<Package> packages) throws Exception {

return Observable
.fromIterable(packages)
.doOnNext(new Consumer<Package>() {
@Override
public void accept(Package aPackage) throws Exception {
Package p = cachedPackages.get(aPackage.getNumber());
Expand All @@ -206,10 +188,11 @@ public void accept(Package aPackage) throws Exception {
p.setReadable(true);
}
}
});
return listObservable;
}
});
})
.toList()
.toObservable();
}
});
}

/**
Expand All @@ -221,24 +204,25 @@ public void accept(Package aPackage) throws Exception {
*/
@Override
public Observable<Package> refreshPackage(@NonNull final String packageId) {
return packagesRemoteDataSource.refreshPackage(packageId)
.publish(new Function<Observable<Package>, ObservableSource<Package>>() {
@Override
public ObservableSource<Package> apply(Observable<Package> packageObservable) throws Exception {
packageObservable.subscribe(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 packageObservable;
}
});
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);
}
}
});
}
});
}

/**
Expand Down Expand Up @@ -321,21 +305,15 @@ private Package getPackageWithNumber(@NonNull String packNumber) {
private Observable<Package> getPackageWithNumberFromLocalRepository(@NonNull final String packNumber) {
return packagesLocalDataSource
.getPackage(packNumber)
.publish(new Function<Observable<Package>, ObservableSource<Package>>() {
.doOnNext(new Consumer<Package>() {
@Override
public ObservableSource<Package> apply(Observable<Package> packageObservable) throws Exception {
packageObservable.subscribe(new Consumer<Package>() {
@Override
public void accept(Package aPackage) throws Exception {
if (cachedPackages == null) {
cachedPackages = new LinkedHashMap<>();
}
cachedPackages.put(packNumber, aPackage);
}
});
return packageObservable;
public void accept(Package aPackage) throws Exception {
if (cachedPackages == null) {
cachedPackages = new LinkedHashMap<>();
}
cachedPackages.put(packNumber, aPackage);
}
});
}

}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
/*
* Copyright(c) 2017 lizhaotailang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.marktony.espresso.data.source.local;

import android.support.annotation.NonNull;
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 @@ -55,27 +36,18 @@ public static CompaniesLocalDataSource getInstance() {

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

@Override
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());
}
});
public Observable<Company> getCompany(@NonNull String companyId) {
Realm rlm = RealmHelper.newRealmInstance();
return Observable
.just(rlm.copyFromRealm(rlm.where(Company.class).equalTo("id", companyId).findFirst()));
}

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

@Override
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));
}
});
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();
}

}
Loading

0 comments on commit 8c65cce

Please sign in to comment.