Use Kotlinx Serialization with channel information.

This commit is contained in:
Isira Seneviratne 2023-01-19 05:31:01 +05:30
parent ef103284af
commit 371fbcd643
8 changed files with 41 additions and 41 deletions

View File

@ -1,17 +1,17 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
import com.fasterxml.jackson.annotation.JsonIgnoreProperties import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true) @Serializable
data class Channel( data class Channel(
var id: String? = null, val id: String,
var name: String? = null, val name: String,
var avatarUrl: String? = null, val avatarUrl: String,
var bannerUrl: String? = null, val bannerUrl: String,
var description: String? = null, val description: String,
var nextpage: String? = null, val nextpage: String? = null,
var subscriberCount: Long = 0, val subscriberCount: Long = 0,
var verified: Boolean = false, val verified: Boolean = false,
var relatedStreams: List<StreamItem>? = listOf(), val relatedStreams: List<StreamItem> = emptyList(),
var tabs: List<ChannelTab>? = listOf() val tabs: List<ChannelTab> = emptyList()
) )

View File

@ -1,9 +1,9 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
import com.fasterxml.jackson.annotation.JsonIgnoreProperties import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true) @Serializable
data class ChannelTab( data class ChannelTab(
val name: String? = null, val name: String,
val data: String? = null val data: String
) )

View File

@ -1,6 +1,9 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
import kotlinx.serialization.Serializable
@Serializable
data class ChannelTabResponse( data class ChannelTabResponse(
val content: List<ContentItem> = listOf(), val content: List<ContentItem> = emptyList(),
val nextpage: String? = null val nextpage: String? = null
) )

View File

