Refactor AboutLinksPreference

This commit is contained in:
Koitharu
2022-06-18 20:16:49 +03:00
parent 00dacc32df
commit 86d8ff3c68
4 changed files with 54 additions and 51 deletions

View File

@@ -1,62 +1,67 @@
package org.koitharu.kotatsu.settings.utils
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import androidx.core.content.ContextCompat.startActivity
import android.view.View
import androidx.appcompat.widget.TooltipCompat
import androidx.core.net.toUri
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.utils.ext.setTooltip
import org.koitharu.kotatsu.databinding.PreferenceAboutLinksBinding
class AboutLinksPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
Preference(context, attrs) {
class AboutLinksPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : Preference(context, attrs), View.OnClickListener {
init {
layoutResource = R.layout.preference_about_links
isSelectable = false
isPersistent = false
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
holder.findViewById(R.id.btn_4pda).apply {
setTooltip(contentDescription.toString())
setOnClickListener { openLink(resources.getString(R.string.url_forpda), contentDescription.toString()) }
}
holder.findViewById(R.id.btn_discord).apply {
setTooltip(contentDescription.toString())
setOnClickListener { openLink(resources.getString(R.string.url_discord), contentDescription.toString()) }
}
holder.findViewById(R.id.btn_twitter).apply {
setTooltip(contentDescription.toString())
setOnClickListener { openLink(resources.getString(R.string.url_twitter), contentDescription.toString()) }
}
holder.findViewById(R.id.btn_reddit).apply {
setTooltip(contentDescription.toString())
setOnClickListener { openLink(resources.getString(R.string.url_reddit), contentDescription.toString()) }
}
holder.findViewById(R.id.btn_github).apply {
setTooltip(contentDescription.toString())
setOnClickListener {
openLink(
resources.getString(R.string.url_github),
contentDescription.toString()
)
}
val binding = PreferenceAboutLinksBinding.bind(holder.itemView)
arrayOf(
binding.btn4pda,
binding.btnDiscord,
binding.btnGithub,
binding.btnReddit,
binding.btnTwitter,
).forEach { button ->
TooltipCompat.setTooltipText(button, button.contentDescription)
button.setOnClickListener(this)
}
}
override fun onClick(v: View) {
val urlResId = when (v.id) {
R.id.btn_4pda -> R.string.url_forpda
R.id.btn_discord -> R.string.url_discord
R.id.btn_twitter -> R.string.url_twitter
R.id.btn_reddit -> R.string.url_reddit
R.id.btn_github -> R.string.url_github
else -> return
}
openLink(v.context.getString(urlResId), v.contentDescription)
}
private fun openLink(url: String, title: CharSequence?) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = url.toUri()
context.startActivity(
if (title != null) {
Intent.createChooser(intent, title)
} else {
intent
}
)
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
try {
context.startActivity(
if (title != null) {
Intent.createChooser(intent, title)
} else {
intent
}
)
} catch (_: ActivityNotFoundException) {
}
}
}

View File

@@ -156,12 +156,4 @@ fun <T : View> ViewGroup.findViewsByType(clazz: Class<T>): Sequence<T> {
}
}
}
}
inline fun View.setTooltip(@StringRes stringRes: Int) {
setTooltip(context.getString(stringRes))
}
inline fun View.setTooltip(text: String) {
TooltipCompat.setTooltipText(this, text)
}