From 25e36659dc27bf12f9f8f9e02692ae22b2b01b6a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 7 Nov 2023 17:35:59 +0100 Subject: [PATCH] refactor: create a custom view for expendable text views --- .../libretube/ui/fragments/ChannelFragment.kt | 6 ----- .../ui/fragments/PlaylistFragment.kt | 13 +--------- .../libretube/ui/views/ExpandableTextView.kt | 25 +++++++++++++++++++ app/src/main/res/layout/fragment_channel.xml | 5 +--- app/src/main/res/layout/fragment_playlist.xml | 10 ++------ 5 files changed, 29 insertions(+), 30 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/views/ExpandableTextView.kt diff --git a/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt index 989bd0a20..4f9d9b78f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt @@ -187,12 +187,6 @@ class ChannelFragment : Fragment() { binding.channelDescription.text = response.description.orEmpty().trim() } - binding.channelDescription.setOnClickListener { - (it as TextView).apply { - it.maxLines = if (it.maxLines == Int.MAX_VALUE) 2 else Int.MAX_VALUE - } - } - ImageHelper.loadImage(response.bannerUrl, binding.channelBanner) ImageHelper.loadImage(response.avatarUrl, binding.channelImage) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index 10326356c..a9f192aa5 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -7,6 +7,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.os.bundleOf +import androidx.core.text.parseAsHtml import androidx.core.view.isGone import androidx.core.view.isVisible import androidx.core.view.updatePadding @@ -139,11 +140,6 @@ class PlaylistFragment : Fragment() { binding.playlistProgress.isGone = true binding.playlistName.text = response.name - binding.playlistName.setOnClickListener { - binding.playlistName.maxLines = - if (binding.playlistName.maxLines == 2) Int.MAX_VALUE else 2 - } - binding.playlistInfo.text = getChannelAndVideoString(response, response.videos) binding.playlistInfo.setOnClickListener { (context as MainActivity).navController.navigate( @@ -156,13 +152,6 @@ class PlaylistFragment : Fragment() { // hide playlist description text view if not provided binding.playlistDescription.isGone = response.description.orEmpty().isBlank() - binding.playlistDescription.let { textView -> - textView.setOnClickListener { - textView.maxLines = - if (textView.maxLines == Int.MAX_VALUE) 3 else Int.MAX_VALUE - } - } - showPlaylistVideos(response) // show playlist options diff --git a/app/src/main/java/com/github/libretube/ui/views/ExpandableTextView.kt b/app/src/main/java/com/github/libretube/ui/views/ExpandableTextView.kt new file mode 100644 index 000000000..ac5496b49 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/views/ExpandableTextView.kt @@ -0,0 +1,25 @@ +package com.github.libretube.ui.views + +import android.content.Context +import android.text.TextUtils +import android.util.AttributeSet +import androidx.appcompat.widget.AppCompatTextView +import com.github.libretube.R + +class ExpandableTextView(context: Context, attributeSet: AttributeSet? = null) : + AppCompatTextView(context, attributeSet) { + + init { + maxLines = DEFAULT_MAX_LINES + ellipsize = TextUtils.TruncateAt.END + setBackgroundResource(R.drawable.rounded_ripple) + + setOnClickListener { + maxLines = if (maxLines == DEFAULT_MAX_LINES) Int.MAX_VALUE else DEFAULT_MAX_LINES + } + } + + companion object { + private const val DEFAULT_MAX_LINES = 2 + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_channel.xml b/app/src/main/res/layout/fragment_channel.xml index bde329a77..77c26fb09 100644 --- a/app/src/main/res/layout/fragment_channel.xml +++ b/app/src/main/res/layout/fragment_channel.xml @@ -100,15 +100,12 @@ - - -