From c6dc5d344de33a62d55177039430189542b0e053 Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 17 Dec 2015 15:33:15 +0100 Subject: [PATCH] Fix some crashes --- .../ui/catalogue/CatalogueFragment.java | 8 +++--- .../ui/catalogue/CataloguePresenter.java | 9 +++++++ .../ui/manga/chapter/ChaptersFragment.java | 3 ++- .../mangafeed/ui/reader/ReaderPresenter.java | 25 +++++++++++-------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java index 5e423ac220..6f3ae7c8fb 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java @@ -89,19 +89,19 @@ public class CatalogueFragment extends BaseRxFragment { CatalogueSpinnerAdapter spinnerAdapter = new CatalogueSpinnerAdapter(themedContext, android.R.layout.simple_spinner_item, getPresenter().getEnabledSources()); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + if (savedState == null) selectedIndex = spinnerAdapter.getEmptyIndex(); spinner.setAdapter(spinnerAdapter); - spinner.setSelection(savedState == null ? spinnerAdapter.getEmptyIndex() : selectedIndex); + spinner.setSelection(selectedIndex); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { Source source = spinnerAdapter.getItem(position); // We add an empty source with id -1 that acts as a placeholder to show a hint // that asks to select a source - if (source.getId() != -1 && selectedIndex != position) { + if (source.getId() != -1 && (selectedIndex != position || adapter.isEmpty())) { // Set previous selection if it's not a valid source and notify the user if (!getPresenter().isValidSource(source)) { - spinner.setSelection(selectedIndex != -1 ? selectedIndex : - spinnerAdapter.getEmptyIndex()); + spinner.setSelection(spinnerAdapter.getEmptyIndex()); ToastUtil.showShort(getActivity(), R.string.source_requires_login); } else { selectedIndex = position; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java index 03a88bdde3..00c70d7368 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java @@ -50,6 +50,10 @@ public class CataloguePresenter extends BasePresenter { protected void onCreate(Bundle savedState) { super.onCreate(savedState); + if (savedState != null) { + onProcessRestart(); + } + mangaDetailSubject = PublishSubject.create(); restartableReplay(GET_MANGA_LIST, @@ -80,6 +84,11 @@ public class CataloguePresenter extends BasePresenter { (view, error) -> Timber.e(error.getMessage())); } + private void onProcessRestart() { + stop(GET_MANGA_LIST); + stop(GET_MANGA_DETAIL); + } + @Override protected void onDestroy() { // Catalogue covers are probably not going to be needed for a long time diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java index 748ade0f87..ff5dcb3e7b 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java @@ -181,7 +181,8 @@ public class ChaptersFragment extends BaseRxFragment implemen private void observeChapterDownloadProgress() { downloadProgressSubscription = getPresenter().getDownloadProgressObs() - .subscribe(this::onDownloadProgressChange); + .subscribe(this::onDownloadProgressChange, + error -> { /* TODO getting a NPE sometimes on 'manga' from presenter */ }); } private void unsubscribeChapterDownloadProgress() { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java index d646158fb2..9a6514ebc0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java @@ -78,9 +78,7 @@ public class ReaderPresenter extends BasePresenter { if (currentPage != 0) view.setSelectedPage(currentPage); }, - (view, error) -> { - view.onChapterError(); - }); + (view, error) -> view.onChapterError()); restartableReplay(GET_PAGE_IMAGES, () -> getPageImagesObservable() @@ -104,10 +102,8 @@ public class ReaderPresenter extends BasePresenter { @Override protected void onDestroy() { unregisterForEvents(); - if (pageList != null && isChapterFinished()) { - updateMangaSyncLastChapterRead(); - } onChapterLeft(); + updateMangaSyncLastChapterRead(); super.onDestroy(); } @@ -241,6 +237,9 @@ public class ReaderPresenter extends BasePresenter { } private void updateMangaSyncLastChapterRead() { + if (pageList == null) + return; + db.getMangaSync(manga).createObservable() .take(1) .flatMap(Observable::from) @@ -249,15 +248,21 @@ public class ReaderPresenter extends BasePresenter { if (!service.isLogged()) return; - int lastChapterReadLocal = (int) Math.floor(chapter.chapter_number); - int lastChapterReadRemote = mangaSync.last_chapter_read; + int lastChapterReadLocal = 0; + // If the current chapter has been read, we check with this one + if (chapter.read) + lastChapterReadLocal = (int) Math.floor(chapter.chapter_number); + // If not, we check if the previous chapter has been read + else if (previousChapter != null && previousChapter.read) + lastChapterReadLocal = (int) Math.floor(previousChapter.chapter_number); - if (lastChapterReadLocal > lastChapterReadRemote) { + if (lastChapterReadLocal > mangaSync.last_chapter_read) { mangaSync.last_chapter_read = lastChapterReadLocal; UpdateMangaSyncService.start(getContext(), mangaSync); } }) - .subscribe(); + .subscribe(next -> {}, + error -> Timber.e(error.getCause(), error.getMessage())); } public void setCurrentPage(int currentPage) {