Skip to content

Commit

Permalink
Merge branch 'feature/uncouple_subscribers_form_usecase_implementatio…
Browse files Browse the repository at this point in the history
…ns' into develop
  • Loading branch information
saulmm committed Dec 21, 2015
2 parents 6bb4e34 + 22d40cc commit ab4bd7f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public AvengerInformationModule(int characterId) {
mCharacterId = characterId;
}

@Provides @Activity CharacterDetailsUsecase provideGetCharacterInformationUsecase (CharacterRepository repository, @Named("ui_thread") Scheduler uiThread,
@Named("executor_thread") Scheduler executorThread) {
return new CharacterDetailsUsecase(mCharacterId, repository, uiThread, executorThread);
@Provides @Activity CharacterDetailsUsecase provideGetCharacterInformationUsecase (CharacterRepository repository) {
return new CharacterDetailsUsecase(mCharacterId, repository);
}

@Provides @Activity GetCollectionUsecase provideGetCharacterComicsUsecase (CharacterRepository repository) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void askForNewCharacters() {
mAvengersView.showLoadingMoreCharactersIndicator();
mIsTheCharacterRequestRunning = true;

mCharactersSubscription = mCharactersUsecase.executeIncreasingOffset()
mCharactersSubscription = mCharactersUsecase.execute()
.subscribe(this::onNewCharactersReceived, this::onNewCharactersError);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public void attachView(View v) {

@Override
public void onCreate() {
mGetCollectionUsecase.execute(mCollectionType)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
mGetCollectionUsecase.setType(mCollectionType);
mGetCollectionUsecase.execute()
.subscribe(this::onCollectionItemsReceived);
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/test/java/ListPresenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class ListPresenterTest {
@Test public void testThatPresenterShowsALightErrorLoadingMoreCharacters() throws Exception {
CharacterListPresenter listPresenter = givenAListPresenter();

when(mockGetCharacterUsecase.executeIncreasingOffset()).thenReturn(Observable.error(new Exception()));
when(mockGetCharacterUsecase.execute()).thenReturn(Observable.error(new Exception()));
listPresenter.askForNewCharacters();

verify(mockCharacterListView, times(1)).showLightError();
Expand All @@ -66,10 +66,10 @@ public class ListPresenterTest {
@Test public void testThatPresenterRequestMoreCharacters() throws Exception {
CharacterListPresenter listPresenter = givenAListPresenter();

when(mockGetCharacterUsecase.executeIncreasingOffset()).thenReturn(getFakeObservableCharacterList());
when(mockGetCharacterUsecase.execute()).thenReturn(getFakeObservableCharacterList());
listPresenter.askForNewCharacters();

verify(mockGetCharacterUsecase, only()).executeIncreasingOffset();
verify(mockGetCharacterUsecase, only()).execute();
}

private ArrayList<MarvelCharacter> givenAFakeCharacterList() {
Expand Down
16 changes: 4 additions & 12 deletions domain/src/main/java/saulmm/avengers/CharacterDetailsUsecase.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@
import saulmm.avengers.entities.MarvelCharacter;
import saulmm.avengers.repository.CharacterRepository;

public class CharacterDetailsUsecase implements Usecase<MarvelCharacter> {
public class CharacterDetailsUsecase extends Usecase<MarvelCharacter> {
private final CharacterRepository mRepository;
private final Scheduler mResultsThread;
private final Scheduler mExecutorThread;
private int mCharacterId;

@Inject public CharacterDetailsUsecase(int characterId,
CharacterRepository repository,
@Named("ui_thread") Scheduler uiThread,
@Named("executor_thread") Scheduler executorThread) {
CharacterRepository repository) {

mCharacterId = characterId;
mRepository = repository;
mResultsThread = uiThread;
mExecutorThread = executorThread;
}

@Override
public Observable<MarvelCharacter> execute() {
return mRepository.getCharacter(mCharacterId)
.observeOn(mResultsThread)
.subscribeOn(mExecutorThread);
public Observable<MarvelCharacter> buildObservable() {
return mRepository.getCharacter(mCharacterId);
}
}
52 changes: 21 additions & 31 deletions domain/src/main/java/saulmm/avengers/GetCharactersUsecase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,42 @@
import saulmm.avengers.entities.MarvelCharacter;
import saulmm.avengers.repository.CharacterRepository;

public class GetCharactersUsecase implements Usecase<List<MarvelCharacter>> {
public class GetCharactersUsecase extends Usecase<List<MarvelCharacter>> {
public final static int DEFAULT_CHARACTERS_LIMIT = 20;

private int mCharactersLimit = DEFAULT_CHARACTERS_LIMIT;
private final Scheduler mResultsThread;
private final Scheduler mExecutorThread;

private final CharacterRepository mRepository;
private int mCurrentOffset;

@Inject public GetCharactersUsecase(CharacterRepository repository,
@Named("ui_thread") Scheduler uiThread,
@Named("executor_thread") Scheduler executorThread) {
Scheduler uiThread, executorThread;


@Inject public GetCharactersUsecase(CharacterRepository repository, @Named("ui_thread") Scheduler uiThread, @Named("executor_thread") Scheduler executorThread) {
mRepository = repository;
mResultsThread = uiThread;
mExecutorThread = executorThread;
this.uiThread = uiThread;
this.executorThread = executorThread;
}

@Override
public Observable<List<MarvelCharacter>> execute() {
return mRepository.getCharacters(mCurrentOffset)
.observeOn(mResultsThread)
.subscribeOn(mExecutorThread);
}

public Observable<List<MarvelCharacter>> executeIncreasingOffset() {
mCurrentOffset += mCharactersLimit;

public Observable<List<MarvelCharacter>> buildObservable() {
return mRepository.getCharacters(mCurrentOffset)
.observeOn(mResultsThread)
.subscribeOn(mExecutorThread)
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
mCurrentOffset -= mCharactersLimit;
}
});
.observeOn(uiThread)
.subscribeOn(executorThread)
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
mCurrentOffset -= mCharactersLimit;
}
});
}

public void setCharactersLimit(int charactersLimit) {
mCharactersLimit = charactersLimit;
@Override
public Observable<List<MarvelCharacter>> execute() {
increaseOffset();
return super.execute();
}

public void setCurrentOffset(int currentOffset) {
mCurrentOffset = currentOffset;
public void increaseOffset() {
mCurrentOffset += mCharactersLimit;
}

public int getCurrentOffset() {
Expand Down
20 changes: 11 additions & 9 deletions domain/src/main/java/saulmm/avengers/GetCollectionUsecase.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
import saulmm.avengers.entities.CollectionItem;
import saulmm.avengers.repository.CharacterRepository;

public class GetCollectionUsecase implements Usecase<List<CollectionItem>> {
public class GetCollectionUsecase extends Usecase<List<CollectionItem>> {
private final CharacterRepository mRepository;
private final int mCharacterId;
private String mType;

@Inject public GetCollectionUsecase(int characterId, CharacterRepository repository) {
mRepository = repository;
mCharacterId = characterId;
}

@Override
public Observable<List<CollectionItem>> execute() {
return null;
}

public Observable<List<CollectionItem>> execute(String type) {
public void setType(String type) {
if (!type.equals(CollectionItem.COMICS) && !type.equals(CollectionItem.EVENTS) && !type.equals(
CollectionItem.SERIES) && !type.equals(CollectionItem.STORIES))
CollectionItem.SERIES) && !type.equals(CollectionItem.STORIES))

throw new IllegalArgumentException("Collection type must be events|series|comics|stories");

return mRepository.getCharacterCollection(mCharacterId, type);
mType = type;
}

@Override
public Observable<List<CollectionItem>> buildObservable() {
return mRepository.getCharacterCollection(mCharacterId, mType);
}
}
12 changes: 10 additions & 2 deletions domain/src/main/java/saulmm/avengers/Usecase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
*/
package saulmm.avengers;

import javax.inject.Inject;
import javax.inject.Named;

import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;

public interface Usecase<T> {
public abstract class Usecase<T> {
public abstract Observable<T> buildObservable();

Observable<T> execute();
public Observable<T> execute() {
return buildObservable();
}
}
2 changes: 1 addition & 1 deletion domain/src/test/java/GetCharacterDetailsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class GetCharacterDetailsTest {
}

private CharacterDetailsUsecase givenACharacterUsecase() {
return new CharacterDetailsUsecase(FAKE_CHARACTER_ID, mRepository, mockScheduler, mockScheduler);
return new CharacterDetailsUsecase(FAKE_CHARACTER_ID, mRepository);
}

private Observable<MarvelCharacter> getFakeCharacterObservable() {
Expand Down
23 changes: 13 additions & 10 deletions domain/src/test/java/GetCharactersUsecaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.when;
import static saulmm.avengers.GetCharactersUsecase.DEFAULT_CHARACTERS_LIMIT;

public class GetCharactersUsecaseTest {
@Mock CharacterRepository mockRepository;
@Mock Scheduler mockUiScheduler;
@Mock Scheduler mockExecutorScheduler;

@Before public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
Expand All @@ -36,26 +39,26 @@ public class GetCharactersUsecaseTest {

@Test public void testThatCharactersUsecaseIncrementsOffset() throws Exception {
GetCharactersUsecase charactersUsecase = givenACharactersUsecase();

when(mockRepository.getCharacters(anyInt())).thenReturn(getFakeObservableCharacterList());
charactersUsecase.executeIncreasingOffset();

assertThat(charactersUsecase.getCurrentOffset(), is(DEFAULT_CHARACTERS_LIMIT));
charactersUsecase.execute();
charactersUsecase.execute();
charactersUsecase.execute();

assertThat(charactersUsecase.getCurrentOffset(), is(DEFAULT_CHARACTERS_LIMIT * 3));
}

@Test public void testThatCharactersUsecaseWithOffsetIsCalledOnce() throws Exception {
GetCharactersUsecase charactersUsecase = givenACharactersUsecase();
int fakeCurrentOffset = 20;
when(mockRepository.getCharacters(anyInt())).thenReturn(getFakeObservableCharacterList());

charactersUsecase.execute();

when(mockRepository.getCharacters(fakeCurrentOffset)).thenReturn(getFakeObservableCharacterList());
charactersUsecase.executeIncreasingOffset();

Mockito.verify(mockRepository, only()).getCharacters(fakeCurrentOffset);
Mockito.verify(mockRepository, only()).getCharacters(anyInt());
}

private GetCharactersUsecase givenACharactersUsecase() {
return new GetCharactersUsecase(mockRepository,
Schedulers.io(), Schedulers.newThread());
return new GetCharactersUsecase(mockRepository, mockUiScheduler, mockExecutorScheduler);
}

private Observable<List<MarvelCharacter>> getFakeObservableCharacterList() {
Expand Down

0 comments on commit ab4bd7f

Please sign in to comment.