2022-02-05 00:25:05 +05:30
|
|
|
package com.github.libretube
|
|
|
|
|
2022-02-13 17:23:04 +05:30
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.Context
|
2022-02-05 00:25:05 +05:30
|
|
|
import android.os.Bundle
|
|
|
|
import android.util.Log
|
|
|
|
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
|
2022-02-15 00:23:01 +05:30
|
|
|
import androidx.fragment.app.Fragment
|
2022-02-05 00:25:05 +05:30
|
|
|
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
|
2022-04-06 15:48:18 +05:30
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
2022-02-05 15:00:29 +05:30
|
|
|
import com.github.libretube.adapters.ChannelAdapter
|
2022-02-13 17:23:04 +05:30
|
|
|
import com.github.libretube.obj.Subscribe
|
|
|
|
import com.google.android.material.button.MaterialButton
|
2022-02-05 00:25:05 +05:30
|
|
|
import com.squareup.picasso.Picasso
|
2022-05-22 14:26:49 +05:30
|
|
|
import java.io.IOException
|
2022-05-22 15:29:35 +05:30
|
|
|
import retrofit2.HttpException
|
2022-02-05 00:25:05 +05:30
|
|
|
|
|
|
|
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-05-20 03:52:10 +05:30
|
|
|
var nextPage: String? = null
|
2022-02-06 18:40:27 +05:30
|
|
|
var channelAdapter: ChannelAdapter? = null
|
|
|
|
var isLoading = true
|
2022-05-20 03:52:10 +05:30
|
|
|
var isSubscribed: Boolean = false
|
2022-04-06 15:48:18 +05:30
|
|
|
private var refreshLayout: SwipeRefreshLayout? = null
|
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(
|
2022-05-20 03:52:10 +05:30
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
2022-02-05 00:25:05 +05:30
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
// Inflate the layout for this fragment
|
|
|
|
return inflater.inflate(R.layout.fragment_channel, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2022-02-05 22:40:43 +05:30
|
|
|
|
2022-05-20 03:52:10 +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-04-06 15:48:18 +05:30
|
|
|
refreshLayout = view.findViewById(R.id.channel_refresh)
|
|
|
|
|
|
|
|
val refreshChannel = {
|
|
|
|
refreshLayout?.isRefreshing = true
|
|
|
|
fetchChannel(view)
|
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
2022-02-13 23:05:51 +05:30
|
|
|
val subButton = view.findViewById<MaterialButton>(R.id.channel_subscribe)
|
2022-05-20 03:52:10 +05:30
|
|
|
if (sharedPref?.getString("token", "") != "") {
|
2022-04-06 15:48:18 +05:30
|
|
|
isSubscribed(subButton)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refreshChannel()
|
|
|
|
refreshLayout?.setOnRefreshListener {
|
|
|
|
refreshChannel()
|
2022-02-13 23:05:51 +05:30
|
|
|
}
|
2022-04-06 15:48:18 +05:30
|
|
|
|
2022-02-08 14:58:50 +05:30
|
|
|
val scrollView = view.findViewById<ScrollView>(R.id.channel_scrollView)
|
|
|
|
scrollView.viewTreeObserver
|
|
|
|
.addOnScrollChangedListener {
|
|
|
|
if (scrollView.getChildAt(0).bottom
|
2022-05-20 03:52:10 +05:30
|
|
|
== (scrollView.height + scrollView.scrollY)
|
|
|
|
) {
|
|
|
|
// scroll view is at bottom
|
|
|
|
if (nextPage != null && !isLoading) {
|
|
|
|
isLoading = true
|
|
|
|
refreshLayout?.isRefreshing = true
|
2022-02-08 14:58:50 +05:30
|
|
|
fetchNextPage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 17:23:04 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun isSubscribed(button: MaterialButton) {
|
2022-02-13 17:23:04 +05:30
|
|
|
@SuppressLint("ResourceAsColor")
|
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
2022-05-21 13:32:04 +05:30
|
|
|
RetrofitInstance.api.isSubscribed(
|
|
|
|
channel_id!!,
|
|
|
|
sharedPref?.getString("token", "")!!
|
|
|
|
)
|
2022-05-20 03:52:10 +05:30
|
|
|
} catch (e: IOException) {
|
2022-02-13 17:23:04 +05:30
|
|
|
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-15 00:23:01 +05:30
|
|
|
|
2022-02-13 17:23:04 +05:30
|
|
|
runOnUiThread {
|
2022-05-20 03:52:10 +05:30
|
|
|
if (response.subscribed == true) {
|
|
|
|
isSubscribed = true
|
|
|
|
button.text = getString(R.string.unsubscribe)
|
2022-02-13 17:23:04 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
if (response.subscribed != null) {
|
|
|
|
button.setOnClickListener {
|
|
|
|
if (isSubscribed) {
|
|
|
|
unsubscribe()
|
|
|
|
button.text = getString(R.string.subscribe)
|
|
|
|
} else {
|
|
|
|
subscribe()
|
|
|
|
button.text = getString(R.string.unsubscribe)
|
|
|
|
}
|
2022-02-13 17:23:04 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
}
|
2022-02-13 17:23:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun subscribe() {
|
2022-02-13 17:23:04 +05:30
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
2022-05-21 13:32:04 +05:30
|
|
|
RetrofitInstance.api.subscribe(
|
|
|
|
sharedPref?.getString("token", "")!!,
|
|
|
|
Subscribe(channel_id)
|
|
|
|
)
|
2022-05-20 03:52:10 +05:30
|
|
|
} catch (e: IOException) {
|
2022-02-13 17:23:04 +05:30
|
|
|
println(e)
|
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
|
|
|
Log.e(TAG, "HttpException, unexpected response$e")
|
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
isSubscribed = true
|
2022-02-13 17:23:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun unsubscribe() {
|
2022-02-13 17:23:04 +05:30
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
2022-05-21 13:32:04 +05:30
|
|
|
RetrofitInstance.api.unsubscribe(
|
|
|
|
sharedPref?.getString("token", "")!!,
|
|
|
|
Subscribe(channel_id)
|
|
|
|
)
|
2022-05-20 03:52:10 +05:30
|
|
|
} catch (e: IOException) {
|
2022-02-13 17:23:04 +05:30
|
|
|
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-05-20 03:52:10 +05:30
|
|
|
isSubscribed = false
|
2022-02-13 17:23:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
2022-02-05 00:25:05 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun fetchChannel(view: View) {
|
2022-02-05 00:25:05 +05:30
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
|
|
|
RetrofitInstance.api.getChannel(channel_id!!)
|
2022-05-20 03:52:10 +05:30
|
|
|
} catch (e: IOException) {
|
|
|
|
refreshLayout?.isRefreshing = false
|
2022-02-05 00:25:05 +05:30
|
|
|
println(e)
|
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
2022-05-20 03:52:10 +05:30
|
|
|
refreshLayout?.isRefreshing = false
|
2022-02-05 00:25:05 +05:30
|
|
|
Log.e(TAG, "HttpException, unexpected response")
|
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
2022-02-06 18:40:27 +05:30
|
|
|
nextPage = response.nextpage
|
2022-05-20 03:52:10 +05:30
|
|
|
isLoading = false
|
|
|
|
refreshLayout?.isRefreshing = false
|
2022-02-05 00:25:05 +05:30
|
|
|
runOnUiThread {
|
2022-04-06 15:48:18 +05:30
|
|
|
view.findViewById<ScrollView>(R.id.channel_scrollView).visibility = View.VISIBLE
|
2022-05-18 14:09:57 +05:30
|
|
|
val channelName = view.findViewById<TextView>(R.id.channel_name)
|
2022-05-22 14:26:49 +05:30
|
|
|
channelName.text = response.name
|
|
|
|
if (response.verified) {
|
2022-05-22 15:29:35 +05:30
|
|
|
channelName.setCompoundDrawablesWithIntrinsicBounds(
|
|
|
|
0, 0, R.drawable.ic_verified, 0
|
|
|
|
)
|
2022-05-22 14:26:49 +05:30
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
view.findViewById<TextView>(R.id.channel_subs).text = resources.getString(
|
|
|
|
R.string.subscribers,
|
|
|
|
response.subscriberCount.formatShort()
|
|
|
|
)
|
2022-05-18 14:00:35 +05:30
|
|
|
val channelDescription = view.findViewById<TextView>(R.id.channel_description)
|
2022-05-21 13:32:04 +05:30
|
|
|
if (response.description?.trim() == "")
|
|
|
|
channelDescription.visibility = View.GONE
|
|
|
|
else
|
|
|
|
channelDescription.text = response.description?.trim()
|
2022-02-05 00:25:05 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-05-20 03:52:10 +05:30
|
|
|
private fun fetchNextPage() {
|
2022-02-05 19:39:50 +05:30
|
|
|
fun run() {
|
2022-02-05 22:40:43 +05:30
|
|
|
|
2022-02-05 19:39:50 +05:30
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
2022-05-20 03:52:10 +05:30
|
|
|
RetrofitInstance.api.getChannelNextPage(channel_id!!, nextPage!!)
|
2022-02-05 19:39:50 +05:30
|
|
|
} catch (e: IOException) {
|
2022-04-12 23:55:08 +05:30
|
|
|
refreshLayout?.isRefreshing = false
|
2022-02-05 19:39:50 +05:30
|
|
|
println(e)
|
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
2022-04-12 23:55:08 +05:30
|
|
|
refreshLayout?.isRefreshing = false
|
2022-05-20 03:52:10 +05:30
|
|
|
Log.e(TAG, "HttpException, unexpected response," + e.response())
|
2022-02-05 19:39:50 +05:30
|
|
|
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!!)
|
2022-05-20 03:52:10 +05:30
|
|
|
isLoading = false
|
2022-04-12 23:55:08 +05:30
|
|
|
refreshLayout?.isRefreshing = false
|
2022-02-05 00:25:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-02-05 00:25:05 +05:30
|
|
|
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 {
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
channelAdapter = null
|
|
|
|
view?.findViewById<RecyclerView>(R.id.channel_recView)?.adapter = null
|
2022-02-05 22:40:43 +05:30
|
|
|
super.onDestroyView()
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
}
|