Bläddra i källkod

Fix wrong manga received through EventBus

inorichi 9 år sedan
förälder
incheckning
49c69be38e

+ 18 - 6
app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java

@@ -39,7 +39,7 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
                 this::getDbChaptersObs,
                 (view, chapters) -> {
                     view.onNextChapters(chapters);
-                    EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size()));
+                    EventBus.getDefault().postSticky( new ChapterCountEvent(chapters.size()) );
                 }
         );
 
@@ -61,19 +61,30 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
         super.onDropView();
     }
 
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        EventBus.getDefault().removeStickyEvent(ChapterCountEvent.class);
+    }
+
     @EventBusHook
     public void onEventMainThread(Manga manga) {
         if (this.manga == null) {
             this.manga = manga;
             start(DB_CHAPTERS);
+
+            // Get chapters if it's an online source
+            if (getView() != null && getView().isOnlineManga()) {
+                refreshChapters();
+            }
         }
     }
 
-    public void refreshChapters(MangaChaptersFragment view) {
-        if (manga != null) {
-            view.setSwipeRefreshing();
-            start(ONLINE_CHAPTERS);
-        }
+    public void refreshChapters() {
+        if (getView() != null)
+            getView().setSwipeRefreshing();
+
+        start(ONLINE_CHAPTERS);
     }
 
     private Observable<List<Chapter>> getDbChaptersObs() {
@@ -89,4 +100,5 @@ public class MangaChaptersPresenter extends BasePresenter<MangaChaptersFragment>
                 .flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
                 .observeOn(AndroidSchedulers.mainThread());
     }
+
 }

+ 26 - 18
app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java

@@ -1,5 +1,7 @@
 package eu.kanade.mangafeed.presenter;
 
+import android.os.Bundle;
+
 import javax.inject.Inject;
 
 import de.greenrobot.event.EventBus;
@@ -7,7 +9,6 @@ import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
 import eu.kanade.mangafeed.data.models.Manga;
 import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
 import rx.Observable;
-import rx.Subscription;
 import rx.android.schedulers.AndroidSchedulers;
 import rx.schedulers.Schedulers;
 
@@ -15,32 +16,39 @@ public class MangaDetailPresenter extends BasePresenter<MangaDetailActivity> {
 
     @Inject DatabaseHelper db;
 
-    private Manga manga;
-    private Subscription mangaSubscription;
+    private long mangaId;
+    private static final int DB_MANGA = 1;
 
     @Override
-    protected void onTakeView(MangaDetailActivity view) {
-        super.onTakeView(view);
-        if (manga != null)
-            view.setManga(manga);
+    protected void onCreate(Bundle savedState) {
+        super.onCreate(savedState);
 
-        getManga(view);
+        restartableLatestCache(DB_MANGA,
+                this::getDbMangaObservable,
+                (view, manga) -> {
+                    view.setManga(manga);
+                    EventBus.getDefault().postSticky(manga);
+                });
     }
 
-    private void getManga(MangaDetailActivity view) {
-        if (mangaSubscription != null)
-            return;
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        // Avoid fragments receiving wrong manga
+        EventBus.getDefault().removeStickyEvent(Manga.class);
+    }
 
-        add(mangaSubscription = db.getManga(view.getMangaId())
+    private Observable<Manga> getDbMangaObservable() {
+        return db.getManga(mangaId)
                 .subscribeOn(Schedulers.io())
                 .take(1)
                 .flatMap(Observable::from)
-                .observeOn(AndroidSchedulers.mainThread())
-                .subscribe(manga -> {
-                    this.manga = manga;
-                    view.setManga(manga);
-                    EventBus.getDefault().postSticky(manga);
-                }));
+                .observeOn(AndroidSchedulers.mainThread());
+    }
+
+    public void queryManga(long mangaId) {
+        this.mangaId = mangaId;
+        start(DB_MANGA);
     }
 
 }

+ 2 - 7
app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java

@@ -10,9 +10,6 @@ import eu.kanade.mangafeed.ui.fragment.MangaInfoFragment;
 import eu.kanade.mangafeed.util.EventBusHook;
 import eu.kanade.mangafeed.util.events.ChapterCountEvent;
 import rx.Observable;
-import rx.Subscription;
-import rx.android.schedulers.AndroidSchedulers;
-import rx.schedulers.Schedulers;
 import timber.log.Timber;
 
 public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@@ -52,10 +49,8 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
 
     @EventBusHook
     public void onEventMainThread(Manga manga) {
-        if (!manga.equals(this.manga)) {
-            this.manga = manga;
-            start(GET_MANGA);
-        }
+        this.manga = manga;
+        start(GET_MANGA);
     }
 
     @EventBusHook

+ 7 - 0
app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java

@@ -56,6 +56,9 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
         is_online = intent.getBooleanExtra(MANGA_ONLINE, false);
 
         setupViewPager();
+
+        if (savedInstanceState == null)
+            getPresenter().queryManga(manga_id);
     }
 
     @Override
@@ -95,6 +98,10 @@ public class MangaDetailActivity extends BaseActivity<MangaDetailPresenter> {
         setToolbarTitle(manga.title);
     }
 
+    public boolean isOnlineManga() {
+        return is_online;
+    }
+
     class MangaDetailAdapter extends FragmentPagerAdapter {
 
         final int PAGE_COUNT = 2;

+ 7 - 2
app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java

@@ -19,6 +19,7 @@ import butterknife.ButterKnife;
 import eu.kanade.mangafeed.R;
 import eu.kanade.mangafeed.data.models.Chapter;
 import eu.kanade.mangafeed.presenter.MangaChaptersPresenter;
+import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
 import eu.kanade.mangafeed.ui.adapter.ChapterListHolder;
 import nucleus.factory.RequiresPresenter;
 import uk.co.ribot.easyadapter.EasyRecyclerAdapter;
@@ -65,7 +66,7 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case R.id.action_refresh:
-                getPresenter().refreshChapters(this);
+                getPresenter().refreshChapters();
                 break;
         }
         return super.onOptionsItemSelected(item);
@@ -77,7 +78,7 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
     }
 
     private void setSwipeRefreshListener() {
-        swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters(this));
+        swipeRefresh.setOnRefreshListener(() -> getPresenter().refreshChapters());
     }
 
     public void onNextChapters(List<Chapter> chapters) {
@@ -91,4 +92,8 @@ public class MangaChaptersFragment extends BaseFragment<MangaChaptersPresenter>
     public void setSwipeRefreshing() {
         swipeRefresh.setRefreshing(true);
     }
+
+    public boolean isOnlineManga() {
+        return ((MangaDetailActivity)getActivity()).isOnlineManga();
+    }
 }