Skip to content

Commit

Permalink
Option to update trackers when chapter marked as read (mihonapp/mihon…
Browse files Browse the repository at this point in the history
…#1177)

* Track when marked as read

* Add dismiss to snack bar

* i18n & ignore decimal chapters

* Detekt would have caught that 🤣

* `Ok` > `Yes`

* Dont prompt if untracked or current > new

* Move to MangaScreenModel

* Suggestions

Co-Authored-By: AntsyLich <59261191+AntsyLich@users.noreply.github.com>

* Review 2

* toggleAllSelections first

---------

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
(cherry picked from commit abfb72c)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt
  • Loading branch information
Animeboynz authored and cuong-tran committed Sep 4, 2024
1 parent 8845c2c commit 11c2ae7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
47 changes: 19 additions & 28 deletions app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.kanade.tachiyomi.ui.manga

import android.app.Application
import android.content.Context
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -174,9 +174,9 @@ class MangaScreenModel(
// KMK -->
private val sourcePreferences: SourcePreferences = Injekt.get(),
private val trackPreferences: TrackPreferences = Injekt.get(),
private val trackChapter: TrackChapter = Injekt.get(),
// KMK <--
private val trackerManager: TrackerManager = Injekt.get(),
private val trackChapter: TrackChapter = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get(),
private val downloadCache: DownloadCache = Injekt.get(),
private val getMangaAndChapters: GetMangaWithChapters = Injekt.get(),
Expand Down Expand Up @@ -1194,11 +1194,6 @@ class MangaScreenModel(
when (swipeAction) {
LibraryPreferences.ChapterSwipeAction.ToggleRead -> {
markChaptersRead(listOf(chapter), !chapter.read)
// KMK -->
if (!chapter.read) {
updateTrackChapterMarkedAsRead(chapter)
}
// KMK <--
}
LibraryPreferences.ChapterSwipeAction.ToggleBookmark -> {
bookmarkChapters(listOf(chapter), !chapter.bookmark)
Expand Down Expand Up @@ -1336,37 +1331,33 @@ class MangaScreenModel(
* @param read whether to mark chapters as read or unread.
*/
fun markChaptersRead(chapters: List<Chapter>, read: Boolean) {
toggleAllSelection(false)
screenModelScope.launchIO {
setReadStatus.await(
read = read,
chapters = chapters.toTypedArray(),
)
}
// KMK -->
if (read) {
chapters.maxByOrNull { it.chapterNumber }
?.also { updateTrackChapterMarkedAsRead(it) }
}
// KMK <--
toggleAllSelection(false)
}

// KMK -->
/**
* Starts the service that updates the chapter marked as read in sync services. This operation
* will run in a background thread and errors are ignored.
*/
private fun updateTrackChapterMarkedAsRead(chapter: Chapter) {
if (!trackPreferences.updateTrackMarkedRead().get()) return
if (!read) return@launchIO

val manga = manga ?: return
val context = Injekt.get<Application>()
val tracks = getTracks.await(mangaId)
val maxChapterNumber = chapters.maxOf { it.chapterNumber }
val shouldPromptTrackingUpdate = tracks.any { track -> maxChapterNumber > track.lastChapterRead }

screenModelScope.launchNonCancellable {
trackChapter.await(context, manga.id, chapter.chapterNumber)
if (!shouldPromptTrackingUpdate) return@launchIO

val result = snackbarHostState.showSnackbar(
message = context.stringResource(MR.strings.confirm_tracker_update, maxChapterNumber.toInt()),
actionLabel = context.stringResource(MR.strings.action_ok),
duration = SnackbarDuration.Short,
withDismissAction = true,
)

if (result == SnackbarResult.ActionPerformed) {
trackChapter.await(context, mangaId, maxChapterNumber)
}
}
}
// KMK <--

/**
* Downloads the given list of chapters with the manager.
Expand Down
1 change: 1 addition & 0 deletions i18n/src/commonMain/moko-resources/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@
<string name="are_you_sure">Are you sure?</string>
<string name="exclude_scanlators">Exclude scanlators</string>
<string name="no_scanlators_found">No scanlators found</string>
<string name="confirm_tracker_update">Update trackers to chapter %d?</string>

<!-- Tracking Screen -->
<string name="manga_tracking_tab">Tracking</string>
Expand Down

0 comments on commit 11c2ae7

Please sign in to comment.