Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rxjava2 migration #201

Merged
merged 2 commits into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath "com.fernandocejas.frodo:frodo-plugin:0.8.3"
}
}

Expand Down
14 changes: 7 additions & 7 deletions buildsystem/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ ext {
daggerVersion = '2.8'
butterKnifeVersion = '7.0.1'
recyclerViewVersion = '21.0.3'
rxJavaVersion = '1.0.14'
rxAndroidVersion = '1.0.1'
rxJavaVersion = '2.0.2'
rxAndroidVersion = '2.0.1'
javaxAnnotationVersion = '1.0'
javaxInjectVersion = '1'
gsonVersion = '2.3'
Expand All @@ -41,8 +41,8 @@ ext {
dagger: "com.google.dagger:dagger:${daggerVersion}",
butterKnife: "com.jakewharton:butterknife:${butterKnifeVersion}",
recyclerView: "com.android.support:recyclerview-v7:${recyclerViewVersion}",
rxJava: "io.reactivex:rxjava:${rxJavaVersion}",
rxAndroid: "io.reactivex:rxandroid:${rxAndroidVersion}",
rxJava: "io.reactivex.rxjava2:rxjava:${rxJavaVersion}",
rxAndroid: "io.reactivex.rxjava2:rxandroid:${rxAndroidVersion}",
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}"
]

