From 8c65cce9c4bc4cdc44c7c53ec27b6f95d115bfff Mon Sep 17 00:00:00 2001 From: lizhaotailang Date: Fri, 14 Apr 2017 16:57:36 +0800 Subject: [PATCH] Revert --- build.gradle | 2 +- .../data/source/CompaniesRepository.java | 18 +-- .../data/source/PackagesRepository.java | 128 ++++++++---------- .../local/CompaniesLocalDataSource.java | 80 ++++------- .../source/local/PackagesLocalDataSource.java | 80 ++++------- .../remote/PackagesRemoteDataSource.java | 123 ++++++----------- .../main/res/layout/fragment_add_package.xml | 12 +- .../main/res/layout/item_package_status.xml | 2 + mobile/src/main/res/values/styles.xml | 4 + 9 files changed, 158 insertions(+), 291 deletions(-) diff --git a/build.gradle b/build.gradle index 147ea61..dfb47b3 100644 --- a/build.gradle +++ b/build.gradle @@ -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" diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/CompaniesRepository.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/CompaniesRepository.java index 97c7ccb..ea0808a 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/CompaniesRepository.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/CompaniesRepository.java @@ -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; @@ -68,4 +52,4 @@ public Observable> searchCompanies(@NonNull String keyWords) { return localDataSource.searchCompanies(keyWords); } -} +} \ No newline at end of file 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 02dba62..f7819d2 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 @@ -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; @@ -56,6 +40,8 @@ public class PackagesRepository implements PackagesDataSource { private Map cachedPackages; + private Package cachePackage = null; + // Prevent direct instantiation private PackagesRepository(@NonNull PackagesDataSource packagesRemoteDataSource, @NonNull PackagesDataSource packagesLocalDataSource) { @@ -114,21 +100,19 @@ public int compare(Package o1, Package o2) { // Return the cached packages. return packagesLocalDataSource .getPackages() - .publish(new Function>, ObservableSource>>() { + .flatMap(new Function, ObservableSource>>() { @Override - public ObservableSource> apply(Observable> listObservable) throws Exception { - listObservable.flatMapIterable(new Function, Iterable>() { - @Override - public Iterable apply(List packages) throws Exception { - return packages; - } - }).subscribe(new Consumer() { - @Override - public void accept(Package aPackage) throws Exception { - cachedPackages.put(aPackage.getNumber(), aPackage); - } - }); - return listObservable; + public ObservableSource> apply(List packages) throws Exception { + return Observable + .fromIterable(packages) + .doOnNext(new Consumer() { + @Override + public void accept(Package aPackage) throws Exception { + cachedPackages.put(aPackage.getNumber(), aPackage); + } + }) + .toList() + .toObservable(); } }); } @@ -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); } @@ -187,16 +172,13 @@ public void deletePackage(@NonNull String packageId) { public Observable> refreshPackages() { return packagesRemoteDataSource .refreshPackages() - .publish(new Function>, ObservableSource>>() { - @Override - public ObservableSource> apply(Observable> listObservable) throws Exception { - listObservable.flatMapIterable(new Function, Iterable>() { - @Override - public Iterable apply(List packages) throws Exception { - return packages; - } - }) - .subscribe(new Consumer() { + .flatMap(new Function, ObservableSource>>() { + @Override + public ObservableSource> apply(List packages) throws Exception { + + return Observable + .fromIterable(packages) + .doOnNext(new Consumer() { @Override public void accept(Package aPackage) throws Exception { Package p = cachedPackages.get(aPackage.getNumber()); @@ -206,10 +188,11 @@ public void accept(Package aPackage) throws Exception { p.setReadable(true); } } - }); - return listObservable; - } - }); + }) + .toList() + .toObservable(); + } + }); } /** @@ -221,24 +204,25 @@ public void accept(Package aPackage) throws Exception { */ @Override public Observable refreshPackage(@NonNull final String packageId) { - return packagesRemoteDataSource.refreshPackage(packageId) - .publish(new Function, ObservableSource>() { - @Override - public ObservableSource apply(Observable packageObservable) throws Exception { - packageObservable.subscribe(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 packageObservable; - } - }); + 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); + } + } + }); + } + }); } /** @@ -321,21 +305,15 @@ private Package getPackageWithNumber(@NonNull String packNumber) { private Observable getPackageWithNumberFromLocalRepository(@NonNull final String packNumber) { return packagesLocalDataSource .getPackage(packNumber) - .publish(new Function, ObservableSource>() { + .doOnNext(new Consumer() { @Override - public ObservableSource apply(Observable packageObservable) throws Exception { - packageObservable.subscribe(new Consumer() { - @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); } }); } -} +} \ No newline at end of file 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 19428fd..69d897e 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 @@ -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; @@ -55,27 +36,18 @@ public static CompaniesLocalDataSource getInstance() { @Override public Observable> getCompanies() { - 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)); - } - }); + Realm rlm = RealmHelper.newRealmInstance(); + return Observable + .fromIterable(rlm.copyFromRealm(rlm.where(Company.class).findAllSorted("alphabet", Sort.ASCENDING))) + .toList() + .toObservable(); } @Override - 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()); - } - }); + public Observable getCompany(@NonNull String companyId) { + Realm rlm = RealmHelper.newRealmInstance(); + return Observable + .just(rlm.copyFromRealm(rlm.where(Company.class).equalTo("id", companyId).findFirst())); } @Override @@ -730,25 +702,21 @@ public void initData() { } @Override - 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)); - } - }); + 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(); } } \ 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 47a14a0..83fee7a 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 @@ -1,26 +1,9 @@ -/* - * 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.Package; import io.github.marktony.espresso.data.source.PackagesDataSource; @@ -65,14 +48,10 @@ public static void destroyInstance() { */ @Override public Observable> getPackages() { - 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)); - } - }); + Realm rlm = RealmHelper.newRealmInstance(); + + return Observable.just(rlm.copyFromRealm(rlm.where(Package.class) + .findAllSorted("timestamp", Sort.DESCENDING))); } /** @@ -83,16 +62,11 @@ public List call() throws Exception { * @return The observable package from database. */ @Override - 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()); - } - }); + public Observable getPackage(@NonNull String packNumber) { + Realm rlm = RealmHelper.newRealmInstance(); + return Observable.just(rlm.copyFromRealm(rlm.where(Package.class) + .equalTo("number", packNumber) + .findFirst())); } /** @@ -216,26 +190,20 @@ public void updatePackageName(@NonNull String packageId, @NonNull String name) { } @Override - 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()); - } - }); + 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(); } -} +} \ No newline at end of file diff --git a/mobile/src/main/java/io/github/marktony/espresso/data/source/remote/PackagesRemoteDataSource.java b/mobile/src/main/java/io/github/marktony/espresso/data/source/remote/PackagesRemoteDataSource.java index ec542ab..654004d 100644 --- a/mobile/src/main/java/io/github/marktony/espresso/data/source/remote/PackagesRemoteDataSource.java +++ b/mobile/src/main/java/io/github/marktony/espresso/data/source/remote/PackagesRemoteDataSource.java @@ -1,29 +1,13 @@ -/* - * 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.remote; 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.Package; import io.github.marktony.espresso.data.source.PackagesDataSource; +import io.github.marktony.espresso.realm.RealmHelper; import io.github.marktony.espresso.retrofit.RetrofitClient; import io.github.marktony.espresso.retrofit.RetrofitService; import io.reactivex.Observable; @@ -35,8 +19,6 @@ import io.realm.Realm; import io.realm.RealmConfiguration; -import static io.github.marktony.espresso.realm.RealmHelper.DATABASE_NAME; - /** * Created by lizhaotailang on 2017/3/7. * Implementation of the data source that adds a latency simulating network. @@ -99,33 +81,19 @@ public void deletePackage(@NonNull String packageId) { public Observable> refreshPackages() { // It is necessary to build a new realm instance // in a different thread. + Realm realm = RealmHelper.newRealmInstance(); - return Observable.fromCallable(new Callable>() { - @Override - public List call() throws Exception { - Realm realm = Realm.getInstance(new RealmConfiguration.Builder() - .deleteRealmIfMigrationNeeded() - .name(DATABASE_NAME) - .build()); - return realm.copyFromRealm(realm.where(Package.class).findAll()); - } - }).publish(new Function>, ObservableSource>>() { - @Override - public ObservableSource> apply(Observable> listObservable) throws Exception { - listObservable.flatMapIterable(new Function, Iterable>() { - @Override - public Iterable apply(List packages) throws Exception { - return packages; - } - }).subscribe(new Consumer() { + return Observable.fromIterable(realm.copyFromRealm(realm.where(Package.class).findAll())) + .subscribeOn(Schedulers.io()) + .flatMap(new Function>() { @Override - public void accept(Package aPackage) throws Exception { - refreshPackage(aPackage.getNumber()); + public ObservableSource apply(Package aPackage) throws Exception { + // A nested request. + return refreshPackage(aPackage.getNumber()); } - }); - return listObservable; - } - }); + }) + .toList() + .toObservable(); } /** @@ -134,14 +102,10 @@ public void accept(Package aPackage) throws Exception { * @return The observable package of latest status. */ @Override - public Observable refreshPackage(@NonNull - final String packageId) { + public Observable refreshPackage(@NonNull String packageId) { // It is necessary to build a new realm instance // in a different thread. - Realm realm = Realm.getInstance(new RealmConfiguration.Builder() - .deleteRealmIfMigrationNeeded() - .name(DATABASE_NAME) - .build()); + Realm realm = RealmHelper.newRealmInstance(); // Set a copy rather than use the raw data. final Package p = realm.copyFromRealm(realm.where(Package.class) .equalTo("number", packageId) @@ -158,43 +122,34 @@ public boolean test(Package aPackage) throws Exception { } }) .subscribeOn(Schedulers.io()) - .publish(new Function, ObservableSource>() { + .doOnNext(new Consumer() { @Override - public ObservableSource apply(Observable packageObservable) throws Exception { - packageObservable.subscribe(new Consumer() { - @Override - public void accept(Package aPackage) throws Exception { - - // To avoid the server error or other problems - // making the data in database being dirty. - if (aPackage != null && aPackage.getData() != null) { - // It is necessary to build a new realm instance - // in a different thread. - Realm rlm = Realm.getInstance(new RealmConfiguration.Builder() - .deleteRealmIfMigrationNeeded() - .name(DATABASE_NAME) - .build()); - - // Only when the origin data is null or the origin - // data's size is less than the latest data's size - // set the package unread new(readable = true). - if (p.getData() == null || aPackage.getData().size() > p.getData().size()) { - p.setReadable(true); - p.setPushable(true); - p.setState(aPackage.getState()); - } - - p.setData(aPackage.getData()); - // DO NOT forget to begin a transaction. - rlm.beginTransaction(); - rlm.copyToRealmOrUpdate(p); - rlm.commitTransaction(); - - rlm.close(); - } + public void accept(Package aPackage) throws Exception { + + // To avoid the server error or other problems + // making the data in database being dirty. + if (aPackage != null && aPackage.getData() != null) { + // It is necessary to build a new realm instance + // in a different thread. + Realm rlm = RealmHelper.newRealmInstance(); + + // Only when the origin data is null or the origin + // data's size is less than the latest data's size + // set the package unread new(readable = true). + if (p.getData() == null || aPackage.getData().size() > p.getData().size()) { + p.setReadable(true); + p.setPushable(true); + p.setState(aPackage.getState()); } - }); - return packageObservable; + + p.setData(aPackage.getData()); + // DO NOT forget to begin a transaction. + rlm.beginTransaction(); + rlm.copyToRealmOrUpdate(p); + rlm.commitTransaction(); + + rlm.close(); + } } }); } diff --git a/mobile/src/main/res/layout/fragment_add_package.xml b/mobile/src/main/res/layout/fragment_add_package.xml index 61a16d9..d968abc 100644 --- a/mobile/src/main/res/layout/fragment_add_package.xml +++ b/mobile/src/main/res/layout/fragment_add_package.xml @@ -88,15 +88,19 @@ android:layout_height="wrap_content" android:id="@+id/inputLayoutNumber" app:counterEnabled="true" + app:counterTextAppearance="@style/InputTextLayoutCounterStyle" app:counterMaxLength="24" - app:errorEnabled="true"> + app:errorEnabled="true" + android:textColorHint="@color/colorSecondaryText"> @@ -116,13 +120,17 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputLayoutName" - app:errorEnabled="true"> + app:errorEnabled="true" + android:textColorHint="@color/colorSecondaryText"> diff --git a/mobile/src/main/res/layout/item_package_status.xml b/mobile/src/main/res/layout/item_package_status.xml index b251f06..d2e381f 100644 --- a/mobile/src/main/res/layout/item_package_status.xml +++ b/mobile/src/main/res/layout/item_package_status.xml @@ -59,6 +59,7 @@ android:id="@+id/contactCard" android:stateListAnimator="@animator/lift_on_touch" android:visibility="gone" + app:cardBackgroundColor="@color/layout_background" app:cardElevation="2dp" app:cardCornerRadius="2dp" app:cardUseCompatPadding="true"> @@ -107,6 +108,7 @@ android:layout_width="24dp" android:layout_height="24dp" android:src="@drawable/ic_phone_black_24dp" + android:tint="@color/colorPrimaryText" android:layout_gravity="center_vertical" android:layout_margin="8dp"/> diff --git a/mobile/src/main/res/values/styles.xml b/mobile/src/main/res/values/styles.xml index 3f2ca38..c429a2f 100644 --- a/mobile/src/main/res/values/styles.xml +++ b/mobile/src/main/res/values/styles.xml @@ -40,6 +40,10 @@ true + +