Manga details tracking renaming + async

When refreshing tracking, all tracked services will be called async
This commit is contained in:
Jay 2020-05-07 19:27:23 -04:00
parent 4503d408df
commit e69b51accb
2 changed files with 17 additions and 15 deletions

View File

@ -457,7 +457,7 @@ class MangaDetailsController : BaseController,
presenter.fetchChapters(refreshTracker == null) presenter.fetchChapters(refreshTracker == null)
if (refreshTracker != null) { if (refreshTracker != null) {
trackingBottomSheet?.refreshItem(refreshTracker ?: 0) trackingBottomSheet?.refreshItem(refreshTracker ?: 0)
presenter.refreshTrackers() presenter.refreshTracking()
refreshTracker = null refreshTracker = null
} }
val isCurrentController = router?.backstack?.lastOrNull()?.controller() == val isCurrentController = router?.backstack?.lastOrNull()?.controller() ==
@ -839,7 +839,7 @@ class MangaDetailsController : BaseController,
} }
} }
R.id.action_open_in_web_view -> openInWebView() R.id.action_open_in_web_view -> openInWebView()
R.id.action_refresh_tracking -> presenter.refreshTrackers(true) R.id.action_refresh_tracking -> presenter.refreshTracking(true)
R.id.action_migrate -> R.id.action_migrate ->
if (!isNotOnline()) { if (!isNotOnline()) {
PreMigrationController.navigateToMigration( PreMigrationController.navigateToMigration(

View File

@ -37,6 +37,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -92,8 +93,8 @@ class MangaDetailsPresenter(
updateChapters() updateChapters()
controller.updateChapters(this.chapters) controller.updateChapters(this.chapters)
} }
fetchTrackings() setTrackItems()
refreshTrackers(false) refreshTracking(false)
} }
fun onDestroy() { fun onDestroy() {
@ -108,7 +109,7 @@ class MangaDetailsPresenter(
fun fetchChapters(andTracking: Boolean = true) { fun fetchChapters(andTracking: Boolean = true) {
scope.launch { scope.launch {
getChapters() getChapters()
if (andTracking) refreshTracking() if (andTracking) fetchTracks()
withContext(Dispatchers.Main) { controller.updateChapters(chapters) } withContext(Dispatchers.Main) { controller.updateChapters(chapters) }
} }
} }
@ -749,7 +750,7 @@ class MangaDetailsPresenter(
fun hasTrackers(): Boolean = loggedServices.isNotEmpty() fun hasTrackers(): Boolean = loggedServices.isNotEmpty()
// Tracking // Tracking
private fun fetchTrackings() { private fun setTrackItems() {
scope.launch { scope.launch {
trackList = loggedServices.map { service -> trackList = loggedServices.map { service ->
TrackItem(tracks.find { it.sync_id == service.id }, service) TrackItem(tracks.find { it.sync_id == service.id }, service)
@ -757,7 +758,7 @@ class MangaDetailsPresenter(
} }
} }
private suspend fun refreshTracking() { private suspend fun fetchTracks() {
tracks = withContext(Dispatchers.IO) { db.getTracks(manga).executeAsBlocking() } tracks = withContext(Dispatchers.IO) { db.getTracks(manga).executeAsBlocking() }
trackList = loggedServices.map { service -> trackList = loggedServices.map { service ->
TrackItem(tracks.find { it.sync_id == service.id }, service) TrackItem(tracks.find { it.sync_id == service.id }, service)
@ -765,11 +766,11 @@ class MangaDetailsPresenter(
withContext(Dispatchers.Main) { controller.refreshTracking(trackList) } withContext(Dispatchers.Main) { controller.refreshTracking(trackList) }
} }
fun refreshTrackers(showOfflineSnack: Boolean = false) { fun refreshTracking(showOfflineSnack: Boolean = false) {
if (controller.isNotOnline(showOfflineSnack)) { if (!controller.isNotOnline(showOfflineSnack)) {
scope.launch { scope.launch {
trackList.filter { it.track != null }.map { item -> val asyncList = trackList.filter { it.track != null }.map { item ->
withContext(Dispatchers.IO) { async(Dispatchers.IO) {
val trackItem = try { val trackItem = try {
item.service.refresh(item.track!!) item.service.refresh(item.track!!)
} catch (e: Exception) { } catch (e: Exception) {
@ -782,7 +783,8 @@ class MangaDetailsPresenter(
} else item.track } else item.track
} }
} }
refreshTracking() asyncList.awaitAll()
fetchTracks()
} }
} }
} }
@ -817,14 +819,14 @@ class MangaDetailsPresenter(
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (binding != null) db.insertTrack(binding).executeAsBlocking() if (binding != null) db.insertTrack(binding).executeAsBlocking()
} }
refreshTracking() fetchTracks()
} }
} else { } else {
scope.launch { scope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
db.deleteTrackForManga(manga, service).executeAsBlocking() db.deleteTrackForManga(manga, service).executeAsBlocking()
} }
refreshTracking() fetchTracks()
} }
} }
} }
@ -839,7 +841,7 @@ class MangaDetailsPresenter(
} }
if (binding != null) { if (binding != null) {
withContext(Dispatchers.IO) { db.insertTrack(binding).executeAsBlocking() } withContext(Dispatchers.IO) { db.insertTrack(binding).executeAsBlocking() }
refreshTracking() fetchTracks()
} else trackRefreshDone() } else trackRefreshDone()
} }
} }