Skip to content

Commit

Permalink
Use the Dribble ShotsRepository in search also
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Sep 3, 2018
1 parent 563a898 commit 1475478
Showing 1 changed file with 21 additions and 49 deletions.
70 changes: 21 additions & 49 deletions core/src/main/java/io/plaidapp/core/data/SearchDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@
package io.plaidapp.core.data;

import android.content.Context;

import io.plaidapp.core.designernews.Injection;
import io.plaidapp.core.designernews.domain.SearchStoriesUseCase;
import io.plaidapp.core.dribbble.DribbbleInjection;
import io.plaidapp.core.dribbble.data.ShotsRepository;
import io.plaidapp.core.dribbble.data.api.model.Shot;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import io.plaidapp.core.dribbble.data.search.DribbbleSearchService;
import io.plaidapp.core.dribbble.data.api.model.Shot;
import io.plaidapp.core.designernews.Injection;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
* Responsible for loading search results from dribbble and designer news. Instantiating classes are
* responsible for providing the {code onDataLoaded} method to do something with the data.
Expand All @@ -41,15 +37,16 @@ public abstract class SearchDataManager extends BaseDataManager<List<? extends P
implements LoadSourceCallback {

private final SearchStoriesUseCase searchStoriesUseCase;
private final ShotsRepository shotsRepository;

// state
private String query = "";
private int page = 1;
private List<Call> inflight;

public SearchDataManager(Context context) {
super();
searchStoriesUseCase = Injection.provideSearchStoriesUseCase(context);
inflight = new ArrayList<>();
shotsRepository = DribbbleInjection.provideShotsRepository();
}

public void searchFor(String query) {
Expand All @@ -76,13 +73,8 @@ public void clear() {

@Override
public void cancelLoading() {
if (inflight.size() > 0) {
for (Call call : inflight) {
call.cancel();
}
inflight.clear();
searchStoriesUseCase.cancelAllRequests();
}
searchStoriesUseCase.cancelAllRequests();
shotsRepository.cancelAllSearches();
}

public String getQuery() {
Expand All @@ -97,38 +89,23 @@ private void searchDesignerNews(final String query, final int resultsPage) {

private void searchDribbble(final String query, final int resultsPage) {
loadStarted();
final Call<List<Shot>> dribbbleSearchCall = getDribbbleSearchApi().search(
query, resultsPage, DribbbleSearchService.PER_PAGE_DEFAULT,
DribbbleSearchService.SORT_POPULAR);
dribbbleSearchCall.enqueue(new Callback<List<Shot>>() {
@Override
public void onResponse(Call<List<Shot>> call, Response<List<Shot>> response) {
if (response.isSuccessful()) {
loadFinished();
final List<Shot> shots = response.body();
if (shots != null) {
setPage(shots, resultsPage);
setDataSource(shots,
Source.DribbbleSearchSource.DRIBBBLE_QUERY_PREFIX + query);
onDataLoaded(shots);
}
inflight.remove(dribbbleSearchCall);
} else {
failure(dribbbleSearchCall);
}
}

@Override
public void onFailure(Call<List<Shot>> call, Throwable t) {
failure(dribbbleSearchCall);
shotsRepository.search(query, page, result -> {
loadFinished();
if (result instanceof Result.Success) {
List<Shot> shots = ((Result.Success<? extends List<Shot>>) result).getData();
setPage(shots, resultsPage);
setDataSource(shots,
Source.DribbbleSearchSource.DRIBBBLE_QUERY_PREFIX + query);
onDataLoaded(shots);
}
return Unit.INSTANCE;
});
inflight.add(dribbbleSearchCall);

}

@Override
public void sourceLoaded(@Nullable List<? extends PlaidItem> result, int page,
@NotNull String source) {
@NotNull String source) {
loadFinished();
if (result != null) {
setPage(result, page);
Expand All @@ -143,9 +120,4 @@ public void loadFailed(@NotNull String source) {
loadFinished();
}

private void failure(Call call) {
loadFinished();
inflight.remove(call);
}

}

0 comments on commit 1475478

Please sign in to comment.