Skip to content

Commit

Permalink
kotlin synthetics migration 4
Browse files Browse the repository at this point in the history
  • Loading branch information
az4521 committed Jan 16, 2024
1 parent 3d9ed8a commit 5fb718f
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import com.tfcporciuncula.flow.Preference
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.MigrationBottomSheetBinding
import eu.kanade.tachiyomi.ui.migration.MigrationFlags
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.visible
import kotlinx.android.synthetic.main.migration_bottom_sheet.*
import kotlinx.android.synthetic.main.migration_bottom_sheet.extra_search_param
import kotlinx.android.synthetic.main.migration_bottom_sheet.extra_search_param_text
import kotlinx.android.synthetic.main.migration_bottom_sheet.mig_categories
import kotlinx.android.synthetic.main.migration_bottom_sheet.mig_chapters
import kotlinx.android.synthetic.main.migration_bottom_sheet.mig_tracking
import kotlinx.android.synthetic.main.migration_bottom_sheet.use_smart_search
import uy.kohesive.injekt.injectLazy

class MigrationBottomSheetDialog(
Expand All @@ -41,15 +35,15 @@ class MigrationBottomSheetDialog(
*/
private val preferences by injectLazy<PreferencesHelper>()

private val binding = MigrationBottomSheetBinding.inflate(activity.layoutInflater)
init {
// Use activity theme for this layout
val view = activity.layoutInflater.inflate(R.layout.migration_bottom_sheet, null)
// val scroll = NestedScrollView(context)
// scroll.addView(view)

setContentView(view)
setContentView(binding.root)
if (activity.resources.configuration?.orientation == Configuration.ORIENTATION_LANDSCAPE) {
sourceGroup.orientation = LinearLayout.HORIZONTAL
binding.sourceGroup.orientation = LinearLayout.HORIZONTAL
}
window?.setBackgroundDrawable(null)
}
Expand All @@ -62,11 +56,11 @@ class MigrationBottomSheetDialog(

initPreferences()

fab.setOnClickListener {
preferences.skipPreMigration().set(skip_step.isChecked)
binding.fab.setOnClickListener {
preferences.skipPreMigration().set(binding.skipStep.isChecked)
listener.startMigration(
if (use_smart_search.isChecked && extra_search_param_text.text.isNotBlank()) {
extra_search_param_text.text.toString()
if (binding.useSmartSearch.isChecked && binding.extraSearchParamText.text.isNotBlank()) {
binding.extraSearchParamText.text.toString()
} else null
)
dismiss()
Expand All @@ -79,27 +73,27 @@ class MigrationBottomSheetDialog(
private fun initPreferences() {
val flags = preferences.migrateFlags().get()

mig_chapters.isChecked = MigrationFlags.hasChapters(flags)
mig_categories.isChecked = MigrationFlags.hasCategories(flags)
mig_tracking.isChecked = MigrationFlags.hasTracks(flags)
binding.migChapters.isChecked = MigrationFlags.hasChapters(flags)
binding.migCategories.isChecked = MigrationFlags.hasCategories(flags)
binding.migTracking.isChecked = MigrationFlags.hasTracks(flags)

mig_chapters.setOnCheckedChangeListener { _, _ -> setFlags() }
mig_categories.setOnCheckedChangeListener { _, _ -> setFlags() }
mig_tracking.setOnCheckedChangeListener { _, _ -> setFlags() }
binding.migChapters.setOnCheckedChangeListener { _, _ -> setFlags() }
binding.migCategories.setOnCheckedChangeListener { _, _ -> setFlags() }
binding.migTracking.setOnCheckedChangeListener { _, _ -> setFlags() }

use_smart_search.bindToPreference(preferences.smartMigration())
extra_search_param_text.gone()
extra_search_param.setOnCheckedChangeListener { _, isChecked ->
binding.useSmartSearch.bindToPreference(preferences.smartMigration())
binding.extraSearchParamText.gone()
binding.extraSearchParam.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
extra_search_param_text.visible()
binding.extraSearchParamText.visible()
} else {
extra_search_param_text.gone()
binding.extraSearchParamText.gone()
}
}
sourceGroup.bindToPreference(preferences.useSourceWithMost())
binding.sourceGroup.bindToPreference(preferences.useSourceWithMost())

skip_step.isChecked = preferences.skipPreMigration().get()
skip_step.setOnCheckedChangeListener { _, isChecked ->
binding.skipStep.isChecked = preferences.skipPreMigration().get()
binding.skipStep.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
(listener as? Controller)?.activity?.toast(
R.string.pre_migration_skip_toast,
Expand All @@ -111,9 +105,9 @@ class MigrationBottomSheetDialog(

private fun setFlags() {
var flags = 0
if (mig_chapters.isChecked) flags = flags or MigrationFlags.CHAPTERS
if (mig_categories.isChecked) flags = flags or MigrationFlags.CATEGORIES
if (mig_tracking.isChecked) flags = flags or MigrationFlags.TRACK
if (binding.migChapters.isChecked) flags = flags or MigrationFlags.CHAPTERS
if (binding.migCategories.isChecked) flags = flags or MigrationFlags.CATEGORIES
if (binding.migTracking.isChecked) flags = flags or MigrationFlags.TRACK
preferences.migrateFlags().set(flags)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.data.glide.toMangaThumbnail
import eu.kanade.tachiyomi.databinding.MigrationMangaCardBinding
import eu.kanade.tachiyomi.databinding.MigrationProcessItemBinding
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.all.MergedSource
Expand All @@ -23,17 +25,6 @@ import eu.kanade.tachiyomi.util.view.setVectorCompat
import eu.kanade.tachiyomi.util.view.visible
import exh.MERGED_SOURCE_ID
import java.text.DecimalFormat
import kotlinx.android.synthetic.main.migration_manga_card.view.gradient
import kotlinx.android.synthetic.main.migration_manga_card.view.loading_group
import kotlinx.android.synthetic.main.migration_manga_card.view.manga_chapters
import kotlinx.android.synthetic.main.migration_manga_card.view.manga_last_chapter_label
import kotlinx.android.synthetic.main.migration_manga_card.view.manga_source_label
import kotlinx.android.synthetic.main.migration_manga_card.view.thumbnail
import kotlinx.android.synthetic.main.migration_manga_card.view.title
import kotlinx.android.synthetic.main.migration_process_item.migration_manga_card_from
import kotlinx.android.synthetic.main.migration_process_item.migration_manga_card_to
import kotlinx.android.synthetic.main.migration_process_item.migration_menu
import kotlinx.android.synthetic.main.migration_process_item.skip_manga
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.injectLazy
Expand All @@ -48,12 +39,14 @@ class MigrationProcessHolder(
private var item: MigrationProcessItem? = null
private val gson: Gson by injectLazy()

private val binding = MigrationProcessItemBinding.bind(view)

init {
// We need to post a Runnable to show the popup to make sure that the PopupMenu is
// correctly positioned. The reason being that the view may change position before the
// PopupMenu is shown.
migration_menu.setOnClickListener { it.post { showPopupMenu(it) } }
skip_manga.setOnClickListener { it.post { adapter.removeManga(bindingAdapterPosition) } }
binding.migrationMenu.setOnClickListener { it.post { showPopupMenu(it) } }
binding.skipManga.setOnClickListener { it.post { adapter.removeManga(bindingAdapterPosition) } }
}

fun bind(item: MigrationProcessItem) {
Expand All @@ -62,25 +55,25 @@ class MigrationProcessHolder(
val manga = item.manga.manga()
val source = item.manga.mangaSource()

migration_menu.setVectorCompat(
binding.migrationMenu.setVectorCompat(
R.drawable.ic_more_vert_24dp,
view.context
.getResourceColor(R.attr.colorOnPrimary)
)
skip_manga.setVectorCompat(
binding.skipManga.setVectorCompat(
R.drawable.ic_close_24dp,
view.context.getResourceColor(
R
.attr.colorOnPrimary
)
)
migration_menu.invisible()
skip_manga.visible()
migration_manga_card_to.resetManga()
binding.migrationMenu.invisible()
binding.skipManga.visible()
binding.migrationMangaCardTo.resetManga()
if (manga != null) {
withContext(Dispatchers.Main) {
migration_manga_card_from.attachManga(manga, source)
migration_manga_card_from.setOnClickListener {
binding.migrationMangaCardFrom.attachManga(manga, source)
binding.migrationMangaCardFrom.root.setOnClickListener {
adapter.controller.router.pushController(
MangaController(
manga,
Expand Down Expand Up @@ -114,76 +107,74 @@ class MigrationProcessHolder(
return@withContext
}
if (searchResult != null && resultSource != null) {
migration_manga_card_to.attachManga(searchResult, resultSource)
migration_manga_card_to.setOnClickListener {
binding.migrationMangaCardTo.attachManga(searchResult, resultSource)
binding.migrationMangaCardTo.root.setOnClickListener {
adapter.controller.router.pushController(
MangaController(
searchResult, true
).withFadeTransaction()
)
}
} else {
migration_manga_card_to.loading_group.gone()
migration_manga_card_to.title.text = view.context.applicationContext
binding.migrationMangaCardTo.loadingGroup.gone()
binding.migrationMangaCardTo.title.text = view.context.applicationContext
.getString(R.string.no_alternatives_found)
}
migration_menu.visible()
skip_manga.gone()
binding.migrationMenu.visible()
binding.skipManga.gone()
adapter.sourceFinished()
}
}
}
}

private fun View.resetManga() {
loading_group.visible()
private fun MigrationMangaCardBinding.resetManga() {
loadingGroup.visible()
thumbnail.setImageDrawable(null)
title.text = ""
manga_source_label.text = ""
manga_chapters.text = ""
manga_chapters.gone()
manga_last_chapter_label.text = ""
migration_manga_card_to.setOnClickListener(null)
mangaSourceLabel.text = ""
mangaChapters.text = ""
mangaChapters.gone()
mangaLastChapterLabel.text = ""
root.setOnClickListener(null)
}

private fun View.attachManga(manga: Manga, source: Source) {
loading_group.gone()
private fun MigrationMangaCardBinding.attachManga(manga: Manga, source: Source) {
loadingGroup.gone()
GlideApp.with(view.context.applicationContext)
.load(manga.toMangaThumbnail())
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.centerCrop()
.dontAnimate()
.into(thumbnail)

title.text = if (manga.title.isBlank()) {
title.text = manga.title.ifBlank {
view.context.getString(R.string.unknown)
} else {
manga.title
}

gradient.visible()
manga_source_label.text = if (source.id == MERGED_SOURCE_ID) {
mangaSourceLabel.text = if (source.id == MERGED_SOURCE_ID) {
MergedSource.MangaConfig.readFromUrl(gson, manga.url).children.map {
sourceManager.getOrStub(it.source).toString()
}.distinct().joinToString()
} else {
source.toString()
}

val mangaChapters = db.getChapters(manga).executeAsBlocking()
manga_chapters.visible()
manga_chapters.text = mangaChapters.size.toString()
val latestChapter = mangaChapters.maxByOrNull { it.chapter_number }?.chapter_number ?: -1f
val mangaChaptersDB = db.getChapters(manga).executeAsBlocking()
mangaChapters.visible()
mangaChapters.text = mangaChaptersDB.size.toString()
val latestChapter = mangaChaptersDB.maxByOrNull { it.chapter_number }?.chapter_number ?: -1f

if (latestChapter > 0f) {
manga_last_chapter_label.text = context.getString(
mangaLastChapterLabel.text = root.context.getString(
R.string.latest_,
DecimalFormat("#.#").format(latestChapter)
)
} else {
manga_last_chapter_label.text = context.getString(
mangaLastChapterLabel.text = root.context.getString(
R.string.latest_,
context.getString(R.string.unknown)
root.context.getString(R.string.unknown)
)
}
}
Expand Down
Loading

0 comments on commit 5fb718f

Please sign in to comment.