From db49c3ee88a661abc1721877d5dbec9996365979 Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 1 Oct 2015 00:11:19 +0200 Subject: [PATCH] Create adapter in presenter --- .../mangafeed/presenter/LibraryPresenter.java | 25 ++++++-------- .../mangafeed/ui/adapter/LibraryAdapter.java | 2 +- .../ui/fragment/LibraryFragment.java | 34 +++---------------- .../eu/kanade/mangafeed/view/LibraryView.java | 9 ++--- 4 files changed, 18 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java index af8d80d34..ff699ccb3 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -9,14 +9,8 @@ import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; +import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; import eu.kanade.mangafeed.view.LibraryView; -import rx.Observable; -import rx.Subscription; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; -import rx.subjects.PublishSubject; -import timber.log.Timber; -import uk.co.ribot.easyadapter.EasyAdapter; import static rx.android.schedulers.AndroidSchedulers.mainThread; @@ -24,11 +18,10 @@ public class LibraryPresenter extends BasePresenter { private LibraryView view; - @Inject - DatabaseHelper db; + @Inject DatabaseHelper db; + @Inject PreferencesHelper prefs; - @Inject - PreferencesHelper prefs; + LibraryAdapter adapter; public LibraryPresenter(LibraryView view) { this.view = view; @@ -43,7 +36,7 @@ public class LibraryPresenter extends BasePresenter { } - public void onMangaClick(EasyAdapter adapter, int position) { + public void onMangaClick(int position) { Intent intent = MangaDetailActivity.newIntent( view.getActivity(), adapter.getItem(position) @@ -52,13 +45,17 @@ public class LibraryPresenter extends BasePresenter { } public void initializeMangas() { + db.manga.get() .observeOn(mainThread()) - .subscribe(view::setMangas); + .subscribe(mangas -> { + adapter = new LibraryAdapter<>(view.getActivity(), mangas); + view.setAdapter(adapter); + }); } public void onQueryTextChange(String query) { - view.getAdapter().getFilter().filter(query); + adapter.getFilter().filter(query); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java index 6cfad0b4c..d4895bc35 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java @@ -32,7 +32,7 @@ public class LibraryAdapter extends EasyAdapter implements Filterable { FilterResults results = new FilterResults(); String query = charSequence.toString().toLowerCase(); - if (query == null || query.length() == 0) { + if (query.length() == 0) { results.values = mangas; results.count = mangas.size(); } else { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java index 59b21aecb..aa211ee61 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java @@ -9,18 +9,12 @@ import android.view.View; import android.view.ViewGroup; import android.widget.GridView; -import java.util.List; - import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; -import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.LibraryPresenter; import eu.kanade.mangafeed.ui.activity.MainActivity; -import eu.kanade.mangafeed.ui.adapter.CatalogueArrayAdapter; -import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder; import eu.kanade.mangafeed.view.LibraryView; -import timber.log.Timber; import uk.co.ribot.easyadapter.EasyAdapter; @@ -28,7 +22,6 @@ public class LibraryFragment extends BaseFragment implements LibraryView { @Bind(R.id.gridView) GridView grid; LibraryPresenter presenter; - CatalogueArrayAdapter adapter; MainActivity activity; public static LibraryFragment newInstance() { @@ -62,7 +55,6 @@ public class LibraryFragment extends BaseFragment implements LibraryView { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - setupToolbar(); setMangaClickListener(); presenter.initializeMangas(); } @@ -89,35 +81,17 @@ public class LibraryFragment extends BaseFragment implements LibraryView { }); } - // LibraryView - - public void setMangas(List mangas) { - if (adapter == null) { - adapter = new CatalogueArrayAdapter<>( - getActivity(), - MangaLibraryHolder.class, - mangas - ); - grid.setAdapter(adapter); - } else { - adapter.setItems(mangas); - } - - } - private void setMangaClickListener() { grid.setOnItemClickListener( (parent, view, position, id) -> - presenter.onMangaClick(adapter, position) + presenter.onMangaClick(position) ); } - private void setupToolbar() { - //activity.getSupportActionBar(). - } + // LibraryView - public CatalogueArrayAdapter getAdapter() { - return adapter; + public void setAdapter(EasyAdapter adapter) { + grid.setAdapter(adapter); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java index 64e3cedea..0141d44ce 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java @@ -1,13 +1,8 @@ package eu.kanade.mangafeed.view; -import java.util.ArrayList; -import java.util.List; - -import eu.kanade.mangafeed.data.models.Manga; -import eu.kanade.mangafeed.ui.adapter.CatalogueArrayAdapter; +import uk.co.ribot.easyadapter.EasyAdapter; public interface LibraryView extends BaseView { - void setMangas(List mangas); - CatalogueArrayAdapter getAdapter(); + void setAdapter(EasyAdapter mangas); }