Search in sources :

Example 1 with ListUtils

use of com.kickstarter.libs.utils.ListUtils in project android-oss by kickstarter.

the class ThanksViewModel method relatedProjects.

/**
   * Returns a shuffled list of 3 recommended projects, with fallbacks to similar and staff picked projects
   * for users with fewer than 3 recommendations.
   */
@NonNull
private Observable<List<Project>> relatedProjects(@NonNull final Project project) {
    final DiscoveryParams recommendedParams = DiscoveryParams.builder().backed(-1).recommended(true).perPage(6).build();
    final DiscoveryParams similarToParams = DiscoveryParams.builder().backed(-1).similarTo(project).perPage(3).build();
    final Category category = project.category();
    final DiscoveryParams staffPickParams = DiscoveryParams.builder().category(category == null ? null : category.root()).backed(-1).staffPicks(true).perPage(3).build();
    final Observable<Project> recommendedProjects = apiClient.fetchProjects(recommendedParams).retry(2).map(DiscoverEnvelope::projects).map(ListUtils::shuffle).flatMap(Observable::from).take(3);
    final Observable<Project> similarToProjects = apiClient.fetchProjects(similarToParams).retry(2).map(DiscoverEnvelope::projects).flatMap(Observable::from);
    final Observable<Project> staffPickProjects = apiClient.fetchProjects(staffPickParams).retry(2).map(DiscoverEnvelope::projects).flatMap(Observable::from);
    return Observable.concat(recommendedProjects, similarToProjects, staffPickProjects).compose(neverError()).distinct().take(3).toList();
}
Also used : Project(com.kickstarter.models.Project) Category(com.kickstarter.models.Category) DiscoveryParams(com.kickstarter.services.DiscoveryParams) ListUtils(com.kickstarter.libs.utils.ListUtils) Observable(rx.Observable) NonNull(android.support.annotation.NonNull)

Aggregations

NonNull (android.support.annotation.NonNull)1 ListUtils (com.kickstarter.libs.utils.ListUtils)1 Category (com.kickstarter.models.Category)1 Project (com.kickstarter.models.Project)1 DiscoveryParams (com.kickstarter.services.DiscoveryParams)1 Observable (rx.Observable)1