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
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class ContentItem(
var url: String? = null,
val url: String? = null,
val type: String? = null,
var thumbnail: String? = null,
var uploaderName: String? = null,
var uploaded: Long? = null,
var shortDescription: String? = null,
val thumbnail: String? = null,
val uploaderName: String? = null,
val uploaded: Long? = null,
val shortDescription: String? = null,
// Video only attributes
var title: String? = null,
var uploaderUrl: String? = null,
var uploaderAvatar: String? = null,
var uploadedDate: String? = null,
var duration: Long? = null,
var views: Long? = null,
var isShort: Boolean? = null,
var uploaderVerified: Boolean? = null,
val title: String? = null,
val uploaderUrl: String? = null,
val uploaderAvatar: String? = null,
val uploadedDate: String? = null,
val duration: Long = -1,
val views: Long = -1,
val isShort: Boolean? = null,
val uploaderVerified: Boolean? = null,
// Channel and Playlist attributes
var name: String? = null,
var description: String? = null,
var subscribers: Long? = -1,
var videos: Long? = -1,
var verified: Boolean? = null
val name: String? = null,
val description: String? = null,
val subscribers: Long = -1,
val videos: Long = -1,
val verified: Boolean? = null
)

View File

@ -1,11 +1,11 @@
package com.github.libretube.api.obj
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class SearchResult(
val items: MutableList<ContentItem>? = arrayListOf(),
val items: List<ContentItem> = emptyList(),
val nextpage: String? = null,
val suggestion: String? = "",
val suggestion: String? = null,
val corrected: Boolean? = null
)

View File

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

View File

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