Don't restart library subscription

This commit is contained in:
inorichi 2015-10-18 02:33:15 +02:00
parent c6df23b1aa
commit 920a71601b
5 changed files with 19 additions and 26 deletions

View file

@ -1,7 +1,5 @@
package eu.kanade.mangafeed.data.models;
import android.support.annotation.Nullable;
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn;
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType;

View file

@ -7,13 +7,12 @@ import javax.inject.Inject;
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.adapter.LibraryAdapter;
import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import uk.co.ribot.easyadapter.EasyAdapter;
public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@ -36,31 +35,26 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
public void getFavoriteMangas() {
if (mFavoriteMangasSubscription != null)
remove(mFavoriteMangasSubscription);
return;
mFavoriteMangasSubscription = db.getMangasWithUnread()
add(mFavoriteMangasSubscription = db.getMangasWithUnread()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(deliverLatestCache())
.subscribe(this.split((view, mangas) -> {
view.getAdapter().setNewItems(mangas);
}));
add(mFavoriteMangasSubscription);
.subscribe(this.split(LibraryFragment::onNextMangas)));
}
public void onDelete(SparseBooleanArray checkedItems, EasyAdapter<Manga> adapter) {
remove(mDeleteMangaSubscription);
public void onDelete(SparseBooleanArray checkedItems, LibraryAdapter adapter) {
if (mDeleteMangaSubscription != null)
remove(mDeleteMangaSubscription);
mDeleteMangaSubscription = Observable.range(0, checkedItems.size())
add(mDeleteMangaSubscription = Observable.range(0, checkedItems.size())
.observeOn(Schedulers.io())
.map(checkedItems::keyAt)
.map(adapter::getItem)
.toList()
.flatMap(db::deleteMangas)
.subscribe();
add(mDeleteMangaSubscription);
.subscribe());
}
}

View file

@ -8,7 +8,6 @@ import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {

View file

@ -10,7 +10,7 @@ import eu.kanade.mangafeed.data.models.Manga;
import rx.Observable;
import uk.co.ribot.easyadapter.EasyAdapter;
public class LibraryAdapter<T> extends EasyAdapter<T> implements Filterable {
public class LibraryAdapter extends EasyAdapter<Manga> implements Filterable {
List<Manga> mangas;
Filter filter;
@ -20,9 +20,9 @@ public class LibraryAdapter<T> extends EasyAdapter<T> implements Filterable {
filter = new LibraryFilter();
}
public void setNewItems(List<T> list) {
public void setNewItems(List<Manga> list) {
super.setItems(list);
mangas = (List<Manga>)list;
mangas = list;
}
@Override
@ -57,7 +57,7 @@ public class LibraryAdapter<T> extends EasyAdapter<T> implements Filterable {
@Override
public void publishResults(CharSequence constraint, FilterResults results) {
setItems((List<T>) results.values);
setItems((List<Manga >) results.values);
}
}

View file

@ -12,6 +12,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
@ -28,7 +30,7 @@ public class LibraryFragment extends BaseFragment<LibraryPresenter> {
@Bind(R.id.gridView) GridView grid;
private MainActivity activity;
private LibraryAdapter<Manga> adapter;
private LibraryAdapter adapter;
public static LibraryFragment newInstance() {
LibraryFragment fragment = new LibraryFragment();
@ -82,12 +84,12 @@ public class LibraryFragment extends BaseFragment<LibraryPresenter> {
}
private void createAdapter() {
adapter = new LibraryAdapter<>(getActivity());
adapter = new LibraryAdapter(getActivity());
grid.setAdapter(adapter);
}
public LibraryAdapter<Manga> getAdapter() {
return adapter;
public void onNextMangas(List<Manga> mangas) {
adapter.setNewItems(mangas);
}
@OnItemClick(R.id.gridView)