Skip to content

Commit 32a8833

Browse files
committed
Lambda-nize data layer.
1 parent ea29aff commit 32a8833

File tree

3 files changed

+44
-56
lines changed

3 files changed

+44
-56
lines changed

data/src/main/java/com/fernandocejas/android10/sample/data/cache/UserCacheImpl.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.inject.Inject;
2525
import javax.inject.Singleton;
2626
import rx.Observable;
27-
import rx.Subscriber;
2827

2928
/**
3029
* {@link UserCache} implementation.
@@ -65,18 +64,16 @@ public UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
6564
}
6665

6766
@Override public Observable<UserEntity> get(final int userId) {
68-
return Observable.create(new Observable.OnSubscribe<UserEntity>() {
69-
@Override public void call(Subscriber<? super UserEntity> subscriber) {
70-
File userEntityFile = UserCacheImpl.this.buildFile(userId);
71-
String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
72-
UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent);
73-
74-
if (userEntity != null) {
75-
subscriber.onNext(userEntity);
76-
subscriber.onCompleted();
77-
} else {
78-
subscriber.onError(new UserNotFoundException());
79-
}
67+
return Observable.create(subscriber -> {
68+
File userEntityFile = UserCacheImpl.this.buildFile(userId);
69+
String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
70+
UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent);
71+
72+
if (userEntity != null) {
73+
subscriber.onNext(userEntity);
74+
subscriber.onCompleted();
75+
} else {
76+
subscriber.onError(new UserNotFoundException());
8077
}
8178
});
8279
}

data/src/main/java/com/fernandocejas/android10/sample/data/net/RestApiImpl.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,7 +25,6 @@
2525
import java.net.MalformedURLException;
2626
import java.util.List;
2727
import rx.Observable;
28-
import rx.Subscriber;
2928

3029
/**
3130
* {@link RestApi} implementation for retrieving data from the network.
@@ -51,49 +50,43 @@ public RestApiImpl(Context context, UserEntityJsonMapper userEntityJsonMapper) {
5150

5251
@RxLogObservable
5352
@Override public Observable<List<UserEntity>> userEntityList() {
54-
return Observable.create(new Observable.OnSubscribe<List<UserEntity>>() {
55-
@Override public void call(Subscriber<? super List<UserEntity>> subscriber) {
56-
57-
if (isThereInternetConnection()) {
58-
try {
59-
String responseUserEntities = getUserEntitiesFromApi();
60-
if (responseUserEntities != null) {
61-
subscriber.onNext(userEntityJsonMapper.transformUserEntityCollection(
62-
responseUserEntities));
63-
subscriber.onCompleted();
64-
} else {
65-
subscriber.onError(new NetworkConnectionException());
66-
}
67-
} catch (Exception e) {
68-
subscriber.onError(new NetworkConnectionException(e.getCause()));
53+
return Observable.create(subscriber -> {
54+
if (isThereInternetConnection()) {
55+
try {
56+
String responseUserEntities = getUserEntitiesFromApi();
57+
if (responseUserEntities != null) {
58+
subscriber.onNext(userEntityJsonMapper.transformUserEntityCollection(
59+
responseUserEntities));
60+
subscriber.onCompleted();
61+
} else {
62+
subscriber.onError(new NetworkConnectionException());
6963
}
70-
} else {
71-
subscriber.onError(new NetworkConnectionException());
64+
} catch (Exception e) {
65+
subscriber.onError(new NetworkConnectionException(e.getCause()));
7266
}
67+
} else {
68+
subscriber.onError(new NetworkConnectionException());
7369
}
7470
});
7571
}
7672

7773
@RxLogObservable
7874
@Override public Observable<UserEntity> userEntityById(final int userId) {
79-
return Observable.create(new Observable.OnSubscribe<UserEntity>() {
80-
@Override public void call(Subscriber<? super UserEntity> subscriber) {
81-
82-
if (isThereInternetConnection()) {
83-
try {
84-
String responseUserDetails = getUserDetailsFromApi(userId);
85-
if (responseUserDetails != null) {
86-
subscriber.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails));
87-
subscriber.onCompleted();
88-
} else {
89-
subscriber.onError(new NetworkConnectionException());
90-
}
91-
} catch (Exception e) {
92-
subscriber.onError(new NetworkConnectionException(e.getCause()));
75+
return Observable.create(subscriber -> {
76+
if (isThereInternetConnection()) {
77+
try {
78+
String responseUserDetails = getUserDetailsFromApi(userId);
79+
if (responseUserDetails != null) {
80+
subscriber.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails));
81+
subscriber.onCompleted();
82+
} else {
83+
subscriber.onError(new NetworkConnectionException());
9384
}
94-
} else {
95-
subscriber.onError(new NetworkConnectionException());
85+
} catch (Exception e) {
86+
subscriber.onError(new NetworkConnectionException(e.getCause()));
9687
}
88+
} else {
89+
subscriber.onError(new NetworkConnectionException());
9790
}
9891
});
9992
}

data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/CloudUserDataStore.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ public class CloudUserDataStore implements UserDataStore {
3030
private final RestApi restApi;
3131
private final UserCache userCache;
3232

33-
private final Action1<UserEntity> saveToCacheAction =
34-
userEntity -> {
35-
if (userEntity != null) {
36-
CloudUserDataStore.this.userCache.put(userEntity);
37-
}
38-
};
33+
private final Action1<UserEntity> saveToCacheAction = userEntity -> {
34+
if (userEntity != null) {
35+
CloudUserDataStore.this.userCache.put(userEntity);
36+
}
37+
};
3938

4039
/**
4140
* Construct a {@link UserDataStore} based on connections to the api (Cloud).
@@ -53,7 +52,6 @@ public CloudUserDataStore(RestApi restApi, UserCache userCache) {
5352
}
5453

5554
@Override public Observable<UserEntity> userEntityDetails(final int userId) {
56-
return this.restApi.userEntityById(userId)
57-
.doOnNext(saveToCacheAction);
55+
return this.restApi.userEntityById(userId).doOnNext(saveToCacheAction);
5856
}
5957
}

0 commit comments

Comments
 (0)