mirror of
https://github.com/android10/Android-CleanArchitecture.git
synced 2025-08-26 07:17:04 +08:00
Lambda-nize data layer.
This commit is contained in:
@ -24,7 +24,6 @@ import java.io.File;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import rx.Observable;
|
||||
import rx.Subscriber;
|
||||
|
||||
/**
|
||||
* {@link UserCache} implementation.
|
||||
@ -65,18 +64,16 @@ public class UserCacheImpl implements UserCache {
|
||||
}
|
||||
|
||||
@Override public Observable<UserEntity> get(final int userId) {
|
||||
return Observable.create(new Observable.OnSubscribe<UserEntity>() {
|
||||
@Override public void call(Subscriber<? super UserEntity> subscriber) {
|
||||
File userEntityFile = UserCacheImpl.this.buildFile(userId);
|
||||
String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
|
||||
UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent);
|
||||
return Observable.create(subscriber -> {
|
||||
File userEntityFile = UserCacheImpl.this.buildFile(userId);
|
||||
String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
|
||||
UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent);
|
||||
|
||||
if (userEntity != null) {
|
||||
subscriber.onNext(userEntity);
|
||||
subscriber.onCompleted();
|
||||
} else {
|
||||
subscriber.onError(new UserNotFoundException());
|
||||
}
|
||||
if (userEntity != null) {
|
||||
subscriber.onNext(userEntity);
|
||||
subscriber.onCompleted();
|
||||
} else {
|
||||
subscriber.onError(new UserNotFoundException());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
* 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
|
||||
* 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,
|
||||
@ -25,7 +25,6 @@ import com.fernandocejas.frodo.annotation.RxLogObservable;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.List;
|
||||
import rx.Observable;
|
||||
import rx.Subscriber;
|
||||
|
||||
/**
|
||||
* {@link RestApi} implementation for retrieving data from the network.
|
||||
@ -51,49 +50,43 @@ public class RestApiImpl implements RestApi {
|
||||
|
||||
@RxLogObservable
|
||||
@Override public Observable<List<UserEntity>> userEntityList() {
|
||||
return Observable.create(new Observable.OnSubscribe<List<UserEntity>>() {
|
||||
@Override public void call(Subscriber<? super List<UserEntity>> subscriber) {
|
||||
|
||||
if (isThereInternetConnection()) {
|
||||
try {
|
||||
String responseUserEntities = getUserEntitiesFromApi();
|
||||
if (responseUserEntities != null) {
|
||||
subscriber.onNext(userEntityJsonMapper.transformUserEntityCollection(
|
||||
responseUserEntities));
|
||||
subscriber.onCompleted();
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
subscriber.onError(new NetworkConnectionException(e.getCause()));
|
||||
return Observable.create(subscriber -> {
|
||||
if (isThereInternetConnection()) {
|
||||
try {
|
||||
String responseUserEntities = getUserEntitiesFromApi();
|
||||
if (responseUserEntities != null) {
|
||||
subscriber.onNext(userEntityJsonMapper.transformUserEntityCollection(
|
||||
responseUserEntities));
|
||||
subscriber.onCompleted();
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
}
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
} catch (Exception e) {
|
||||
subscriber.onError(new NetworkConnectionException(e.getCause()));
|
||||
}
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@RxLogObservable
|
||||
@Override public Observable<UserEntity> userEntityById(final int userId) {
|
||||
return Observable.create(new Observable.OnSubscribe<UserEntity>() {
|
||||
@Override public void call(Subscriber<? super UserEntity> subscriber) {
|
||||
|
||||
if (isThereInternetConnection()) {
|
||||
try {
|
||||
String responseUserDetails = getUserDetailsFromApi(userId);
|
||||
if (responseUserDetails != null) {
|
||||
subscriber.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails));
|
||||
subscriber.onCompleted();
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
subscriber.onError(new NetworkConnectionException(e.getCause()));
|
||||
return Observable.create(subscriber -> {
|
||||
if (isThereInternetConnection()) {
|
||||
try {
|
||||
String responseUserDetails = getUserDetailsFromApi(userId);
|
||||
if (responseUserDetails != null) {
|
||||
subscriber.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails));
|
||||
subscriber.onCompleted();
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
}
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
} catch (Exception e) {
|
||||
subscriber.onError(new NetworkConnectionException(e.getCause()));
|
||||
}
|
||||
} else {
|
||||
subscriber.onError(new NetworkConnectionException());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -30,12 +30,11 @@ public class CloudUserDataStore implements UserDataStore {
|
||||
private final RestApi restApi;
|
||||
private final UserCache userCache;
|
||||
|
||||
private final Action1<UserEntity> saveToCacheAction =
|
||||
userEntity -> {
|
||||
if (userEntity != null) {
|
||||
CloudUserDataStore.this.userCache.put(userEntity);
|
||||
}
|
||||
};
|
||||
private final Action1<UserEntity> saveToCacheAction = userEntity -> {
|
||||
if (userEntity != null) {
|
||||
CloudUserDataStore.this.userCache.put(userEntity);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Construct a {@link UserDataStore} based on connections to the api (Cloud).
|
||||
@ -53,7 +52,6 @@ public class CloudUserDataStore implements UserDataStore {
|
||||
}
|
||||
|
||||
@Override public Observable<UserEntity> userEntityDetails(final int userId) {
|
||||
return this.restApi.userEntityById(userId)
|
||||
.doOnNext(saveToCacheAction);
|
||||
return this.restApi.userEntityById(userId).doOnNext(saveToCacheAction);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user