mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-30 22:07:57 +01:00 
			
		
		
		
	Add composite subscriptions
This commit is contained in:
		| @@ -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(); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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) | ||||
|         ); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -5,4 +5,5 @@ import uk.co.ribot.easyadapter.EasyAdapter; | ||||
| public interface LibraryView extends BaseView { | ||||
|  | ||||
|     void setAdapter(EasyAdapter mangas); | ||||
|     void setMangaClickListener(); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user