/**
 * Copyright (C) 2015 Fernando Cejas Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fernandocejas.android10.sample.presentation.presenter;

import android.support.annotation.NonNull;
import com.fernandocejas.android10.sample.domain.User;
import com.fernandocejas.android10.sample.domain.exception.DefaultErrorBundle;
import com.fernandocejas.android10.sample.domain.exception.ErrorBundle;
import com.fernandocejas.android10.sample.domain.interactor.DefaultSubscriber;
import com.fernandocejas.android10.sample.domain.interactor.UseCase;
import com.fernandocejas.android10.sample.presentation.exception.ErrorMessageFactory;
import com.fernandocejas.android10.sample.presentation.internal.di.PerActivity;
import com.fernandocejas.android10.sample.presentation.mapper.UserModelDataMapper;
import com.fernandocejas.android10.sample.presentation.model.UserModel;
import com.fernandocejas.android10.sample.presentation.view.UserListView;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * {@link Presenter} that controls communication between views and models of the presentation
 * layer.
 */
@PerActivity
public class UserListPresenter extends DefaultSubscriber<List<User>> implements Presenter {

  private UserListView viewListView;

  private final UseCase getUserListUseCase;
  private final UserModelDataMapper userModelDataMapper;

  @Inject
  public UserListPresenter(@Named("userList") UseCase getUserListUserCase, UserModelDataMapper userModelDataMapper) {
    this.getUserListUseCase = getUserListUserCase;
    this.userModelDataMapper = userModelDataMapper;
  }

  public void setView(@NonNull UserListView view) {
    this.viewListView = view;
  }

  @Override public void resume() {}

  @Override public void pause() {}

  @Override public void destroy() {
    this.getUserListUseCase.unsubscribe();
  }

  /**
   * Initializes the presenter by start retrieving the user list.
   */
  public void initialize() {
    this.loadUserList();
  }

  /**
   * Loads all users.
   */
  private void loadUserList() {
    this.hideViewRetry();
    this.showViewLoading();
    this.getUserList();
  }

  public void onUserClicked(UserModel userModel) {
    this.viewListView.viewUser(userModel);
  }

  private void showViewLoading() {
    this.viewListView.showLoading();
  }

  private void hideViewLoading() {
    this.viewListView.hideLoading();
  }

  private void showViewRetry() {
    this.viewListView.showRetry();
  }

  private void hideViewRetry() {
    this.viewListView.hideRetry();
  }

  private void showErrorMessage(ErrorBundle errorBundle) {
    String errorMessage = ErrorMessageFactory.create(this.viewListView.getContext(),
        errorBundle.getException());
    this.viewListView.showError(errorMessage);
  }

  private void showUsersCollectionInView(Collection<User> usersCollection) {
    final Collection<UserModel> userModelsCollection =
        this.userModelDataMapper.transform(usersCollection);
    this.viewListView.renderUserList(userModelsCollection);
  }

  private void getUserList() {
    this.getUserListUseCase.execute(this);
  }

  @Override
  public void onCompleted() {
    UserListPresenter.this.hideViewLoading();
  }

  @Override
  public void onError(Throwable e) {
    UserListPresenter.this.hideViewLoading();
    UserListPresenter.this.showErrorMessage(new DefaultErrorBundle((Exception) e));
    UserListPresenter.this.showViewRetry();
  }

  @Override
  public void onNext(List<User> users) {
    UserListPresenter.this.showUsersCollectionInView(users);
  }
}

@@ -1744,46 +1744,8 @@
 nter
- extends DefaultSubscriber%3CList%3CUser%3E%3E
  imp
@@ -3632,33 +3632,135 @@
 ute(
-this);%0A  %7D%0A%0A  @Override%0A 
+new UserListSubscriber());%0A  %7D%0A%0A  private final class UserListSubscriber extends DefaultSubscriber%3CList%3CUser%3E%3E %7B%0A%0A    @Override
  pub
@@ -3780,24 +3780,26 @@
 mpleted() %7B%0A
+  
     UserList
@@ -3830,27 +3830,31 @@
 oading();%0A  
+ 
+ 
 %7D%0A%0A
+  
   @Override%0A
@@ -3844,34 +3844,32 @@
 %7D%0A%0A    @Override
-%0A 
  public void onE
@@ -3884,24 +3884,26 @@
 owable e) %7B%0A
+  
     UserList
@@ -3936,24 +3936,26 @@
 ding();%0A    
+ 
+ 
 UserListPres
@@ -4022,24 +4022,26 @@
 n) e));%0A    
+  
 UserListPres
@@ -4068,21 +4068,25 @@
 etry();%0A
+  
   %7D%0A%0A
+  
   @Overr
@@ -4088,18 +4088,16 @@
 Override
-%0A 
  public 
@@ -4128,24 +4128,26 @@
 sers) %7B%0A    
+  
 UserListPres
@@ -4191,14 +4191,20 @@
 users);%0A
+    %7D%0A
   %7D%0A%7D%0A

Remove extends DefaultSubscriber from UserListPresenter