Skip to content

Commit

Permalink
Added: Show manga chapters count on search results in Settings>Browse…
Browse files Browse the repository at this point in the history
…>Global Search
  • Loading branch information
Saud-97 committed Nov 10, 2023
1 parent 1abe09f commit 909a184
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ object PreferenceKeys {

const val onlySearchPinned = "only_search_pinned"

const val fetchMangaChapters = "fetch_manga_chapters"

const val downloadNew = "download_new"

const val libraryLayout = "pref_display_library_layout"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ class PreferencesHelper(val context: Context) {

fun onlySearchPinned() = flowPrefs.getBoolean(Keys.onlySearchPinned, false)

fun fetchMangaChapters() = flowPrefs.getBoolean(Keys.fetchMangaChapters, false)

fun hideInLibraryItems() = flowPrefs.getBoolean("browse_hide_in_library_items", false)

// Tutorial preferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ class SettingsBrowseController : SettingsController() {
key = PreferenceKeys.onlySearchPinned
titleRes = R.string.only_search_pinned_when
}
switchPreference {
key = PreferenceKeys.fetchMangaChapters
titleRes = R.string.fetch_manga_chapters
summaryRes = R.string.fetch_manga_chapters_summary
defaultValue = false
}
}

preferenceCategory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,6 @@ open class GlobalSearchController(
*/
fun onMangaInitialized(source: CatalogueSource, manga: Manga) {
getHolder(source)?.setImage(manga)
getHolder(source)?.setChaptersCount(manga)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class GlobalSearchHolder(view: View, val adapter: GlobalSearchAdapter) :
getHolder(manga)?.setImage(manga)
}

fun setChaptersCount(manga: Manga) {
getHolder(manga)?.setChaptersCount(manga)
}

/**
* Returns the view holder for the given manga.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import coil.Coil
import coil.dispose
import coil.request.CachePolicy
import coil.request.ImageRequest
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.image.coil.CoverViewTarget
import eu.kanade.tachiyomi.data.image.coil.MangaCoverFetcher
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.SourceGlobalSearchControllerCardItemBinding
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.view.makeShapeCorners
import eu.kanade.tachiyomi.util.view.setCards
import uy.kohesive.injekt.injectLazy
import java.text.DecimalFormat

class GlobalSearchMangaHolder(view: View, adapter: GlobalSearchCardAdapter) :
BaseFlexibleViewHolder(view, adapter) {
Expand Down Expand Up @@ -48,6 +53,7 @@ class GlobalSearchMangaHolder(view: View, adapter: GlobalSearchCardAdapter) :
binding.title.text = manga.title
binding.favoriteButton.isVisible = manga.favorite
setImage(manga)
setChaptersCount(manga)
}

fun setImage(manga: Manga) {
Expand All @@ -62,4 +68,36 @@ class GlobalSearchMangaHolder(view: View, adapter: GlobalSearchCardAdapter) :
Coil.imageLoader(itemView.context).enqueue(request)
}
}

fun setChaptersCount(manga: Manga) {
if (!preferences.fetchMangaChapters().get()) {
return
}
val mangaChapters = db.getChapters(manga).executeAsBlocking()
if (mangaChapters.isEmpty()) {
return
}

if (!manga.favorite) {
binding.unreadDownloadBadge.badgeView.setChapters(mangaChapters.size)
}

val latestChapter = mangaChapters.maxOfOrNull { it.chapter_number } ?: -1f
if (latestChapter >= 0f) {
binding.subtitle.text = binding.root.context.getString(
R.string.latest_,
DecimalFormat("#.#").format(latestChapter),
)
} else {
binding.subtitle.text = binding.root.context.getString(
R.string.latest_,
binding.root.context.getString(R.string.unknown),
)
}
}

private companion object {
private val db: DatabaseHelper by injectLazy()
private val preferences: PreferencesHelper by injectLazy()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.ui.base.presenter.BaseCoroutinePresenter
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.system.launchIO
import eu.kanade.tachiyomi.util.system.launchUI
import eu.kanade.tachiyomi.util.system.withUIContext
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
Expand Down Expand Up @@ -226,15 +229,20 @@ open class GlobalSearchPresenter(
fetchImageJob?.cancel()
fetchImageJob = fetchImageFlow.onEach { (mangaList, source) ->
mangaList
.filter { it.thumbnail_url == null && !it.initialized }
.filter { (it.thumbnail_url == null && !it.initialized) || preferences.fetchMangaChapters().get() }
.forEach {
presenterScope.launchIO {
try {
val manga = getMangaDetails(it, source)
if (preferences.fetchMangaChapters().get()) {
// to let the database transaction for newly added manga chapters complete before adding the chapters count badge
delay(1250)
}
withUIContext {
view?.onMangaInitialized(source as CatalogueSource, manga)
}
} catch (_: Exception) {
} catch (e: Exception) {
Timber.e(e)
withUIContext {
view?.onMangaInitialized(source as CatalogueSource, it)
}
Expand All @@ -255,6 +263,9 @@ open class GlobalSearchPresenter(
manga.copyFrom(networkManga)
manga.initialized = true
db.insertManga(manga).executeAsBlocking()
if (preferences.fetchMangaChapters().get()) {
fetchChaptersFromSource(manga, source)
}
return manga
}

Expand All @@ -280,4 +291,15 @@ open class GlobalSearchPresenter(
}
return localManga
}

private suspend fun fetchChaptersFromSource(manga: Manga, source: Source) {
try {
val chapters = source.getChapterList(manga)
syncChaptersWithSource(db, chapters, manga, source)
} catch (e: Exception) {
if (!e.message.isNullOrBlank() && !e.message!!.contains("No chapters found")) {
Timber.e(e)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
tools:ignore="ContentDescription"
tools:src="@mipmap/ic_launcher" />

<include
layout="@layout/unread_download_badge"
android:id="@+id/unread_download_badge"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleSmall"
Expand Down Expand Up @@ -87,4 +95,25 @@
app:layout_constraintTop_toBottomOf="@+id/card"
tools:text="Sample name" />

<LinearLayout
android:id="@+id/text_layout"
android:layout_width="110dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
android:orientation="vertical"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/subtitle"
style="?textAppearanceBodySmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/library_comfortable_subtitle_selector"
android:layout_marginTop="-1dp"
android:ellipsize="end"
android:singleLine="true"
tools:text="Sample artist" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@
<string name="notify_extension_updated">Notify extension has been updated</string>
<string name="some_extensions_may_not_update">Some extensions may not be auto-updated if they were installed outside this app</string>
<string name="only_search_pinned_when">Only search pinned sources</string>
<string name="fetch_manga_chapters">Show manga chapters count on search results</string>
<string name="fetch_manga_chapters_summary">Automatically fetch latest manga details and chapters and save it in database\nWarning: this feature will greatly impact device performance and increase network traffic to sources</string>
<string name="hide_in_library_items">Hide entries already in library</string>
<string name="match_pinned_sources">Match pinned sources</string>
<string name="match_enabled_sources">Match enabled sources</string>
Expand Down

0 comments on commit 909a184

Please sign in to comment.