@ -4,9 +4,9 @@ import kotlinx.serialization.Serializable
@Serializable @Serializable
data class ContentItem( data class ContentItem(
val url: String? = null, val url: String,
val type: String? = null, val type: String,
val thumbnail: String? = null, val thumbnail: String,
val uploaderName: String? = null, val uploaderName: String? = null,
val uploaded: Long? = null, val uploaded: Long? = null,
val shortDescription: String? = null, val shortDescription: String? = null,

View File

@ -98,7 +98,7 @@ class SearchAdapter(
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, item.url) NavigationHelper.navigateVideo(root.context, item.url)
} }
val videoId = item.url!!.toID() val videoId = item.url.toID()
val videoName = item.title!! val videoName = item.title!!
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsBottomSheet(videoId, videoName) VideoOptionsBottomSheet(videoId, videoName)
@ -135,12 +135,12 @@ class SearchAdapter(
} }
root.setOnLongClickListener { root.setOnLongClickListener {
ChannelOptionsBottomSheet(item.url!!.toID(), item.name) ChannelOptionsBottomSheet(item.url.toID(), item.name)
.show((root.context as BaseActivity).supportFragmentManager) .show((root.context as BaseActivity).supportFragmentManager)
true true
} }
binding.searchSubButton.setupSubscriptionButton(item.url?.toID(), item.name?.toID()) binding.searchSubButton.setupSubscriptionButton(item.url.toID(), item.name?.toID())
} }
} }
@ -158,7 +158,7 @@ class SearchAdapter(
} }
deletePlaylist.visibility = View.GONE deletePlaylist.visibility = View.GONE
root.setOnLongClickListener { root.setOnLongClickListener {
val playlistId = item.url!!.toID() val playlistId = item.url.toID()
val playlistName = item.name!! val playlistName = item.name!!
PlaylistOptionsBottomSheet(playlistId, playlistName, PlaylistType.PUBLIC) PlaylistOptionsBottomSheet(playlistId, playlistName, PlaylistType.PUBLIC)
.show( .show(

View File

@ -142,7 +142,7 @@ class ChannelFragment : BaseFragment() {
binding.channelShare.setOnClickListener { binding.channelShare.setOnClickListener {
val shareDialog = ShareDialog( val shareDialog = ShareDialog(
response.id!!.toID(), response.id.toID(),
ShareObjectType.CHANNEL, ShareObjectType.CHANNEL,
shareData shareData
) )
@ -169,10 +169,10 @@ class ChannelFragment : BaseFragment() {
R.string.subscribers, R.string.subscribers,
response.subscriberCount.formatShort() response.subscriberCount.formatShort()
) )
if (response.description?.trim() == "") { if (response.description.isBlank()) {
binding.channelDescription.visibility = View.GONE binding.channelDescription.visibility = View.GONE
} else { } else {
binding.channelDescription.text = response.description?.trim() binding.channelDescription.text = response.description.trim()
} }
binding.channelDescription.setOnClickListener { binding.channelDescription.setOnClickListener {
@ -186,13 +186,13 @@ class ChannelFragment : BaseFragment() {
// recyclerview of the videos by the channel // recyclerview of the videos by the channel
channelAdapter = VideosAdapter( channelAdapter = VideosAdapter(
response.relatedStreams.orEmpty().toMutableList(), response.relatedStreams.toMutableList(),
forceMode = VideosAdapter.Companion.ForceMode.CHANNEL forceMode = VideosAdapter.Companion.ForceMode.CHANNEL
) )
binding.channelRecView.adapter = channelAdapter binding.channelRecView.adapter = channelAdapter
} }
response.tabs?.let { setupTabs(it) } setupTabs(response.tabs)
} }
} }
@ -230,16 +230,13 @@ class ChannelFragment : BaseFragment() {
private fun loadTab(tab: ChannelTab) { private fun loadTab(tab: ChannelTab) {
scope.launch { scope.launch {
tab.data ?: return@launch
val response = try { val response = try {
RetrofitInstance.api.getChannelTab(tab.data) RetrofitInstance.api.getChannelTab(tab.data)
} catch (e: Exception) { } catch (e: Exception) {
return@launch return@launch
} }
val adapter = SearchAdapter( val adapter = SearchAdapter(response.content.toMutableList())
response.content.toMutableList()
)
runOnUiThread { runOnUiThread {
binding.channelRecView.adapter = adapter binding.channelRecView.adapter = adapter
@ -275,7 +272,7 @@ class ChannelFragment : BaseFragment() {
return@launchWhenCreated return@launchWhenCreated
} }
nextPage = response.nextpage nextPage = response.nextpage
channelAdapter?.insertItems(response.relatedStreams.orEmpty()) channelAdapter?.insertItems(response.relatedStreams)
isLoading = false isLoading = false
binding.channelRefresh.isRefreshing = false binding.channelRefresh.isRefreshing = false
} }
@ -291,9 +288,9 @@ class ChannelFragment : BaseFragment() {
) { ) {
scope.launch { scope.launch {
val newContent = try { val newContent = try {
RetrofitInstance.api.getChannelTab(tab.data ?: "", nextPage) RetrofitInstance.api.getChannelTab(tab.data, nextPage)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() Log.e(TAG(), "Exception: $e")
null null
} }
onNewNextPage.invoke(newContent?.nextpage) onNewNextPage.invoke(newContent?.nextpage)

View File

@ -20,7 +20,7 @@ import kotlinx.coroutines.runBlocking
*/ */
class ChannelOptionsBottomSheet( class ChannelOptionsBottomSheet(
private val channelId: String, private val channelId: String,
private val channelName: String? channelName: String?
) : BaseBottomSheet() { ) : BaseBottomSheet() {
private val shareData = ShareData(currentChannel = channelName) private val shareData = ShareData(currentChannel = channelName)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -44,7 +44,7 @@ class ChannelOptionsBottomSheet(
val channel = runBlocking { val channel = runBlocking {
RetrofitInstance.api.getChannel(channelId) RetrofitInstance.api.getChannel(channelId)
} }
channel.relatedStreams?.firstOrNull()?.url?.toID()?.let { channel.relatedStreams.firstOrNull()?.url?.toID()?.let {
NavigationHelper.navigateVideo( NavigationHelper.navigateVideo(
requireContext(), requireContext(),
it, it,
@ -60,7 +60,7 @@ class ChannelOptionsBottomSheet(
val channel = runBlocking { val channel = runBlocking {
RetrofitInstance.api.getChannel(channelId) RetrofitInstance.api.getChannel(channelId)
} }
channel.relatedStreams?.firstOrNull()?.url?.toID()?.let { channel.relatedStreams.firstOrNull()?.url?.toID()?.let {
BackgroundHelper.playOnBackground( BackgroundHelper.playOnBackground(
requireContext(), requireContext(),
videoId = it, videoId = it,

View File

@ -147,7 +147,7 @@ object PlayingQueue {
scope.launch { scope.launch {
while (channelNextPage != null) { while (channelNextPage != null) {
RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).apply { RetrofitInstance.api.getChannelNextPage(channelId, nextPage!!).apply {
add(*relatedStreams.orEmpty().toTypedArray()) add(*relatedStreams.toTypedArray())
channelNextPage = this.nextpage channelNextPage = this.nextpage
} }
} }
@ -158,7 +158,7 @@ object PlayingQueue {
scope.launch { scope.launch {
runCatching { runCatching {
val channel = RetrofitInstance.api.getChannel(channelId) val channel = RetrofitInstance.api.getChannel(channelId)
add(*channel.relatedStreams.orEmpty().toTypedArray()) add(*channel.relatedStreams.toTypedArray())
updateCurrent(newCurrentStream) updateCurrent(newCurrentStream)
if (channel.nextpage == null) return@launch if (channel.nextpage == null) return@launch
fetchMoreFromChannel(channelId, channel.nextpage) fetchMoreFromChannel(channelId, channel.nextpage)