Handle null directories as empty arrays

This commit is contained in:
len 2016-12-10 12:22:44 +01:00
parent f98e0858a7
commit 8c1b07c4ba

View file

@ -43,43 +43,17 @@ class RecentChaptersPresenter : BasePresenter<RecentChaptersFragment>() {
*/ */
private var chapters: List<RecentChapter>? = null private var chapters: List<RecentChapter>? = 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?) { override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState) super.onCreate(savedState)
// Used to get recent chapters getRecentChaptersObservable()
restartableLatestCache(GET_RECENT_CHAPTERS, .observeOn(AndroidSchedulers.mainThread())
{ getRecentChaptersObservable() }, .subscribeLatestCache(RecentChaptersFragment::onNextRecentChapters)
{ view, chapters ->
// Update adapter to show recent manga's
view.onNextRecentChapters(chapters)
}
)
// Used to update download status getChapterStatusObservable()
restartableLatestCache(CHAPTER_STATUS_CHANGES, .subscribeLatestCache(RecentChaptersFragment::onChapterStatusChange,
{ getChapterStatusObservable() }, { view, error -> Timber.e(error) })
{ view, download ->
// Set chapter status
view.onChapterStatusChange(download)
},
{ 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<RecentChaptersFragment>() {
} }
} }
} }
.observeOn(AndroidSchedulers.mainThread())
} }
/** /**
@ -157,25 +130,25 @@ class RecentChaptersPresenter : BasePresenter<RecentChaptersFragment>() {
*/ */
private fun setDownloadedChapters(chapters: List<RecentChapter>) { private fun setDownloadedChapters(chapters: List<RecentChapter>) {
// Cached list of downloaded manga directories. // Cached list of downloaded manga directories.
val mangaDirectories = mutableMapOf<Int, Array<UniFile>?>() val mangaDirectories = mutableMapOf<Int, Array<UniFile>>()
// Cached list of downloaded chapter directories for a manga. // Cached list of downloaded chapter directories for a manga.
val chapterDirectories = mutableMapOf<Long, Array<UniFile>?>() val chapterDirectories = mutableMapOf<Long, Array<UniFile>>()
for (chapter in chapters) { for (chapter in chapters) {
val manga = chapter.manga val manga = chapter.manga
val source = sourceManager.get(manga.source) ?: continue val source = sourceManager.get(manga.source) ?: continue
val mangaDirs = mangaDirectories.getOrPut(source.id) { val mangaDirs = mangaDirectories.getOrPut(source.id) {
downloadManager.findSourceDir(source)?.listFiles() downloadManager.findSourceDir(source)?.listFiles() ?: emptyArray()
} ?: continue }
val mangaDirName = downloadManager.getMangaDirName(manga) val mangaDirName = downloadManager.getMangaDirName(manga)
val mangaDir = mangaDirs.find { it.name == mangaDirName } ?: continue val mangaDir = mangaDirs.find { it.name == mangaDirName } ?: continue
val chapterDirs = chapterDirectories.getOrPut(manga.id!!) { val chapterDirs = chapterDirectories.getOrPut(manga.id!!) {
mangaDir.listFiles() mangaDir.listFiles() ?: emptyArray()
} ?: continue }
val chapterDirName = downloadManager.getChapterDirName(chapter) val chapterDirName = downloadManager.getChapterDirName(chapter)
if (chapterDirs.any { it.name == chapterDirName }) { if (chapterDirs.any { it.name == chapterDirName }) {