Browse Source

Background tracker update during Library update (#5166)

* add preference to auto update trackers during library update

* also update trackers when updating chapters and preference is set

* remove unnecessary launch/join

* perform tracking update within the same chapter update loop to avoid double notifications
Gauthier 3 years ago
parent
commit
e76e903060

+ 30 - 20
app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt

@@ -21,6 +21,7 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Companion.start
 import eu.kanade.tachiyomi.data.notification.Notifications
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 import eu.kanade.tachiyomi.data.track.TrackManager
+import eu.kanade.tachiyomi.data.track.TrackService
 import eu.kanade.tachiyomi.data.track.UnattendedTrackService
 import eu.kanade.tachiyomi.source.SourceManager
 import eu.kanade.tachiyomi.source.model.SManga
@@ -273,6 +274,7 @@ class LibraryUpdateService(
         val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>()
         val failedUpdates = mutableListOf<Pair<Manga, String?>>()
         var hasDownloads = false
+        val loggedServices by lazy { trackManager.services.filter { it.isLogged } }
 
         mangaToUpdate.forEach { manga ->
             if (updateJob?.isActive != true) {
@@ -301,6 +303,10 @@ class LibraryUpdateService(
                 }
                 failedUpdates.add(manga to errorMessage)
             }
+
+            if (preferences.autoUpdateTrackers()) {
+                updateTrackings(manga, loggedServices)
+            }
         }
 
         notifier.cancelProgressNotification()
@@ -409,31 +415,35 @@ class LibraryUpdateService(
             notifier.showProgressNotification(manga, progressCount++, mangaToUpdate.size)
 
             // Update the tracking details.
-            db.getTracks(manga).executeAsBlocking()
-                .map { track ->
-                    supervisorScope {
-                        async {
-                            val service = trackManager.getService(track.sync_id)
-                            if (service != null && service in loggedServices) {
-                                try {
-                                    val updatedTrack = service.refresh(track)
-                                    db.insertTrack(updatedTrack).executeAsBlocking()
-
-                                    if (service is UnattendedTrackService) {
-                                        syncChaptersWithTrackServiceTwoWay(db, db.getChapters(manga).executeAsBlocking(), track, service)
-                                    }
-                                } catch (e: Throwable) {
-                                    // Ignore errors and continue
-                                    Timber.e(e)
+            updateTrackings(manga, loggedServices)
+        }
+
+        notifier.cancelProgressNotification()
+    }
+
+    private suspend fun updateTrackings(manga: LibraryManga, loggedServices: List<TrackService>) {
+        db.getTracks(manga).executeAsBlocking()
+            .map { track ->
+                supervisorScope {
+                    async {
+                        val service = trackManager.getService(track.sync_id)
+                        if (service != null && service in loggedServices) {
+                            try {
+                                val updatedTrack = service.refresh(track)
+                                db.insertTrack(updatedTrack).executeAsBlocking()
+
+                                if (service is UnattendedTrackService) {
+                                    syncChaptersWithTrackServiceTwoWay(db, db.getChapters(manga).executeAsBlocking(), track, service)
                                 }
+                            } catch (e: Throwable) {
+                                // Ignore errors and continue
+                                Timber.e(e)
                             }
                         }
                     }
                 }
-                .awaitAll()
-        }
-
-        notifier.cancelProgressNotification()
+            }
+            .awaitAll()
     }
 
     /**

+ 2 - 0
app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt

@@ -168,6 +168,8 @@ object PreferenceKeys {
 
     const val autoUpdateMetadata = "auto_update_metadata"
 
+    const val autoUpdateTrackers = "auto_update_trackers"
+
     const val showLibraryUpdateErrors = "show_library_update_errors"
 
     const val downloadNew = "download_new"

+ 2 - 0
app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt

@@ -79,6 +79,8 @@ class PreferencesHelper(val context: Context) {
 
     fun autoUpdateMetadata() = prefs.getBoolean(Keys.autoUpdateMetadata, false)
 
+    fun autoUpdateTrackers() = prefs.getBoolean(Keys.autoUpdateTrackers, false)
+
     fun showLibraryUpdateErrors() = prefs.getBoolean(Keys.showLibraryUpdateErrors, false)
 
     fun clear() = prefs.edit { clear() }

+ 6 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt

@@ -241,6 +241,12 @@ class SettingsLibraryController : SettingsController() {
                 summaryRes = R.string.pref_library_update_refresh_metadata_summary
                 defaultValue = false
             }
+            switchPreference {
+                key = Keys.autoUpdateTrackers
+                titleRes = R.string.pref_library_update_refresh_trackers
+                summaryRes = R.string.pref_library_update_refresh_trackers_summary
+                defaultValue = false
+            }
             switchPreference {
                 key = Keys.showLibraryUpdateErrors
                 titleRes = R.string.pref_library_update_error_notification

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -218,6 +218,8 @@
     <string name="pref_update_only_non_completed">Only update ongoing manga</string>
     <string name="pref_library_update_refresh_metadata">Automatically refresh metadata</string>
     <string name="pref_library_update_refresh_metadata_summary">Check for new cover and details when updating library</string>
+    <string name="pref_library_update_refresh_trackers">Automatically update trackers</string>
+    <string name="pref_library_update_refresh_trackers_summary">Update trackers when updating library</string>
     <string name="pref_library_update_error_notification">Show update errors notifications</string>
 
     <string name="default_category">Default category</string>