Expand All @@ -57,7 +57,7 @@ ext {
domainDependencies = [
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
javaxInject: "javax.inject:javax.inject:${javaxInjectVersion}",
rxJava: "io.reactivex:rxjava:${rxJavaVersion}",
rxJava: "io.reactivex.rxjava2:rxjava:${rxJavaVersion}",
arrow: "com.fernandocejas:arrow:${arrowVersion}"
]

Expand All @@ -72,8 +72,8 @@ ext {
dagger: "com.google.dagger:dagger:${daggerVersion}",
okHttp: "com.squareup.okhttp:okhttp:${okHttpVersion}",
gson: "com.google.code.gson:gson:${gsonVersion}",
rxJava: "io.reactivex:rxjava:${rxJavaVersion}",
rxAndroid: "io.reactivex:rxandroid:${rxAndroidVersion}",
rxJava: "io.reactivex.rxjava2:rxjava:${rxJavaVersion}",
rxAndroid: "io.reactivex.rxjava2:rxandroid:${rxAndroidVersion}",
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
javaxInject: "javax.inject:javax.inject:${javaxInjectVersion}",
androidAnnotations: "com.android.support:support-annotations:${androidAnnotationsVersion}"
Expand Down
1 change: 0 additions & 1 deletion data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ buildscript {

apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.fernandocejas.frodo'
apply plugin: 'me.tatarka.retrolambda'

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
package com.fernandocejas.android10.sample.data.cache;

import com.fernandocejas.android10.sample.data.entity.UserEntity;
import rx.Observable;
import io.reactivex.Observable;

/**
* An interface representing a user Cache.
*/
public interface UserCache {
/**
* Gets an {@link rx.Observable} which will emit a {@link UserEntity}.
* Gets an {@link Observable} which will emit a {@link UserEntity}.
*
* @param userId The user id to retrieve data.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.fernandocejas.android10.sample.data.entity.UserEntity;
import com.fernandocejas.android10.sample.data.exception.UserNotFoundException;
import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor;
import io.reactivex.Observable;
import java.io.File;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observable;

/**
* {@link UserCache} implementation.
Expand Down Expand Up @@ -63,17 +63,17 @@ public class UserCacheImpl implements UserCache {
}

@Override public Observable<UserEntity> get(final int userId) {
return Observable.create(subscriber -> {
return Observable.create(emitter -> {
final File userEntityFile = UserCacheImpl.this.buildFile(userId);
final String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
final UserEntity userEntity =
UserCacheImpl.this.serializer.deserialize(fileContent, UserEntity.class);

if (userEntity != null) {
subscriber.onNext(userEntity);
subscriber.onCompleted();
emitter.onNext(userEntity);
emitter.onComplete();
} else {
subscriber.onError(new UserNotFoundException());
emitter.onError(new UserNotFoundException());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.fernandocejas.android10.sample.data.net;

import com.fernandocejas.android10.sample.data.entity.UserEntity;
import io.reactivex.Observable;
import java.util.List;
import rx.Observable;

/**
* RestApi for retrieving data from the network.
Expand All @@ -32,12 +32,12 @@ public interface RestApi {
String API_URL_GET_USER_DETAILS = API_BASE_URL + "user_";

/**
* Retrieves an {@link rx.Observable} which will emit a List of {@link UserEntity}.
* Retrieves an {@link Observable} which will emit a List of {@link UserEntity}.
*/
Observable<List<UserEntity>> userEntityList();

/**
* Retrieves an {@link rx.Observable} which will emit a {@link UserEntity}.
* Retrieves an {@link Observable} which will emit a {@link UserEntity}.
*
* @param userId The user id used to get user data.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import com.fernandocejas.android10.sample.data.entity.UserEntity;
import com.fernandocejas.android10.sample.data.entity.mapper.UserEntityJsonMapper;
import com.fernandocejas.android10.sample.data.exception.NetworkConnectionException;
import com.fernandocejas.frodo.annotation.RxLogObservable;
import io.reactivex.Observable;
import java.net.MalformedURLException;
import java.util.List;
import rx.Observable;

/**
* {@link RestApi} implementation for retrieving data from the network.
Expand All @@ -48,45 +47,43 @@ public RestApiImpl(Context context, UserEntityJsonMapper userEntityJsonMapper) {
this.userEntityJsonMapper = userEntityJsonMapper;
}

@RxLogObservable
@Override public Observable<List<UserEntity>> userEntityList() {
return Observable.create(subscriber -> {
return Observable.create(emitter -> {
if (isThereInternetConnection()) {
try {
String responseUserEntities = getUserEntitiesFromApi();
if (responseUserEntities != null) {
subscriber.onNext(userEntityJsonMapper.transformUserEntityCollection(
emitter.onNext(userEntityJsonMapper.transformUserEntityCollection(
responseUserEntities));
subscriber.onCompleted();
emitter.onComplete();
} else {
subscriber.onError(new NetworkConnectionException());
emitter.onError(new NetworkConnectionException());
}
} catch (Exception e) {
subscriber.onError(new NetworkConnectionException(e.getCause()));
emitter.onError(new NetworkConnectionException(e.getCause()));
}
} else {
subscriber.onError(new NetworkConnectionException());
emitter.onError(new NetworkConnectionException());
}
});
}

@RxLogObservable
@Override public Observable<UserEntity> userEntityById(final int userId) {
return Observable.create(subscriber -> {
return Observable.create(emitter -> {
if (isThereInternetConnection()) {
try {
String responseUserDetails = getUserDetailsFromApi(userId);
if (responseUserDetails != null) {
subscriber.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails));
subscriber.onCompleted();
emitter.onNext(userEntityJsonMapper.transformUserEntity(responseUserDetails));
emitter.onComplete();
} else {
subscriber.onError(new NetworkConnectionException());
emitter.onError(new NetworkConnectionException());
}
} catch (Exception e) {
subscriber.onError(new NetworkConnectionException(e.getCause()));
emitter.onError(new NetworkConnectionException(e.getCause()));
}
} else {
subscriber.onError(new NetworkConnectionException());
emitter.onError(new NetworkConnectionException());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory;
import com.fernandocejas.android10.sample.domain.User;
import com.fernandocejas.android10.sample.domain.repository.UserRepository;
import io.reactivex.Observable;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observable;

/**
* {@link UserRepository} for retrieving user data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import com.fernandocejas.android10.sample.data.cache.UserCache;
import com.fernandocejas.android10.sample.data.entity.UserEntity;
import com.fernandocejas.android10.sample.data.net.RestApi;
import io.reactivex.Observable;
import java.util.List;
import rx.Observable;
import rx.functions.Action1;

/**
* {@link UserDataStore} implementation based on connections to the api (Cloud).
Expand All @@ -30,12 +29,6 @@ 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);
}
};

/**
* Construct a {@link UserDataStore} based on connections to the api (Cloud).
*
Expand All @@ -52,6 +45,6 @@ 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(CloudUserDataStore.this.userCache::put);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import com.fernandocejas.android10.sample.data.cache.UserCache;
import com.fernandocejas.android10.sample.data.entity.UserEntity;
import io.reactivex.Observable;
import java.util.List;
import rx.Observable;

/**
* {@link UserDataStore} implementation based on file system data store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
package com.fernandocejas.android10.sample.data.repository.datasource;

import com.fernandocejas.android10.sample.data.entity.UserEntity;
import io.reactivex.Observable;
import java.util.List;
import rx.Observable;

/**
* Interface that represents a data store from where data is retrieved.
*/
public interface UserDataStore {
/**
* Get an {@link rx.Observable} which will emit a List of {@link UserEntity}.
* Get an {@link Observable} which will emit a List of {@link UserEntity}.
*/
Observable<List<UserEntity>> userEntityList();

/**
* Get an {@link rx.Observable} which will emit a {@link UserEntity} by its id.
* Get an {@link Observable} which will emit a {@link UserEntity} by its id.
*
* @param userId The id to retrieve user data.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStore;
import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory;
import com.fernandocejas.android10.sample.domain.User;
import io.reactivex.Observable;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
Expand All @@ -29,7 +30,6 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import rx.Observable;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyInt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import com.fernandocejas.android10.sample.data.cache.UserCache;
import com.fernandocejas.android10.sample.data.entity.UserEntity;
import com.fernandocejas.android10.sample.data.net.RestApi;
import io.reactivex.Observable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import rx.Observable;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.fernandocejas.android10.sample.domain.executor;

import rx.Scheduler;
import io.reactivex.Scheduler;

/**
* Thread abstraction created to change the execution context from any thread to any other thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
*/
package com.fernandocejas.android10.sample.domain.interactor;

import io.reactivex.observers.DisposableObserver;

/**
* Default subscriber base class to be used whenever you want default error handling.
* Default {@link DisposableObserver} base class to be used whenever you want default error handling.
*/
public class DefaultSubscriber<T> extends rx.Subscriber<T> {
@Override public void onCompleted() {
public class DefaultObserver<T> extends DisposableObserver<T> {
@Override public void onNext(T t) {
// no-op by default.
}

@Override public void onError(Throwable e) {
@Override public void onComplete() {
// no-op by default.
}

@Override public void onNext(T t) {
@Override public void onError(Throwable exception) {
// no-op by default.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.fernandocejas.android10.sample.domain.repository.UserRepository;
import com.fernandocejas.arrow.annotations.VisibleForTesting;
import com.fernandocejas.arrow.optional.Optional;
import io.reactivex.Observable;
import javax.inject.Inject;
import rx.Observable;

/**
* This class is an implementation of {@link UseCase} that represents a use case for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor;
import com.fernandocejas.android10.sample.domain.repository.UserRepository;
import com.fernandocejas.arrow.optional.Optional;
import io.reactivex.Observable;
import javax.inject.Inject;
import rx.Observable;

/**
* This class is an implementation of {@link UseCase} that represents a use case for
Expand Down
Loading