More minor refactorings

Extracted from #7244

Co-authored-by: ivaniskandar <ivaniskandar@users.noreply.github.com>
This commit is contained in:
arkon 2022-06-19 17:32:31 -04:00
parent 68c47a3238
commit cb1830d747
10 changed files with 34 additions and 37 deletions

View File

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.source
import android.graphics.drawable.Drawable
import eu.kanade.domain.source.model.SourceData
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.extension.ExtensionManager
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter
@ -105,3 +106,18 @@ fun Source.icon(): Drawable? = Injekt.get<ExtensionManager>().getAppIconForSourc
fun Source.getPreferenceKey(): String = "source_$id"
fun Source.toSourceData(): SourceData = SourceData(id = id, lang = lang, name = name)
fun Source.getNameForMangaInfo(): String {
val preferences = Injekt.get<PreferencesHelper>()
val enabledLanguages = preferences.enabledLanguages().get()
.filterNot { it in listOf("all", "other") }
val hasOneActiveLanguages = enabledLanguages.size == 1
val isInEnabledLanguages = lang in enabledLanguages
return when {
// For edge cases where user disables a source they got manga of in their library.
hasOneActiveLanguages && !isInEnabledLanguages -> toString()
// Hide the language tag when only one language is used.
hasOneActiveLanguages && isInEnabledLanguages -> name
else -> toString()
}
}

View File

@ -84,7 +84,7 @@ class SearchController(
if (!isReplacingManga) {
router.popController(this)
if (newManga != null) {
val newMangaController = RouterTransaction.with(MangaController(newManga))
val newMangaController = RouterTransaction.with(MangaController(newManga.id!!))
if (router.backstack.lastOrNull()?.controller is MangaController) {
// Replace old MangaController
router.replaceTopController(newMangaController)
@ -140,7 +140,7 @@ class SearchController(
}
.setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _ ->
dismissDialog()
router.pushController(MangaController(newManga))
router.pushController(MangaController(newManga!!.id!!))
}
.create()
}

View File

@ -575,7 +575,7 @@ open class BrowseSourceController(bundle: Bundle) :
*/
override fun onItemClick(view: View, position: Int): Boolean {
val item = adapter?.getItem(position) as? SourceItem ?: return false
router.pushController(MangaController(item.manga, true))
router.pushController(MangaController(item.manga.id!!, true))
return false
}

View File

@ -65,7 +65,7 @@ open class GlobalSearchController(
* @param manga clicked item containing manga information.
*/
override fun onMangaClick(manga: Manga) {
router.pushController(MangaController(manga, true))
router.pushController(MangaController(manga.id!!, true))
}
/**

View File

@ -493,7 +493,7 @@ class LibraryController(
// Notify the presenter a manga is being opened.
presenter.onOpenManga()
router.pushController(MangaController(manga))
router.pushController(MangaController(manga.id!!))
}
/**

View File

@ -40,7 +40,7 @@ class AddDuplicateMangaDialog(bundle: Bundle? = null) : DialogController(bundle)
.setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _ ->
dismissDialog()
router.pushController(MangaController(libraryManga))
router.pushController(MangaController(libraryManga.id!!))
}
.setCancelable(true)
.create()

View File

@ -122,22 +122,18 @@ class MangaController :
constructor(history: HistoryWithRelations) : this(history.mangaId)
constructor(manga: Manga?, fromSource: Boolean = false) : super(
constructor(mangaId: Long, fromSource: Boolean = false) : super(
bundleOf(
MANGA_EXTRA to (manga?.id ?: 0),
MANGA_EXTRA to mangaId,
FROM_SOURCE_EXTRA to fromSource,
),
) {
this.manga = manga
if (manga != null) {
source = Injekt.get<SourceManager>().getOrStub(manga.source)
this.manga = Injekt.get<DatabaseHelper>().getManga(mangaId).executeAsBlocking()
if (this.manga != null) {
source = Injekt.get<SourceManager>().getOrStub(this.manga!!.source)
}
}
constructor(mangaId: Long) : this(
Injekt.get<DatabaseHelper>().getManga(mangaId).executeAsBlocking(),
)
@Suppress("unused")
constructor(bundle: Bundle) : this(bundle.getLong(MANGA_EXTRA))
@ -309,7 +305,7 @@ class MangaController :
settingsSheet = ChaptersSettingsSheet(router, presenter)
trackSheet = TrackSheet(this, manga!!, (activity as MainActivity).supportFragmentManager)
trackSheet = TrackSheet(this, (activity as MainActivity).supportFragmentManager)
updateFilterIconState()
recyclerViewUpdatesToolbarTitleAlpha(true)

View File

@ -14,6 +14,7 @@ import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.databinding.MangaInfoHeaderBinding
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.getNameForMangaInfo
import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.base.controller.getMainAppBarHeight
@ -247,20 +248,8 @@ class MangaInfoHeaderAdapter(
// If manga source is known update source TextView.
binding.mangaMissingSourceIcon.isVisible = source is SourceManager.StubSource
val mangaSource = source.toString()
with(binding.mangaSource) {
val enabledLanguages = preferences.enabledLanguages().get()
.filterNot { it in listOf("all", "other") }
val hasOneActiveLanguages = enabledLanguages.size == 1
val isInEnabledLanguages = source.lang in enabledLanguages
text = when {
// For edge cases where user disables a source they got manga of in their library.
hasOneActiveLanguages && !isInEnabledLanguages -> mangaSource
// Hide the language tag when only one language is used.
hasOneActiveLanguages && isInEnabledLanguages -> source.name
else -> mangaSource
}
text = source.getNameForMangaInfo()
setOnClickListener {
controller.performSearch(sourceManager.getOrStub(source.id).name)

View File

@ -11,10 +11,8 @@ import com.google.android.material.datepicker.DateValidatorPointBackward
import com.google.android.material.datepicker.DateValidatorPointForward
import com.google.android.material.datepicker.MaterialDatePicker
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.track.EnhancedTrackService
import eu.kanade.tachiyomi.databinding.TrackControllerBinding
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.base.controller.openInBrowser
import eu.kanade.tachiyomi.ui.manga.MangaController
import eu.kanade.tachiyomi.util.lang.launchIO
@ -24,14 +22,10 @@ import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.system.copyToClipboard
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.sheet.BaseBottomSheetDialog
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class TrackSheet(
val controller: MangaController,
val manga: Manga,
val fragmentManager: FragmentManager,
private val sourceManager: SourceManager = Injekt.get(),
) : BaseBottomSheetDialog(controller.activity!!),
TrackAdapter.OnClickListener,
SetTrackStatusDialog.Listener,
@ -80,6 +74,8 @@ class TrackSheet(
override fun onSetClick(position: Int) {
val item = adapter.getItem(position) ?: return
val manga = controller.presenter.manga
val source = controller.presenter.source
if (item.service is EnhancedTrackService) {
if (item.track != null) {
@ -87,7 +83,7 @@ class TrackSheet(
return
}
if (!item.service.accept(sourceManager.getOrStub(manga.source))) {
if (!item.service.accept(source)) {
controller.presenter.view?.applicationContext?.toast(R.string.source_unsupported)
return
}

View File

@ -282,7 +282,7 @@ class UpdatesController :
}
private fun openManga(chapter: UpdatesItem) {
router.pushController(MangaController(chapter.manga))
router.pushController(MangaController(chapter.manga.id!!))
}
/**