mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-18 22:09:14 +01:00
Use RxPresenter in Library
This commit is contained in:
parent
8da5c83cb3
commit
049e5a1ee5
@ -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);
|
||||
}
|
||||
}
|
@ -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<LibraryFragment> {
|
||||
|
||||
@Inject DatabaseHelper db;
|
||||
@Inject PreferencesHelper prefs;
|
||||
|
||||
LibraryAdapter<Manga> 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<Manga> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -32,10 +32,6 @@ public class BaseActivity2<P extends Presenter> extends NucleusAppCompatActivity
|
||||
getSupportActionBar().setTitle(title);
|
||||
}
|
||||
|
||||
protected AppComponent applicationComponent() {
|
||||
return App.get(this).getComponent();
|
||||
}
|
||||
|
||||
public Context getActivity() {
|
||||
return this;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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<LibraryPresenter> {
|
||||
|
||||
@Bind(R.id.gridView) GridView grid;
|
||||
private LibraryPresenter presenter;
|
||||
private MainActivity activity;
|
||||
private LibraryAdapter<Manga> 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<Manga> 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user