Skip to content

Commit

Permalink
Merge pull request #3276 from Isira-Seneviratne/Improve_count_formatting
Browse files Browse the repository at this point in the history
Improve count formatting.
  • Loading branch information
Bnyro authored Mar 10, 2023
2 parents e3a648d + d816612 commit 95bd8b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
23 changes: 10 additions & 13 deletions app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.libretube.ui.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -24,7 +23,6 @@ import com.github.libretube.ui.sheets.ChannelOptionsBottomSheet
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.ui.sheets.VideoOptionsBottomSheet
import com.github.libretube.ui.viewholders.SearchViewHolder
import com.github.libretube.util.TextUtils

class SearchAdapter : ListAdapter<ContentItem, SearchViewHolder>(SearchCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
Expand Down Expand Up @@ -104,22 +102,21 @@ class SearchAdapter : ListAdapter<ContentItem, SearchViewHolder>(SearchCallback)
}
}

@SuppressLint("SetTextI18n")
private fun bindChannel(item: ContentItem, binding: ChannelRowBinding) {
binding.apply {
ImageHelper.loadImage(item.thumbnail, searchChannelImage)
searchChannelName.text = item.name

searchViews.text = listOfNotNull(
root.context.getString(
R.string.subscribers,
item.subscribers.formatShort()
).takeIf { item.subscribers >= 0 },
root.context.getString(
R.string.videoCount,
item.videos.toString()
).takeIf { item.videos >= 0 }
).joinToString(TextUtils.SEPARATOR)
val subscribers = item.subscribers.formatShort()
searchViews.text = if (item.subscribers >= 0 && item.videos >= 0) {
root.context.getString(R.string.subscriberAndVideoCounts, subscribers, item.videos)
} else if (item.subscribers >= 0) {
root.context.getString(R.string.subscribers, subscribers)
} else if (item.videos >= 0) {
root.context.getString(R.string.videoCount, item.videos)
} else {
""
}

root.setOnClickListener {
NavigationHelper.navigateChannel(root.context, item.url)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.libretube.ui.fragments

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
Expand All @@ -16,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Playlist
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.FragmentPlaylistBinding
Expand All @@ -31,7 +31,6 @@ import com.github.libretube.ui.adapters.PlaylistAdapter
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.TextUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -100,7 +99,6 @@ class PlaylistFragment : Fragment() {
)
}

@SuppressLint("SetTextI18n")
private fun fetchPlaylist() {
binding.playlistScrollview.visibility = View.GONE
lifecycleScope.launchWhenCreated {
Expand All @@ -126,9 +124,7 @@ class PlaylistFragment : Fragment() {
if (binding.playlistName.maxLines == 2) Int.MAX_VALUE else 2
}

binding.playlistInfo.text =
(if (response.uploader != null) response.uploader + TextUtils.SEPARATOR else "") +
getString(R.string.videoCount, response.videos.toString())
binding.playlistInfo.text = getChannelAndVideoString(response, response.videos)

// show playlist options
binding.optionsMenu.setOnClickListener {
Expand Down Expand Up @@ -196,18 +192,8 @@ class PlaylistFragment : Fragment() {
)
}

val info = binding.playlistInfo.text.split(TextUtils.SEPARATOR)
binding.playlistInfo.text = (
if (info.size == 2) {
info[0] + TextUtils.SEPARATOR
} else {
""
}
) + getString(
R.string.videoCount,
playlistAdapter!!.itemCount.toString()
)
super.onItemRangeRemoved(positionStart, itemCount)
binding.playlistInfo.text =
getChannelAndVideoString(response, playlistFeed.size)
}
})

Expand Down Expand Up @@ -269,6 +255,12 @@ class PlaylistFragment : Fragment() {
}
}

private fun getChannelAndVideoString(playlist: Playlist, count: Int): String {
return playlist.uploader?.let {
getString(R.string.uploaderAndVideoCount, it, count)
} ?: getString(R.string.videoCount, count)
}

private fun fetchNextPage() {
if (nextPage == null || isLoading) return
isLoading = true
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
<string name="instance">Instance</string>
<string name="customization">Adjustments</string>
<string name="website">Website</string>
<string name="videoCount">%1$s videos</string>
<string name="uploaderAndVideoCount">%1$s • %2$d videos</string>
<string name="subscriberAndVideoCounts">%1$s subscribers • %2$d videos</string>
<string name="videoCount">%1$d videos</string>
<string name="noInternet">Connect to the Internet first.</string>
<string name="retry">Retry</string>
<string name="comments">Comments</string>
Expand Down

0 comments on commit 95bd8b8

Please sign in to comment.