Skip to content

Commit

Permalink
add option to purge chapter page list cache
Browse files Browse the repository at this point in the history
  • Loading branch information
az4521 committed Dec 7, 2024
1 parent 2990a5e commit 4ce1551
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ class ChapterCache(private val context: Context) {
}
}

fun removePageListFromCache(
chapter: Chapter
) {
try {
val key = DiskUtil.hashKeyForDisk(getKey(chapter))
diskCache.remove(key)
diskCache.flush()
} catch (e: Exception) {
// Ignore.
}
}

/**
* Returns true if image is in cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ class ChaptersController :
menu.findItem(R.id.action_remove_bookmark)?.isVisible = chapters.all { it.chapter.bookmark }
menu.findItem(R.id.action_mark_as_read)?.isVisible = chapters.any { !it.chapter.read }
menu.findItem(R.id.action_mark_as_unread)?.isVisible = chapters.all { it.chapter.read }
menu.findItem(R.id.action_remove_page_cache)?.isVisible = true

// Hide FAB to avoid interfering with the bottom action toolbar
binding.fab.hide()
Expand All @@ -497,7 +498,7 @@ class ChaptersController :
R.id.action_mark_as_read -> markAsRead(getSelectedChapters())
R.id.action_mark_as_unread -> markAsUnread(getSelectedChapters())
R.id.action_mark_previous_as_read -> markPreviousAsRead(getSelectedChapters()[0])

R.id.action_remove_page_cache -> removePageCache(getSelectedChapters())
else -> return false
}
return true
Expand Down Expand Up @@ -525,6 +526,7 @@ class ChaptersController :
R.id.action_mark_as_read -> markAsRead(chapters)
R.id.action_mark_as_unread -> markAsUnread(chapters)
R.id.action_mark_previous_as_read -> markPreviousAsRead(chapter)
R.id.action_remove_page_cache -> removePageCache(chapters)
}
}

Expand Down Expand Up @@ -588,6 +590,14 @@ class ChaptersController :
destroyActionModeIfNeeded()
}

private fun removePageCache(
chapters: List<ChapterItem>
) {
destroyActionModeIfNeeded()
presenter.removePageCache(chapters)
destroyActionModeIfNeeded()
}

private fun bookmarkChapters(
chapters: List<ChapterItem>,
bookmarked: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.manga.chapter
import android.os.Bundle
import com.jakewharton.rxrelay.BehaviorRelay
import com.jakewharton.rxrelay.PublishRelay
import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
Expand Down Expand Up @@ -45,7 +46,8 @@ class ChaptersPresenter(
private val mangaFavoriteRelay: PublishRelay<Boolean>,
val preferences: PreferencesHelper = Injekt.get(),
private val db: DatabaseHelper = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get()
private val downloadManager: DownloadManager = Injekt.get(),
private val chapterCache: ChapterCache = Injekt.get()
) : BasePresenter<ChaptersController>() {
/**
* List of chapters of the manga. It's always unfiltered and unsorted.
Expand Down Expand Up @@ -336,6 +338,18 @@ class ChaptersPresenter(
downloadManager.downloadChapters(manga, chapters)
}

/**
* Removes the page cache for the given list of chapters.
* @param chapters the list of chapters from which to purge the page cache
*/
fun removePageCache(
chapters: List<ChapterItem>
) {
chapters.forEach {
chapterCache.removePageListFromCache(it.chapter)
}
}

/**
* Bookmarks the given list of chapters.
* @param selectedChapters the list of chapters to bookmark.
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/chapter_selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@
app:iconTint="?attr/colorOnPrimary"
app:showAsAction="always" />

<item
android:id="@+id/action_remove_page_cache"
android:title="@string/action_remove_page_cache"
app:showAsAction="never"
/>
</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/menu/chapter_single.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
android:id="@+id/action_mark_previous_as_read"
android:title="@string/action_mark_previous_as_read" />

<item
android:id="@+id/action_remove_page_cache"
android:title="@string/action_remove_page_cache" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<string name="action_search_manually">Search manually</string>
<string name="action_migrate_now">Migrate now</string>
<string name="action_copy_now">Copy now</string>
<string name="action_remove_page_cache">Clear page cache</string>

<!-- Operations -->
<string name="loading">Loading…</string>
Expand Down

0 comments on commit 4ce1551

Please sign in to comment.