2015-09-26 18:58:01 +02:00
|
|
|
package eu.kanade.mangafeed.presenter;
|
|
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
import eu.kanade.mangafeed.App;
|
|
|
|
|
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
|
2015-09-29 14:40:36 +02:00
|
|
|
import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
|
2015-09-28 12:42:16 +02:00
|
|
|
import eu.kanade.mangafeed.data.models.Manga;
|
2015-09-26 18:58:01 +02:00
|
|
|
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
|
|
|
|
|
import eu.kanade.mangafeed.view.LibraryView;
|
2015-09-30 19:03:11 +02:00
|
|
|
import rx.Observable;
|
|
|
|
|
import rx.Subscription;
|
|
|
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
|
|
|
import rx.schedulers.Schedulers;
|
|
|
|
|
import rx.subjects.PublishSubject;
|
|
|
|
|
import timber.log.Timber;
|
2015-09-28 02:35:54 +02:00
|
|
|
import uk.co.ribot.easyadapter.EasyAdapter;
|
2015-09-26 18:58:01 +02:00
|
|
|
|
|
|
|
|
import static rx.android.schedulers.AndroidSchedulers.mainThread;
|
|
|
|
|
|
2015-09-30 19:03:11 +02:00
|
|
|
public class LibraryPresenter extends BasePresenter {
|
2015-09-26 18:58:01 +02:00
|
|
|
|
2015-09-28 15:02:46 +02:00
|
|
|
private LibraryView view;
|
2015-09-26 18:58:01 +02:00
|
|
|
|
|
|
|
|
@Inject
|
2015-09-29 14:40:36 +02:00
|
|
|
DatabaseHelper db;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
PreferencesHelper prefs;
|
2015-09-26 18:58:01 +02:00
|
|
|
|
2015-09-30 19:03:11 +02:00
|
|
|
private Subscription searchViewSubscription;
|
|
|
|
|
private PublishSubject<Observable<String>> searchViewPublishSubject;
|
|
|
|
|
|
2015-09-28 15:02:46 +02:00
|
|
|
public LibraryPresenter(LibraryView view) {
|
|
|
|
|
this.view = view;
|
|
|
|
|
App.getComponent(view.getActivity()).inject(this);
|
2015-09-29 14:40:36 +02:00
|
|
|
|
|
|
|
|
//TODO remove, only for testing
|
|
|
|
|
if (prefs.isFirstRun()) {
|
|
|
|
|
db.manga.createDummyManga();
|
|
|
|
|
db.chapter.createDummyChapters();
|
|
|
|
|
prefs.setNotFirstRun();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-26 18:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-28 12:42:16 +02:00
|
|
|
public void onMangaClick(EasyAdapter<Manga> adapter, int position) {
|
|
|
|
|
Intent intent = MangaDetailActivity.newIntent(
|
2015-09-28 15:02:46 +02:00
|
|
|
view.getActivity(),
|
2015-09-28 12:42:16 +02:00
|
|
|
adapter.getItem(position)
|
|
|
|
|
);
|
2015-09-28 15:02:46 +02:00
|
|
|
view.getActivity().startActivity(intent);
|
2015-09-26 18:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-30 19:03:11 +02:00
|
|
|
public void initializeSearch() {
|
|
|
|
|
searchViewPublishSubject = PublishSubject.create();
|
|
|
|
|
searchViewSubscription = Observable.switchOnNext(searchViewPublishSubject)
|
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2015-09-30 21:07:45 +02:00
|
|
|
.subscribe(view.getAdapter().getFilter()::filter);
|
2015-09-30 19:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-26 18:58:01 +02:00
|
|
|
public void initializeMangas() {
|
|
|
|
|
db.manga.get()
|
|
|
|
|
.observeOn(mainThread())
|
2015-09-28 15:02:46 +02:00
|
|
|
.subscribe(view::setMangas);
|
2015-09-26 18:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-30 19:03:11 +02:00
|
|
|
public void onQueryTextChange(String query) {
|
|
|
|
|
if (searchViewPublishSubject != null) {
|
|
|
|
|
searchViewPublishSubject.onNext(Observable.just(query));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-26 18:58:01 +02:00
|
|
|
}
|