Use Kotlinx Serialization with search results.

This commit is contained in:
Isira Seneviratne 2023-01-18 07:33:25 +05:30
parent 219a7d7cfe
commit 165e0677d4
4 changed files with 35 additions and 38 deletions

View File

@ -1,28 +1,28 @@
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 ContentItem( data class ContentItem(
var url: String? = null, val url: String? = null,
val type: String? = null, val type: String? = null,
var thumbnail: String? = null, val thumbnail: String? = null,
var uploaderName: String? = null, val uploaderName: String? = null,
var uploaded: Long? = null, val uploaded: Long? = null,
var shortDescription: String? = null, val shortDescription: String? = null,
// Video only attributes // Video only attributes
var title: String? = null, val title: String? = null,
var uploaderUrl: String? = null, val uploaderUrl: String? = null,
var uploaderAvatar: String? = null, val uploaderAvatar: String? = null,
var uploadedDate: String? = null, val uploadedDate: String? = null,
var duration: Long? = null, val duration: Long = -1,
var views: Long? = null, val views: Long = -1,
var isShort: Boolean? = null, val isShort: Boolean? = null,
var uploaderVerified: Boolean? = null, val uploaderVerified: Boolean? = null,
// Channel and Playlist attributes // Channel and Playlist attributes
var name: String? = null, val name: String? = null,
var description: String? = null, val description: String? = null,
var subscribers: Long? = -1, val subscribers: Long = -1,
var videos: Long? = -1, val videos: Long = -1,
var verified: Boolean? = null val verified: Boolean? = null
) )

View File

@ -1,11 +1,11 @@
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 SearchResult( data class SearchResult(
val items: MutableList<ContentItem>? = arrayListOf(), val items: List<ContentItem> = emptyList(),
val nextpage: String? = null, val nextpage: String? = null,
val suggestion: String? = "", val suggestion: String? = null,
val corrected: Boolean? = null val corrected: Boolean? = null
) )

View File

@ -83,13 +83,13 @@ class SearchAdapter(
private fun bindWatch(item: ContentItem, binding: VideoRowBinding) { private fun bindWatch(item: ContentItem, binding: VideoRowBinding) {
binding.apply { binding.apply {
ImageHelper.loadImage(item.thumbnail, thumbnail) ImageHelper.loadImage(item.thumbnail, thumbnail)
thumbnailDuration.setFormattedDuration(item.duration!!) thumbnailDuration.setFormattedDuration(item.duration)
ImageHelper.loadImage(item.uploaderAvatar, channelImage) ImageHelper.loadImage(item.uploaderAvatar, channelImage)
videoTitle.text = item.title videoTitle.text = item.title
val viewsString = if (item.views?.toInt() != -1) item.views.formatShort() else "" val viewsString = if (item.views != -1L) item.views.formatShort() else ""
val uploadDate = if (item.uploadedDate != null) item.uploadedDate else "" val uploadDate = item.uploadedDate.orEmpty()
videoInfo.text = videoInfo.text =
if (viewsString != "" && uploadDate != "") { if (viewsString.isNotEmpty() && uploadDate.isNotEmpty()) {
"$viewsString$uploadDate" "$viewsString$uploadDate"
} else { } else {
viewsString + uploadDate viewsString + uploadDate
@ -111,7 +111,7 @@ class SearchAdapter(
channelContainer.setOnClickListener { channelContainer.setOnClickListener {
NavigationHelper.navigateChannel(root.context, item.uploaderUrl) NavigationHelper.navigateChannel(root.context, item.uploaderUrl)
} }
watchProgress.setWatchProgressLength(videoId, item.duration!!) watchProgress.setWatchProgressLength(videoId, item.duration)
} }
} }
@ -150,7 +150,7 @@ class SearchAdapter(
) { ) {
binding.apply { binding.apply {
ImageHelper.loadImage(item.thumbnail, playlistThumbnail) ImageHelper.loadImage(item.thumbnail, playlistThumbnail)
if (item.videos?.toInt() != -1) videoCount.text = item.videos.toString() if (item.videos != -1L) videoCount.text = item.videos.toString()
playlistTitle.text = item.name playlistTitle.text = item.name
playlistDescription.text = item.uploaderName playlistDescription.text = item.uploaderName
root.setOnClickListener { root.setOnClickListener {

View File

@ -5,6 +5,7 @@ import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
@ -94,13 +95,9 @@ class SearchResultFragment : BaseFragment() {
return@launchWhenCreated return@launchWhenCreated
} }
runOnUiThread { runOnUiThread {
searchAdapter = SearchAdapter(response.items.orEmpty().toMutableList()) searchAdapter = SearchAdapter(response.items.toMutableList())
binding.searchRecycler.adapter = searchAdapter binding.searchRecycler.adapter = searchAdapter
binding.noSearchResult.visibility = if (response.items.orEmpty().isEmpty()) { binding.noSearchResult.isVisible = response.items.isEmpty()
View.VISIBLE
} else {
View.GONE
}
} }
nextPage = response.nextpage nextPage = response.nextpage
} }
@ -124,8 +121,8 @@ class SearchResultFragment : BaseFragment() {
} }
nextPage = response.nextpage!! nextPage = response.nextpage!!
kotlin.runCatching { kotlin.runCatching {
if (response.items?.isNotEmpty() == true) { if (response.items.isNotEmpty()) {
searchAdapter.updateItems(response.items.toMutableList()) searchAdapter.updateItems(response.items)
} }
} }
} }