From 90b09489686badc942f66ea852beb3ca5381a000 Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 1 Oct 2015 16:52:08 +0200 Subject: [PATCH] Add composite subscriptions --- app/build.gradle | 4 +--- .../mangafeed/presenter/BasePresenter.java | 8 +++++++ .../mangafeed/presenter/LibraryPresenter.java | 22 +++++++---------- .../mangafeed/ui/adapter/LibraryAdapter.java | 11 ++++++--- .../ui/fragment/LibraryFragment.java | 24 +++++++------------ .../eu/kanade/mangafeed/util/NetworkUtil.java | 11 --------- .../eu/kanade/mangafeed/view/LibraryView.java | 1 + 7 files changed, 35 insertions(+), 46 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index af12dc324..fda16632a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,15 +57,13 @@ dependencies { compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION" compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION" compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION" - compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1' compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0' compile 'com.squareup.okhttp:okhttp:2.4.0' + compile 'io.reactivex:rxandroid:1.0.1' compile "com.pushtorefresh.storio:sqlite:$STORIO_VERSION" compile "com.pushtorefresh.storio:sqlite-annotations:$STORIO_VERSION" compile 'de.greenrobot:eventbus:2.4.0' compile 'com.github.bumptech.glide:glide:3.6.1' - compile 'de.hdodenhof:circleimageview:1.3.0' - compile 'io.reactivex:rxandroid:1.0.1' compile 'com.jakewharton:butterknife:7.0.1' compile 'com.jakewharton.timber:timber:3.1.0' compile 'uk.co.ribot:easyadapter:1.5.0@aar' diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter.java index 083a774c4..615543d8a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/BasePresenter.java @@ -1,6 +1,7 @@ package eu.kanade.mangafeed.presenter; import de.greenrobot.event.EventBus; +import rx.subscriptions.CompositeSubscription; public class BasePresenter { @@ -15,4 +16,11 @@ public class BasePresenter { public void unregisterForEvents() { EventBus.getDefault().unregister(this); } + + protected CompositeSubscription subscriptions = new CompositeSubscription(); + + public void destroySubscriptions() { + subscriptions.unsubscribe(); + } + } 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 0297420b3..35a8a82d9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -11,7 +11,6 @@ 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.Subscription; import static rx.android.schedulers.AndroidSchedulers.mainThread; @@ -23,7 +22,6 @@ public class LibraryPresenter extends BasePresenter { @Inject PreferencesHelper prefs; LibraryAdapter adapter; - private Subscription mangaListSubscription; public LibraryPresenter(LibraryView view) { this.view = view; @@ -47,22 +45,18 @@ public class LibraryPresenter extends BasePresenter { } public void initializeMangas() { - mangaListSubscription = db.manga.getWithUnread() - .observeOn(mainThread()) - .subscribe(mangas -> { - adapter = new LibraryAdapter<>(view.getActivity(), mangas); - view.setAdapter(adapter); - }); + adapter = new LibraryAdapter<>(view.getActivity()); + view.setAdapter(adapter); + view.setMangaClickListener(); + + subscriptions.add(db.manga.getWithUnread() + .observeOn(mainThread()) + .subscribe(adapter::setNewItems) + ); } public void onQueryTextChange(String query) { adapter.getFilter().filter(query); } - public void destroySubscriptions() { - if (mangaListSubscription != null) { - mangaListSubscription.unsubscribe(); - } - } - } 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 d4895bc35..84ca3ea4d 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 @@ -4,6 +4,7 @@ import android.content.Context; import android.widget.Filter; import android.widget.Filterable; +import java.util.ArrayList; import java.util.List; import eu.kanade.mangafeed.data.models.Manga; @@ -15,12 +16,16 @@ public class LibraryAdapter extends EasyAdapter implements Filterable { List mangas; Filter filter; - public LibraryAdapter(Context context, List listItems) { - super(context, MangaLibraryHolder.class, listItems); - mangas = (List)getItems(); + public LibraryAdapter(Context context) { + super(context, MangaLibraryHolder.class); filter = new CatalogueFilter(); } + public void setNewItems(List list) { + super.setItems(list); + mangas = (List)list; + } + @Override public Filter getFilter() { return filter; 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 ce2f776d0..38a60a193 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 @@ -48,21 +48,14 @@ public class LibraryFragment extends BaseFragment implements LibraryView { activity.setToolbarTitle(getString(R.string.library_title)); ButterKnife.bind(this, view); - return view; - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - - setMangaClickListener(); presenter.initializeMangas(); + + return view; } @Override public void onDestroy() { super.onDestroy(); - presenter.destroySubscriptions(); } @@ -88,12 +81,6 @@ public class LibraryFragment extends BaseFragment implements LibraryView { }); } - private void setMangaClickListener() { - grid.setOnItemClickListener( - (parent, view, position, id) -> - presenter.onMangaClick(position) - ); - } // LibraryView @@ -101,4 +88,11 @@ public class LibraryFragment extends BaseFragment implements LibraryView { grid.setAdapter(adapter); } + public void setMangaClickListener() { + grid.setOnItemClickListener( + (parent, view, position, id) -> + presenter.onMangaClick(position) + ); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/util/NetworkUtil.java b/app/src/main/java/eu/kanade/mangafeed/util/NetworkUtil.java index c5fdcf15e..b421b412f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/util/NetworkUtil.java +++ b/app/src/main/java/eu/kanade/mangafeed/util/NetworkUtil.java @@ -4,19 +4,8 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import retrofit.HttpException; - public class NetworkUtil { - /** - * Returns true if the Throwable is an instance of RetrofitError with an - * http status code equals to the given one. - */ - public static boolean isHttpStatusCode(Throwable throwable, int statusCode) { - return throwable instanceof HttpException - && ((HttpException) throwable).code() == statusCode; - } - public static boolean isNetworkConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 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 0141d44ce..4863c5a13 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java @@ -5,4 +5,5 @@ import uk.co.ribot.easyadapter.EasyAdapter; public interface LibraryView extends BaseView { void setAdapter(EasyAdapter mangas); + void setMangaClickListener(); }