Skip to content

Commit

Permalink
kotlin synthetics migration 1
Browse files Browse the repository at this point in the history
  • Loading branch information
az4521 committed Jan 15, 2024
1 parent f1cebf9 commit 1119e89
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.ControllerChangeType
import com.bluelinelabs.conductor.RestoreViewOnCreateController
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.clearFindViewByIdCache
import timber.log.Timber

abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
Expand Down Expand Up @@ -52,11 +51,6 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
return inflateView(inflater, container)
}

override fun onDestroyView(view: View) {
super.onDestroyView(view)
clearFindViewByIdCache()
}

abstract fun inflateView(inflater: LayoutInflater, container: ViewGroup): View

open fun onViewCreated(view: View) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package eu.kanade.tachiyomi.ui.category

import android.view.View
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.databinding.CategoriesItemBinding
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import kotlinx.android.synthetic.main.categories_item.reorder
import kotlinx.android.synthetic.main.categories_item.title

/**
* Holder used to display category items.
Expand All @@ -14,8 +13,9 @@ import kotlinx.android.synthetic.main.categories_item.title
*/
class CategoryHolder(view: View, val adapter: CategoryAdapter) : BaseFlexibleViewHolder(view, adapter) {

private val binding = CategoriesItemBinding.bind(view)
init {
setDragHandleView(reorder)
setDragHandleView(binding.reorder)
}

/**
Expand All @@ -25,7 +25,7 @@ class CategoryHolder(view: View, val adapter: CategoryAdapter) : BaseFlexibleVie
*/
fun bind(category: Category) {
// DON'T capitalize category names
title.text = category.name // .capitalize()
binding.title.text = category.name // .capitalize()
}

/**
Expand Down
33 changes: 15 additions & 18 deletions app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ package eu.kanade.tachiyomi.ui.download
import android.view.View
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.databinding.DownloadItemBinding
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import eu.kanade.tachiyomi.util.view.popupMenu
import kotlinx.android.synthetic.main.download_item.chapter_title
import kotlinx.android.synthetic.main.download_item.download_progress
import kotlinx.android.synthetic.main.download_item.download_progress_text
import kotlinx.android.synthetic.main.download_item.manga_full_title
import kotlinx.android.synthetic.main.download_item.menu
import kotlinx.android.synthetic.main.download_item.reorder

/**
* Class used to hold the data of a download.
Expand All @@ -22,9 +17,11 @@ import kotlinx.android.synthetic.main.download_item.reorder
class DownloadHolder(private val view: View, val adapter: DownloadAdapter) :
BaseFlexibleViewHolder(view, adapter) {

private val binding = DownloadItemBinding.bind(view)

init {
setDragHandleView(reorder)
menu.setOnClickListener { it.post { showPopupMenu(it) } }
setDragHandleView(binding.reorder)
binding.menu.setOnClickListener { it.post { showPopupMenu(it) } }
}

private lateinit var download: Download
Expand All @@ -37,19 +34,19 @@ class DownloadHolder(private val view: View, val adapter: DownloadAdapter) :
fun bind(download: Download) {
this.download = download
// Update the chapter name.
chapter_title.text = download.chapter.name
binding.chapterTitle.text = download.chapter.name

// Update the manga title
manga_full_title.text = download.manga.title
binding.mangaFullTitle.text = download.manga.title

// Update the progress bar and the number of downloaded pages
val pages = download.pages
if (pages == null) {
download_progress.progress = 0
download_progress.max = 1
download_progress_text.text = ""
binding.downloadProgress.progress = 0
binding.downloadProgress.max = 1
binding.downloadProgressText.text = ""
} else {
download_progress.max = pages.size * 100
binding.downloadProgress.max = pages.size * 100
notifyProgress()
notifyDownloadedPages()
}
Expand All @@ -60,18 +57,18 @@ class DownloadHolder(private val view: View, val adapter: DownloadAdapter) :
*/
fun notifyProgress() {
val pages = download.pages ?: return
if (download_progress.max == 1) {
download_progress.max = pages.size * 100
if (binding.downloadProgress.max == 1) {
binding.downloadProgress.max = pages.size * 100
}
download_progress.progress = download.totalProgress
binding.downloadProgress.progress = download.totalProgress
}

/**
* Updates the text field of the number of downloaded pages.
*/
fun notifyDownloadedPages() {
val pages = download.pages ?: return
download_progress_text.text = "${download.downloadedImages}/${pages.size}"
binding.downloadProgressText.text = "${download.downloadedImages}/${pages.size}"
}

override fun onItemReleased(position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import android.annotation.SuppressLint
import android.view.View
import androidx.core.view.isVisible
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.databinding.ExtensionCardHeaderBinding
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import kotlinx.android.synthetic.main.extension_card_header.action_button
import kotlinx.android.synthetic.main.extension_card_header.title

class ExtensionGroupHolder(view: View, adapter: FlexibleAdapter<*>) :
BaseFlexibleViewHolder(view, adapter) {

private val binding = ExtensionCardHeaderBinding.bind(view)
@SuppressLint("SetTextI18n")
fun bind(item: ExtensionGroupItem) {
var text = item.name
if (item.showSize) {
text += " (${item.size})"
}

title.text = text
binding.title.text = text

action_button.isVisible = item.actionLabel != null && item.actionOnClick != null
action_button.text = item.actionLabel
action_button.setOnClickListener(if (item.actionLabel != null) item.actionOnClick else null)
binding.actionButton.isVisible = item.actionLabel != null && item.actionOnClick != null
binding.actionButton.text = item.actionLabel
binding.actionButton.setOnClickListener(if (item.actionLabel != null) item.actionOnClick else null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.view.View
import androidx.core.view.isVisible
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.databinding.ExtensionCardItemBinding
import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi
import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.InstallStep
Expand All @@ -12,30 +13,25 @@ import eu.kanade.tachiyomi.ui.base.holder.SlicedHolder
import eu.kanade.tachiyomi.util.system.LocaleHelper
import eu.kanade.tachiyomi.util.system.getResourceColor
import io.github.mthli.slice.Slice
import kotlinx.android.synthetic.main.extension_card_item.cancel_button
import kotlinx.android.synthetic.main.extension_card_item.card
import kotlinx.android.synthetic.main.extension_card_item.ext_button
import kotlinx.android.synthetic.main.extension_card_item.ext_title
import kotlinx.android.synthetic.main.extension_card_item.image
import kotlinx.android.synthetic.main.extension_card_item.lang
import kotlinx.android.synthetic.main.extension_card_item.version

class ExtensionHolder(view: View, override val adapter: ExtensionAdapter) :
BaseFlexibleViewHolder(view, adapter),
SlicedHolder {

override val slice = Slice(card).apply {
private val binding = ExtensionCardItemBinding.bind(view)

override val slice = Slice(binding.card).apply {
setColor(adapter.cardBackground)
}

override val viewToSlice: View
get() = card
get() = binding.card

init {
ext_button.setOnClickListener {
binding.extButton.setOnClickListener {
adapter.buttonClickListener.onButtonClick(bindingAdapterPosition)
}
cancel_button.setOnClickListener {
binding.cancelButton.setOnClickListener {
adapter.buttonClickListener.onCancelButtonClick(bindingAdapterPosition)
}
}
Expand All @@ -45,27 +41,27 @@ class ExtensionHolder(view: View, override val adapter: ExtensionAdapter) :
setCardEdges(item)

// Set source name
ext_title.text = extension.name
version.text = extension.versionName
lang.text = if (extension !is Extension.Untrusted) {
binding.extTitle.text = extension.name
binding.version.text = extension.versionName
binding.lang.text = if (extension !is Extension.Untrusted) {
LocaleHelper.getSourceDisplayName(extension.lang, itemView.context)
} else {
itemView.context.getString(R.string.ext_untrusted).toUpperCase()
itemView.context.getString(R.string.ext_untrusted).uppercase()
}

GlideApp.with(itemView.context).clear(image)
GlideApp.with(itemView.context).clear(binding.image)
if (extension is Extension.Available) {
GlideApp.with(itemView.context)
.load(extension.iconUrl)
.into(image)
.into(binding.image)
} else {
extension.getApplicationIcon(itemView.context)?.let { image.setImageDrawable(it) }
extension.getApplicationIcon(itemView.context)?.let { binding.image.setImageDrawable(it) }
}
bindButtons(item)
}

@Suppress("ResourceType")
fun bindButtons(item: ExtensionItem) = with(ext_button) {
fun bindButtons(item: ExtensionItem) = with(binding.extButton) {
setTextColor(context.getResourceColor(R.attr.colorAccent))

val extension = item.extension
Expand Down Expand Up @@ -110,7 +106,7 @@ class ExtensionHolder(view: View, override val adapter: ExtensionAdapter) :
)

val isIdle = installStep == InstallStep.Idle || installStep == InstallStep.Error
cancel_button.isVisible = !isIdle
binding.cancelButton.isVisible = !isIdle
isEnabled = isIdle
isClickable = isIdle
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RepoPresenter(
super.onCreate(savedState)

preferences.extensionRepos().asFlow().onEach { repos ->
this.repos = repos.toList().sortedBy { it.toLowerCase() }
this.repos = repos.toList().sortedBy { it.lowercase() }

Observable.just(this.repos)
.map { it.map(::RepoItem) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eu.kanade.tachiyomi.ui.library

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.databinding.LibraryCategoryBinding
import eu.kanade.tachiyomi.util.view.inflate
import eu.kanade.tachiyomi.widget.RecyclerViewPagerAdapter

Expand Down Expand Up @@ -34,8 +35,9 @@ class LibraryAdapter(private val controller: LibraryController) : RecyclerViewPa
* @return a new view.
*/
override fun createView(container: ViewGroup): View {
val view = container.inflate(R.layout.library_category) as LibraryCategoryView
view.onCreate(controller)
val binding = LibraryCategoryBinding.inflate(LayoutInflater.from(container.context))
val view: LibraryCategoryView = binding.root
view.onCreate(controller, binding)
return view
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferenceValues.DisplayMode
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.LibraryCategoryBinding
import eu.kanade.tachiyomi.ui.category.CategoryAdapter
import eu.kanade.tachiyomi.util.lang.plusAssign
import eu.kanade.tachiyomi.util.lang.removeArticles
Expand All @@ -23,7 +24,6 @@ import eu.kanade.tachiyomi.util.view.inflate
import eu.kanade.tachiyomi.widget.AutofitRecyclerView
import exh.ui.LoadingHandle
import java.util.concurrent.TimeUnit
import kotlinx.android.synthetic.main.library_category.view.swipe_refresh
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -90,15 +90,15 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
}
// EXH <--

fun onCreate(controller: LibraryController) {
fun onCreate(controller: LibraryController, binding: LibraryCategoryBinding) {
this.controller = controller

recycler = if (preferences.libraryDisplayMode().get() == DisplayMode.LIST) {
(swipe_refresh.inflate(R.layout.library_list_recycler) as RecyclerView).apply {
(binding.swipeRefresh.inflate(R.layout.library_list_recycler) as RecyclerView).apply {
layoutManager = LinearLayoutManager(context)
}
} else {
(swipe_refresh.inflate(R.layout.library_grid_recycler) as AutofitRecyclerView).apply {
(binding.swipeRefresh.inflate(R.layout.library_grid_recycler) as AutofitRecyclerView).apply {
spanCount = controller.mangaPerRow
}
}
Expand All @@ -107,27 +107,27 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att

recycler.setHasFixedSize(true)
recycler.adapter = adapter
swipe_refresh.addView(recycler)
binding.swipeRefresh.addView(recycler)

recycler.scrollStateChanges()
.onEach {
// Disable swipe refresh when view is not at the top
val firstPos = (recycler.layoutManager as LinearLayoutManager)
.findFirstCompletelyVisibleItemPosition()
swipe_refresh.isEnabled = firstPos <= 0
binding.swipeRefresh.isEnabled = firstPos <= 0
}
.launchIn(scope)

// Double the distance required to trigger sync
swipe_refresh.setDistanceToTriggerSync((2 * 64 * resources.displayMetrics.density).toInt())
swipe_refresh.refreshes()
binding.swipeRefresh.setDistanceToTriggerSync((2 * 64 * resources.displayMetrics.density).toInt())
binding.swipeRefresh.refreshes()
.onEach {
if (LibraryUpdateService.start(context, category)) {
context.toast(R.string.updating_category)
}

// It can be a very long operation, so we disable swipe refresh and show a toast.
swipe_refresh.isRefreshing = false
binding.swipeRefresh.isRefreshing = false
}
.launchIn(scope)
}
Expand Down
Loading

0 comments on commit 1119e89

Please sign in to comment.