From 23edd3b5d9a1a3c513df1ade9289c6cf9602c3ef Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 11 Oct 2024 12:23:16 +0200 Subject: [PATCH] refactor: unify views string formatting logic to avoid bugs --- .../ui/adapters/SearchChannelAdapter.kt | 11 +------- .../ui/adapters/SearchResultsAdapter.kt | 11 +------- .../libretube/ui/adapters/VideosAdapter.kt | 26 +++---------------- .../libretube/ui/views/DescriptionLayout.kt | 14 ++-------- .../com/github/libretube/util/TextUtils.kt | 11 ++++++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 19 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SearchChannelAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SearchChannelAdapter.kt index c4b7caf72..135a29e5e 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SearchChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SearchChannelAdapter.kt @@ -73,16 +73,7 @@ class SearchChannelAdapter : ListAdapter(SearchCa ImageHelper.loadImage(item.thumbnail, thumbnail) thumbnailDuration.setFormattedDuration(item.duration, item.isShort) videoTitle.text = item.title - - val viewsString = item.views.takeIf { it != -1L }?.formatShort().orEmpty() - val uploadDate = item.uploaded.takeIf { it > 0 }?.let { - " ${TextUtils.SEPARATOR} ${TextUtils.formatRelativeDate(root.context, it)}" - }.orEmpty() - videoInfo.text = root.context.getString( - R.string.normal_views, - viewsString, - uploadDate - ) + videoInfo.text = TextUtils.formatViewsString(root.context, item.views, item.uploaded) channelContainer.isGone = true diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SearchResultsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SearchResultsAdapter.kt index 39a224876..68bcf8d6d 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SearchResultsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SearchResultsAdapter.kt @@ -82,16 +82,7 @@ class SearchResultsAdapter( ImageHelper.loadImage(item.thumbnail, thumbnail) thumbnailDuration.setFormattedDuration(item.duration, item.isShort) videoTitle.text = item.title - - val viewsString = item.views.takeIf { it != -1L }?.formatShort().orEmpty() - val uploadDate = item.uploaded.takeIf { it > 0 }?.let { - " ${TextUtils.SEPARATOR} ${TextUtils.formatRelativeDate(root.context, it)}" - }.orEmpty() - videoInfo.text = root.context.getString( - R.string.normal_views, - viewsString, - uploadDate - ) + videoInfo.text = TextUtils.formatViewsString(root.context, item.views, item.uploaded) channelName.text = item.uploaderName ImageHelper.loadImage(item.uploaderAvatar, channelImage, true) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt index 5fd1cd5f7..31eecdd8f 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt @@ -11,7 +11,6 @@ import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.LayoutManager -import com.github.libretube.R import com.github.libretube.api.obj.StreamItem import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys @@ -20,7 +19,6 @@ import com.github.libretube.databinding.TrendingRowBinding import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.extensions.ceilHalf import com.github.libretube.extensions.dpToPx -import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.toID import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.NavigationHelper @@ -102,10 +100,6 @@ class VideosAdapter( val activity = (context as BaseActivity) val fragmentManager = activity.supportFragmentManager - val uploadDateStr = video.uploaded.takeIf { it > 0 } - ?.let { TextUtils.formatRelativeDate(context, it) } - ?.toString() - // Trending layout holder.trendingRowBinding?.apply { // set a fixed width for better visuals @@ -116,16 +110,8 @@ class VideosAdapter( } textViewTitle.text = video.title - textViewChannel.text = if ((video.views ?: 0L) > 0L) { - root.context.getString( - R.string.trending_views, - video.uploaderName, - video.views.formatShort(), - uploadDateStr - ) - } else { - "${video.uploaderName} ${TextUtils.SEPARATOR} $uploadDateStr" - } + textViewChannel.text = TextUtils.formatViewsString(root.context, video.views ?: -1, video.uploaded, video.uploaderName) + video.duration?.let { thumbnailDuration.setFormattedDuration(it, video.isShort) } channelImage.setOnClickListener { NavigationHelper.navigateChannel(root.context, video.uploaderUrl) @@ -153,15 +139,9 @@ class VideosAdapter( // Normal videos row layout holder.videoRowBinding?.apply { videoTitle.text = video.title - - videoInfo.text = root.context.getString( - R.string.normal_views, - video.views.formatShort(), - uploadDateStr?.let { " ${TextUtils.SEPARATOR} $it" } - ) + videoInfo.text = TextUtils.formatViewsString(root.context, video.views ?: -1, video.uploaded) thumbnailDuration.text = video.duration?.let { DateUtils.formatElapsedTime(it) } - ImageHelper.loadImage(video.thumbnail, thumbnail) if (forceMode != LayoutMode.CHANNEL_ROW) { diff --git a/app/src/main/java/com/github/libretube/ui/views/DescriptionLayout.kt b/app/src/main/java/com/github/libretube/ui/views/DescriptionLayout.kt index ec5361dca..51c075713 100644 --- a/app/src/main/java/com/github/libretube/ui/views/DescriptionLayout.kt +++ b/app/src/main/java/com/github/libretube/ui/views/DescriptionLayout.kt @@ -20,8 +20,6 @@ import com.github.libretube.ui.activities.VideoTagsAdapter import com.github.libretube.util.HtmlParser import com.github.libretube.util.LinkHandler import com.github.libretube.util.TextUtils -import kotlinx.datetime.TimeZone -import kotlinx.datetime.toLocalDateTime import java.util.Locale class DescriptionLayout( @@ -48,7 +46,7 @@ class DescriptionLayout( val views = streams.views.formatShort() binding.run { - playerViewsInfo.text = context.getString(R.string.normal_views, views, localizeDate(streams)) + playerViewsInfo.text = context.getString(R.string.normal_views, views, TextUtils.formatRelativeDate(context, streams.uploaded ?: -1L)) textLike.text = streams.likes.formatShort() textDislike.isVisible = streams.dislikes >= 0 @@ -119,7 +117,7 @@ class DescriptionLayout( // show exact view count "%,d".format(streams.views) } - val viewInfo = context.getString(R.string.normal_views, views, localizeDate(streams)) + val viewInfo = context.getString(R.string.normal_views, views, TextUtils.formatRelativeDate(context, streams.uploaded ?: -1L)) if (binding.descLinLayout.isVisible) { // hide the description and chapters binding.playerDescriptionArrow.animate().rotation( @@ -147,14 +145,6 @@ class DescriptionLayout( binding.playerViewsInfo.text = viewInfo } - private fun localizeDate(streams: Streams): String { - if (streams.livestream || streams.uploadTimestamp == null) return "" - - val date = streams.uploadTimestamp.toLocalDateTime(TimeZone.currentSystemDefault()).date - - return TextUtils.SEPARATOR + TextUtils.localizeDate(date) - } - companion object { private const val ANIMATION_DURATION = 250L } diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index 0ee90747e..1d00707b3 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -7,6 +7,7 @@ import android.os.Build import android.text.format.DateUtils import com.github.libretube.BuildConfig import com.github.libretube.R +import com.github.libretube.extensions.formatShort import com.google.common.math.IntMath.pow import kotlinx.datetime.toJavaLocalDate import java.time.Instant @@ -146,4 +147,14 @@ object TextUtils { fun getUserAgent(context: Context): String { return "${context.packageName}/${BuildConfig.VERSION_NAME}" } + + fun formatViewsString(context: Context, views: Long, uploaded: Long, uploader: String? = null): String { + val viewsString = views.takeIf { it != -1L }?.formatShort()?.let { + context.getString(R.string.view_count, it) + } + val uploadDate = uploaded.takeIf { it > 0 }?.let { + formatRelativeDate(context, it) + } + return listOfNotNull(uploader, viewsString, uploadDate).joinToString(SEPARATOR) + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cb00a7392..8ed36e69f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -518,6 +518,7 @@ Directly fetch video playback information from YouTube without using Piped. Local Return Youtube Dislikes Directly fetch dislike information from https://returnyoutubedislikeapi.com + %1$s views Download Service