From 049e5a1ee5021db0a5643d43b2873f8e8e96f393 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 17 Oct 2015 04:03:39 +0200 Subject: [PATCH] Use RxPresenter in Library --- .../mangafeed/presenter/BasePresenter2.java | 20 +++++++ .../mangafeed/presenter/LibraryPresenter.java | 54 +++++++++---------- .../mangafeed/ui/activity/BaseActivity2.java | 4 -- .../mangafeed/ui/fragment/BaseFragment2.java | 23 ++++++++ .../ui/fragment/LibraryFragment.java | 34 ++++++------ 5 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter2.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment2.java diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter2.java b/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter2.java new file mode 100644 index 0000000000..c862dbf627 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter2.java @@ -0,0 +1,20 @@ +package eu.kanade.mangafeed.presenter; + +import de.greenrobot.event.EventBus; +import nucleus.presenter.RxPresenter; +import nucleus.view.ViewWithPresenter; + +public class BasePresenter2 extends RxPresenter { + + public void registerForStickyEvents() { + EventBus.getDefault().registerSticky(this); + } + + public void registerForEvents() { + EventBus.getDefault().register(this); + } + + public void unregisterForEvents() { + EventBus.getDefault().unregister(this); + } +} 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 f8158ee82e..7534e263b4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -1,6 +1,7 @@ package eu.kanade.mangafeed.presenter; import android.content.Intent; +import android.os.Bundle; import android.util.SparseBooleanArray; import javax.inject.Inject; @@ -11,63 +12,60 @@ 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.ui.fragment.LibraryFragment; import eu.kanade.mangafeed.util.DummyDataUtil; import eu.kanade.mangafeed.view.LibraryView; import rx.Observable; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; +import rx.internal.util.SubscriptionList; import rx.schedulers.Schedulers; +import uk.co.ribot.easyadapter.EasyAdapter; -public class LibraryPresenter extends BasePresenter { - - private LibraryView view; +public class LibraryPresenter extends BasePresenter2 { @Inject DatabaseHelper db; @Inject PreferencesHelper prefs; - LibraryAdapter adapter; - private Subscription mFavoriteMangasSubscription; private Subscription mDeleteMangaSubscription; - public LibraryPresenter(LibraryView view) { - this.view = view; - App.getComponent(view.getActivity()).inject(this); + @Override + protected void onCreate(Bundle savedState) { + super.onCreate(savedState); } - public void onMangaClick(int position) { + @Override + protected void onTakeView(LibraryFragment view) { + super.onTakeView(view); + getFavoriteMangas(); + } + + public void onMangaClick(LibraryFragment view, int position) { Intent intent = MangaDetailActivity.newIntent( view.getActivity(), - adapter.getItem(position) + view.getAdapter().getItem(position) ); view.getActivity().startActivity(intent); } - public void initialize() { - adapter = new LibraryAdapter<>(view.getActivity()); - view.setAdapter(adapter); - view.setMangaClickListener(); - - getFavoriteMangas(); - } - public void getFavoriteMangas() { - subscriptions.remove(mFavoriteMangasSubscription); + if (mFavoriteMangasSubscription != null) + remove(mFavoriteMangasSubscription); mFavoriteMangasSubscription = db.getMangasWithUnread() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(adapter::setNewItems); + .compose(deliverLatestCache()) + .subscribe(this.split((view, mangas) -> { + view.getAdapter().setNewItems(mangas); + })); - subscriptions.add(mFavoriteMangasSubscription); + add(mFavoriteMangasSubscription); } - public void onQueryTextChange(String query) { - adapter.getFilter().filter(query); - } - - public void onDelete(SparseBooleanArray checkedItems) { - subscriptions.remove(mDeleteMangaSubscription); + public void onDelete(SparseBooleanArray checkedItems, EasyAdapter adapter) { + remove(mDeleteMangaSubscription); mDeleteMangaSubscription = Observable.range(0, checkedItems.size()) .observeOn(Schedulers.io()) @@ -77,7 +75,7 @@ public class LibraryPresenter extends BasePresenter { .flatMap(db::deleteMangas) .subscribe(); - subscriptions.add(mDeleteMangaSubscription); + add(mDeleteMangaSubscription); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity2.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity2.java index d93a89847a..04ff16146c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity2.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity2.java @@ -32,10 +32,6 @@ public class BaseActivity2

extends NucleusAppCompatActivity getSupportActionBar().setTitle(title); } - protected AppComponent applicationComponent() { - return App.get(this).getComponent(); - } - public Context getActivity() { return this; } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment2.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment2.java new file mode 100644 index 0000000000..1a4832917c --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/BaseFragment2.java @@ -0,0 +1,23 @@ +package eu.kanade.mangafeed.ui.fragment; + +import android.os.Bundle; + +import eu.kanade.mangafeed.App; +import nucleus.factory.PresenterFactory; +import nucleus.presenter.Presenter; +import nucleus.view.NucleusSupportFragment; + +public class BaseFragment2

extends NucleusSupportFragment

{ + + @Override + public void onCreate(Bundle savedInstanceState) { + final PresenterFactory

superFactory = super.getPresenterFactory(); + setPresenterFactory(() -> { + P presenter = superFactory.createPresenter(); + App.getComponentReflection(getActivity()).inject(presenter); + return presenter; + }); + super.onCreate(savedInstanceState); + } + +} \ No newline at end of file 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 7944df6041..29e48cbfed 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 @@ -14,17 +14,20 @@ import android.widget.GridView; 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.LibraryAdapter; import eu.kanade.mangafeed.view.LibraryView; +import nucleus.factory.RequiresPresenter; import uk.co.ribot.easyadapter.EasyAdapter; - -public class LibraryFragment extends BaseFragment implements LibraryView { +@RequiresPresenter(LibraryPresenter.class) +public class LibraryFragment extends BaseFragment2 { @Bind(R.id.gridView) GridView grid; - private LibraryPresenter presenter; private MainActivity activity; + private LibraryAdapter adapter; public static LibraryFragment newInstance() { LibraryFragment fragment = new LibraryFragment(); @@ -38,7 +41,6 @@ public class LibraryFragment extends BaseFragment implements LibraryView { super.onCreate(savedInstanceState); setHasOptionsMenu(true); - presenter = new LibraryPresenter(this); activity = (MainActivity)getActivity(); } @@ -50,17 +52,12 @@ public class LibraryFragment extends BaseFragment implements LibraryView { activity.setToolbarTitle(getString(R.string.library_title)); ButterKnife.bind(this, view); - presenter.initialize(); + createAdapter(); + setMangaClickListener(); return view; } - @Override - public void onDestroy() { - super.onDestroy(); - presenter.destroySubscriptions(); - } - @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.library, menu); @@ -77,22 +74,25 @@ public class LibraryFragment extends BaseFragment implements LibraryView { @Override public boolean onQueryTextChange(String newText) { - presenter.onQueryTextChange(newText); + adapter.getFilter().filter(newText); return true; } }); } - // LibraryView - - public void setAdapter(EasyAdapter adapter) { + public void createAdapter() { + adapter = new LibraryAdapter<>(getActivity()); grid.setAdapter(adapter); } + public LibraryAdapter getAdapter() { + return adapter; + } + public void setMangaClickListener() { grid.setOnItemClickListener( (parent, view, position, id) -> - presenter.onMangaClick(position) + getPresenter().onMangaClick(this, position) ); grid.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() { @Override @@ -117,7 +117,7 @@ public class LibraryFragment extends BaseFragment implements LibraryView { public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.action_delete: - presenter.onDelete(grid.getCheckedItemPositions()); + getPresenter().onDelete(grid.getCheckedItemPositions(), adapter); mode.finish(); return true; }