package com.khmelenko.lab.varis.presenter;

import com.khmelenko.lab.varis.event.travis.FindReposEvent;
import com.khmelenko.lab.varis.event.travis.LoadingFailedEvent;
import com.khmelenko.lab.varis.mvp.MvpPresenter;
import com.khmelenko.lab.varis.task.TaskManager;
import com.khmelenko.lab.varis.view.SearchResultsView;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;

/**
 * Search results presenter
 *
 * @author Dmytro Khmelenko (d.khmelenko@gmail.com)
 */
public class SearchResultsPresenter extends MvpPresenter<SearchResultsView> {

    private final TaskManager mTaskManager;
    private final EventBus mEventBus;

    @Inject
    public SearchResultsPresenter(TaskManager taskManager, EventBus eventBus) {
        mTaskManager = taskManager;
        mEventBus = eventBus;
    }

    @Override
    public void onAttach() {
        mEventBus.register(this);
    }

    @Override
    public void onDetach() {
        mEventBus.unregister(this);
        getView().hideProgress();
    }

    /**
     * Raised on loaded repositories
     *
     * @param event Event data
     */
    public void onEvent(FindReposEvent event) {
        getView().hideProgress();
        getView().setSearchResults(event.getRepos());
    }

    /**
     * Raised on failed loading data
     *
     * @param event Event data
     */
    public void onEvent(LoadingFailedEvent event) {
        getView().hideProgress();
        getView().showLoadingError(event.getTaskError().getMessage());
    }

    /**
     * Starts repository search
     *
     * @param query Query string for search
     */
    public void startRepoSearch(String query) {
        mTaskManager.findRepos(query);
    }

}

@@ -48,60 +48,32 @@
 ort 
-com.khmelenko.lab.varis.event.travis.FindReposEvent;
+android.text.TextUtils;%0A
 %0Aimp
@@ -104,39 +104,24 @@
 ris.
-event.travis.LoadingFailedEv
+mvp.MvpPres
 ent
+er
 ;%0Aim
@@ -149,32 +149,37 @@
 b.varis.
-mvp.MvpPresenter
+network.response.Repo
 ;%0Aimport
@@ -207,24 +207,50 @@
 ris.
-task.TaskManager
+network.retrofit.travis.TravisRestClientRx
 ;%0Aim
@@ -303,16 +303,40 @@
 sView;%0A%0A
+import java.util.List;%0A%0A
 import j
@@ -367,35 +367,180 @@
 ort 
-de.greenrobot.event.EventBu
+io.reactivex.Single;%0Aimport io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.disposables.CompositeDisposable;%0Aimport io.reactivex.schedulers.Scheduler
 s;%0A%0A
@@ -732,33 +732,46 @@
 al T
-askManager mTaskManager
+ravisRestClientRx mTravisRestClient
 ;%0A
+%0A
     
@@ -788,25 +788,41 @@
 nal 
-EventBus mEventBu
+CompositeDisposable mSubscription
 s;%0A%0A
@@ -872,117 +872,143 @@
 er(T
-askManager taskManager, EventBus eventBus) %7B%0A        mTaskManager = taskManager;%0A        mEventBus = eventBus
+ravisRestClientRx travisRestClient) %7B%0A        mTravisRestClient = travisRestClient;%0A%0A        mSubscriptions = new CompositeDisposable()
 ;%0A  
@@ -1067,33 +1067,21 @@
     
-mEventBus.register(this);
+// do nothing
 %0A   
@@ -1139,66 +1139,62 @@
     
-mEventBus.unregister(this);%0A        getView().hideProgress
+getView().hideProgress();%0A        mSubscriptions.clear
 ();%0A
@@ -1219,24 +1219,14 @@
   * 
-Raised on loaded
+Starts
  rep
@@ -1235,11 +1235,16 @@
 itor
-ies
+y search
 %0A   
@@ -1265,155 +1265,234 @@
 ram 
-event Event data%0A     */%0A    public void onEvent(FindReposEvent event) %7B%0A        getView().hideProgress();%0A        getView().setSearchResults(event
+query Query string for search%0A     */%0A    public void startRepoSearch(String query) %7B%0A        Single%3CList%3CRepo%3E%3E reposSingle;%0A        if (!TextUtils.isEmpty(query)) %7B%0A            reposSingle = mTravisRestClient.getApiService()
 .get
@@ -1501,457 +1501,568 @@
 pos(
-)
+query
 );%0A    
-%7D%0A
+    %7D else %7B
 %0A    
-/**%0A
+   
      
-* Raised on failed loading data%0A     *%0A     * @param event Event data%0A
+reposSingle = mTravisRestClient.getApiService().getRepos();%0A        %7D%0A        reposSingle.subscribeOn(Schedulers.io())%0A 
      
-*/%0A
     
-public void onEvent(LoadingFailedEvent event) %7B%0A        getView().hideProgress();%0A        getView().showLoadingError(event.getTaskError().getMessage()
+      .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe((repos, throwable) -%3E %7B%0A                    getView().hideProgress();%0A                    if (throwable == null) %7B%0A                        getView().setSearchResults(repos
 );%0A    
-%7D%0A%0A
     
-/**%0A     * Starts repository search
+            %7D else %7B
 %0A     
-*%0A
      
-* @param query Query string for search%0A     */%0A    public void startRepoSearch(String query) %7B%0A        mTaskManager.findRepos(query
+              getView().showLoadingError(throwable.getMessage());%0A                    %7D%0A                %7D
 );%0A 

Migrate SearchResultsPresenter to RxJava approach