channel fragment load more

This commit is contained in:
rimthekid 2022-02-05 18:09:50 +04:00
parent 8782a4af38
commit fdca8cb0af
5 changed files with 61 additions and 4 deletions

View File

@ -1,5 +1,7 @@
package com.github.libretube package com.github.libretube
import android.net.Uri.encode
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -7,7 +9,9 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.ScrollView
import android.widget.TextView import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -16,6 +20,8 @@ import com.github.libretube.adapters.TrendingAdapter
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
// TODO: Rename parameter arguments, choose names that match // TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@ -30,6 +36,8 @@ class ChannelFragment : Fragment() {
private var channel_id: String? = null private var channel_id: String? = null
private val TAG = "ChannelFragment" private val TAG = "ChannelFragment"
lateinit var recyclerView: RecyclerView lateinit var recyclerView: RecyclerView
lateinit var nextPage: String
lateinit var channelAdapter: ChannelAdapter
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -52,7 +60,9 @@ class ChannelFragment : Fragment() {
view.findViewById<TextView>(R.id.channel_name).text=channel_id view.findViewById<TextView>(R.id.channel_name).text=channel_id
recyclerView = view.findViewById(R.id.channel_recView) recyclerView = view.findViewById(R.id.channel_recView)
recyclerView.layoutManager = GridLayoutManager(view.context, 1) recyclerView.layoutManager = GridLayoutManager(view.context, 1)
fetchChannel(view) fetchChannel(view)
} }
private fun fetchChannel(view: View){ private fun fetchChannel(view: View){
@ -68,6 +78,7 @@ class ChannelFragment : Fragment() {
Log.e(TAG, "HttpException, unexpected response") Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated return@launchWhenCreated
} }
nextPage = response.nextpage!!
runOnUiThread { runOnUiThread {
view.findViewById<TextView>(R.id.channel_name).text=response.name view.findViewById<TextView>(R.id.channel_name).text=response.name
view.findViewById<TextView>(R.id.channel_subs).text=response.subscriberCount.videoViews() + " subscribers" view.findViewById<TextView>(R.id.channel_subs).text=response.subscriberCount.videoViews() + " subscribers"
@ -76,7 +87,44 @@ class ChannelFragment : Fragment() {
val channelImage = view.findViewById<ImageView>(R.id.channel_image) val channelImage = view.findViewById<ImageView>(R.id.channel_image)
Picasso.get().load(response.bannerUrl).into(bannerImage) Picasso.get().load(response.bannerUrl).into(bannerImage)
Picasso.get().load(response.avatarUrl).into(channelImage) Picasso.get().load(response.avatarUrl).into(channelImage)
recyclerView.adapter = ChannelAdapter(response.relatedStreams!!) channelAdapter = ChannelAdapter(response.relatedStreams!!.toMutableList())
recyclerView.adapter = channelAdapter
val scrollView = view.findViewById<ScrollView>(R.id.channel_scrollView)
scrollView.viewTreeObserver
.addOnScrollChangedListener {
if (scrollView.getChildAt(0).bottom
== (scrollView.height + scrollView.scrollY)) {
//scroll view is at bottom
println("suck a dick: "+channel_id+"?nextpage="+nextPage)
fetchNextPage()
} else {
//scroll view is not at bottom
}
}
}
}
}
run()
}
private fun fetchNextPage(){
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getChannelNextPage(channel_id!!,nextPage)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response,"+e.response())
return@launchWhenCreated
}
nextPage = response.nextpage!!
runOnUiThread {
channelAdapter.updateItems(response.relatedStreams!!)
} }
} }
} }

View File

@ -26,4 +26,7 @@ interface PipedApi {
@GET("channel/{channelId}") @GET("channel/{channelId}")
suspend fun getChannel(@Path("channelId") channelId: String): Channel suspend fun getChannel(@Path("channelId") channelId: String): Channel
@GET("nextpage/channel/{channelId}")
suspend fun getChannelNextPage(@Path("channelId") channelId: String, @Query("nextpage") nextPage: String): Channel
} }

View File

@ -16,10 +16,15 @@ import com.github.libretube.R
import com.github.libretube.obj.StreamItem import com.github.libretube.obj.StreamItem
import com.github.libretube.videoViews import com.github.libretube.videoViews
class ChannelAdapter(private val videoFeed: List<StreamItem>): RecyclerView.Adapter<ChannelViewHolder>() { class ChannelAdapter(private val videoFeed: MutableList<StreamItem>): RecyclerView.Adapter<ChannelViewHolder>() {
override fun getItemCount(): Int { override fun getItemCount(): Int {
return videoFeed.size return videoFeed.size
} }
fun updateItems(newItems: List<StreamItem>){
videoFeed.addAll(newItems)
println("suck another dick: "+newItems[0].title)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)

View File

@ -9,5 +9,5 @@ data class Playlist(
var uploaderUrl: String? = null, var uploaderUrl: String? = null,
var uploaderAvatar: String? = null, var uploaderAvatar: String? = null,
var videos: Int = 0, var videos: Int = 0,
var relatedStreams: List<StreamItem?>? = null, var relatedStreams: List<StreamItem>? = null,
) )

View File

@ -4,7 +4,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
tools:context=".ChannelFragment"> tools:context=".ChannelFragment"
android:id="@+id/channel_scrollView">
<LinearLayout <LinearLayout
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"