Add composite subscriptions

This commit is contained in:
inorichi 2015-10-01 16:52:08 +02:00
parent e386257d34
commit 90b0948968
7 changed files with 35 additions and 46 deletions

View File

@ -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'

View File

@ -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();
}
}

View File

@ -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<Manga> 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();
}
}
}

View File

@ -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<T> extends EasyAdapter<T> implements Filterable {
List<Manga> mangas;
Filter filter;
public LibraryAdapter(Context context, List<T> listItems) {
super(context, MangaLibraryHolder.class, listItems);
mangas = (List<Manga>)getItems();
public LibraryAdapter(Context context) {
super(context, MangaLibraryHolder.class);
filter = new CatalogueFilter();
}
public void setNewItems(List<T> list) {
super.setItems(list);
mangas = (List<Manga>)list;
}
@Override
public Filter getFilter() {
return filter;

View File

@ -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)
);
}
}

View File

@ -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);

View File

@ -5,4 +5,5 @@ import uk.co.ribot.easyadapter.EasyAdapter;
public interface LibraryView extends BaseView {
void setAdapter(EasyAdapter mangas);
void setMangaClickListener();
}