Reader fixes (MAL not updating in certain scenarios)

This commit is contained in:
len 2016-06-24 13:39:34 +02:00
parent 3ee5774870
commit b6df5e6ee6
2 changed files with 31 additions and 29 deletions

View File

@ -145,8 +145,6 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
} }
override fun onBackPressed() { override fun onBackPressed() {
presenter.onChapterLeft()
val chapterToUpdate = presenter.getMangaSyncChapterToUpdate() val chapterToUpdate = presenter.getMangaSyncChapterToUpdate()
if (chapterToUpdate > 0) { if (chapterToUpdate > 0) {

View File

@ -103,6 +103,11 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
dbChapters.sortedWith(Comparator<Chapter> { c1, c2 -> sortFunction(c1, c2) }) dbChapters.sortedWith(Comparator<Chapter> { c1, c2 -> sortFunction(c1, c2) })
} }
/**
* Map of chapters that have been loaded in the reader.
*/
private val loadedChapters = hashMapOf<Long?, ReaderChapter>()
/** /**
* List of manga services linked to the active manga, or null if auto syncing is not enabled. * List of manga services linked to the active manga, or null if auto syncing is not enabled.
*/ */
@ -164,7 +169,6 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
override fun onSave(state: Bundle) { override fun onSave(state: Bundle) {
chapter.requestedPage = chapter.last_page_read chapter.requestedPage = chapter.last_page_read
onChapterLeft()
state.putSerializable(ReaderPresenter::manga.name, manga) state.putSerializable(ReaderPresenter::manga.name, manga)
state.putSerializable(ReaderPresenter::chapter.name, chapter) state.putSerializable(ReaderPresenter::chapter.name, chapter)
super.onSave(state) super.onSave(state)
@ -172,6 +176,7 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
override fun onDestroy() { override fun onDestroy() {
loader.cleanup() loader.cleanup()
onChapterLeft()
super.onDestroy() super.onDestroy()
} }
@ -208,8 +213,8 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
adjacentChaptersSubscription = Observable adjacentChaptersSubscription = Observable
.fromCallable { getAdjacentChaptersStrategy(chapter) } .fromCallable { getAdjacentChaptersStrategy(chapter) }
.doOnNext { pair -> .doOnNext { pair ->
prevChapter = pair.first prevChapter = loadedChapters.getOrElse(pair.first?.id) { pair.first }
nextChapter = pair.second nextChapter = loadedChapters.getOrElse(pair.second?.id) { pair.second }
} }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -268,7 +273,7 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
// Cleanup any append. // Cleanup any append.
appenderSubscription?.let { remove(it) } appenderSubscription?.let { remove(it) }
this.chapter = chapter this.chapter = loadedChapters.getOrPut(chapter.id) { chapter }
// If the chapter is partially read, set the starting page to the last the user read // If the chapter is partially read, set the starting page to the last the user read
// otherwise use the requested page. // otherwise use the requested page.
@ -303,8 +308,9 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
appenderSubscription?.let { remove(it) } appenderSubscription?.let { remove(it) }
val nextChapter = nextChapter ?: return val nextChapter = nextChapter ?: return
val chapterToLoad = loadedChapters.getOrPut(nextChapter.id) { nextChapter }
appenderSubscription = loader.loadChapter(nextChapter) appenderSubscription = loader.loadChapter(chapterToLoad)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.retryWhen(RetryWithDelay(1, { 3000 })) .retryWhen(RetryWithDelay(1, { 3000 }))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -408,40 +414,38 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
* Returns the chapter to be marked as last read in sync services or 0 if no update required. * Returns the chapter to be marked as last read in sync services or 0 if no update required.
*/ */
fun getMangaSyncChapterToUpdate(): Int { fun getMangaSyncChapterToUpdate(): Int {
if (chapter.pages == null || mangaSyncList == null || mangaSyncList!!.isEmpty()) val mangaSyncList = mangaSyncList
if (chapter.pages == null || mangaSyncList == null || mangaSyncList.isEmpty())
return 0 return 0
var lastChapterReadLocal = 0 val prevChapter = prevChapter
// If the current chapter has been read, we check with this one // Get the last chapter read from the reader.
if (chapter.read) val lastChapterRead = if (chapter.read)
lastChapterReadLocal = Math.floor(chapter.chapter_number.toDouble()).toInt() Math.floor(chapter.chapter_number.toDouble()).toInt()
// If not, we check if the previous chapter has been read else if (prevChapter != null && prevChapter.read)
else if (prevChapter != null && prevChapter!!.read) Math.floor(prevChapter.chapter_number.toDouble()).toInt()
lastChapterReadLocal = Math.floor(prevChapter!!.chapter_number.toDouble()).toInt() else
0
// We know the chapter we have to check, but we don't know yet if an update is required. mangaSyncList.forEach { sync ->
// This boolean is used to return 0 if no update is required if (lastChapterRead > sync.last_chapter_read) {
var hasToUpdate = false sync.last_chapter_read = lastChapterRead
sync.update = true
for (mangaSync in mangaSyncList!!) {
if (lastChapterReadLocal > mangaSync.last_chapter_read) {
mangaSync.last_chapter_read = lastChapterReadLocal
mangaSync.update = true
hasToUpdate = true
} }
} }
return if (hasToUpdate) lastChapterReadLocal else 0
return if (mangaSyncList.any { it.update }) lastChapterRead else 0
} }
/** /**
* Starts the service that updates the last chapter read in sync services * Starts the service that updates the last chapter read in sync services
*/ */
fun updateMangaSyncLastChapterRead() { fun updateMangaSyncLastChapterRead() {
for (mangaSync in mangaSyncList ?: emptyList()) { mangaSyncList?.forEach { sync ->
val service = syncManager.getService(mangaSync.sync_id) ?: continue val service = syncManager.getService(sync.sync_id)
if (service.isLogged && mangaSync.update) { if (service != null && service.isLogged && sync.update) {
UpdateMangaSyncService.start(context, mangaSync) UpdateMangaSyncService.start(context, sync)
} }
} }
} }