LibreTube/app/src/main/java/com/github/libretube/ChannelFragment.kt

140 lines
5.3 KiB
Kotlin
Raw Normal View History

2022-02-05 00:25:05 +05:30
package com.github.libretube
2022-02-06 10:08:50 +05:30
2022-02-05 00:25:05 +05:30
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
2022-02-05 19:39:50 +05:30
import android.widget.ScrollView
2022-02-05 00:25:05 +05:30
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
2022-02-05 21:10:29 +05:30
import androidx.recyclerview.widget.LinearLayoutManager
2022-02-05 15:00:29 +05:30
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.adapters.ChannelAdapter
2022-02-05 00:25:05 +05:30
import com.squareup.picasso.Picasso
import retrofit2.HttpException
import java.io.IOException
class ChannelFragment : Fragment() {
2022-02-05 21:20:16 +05:30
2022-02-05 00:25:05 +05:30
private var channel_id: String? = null
private val TAG = "ChannelFragment"
2022-02-05 22:40:43 +05:30
var nextPage: String? =null
2022-02-06 18:40:27 +05:30
var channelAdapter: ChannelAdapter? = null
var isLoading = true
2022-02-05 15:00:29 +05:30
2022-02-05 00:25:05 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
channel_id = it.getString("channel_id")
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_channel, container, false)
2022-02-05 22:40:43 +05:30
2022-02-05 00:25:05 +05:30
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-02-05 22:40:43 +05:30
2022-02-05 00:25:05 +05:30
channel_id = channel_id!!.replace("/channel/","")
view.findViewById<TextView>(R.id.channel_name).text=channel_id
2022-02-05 21:10:29 +05:30
val recyclerView = view.findViewById<RecyclerView>(R.id.channel_recView)
recyclerView.layoutManager = LinearLayoutManager(context)
2022-02-05 19:39:50 +05:30
2022-02-05 00:25:05 +05:30
fetchChannel(view)
2022-02-05 19:39:50 +05:30
2022-02-05 00:25:05 +05:30
}
private fun fetchChannel(view: View){
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getChannel(channel_id!!)
}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")
return@launchWhenCreated
}
2022-02-06 18:40:27 +05:30
nextPage = response.nextpage
isLoading=false
2022-02-05 00:25:05 +05:30
runOnUiThread {
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_description).text=response.description
val bannerImage = view.findViewById<ImageView>(R.id.channel_banner)
val channelImage = view.findViewById<ImageView>(R.id.channel_image)
Picasso.get().load(response.bannerUrl).into(bannerImage)
Picasso.get().load(response.avatarUrl).into(channelImage)
2022-02-05 19:39:50 +05:30
channelAdapter = ChannelAdapter(response.relatedStreams!!.toMutableList())
2022-02-05 21:10:29 +05:30
view.findViewById<RecyclerView>(R.id.channel_recView).adapter = channelAdapter
2022-02-05 19:39:50 +05:30
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
2022-02-06 18:40:27 +05:30
if(nextPage!=null && !isLoading){
isLoading=true
2022-02-06 10:08:50 +05:30
fetchNextPage()
}
2022-02-05 19:39:50 +05:30
} else {
//scroll view is not at bottom
}
}
}
}
}
run()
}
private fun fetchNextPage(){
fun run() {
2022-02-05 22:40:43 +05:30
2022-02-05 19:39:50 +05:30
lifecycleScope.launchWhenCreated {
val response = try {
2022-02-05 22:40:43 +05:30
RetrofitInstance.api.getChannelNextPage(channel_id!!,nextPage!!)
2022-02-05 19:39:50 +05:30
} 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
}
2022-02-05 22:40:43 +05:30
nextPage = response.nextpage
2022-02-06 18:40:27 +05:30
channelAdapter?.updateItems(response.relatedStreams!!)
isLoading=false
2022-02-05 19:39:50 +05:30
2022-02-05 00:25:05 +05:30
}
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
2022-02-05 22:40:43 +05:30
override fun onDestroyView() {
2022-02-06 18:40:27 +05:30
val scrollView = view?.findViewById<ScrollView>(R.id.channel_scrollView)
scrollView?.viewTreeObserver?.removeOnScrollChangedListener {
}
channelAdapter=null
2022-02-05 22:40:43 +05:30
view?.findViewById<RecyclerView>(R.id.channel_recView)?.adapter=null
super.onDestroyView()
}
2022-02-05 00:25:05 +05:30
}