Refactor AboutLinksPreference

pull/178/head
Koitharu 4 years ago
parent 00dacc32df
commit 86d8ff3c68
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

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

@ -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)
} }

@ -2,10 +2,10 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:width="24dp"
android:height="24dp" android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="1024" android:viewportWidth="1024"
android:viewportHeight="1024"> android:viewportHeight="1024">
<path <path
android:fillColor="#000000" android:fillColor="#000000"
android:fillType="evenOdd" android:pathData="M426.1,112 L112,545.6l0,247.7l486.7,0l0,118.8l313.3,0L912,112ZM599.5,312L599.5,577.6L390.1,577.6Z" />
android:pathData="M402,0 L0,555v317h623v152h401L1024,0ZM624,256L624,596L356,596Z" />
</vector> </vector>

@ -1,5 +1,11 @@
<vector android:height="24dp" android:tint="#000000" <vector
android:viewportHeight="24" android:viewportWidth="24" xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:width="24dp"
<path android:fillColor="#FF000000" android:pathData="M14.5 15.41C14.58 15.5 14.58 15.69 14.5 15.8C13.77 16.5 12.41 16.56 12 16.56C11.61 16.56 10.25 16.5 9.54 15.8C9.44 15.69 9.44 15.5 9.54 15.41C9.65 15.31 9.82 15.31 9.92 15.41C10.38 15.87 11.33 16 12 16C12.69 16 13.66 15.87 14.1 15.41C14.21 15.31 14.38 15.31 14.5 15.41M10.75 13.04C10.75 12.47 10.28 12 9.71 12C9.14 12 8.67 12.47 8.67 13.04C8.67 13.61 9.14 14.09 9.71 14.08C10.28 14.08 10.75 13.61 10.75 13.04M14.29 12C13.72 12 13.25 12.5 13.25 13.05S13.72 14.09 14.29 14.09C14.86 14.09 15.33 13.61 15.33 13.05C15.33 12.5 14.86 12 14.29 12M22 12C22 17.5 17.5 22 12 22S2 17.5 2 12C2 6.5 6.5 2 12 2S22 6.5 22 12M18.67 12C18.67 11.19 18 10.54 17.22 10.54C16.82 10.54 16.46 10.7 16.2 10.95C15.2 10.23 13.83 9.77 12.3 9.71L12.97 6.58L15.14 7.05C15.16 7.6 15.62 8.04 16.18 8.04C16.75 8.04 17.22 7.57 17.22 7C17.22 6.43 16.75 5.96 16.18 5.96C15.77 5.96 15.41 6.2 15.25 6.55L12.82 6.03C12.75 6 12.68 6.03 12.63 6.07C12.57 6.11 12.54 6.17 12.53 6.24L11.79 9.72C10.24 9.77 8.84 10.23 7.82 10.96C7.56 10.71 7.2 10.56 6.81 10.56C6 10.56 5.35 11.21 5.35 12C5.35 12.61 5.71 13.11 6.21 13.34C6.19 13.5 6.18 13.62 6.18 13.78C6.18 16 8.79 17.85 12 17.85C15.23 17.85 17.85 16.03 17.85 13.78C17.85 13.64 17.84 13.5 17.81 13.34C18.31 13.11 18.67 12.6 18.67 12Z"/> android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M14.5 15.41C14.58 15.5 14.58 15.69 14.5 15.8C13.77 16.5 12.41 16.56 12 16.56C11.61 16.56 10.25 16.5 9.54 15.8C9.44 15.69 9.44 15.5 9.54 15.41C9.65 15.31 9.82 15.31 9.92 15.41C10.38 15.87 11.33 16 12 16C12.69 16 13.66 15.87 14.1 15.41C14.21 15.31 14.38 15.31 14.5 15.41M10.75 13.04C10.75 12.47 10.28 12 9.71 12C9.14 12 8.67 12.47 8.67 13.04C8.67 13.61 9.14 14.09 9.71 14.08C10.28 14.08 10.75 13.61 10.75 13.04M14.29 12C13.72 12 13.25 12.5 13.25 13.05S13.72 14.09 14.29 14.09C14.86 14.09 15.33 13.61 15.33 13.05C15.33 12.5 14.86 12 14.29 12M22 12C22 17.5 17.5 22 12 22S2 17.5 2 12C2 6.5 6.5 2 12 2S22 6.5 22 12M18.67 12C18.67 11.19 18 10.54 17.22 10.54C16.82 10.54 16.46 10.7 16.2 10.95C15.2 10.23 13.83 9.77 12.3 9.71L12.97 6.58L15.14 7.05C15.16 7.6 15.62 8.04 16.18 8.04C16.75 8.04 17.22 7.57 17.22 7C17.22 6.43 16.75 5.96 16.18 5.96C15.77 5.96 15.41 6.2 15.25 6.55L12.82 6.03C12.75 6 12.68 6.03 12.63 6.07C12.57 6.11 12.54 6.17 12.53 6.24L11.79 9.72C10.24 9.77 8.84 10.23 7.82 10.96C7.56 10.71 7.2 10.56 6.81 10.56C6 10.56 5.35 11.21 5.35 12C5.35 12.61 5.71 13.11 6.21 13.34C6.19 13.5 6.18 13.62 6.18 13.78C6.18 16 8.79 17.85 12 17.85C15.23 17.85 17.85 16.03 17.85 13.78C17.85 13.64 17.84 13.5 17.81 13.34C18.31 13.11 18.67 12.6 18.67 12Z" />
</vector> </vector>

Loading…
Cancel
Save