Use RxPresenter in Library

This commit is contained in:
inorichi 2015-10-17 04:03:39 +02:00
parent 8da5c83cb3
commit 049e5a1ee5
5 changed files with 86 additions and 49 deletions

View File

@ -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<V extends ViewWithPresenter> extends RxPresenter<V> {
public void registerForStickyEvents() {
EventBus.getDefault().registerSticky(this);
}
public void registerForEvents() {
EventBus.getDefault().register(this);
}
public void unregisterForEvents() {
EventBus.getDefault().unregister(this);
}
}

View File

@ -1,6 +1,7 @@
package eu.kanade.mangafeed.presenter; package eu.kanade.mangafeed.presenter;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import javax.inject.Inject; 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.data.models.Manga;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
import eu.kanade.mangafeed.util.DummyDataUtil; import eu.kanade.mangafeed.util.DummyDataUtil;
import eu.kanade.mangafeed.view.LibraryView; import eu.kanade.mangafeed.view.LibraryView;
import rx.Observable; import rx.Observable;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.internal.util.SubscriptionList;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import uk.co.ribot.easyadapter.EasyAdapter;
public class LibraryPresenter extends BasePresenter { public class LibraryPresenter extends BasePresenter2<LibraryFragment> {
private LibraryView view;
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
@Inject PreferencesHelper prefs; @Inject PreferencesHelper prefs;
LibraryAdapter<Manga> adapter;
private Subscription mFavoriteMangasSubscription; private Subscription mFavoriteMangasSubscription;
private Subscription mDeleteMangaSubscription; private Subscription mDeleteMangaSubscription;
public LibraryPresenter(LibraryView view) { @Override
this.view = view; protected void onCreate(Bundle savedState) {
App.getComponent(view.getActivity()).inject(this); 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( Intent intent = MangaDetailActivity.newIntent(
view.getActivity(), view.getActivity(),
adapter.getItem(position) view.getAdapter().getItem(position)
); );
view.getActivity().startActivity(intent); view.getActivity().startActivity(intent);
} }
public void initialize() {
adapter = new LibraryAdapter<>(view.getActivity());
view.setAdapter(adapter);
view.setMangaClickListener();
getFavoriteMangas();
}
public void getFavoriteMangas() { public void getFavoriteMangas() {
subscriptions.remove(mFavoriteMangasSubscription); if (mFavoriteMangasSubscription != null)
remove(mFavoriteMangasSubscription);
mFavoriteMangasSubscription = db.getMangasWithUnread() mFavoriteMangasSubscription = db.getMangasWithUnread()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .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) { public void onDelete(SparseBooleanArray checkedItems, EasyAdapter<Manga> adapter) {
adapter.getFilter().filter(query); remove(mDeleteMangaSubscription);
}
public void onDelete(SparseBooleanArray checkedItems) {
subscriptions.remove(mDeleteMangaSubscription);
mDeleteMangaSubscription = Observable.range(0, checkedItems.size()) mDeleteMangaSubscription = Observable.range(0, checkedItems.size())
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
@ -77,7 +75,7 @@ public class LibraryPresenter extends BasePresenter {
.flatMap(db::deleteMangas) .flatMap(db::deleteMangas)
.subscribe(); .subscribe();
subscriptions.add(mDeleteMangaSubscription); add(mDeleteMangaSubscription);
} }
} }

View File

@ -32,10 +32,6 @@ public class BaseActivity2<P extends Presenter> extends NucleusAppCompatActivity
getSupportActionBar().setTitle(title); getSupportActionBar().setTitle(title);
} }
protected AppComponent applicationComponent() {
return App.get(this).getComponent();
}
public Context getActivity() { public Context getActivity() {
return this; return this;
} }

View File

@ -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<P extends Presenter> extends NucleusSupportFragment<P> {
@Override
public void onCreate(Bundle savedInstanceState) {
final PresenterFactory<P> superFactory = super.getPresenterFactory();
setPresenterFactory(() -> {
P presenter = superFactory.createPresenter();
App.getComponentReflection(getActivity()).inject(presenter);
return presenter;
});
super.onCreate(savedInstanceState);
}
}

View File

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