From 8c1b07c4ba76aa3569164f70e39053928755df2b Mon Sep 17 00:00:00 2001 From: len Date: Sat, 10 Dec 2016 12:22:44 +0100 Subject: [PATCH] Handle null directories as empty arrays --- .../recent_updates/RecentChaptersPresenter.kt | 51 +++++-------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt index f666e6f2c0..4c9c990575 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersPresenter.kt @@ -43,43 +43,17 @@ class RecentChaptersPresenter : BasePresenter() { */ private var chapters: List? = null - /** - * The id of the restartable. - */ - val GET_RECENT_CHAPTERS = 1 - - /** - * The id of the restartable. - */ - val CHAPTER_STATUS_CHANGES = 2 - override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) - // Used to get recent chapters - restartableLatestCache(GET_RECENT_CHAPTERS, - { getRecentChaptersObservable() }, - { view, chapters -> - // Update adapter to show recent manga's - view.onNextRecentChapters(chapters) - } - ) + getRecentChaptersObservable() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeLatestCache(RecentChaptersFragment::onNextRecentChapters) - // Used to update download status - restartableLatestCache(CHAPTER_STATUS_CHANGES, - { getChapterStatusObservable() }, - { view, download -> - // Set chapter status - view.onChapterStatusChange(download) - }, - { view, error -> Timber.e(error) } - ) + getChapterStatusObservable() + .subscribeLatestCache(RecentChaptersFragment::onChapterStatusChange, + { view, error -> Timber.e(error) }) - if (savedState == null) { - // Start fetching recent chapters - start(GET_RECENT_CHAPTERS) - start(CHAPTER_STATUS_CHANGES) - } } /** @@ -119,7 +93,6 @@ class RecentChaptersPresenter : BasePresenter() { } } } - .observeOn(AndroidSchedulers.mainThread()) } /** @@ -157,25 +130,25 @@ class RecentChaptersPresenter : BasePresenter() { */ private fun setDownloadedChapters(chapters: List) { // Cached list of downloaded manga directories. - val mangaDirectories = mutableMapOf?>() + val mangaDirectories = mutableMapOf>() // Cached list of downloaded chapter directories for a manga. - val chapterDirectories = mutableMapOf?>() + val chapterDirectories = mutableMapOf>() for (chapter in chapters) { val manga = chapter.manga val source = sourceManager.get(manga.source) ?: continue val mangaDirs = mangaDirectories.getOrPut(source.id) { - downloadManager.findSourceDir(source)?.listFiles() - } ?: continue + downloadManager.findSourceDir(source)?.listFiles() ?: emptyArray() + } val mangaDirName = downloadManager.getMangaDirName(manga) val mangaDir = mangaDirs.find { it.name == mangaDirName } ?: continue val chapterDirs = chapterDirectories.getOrPut(manga.id!!) { - mangaDir.listFiles() - } ?: continue + mangaDir.listFiles() ?: emptyArray() + } val chapterDirName = downloadManager.getChapterDirName(chapter) if (chapterDirs.any { it.name == chapterDirName }) {