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

240 lines
8.7 KiB
Kotlin
Raw Normal View History

2022-06-03 00:40:16 +05:30
package com.github.libretube.fragments
2022-02-05 00:25:05 +05:30
2022-02-13 17:23:04 +05:30
import android.annotation.SuppressLint
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 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-06-03 00:40:16 +05:30
import com.github.libretube.R
2022-02-05 15:00:29 +05:30
import com.github.libretube.adapters.ChannelAdapter
2022-07-01 14:41:24 +05:30
import com.github.libretube.databinding.FragmentChannelBinding
2022-02-13 17:23:04 +05:30
import com.github.libretube.obj.Subscribe
2022-07-02 21:53:24 +05:30
import com.github.libretube.preferences.PreferenceHelper
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
2022-06-10 18:33:48 +05:30
import com.github.libretube.util.formatShort
2022-02-05 00:25:05 +05:30
import com.squareup.picasso.Picasso
2022-05-22 15:29:35 +05:30
import retrofit2.HttpException
2022-06-24 20:56:36 +05:30
import java.io.IOException
2022-02-05 00:25:05 +05:30
class ChannelFragment : Fragment() {
private val TAG = "ChannelFragment"
2022-07-01 14:41:24 +05:30
private lateinit var binding: FragmentChannelBinding
private var channelId: String? = null
2022-05-20 03:52:10 +05:30
var nextPage: String? = null
2022-07-01 14:41:24 +05:30
private var channelAdapter: ChannelAdapter? = null
private var isLoading = true
private var isSubscribed: Boolean = false
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 {
2022-07-01 14:41:24 +05:30
channelId = it.getString("channel_id")
2022-02-05 00:25:05 +05:30
}
}
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?
2022-07-01 14:41:24 +05:30
): View {
binding = FragmentChannelBinding.inflate(layoutInflater, container, false)
return binding.root
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-07-01 14:41:24 +05:30
channelId = channelId!!.replace("/channel/", "")
binding.channelName.text = channelId
binding.channelRecView.layoutManager = LinearLayoutManager(context)
val refreshChannel = {
2022-07-01 14:41:24 +05:30
binding.channelRefresh.isRefreshing = true
fetchChannel()
2022-06-26 14:38:10 +05:30
if (PreferenceHelper.getToken(requireContext()) != "") {
2022-07-11 19:55:47 +05:30
isSubscribed()
}
}
refreshChannel()
2022-07-01 14:41:24 +05:30
binding.channelRefresh.setOnRefreshListener {
refreshChannel()
2022-02-13 23:05:51 +05:30
}
2022-07-01 14:41:24 +05:30
binding.channelScrollView.viewTreeObserver
2022-02-08 14:58:50 +05:30
.addOnScrollChangedListener {
2022-07-01 14:41:24 +05:30
if (binding.channelScrollView.getChildAt(0).bottom
== (binding.channelScrollView.height + binding.channelScrollView.scrollY)
2022-05-20 03:52:10 +05:30
) {
// scroll view is at bottom
if (nextPage != null && !isLoading) {
isLoading = true
2022-07-01 14:41:24 +05:30
binding.channelRefresh.isRefreshing = true
2022-02-08 14:58:50 +05:30
fetchNextPage()
}
}
}
2022-02-13 17:23:04 +05:30
}
2022-07-11 19:55:47 +05:30
private fun isSubscribed() {
2022-02-13 17:23:04 +05:30
@SuppressLint("ResourceAsColor")
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
2022-06-26 14:38:10 +05:30
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.isSubscribed(
2022-07-01 14:41:24 +05:30
channelId!!,
2022-06-26 14:38:10 +05:30
token
2022-05-21 13:32:04 +05:30
)
2022-07-11 19:55:47 +05:30
} catch (e: Exception) {
Log.e(TAG, e.toString())
2022-02-13 17:23:04 +05:30
return@launchWhenCreated
}
2022-02-13 17:23:04 +05:30
runOnUiThread {
2022-05-20 03:52:10 +05:30
if (response.subscribed == true) {
isSubscribed = true
2022-07-11 19:55:47 +05:30
binding.channelSubscribe.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) {
2022-07-11 19:55:47 +05:30
binding.channelSubscribe.apply {
setOnClickListener {
text = if (isSubscribed) {
unsubscribe()
getString(R.string.subscribe)
} else {
subscribe()
getString(R.string.unsubscribe)
}
2022-05-20 03:52:10 +05:30
}
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 {
2022-07-11 19:55:47 +05:30
try {
2022-06-26 14:38:10 +05:30
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.subscribe(
2022-06-26 14:38:10 +05:30
token,
2022-07-01 14:41:24 +05:30
Subscribe(channelId)
2022-05-21 13:32:04 +05:30
)
2022-07-11 19:55:47 +05:30
} catch (e: Exception) {
Log.e(TAG, e.toString())
2022-02-13 17:23:04 +05:30
}
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 {
2022-07-11 19:55:47 +05:30
try {
2022-06-26 14:38:10 +05:30
val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.unsubscribe(
2022-06-26 14:38:10 +05:30
token,
2022-07-01 14:41:24 +05:30
Subscribe(channelId)
2022-05-21 13:32:04 +05:30
)
2022-07-11 19:55:47 +05:30
} catch (e: Exception) {
Log.e(TAG, e.toString())
2022-02-13 17:23:04 +05:30
}
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-07-01 14:41:24 +05:30
private fun fetchChannel() {
2022-02-05 00:25:05 +05:30
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
2022-07-01 14:41:24 +05:30
RetrofitInstance.api.getChannel(channelId!!)
2022-05-20 03:52:10 +05:30
} catch (e: IOException) {
2022-07-01 14:41:24 +05:30
binding.channelRefresh.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-07-01 14:41:24 +05:30
binding.channelRefresh.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
2022-07-01 14:41:24 +05:30
binding.channelRefresh.isRefreshing = false
2022-02-05 00:25:05 +05:30
runOnUiThread {
2022-07-01 14:41:24 +05:30
binding.channelScrollView.visibility = View.VISIBLE
binding.channelName.text = response.name
2022-05-22 14:26:49 +05:30
if (response.verified) {
2022-07-01 14:41:24 +05:30
binding.channelName.setCompoundDrawablesWithIntrinsicBounds(
2022-06-24 20:56:36 +05:30
0,
0,
R.drawable.ic_verified,
0
2022-05-22 15:29:35 +05:30
)
2022-05-22 14:26:49 +05:30
}
2022-07-01 14:41:24 +05:30
binding.channelSubs.text = resources.getString(
2022-05-21 13:32:04 +05:30
R.string.subscribers,
response.subscriberCount.formatShort()
)
2022-05-29 16:03:10 +05:30
if (response.description?.trim() == "") {
2022-07-01 14:41:24 +05:30
binding.channelDescription.visibility = View.GONE
2022-05-29 16:03:10 +05:30
} else {
2022-07-01 14:41:24 +05:30
binding.channelDescription.text = response.description?.trim()
2022-05-29 16:03:10 +05:30
}
2022-07-01 14:41:24 +05:30
Picasso.get().load(response.bannerUrl).into(binding.channelBanner)
Picasso.get().load(response.avatarUrl).into(binding.channelImage)
channelAdapter = ChannelAdapter(
response.relatedStreams!!.toMutableList(),
childFragmentManager
)
2022-07-01 14:41:24 +05:30
binding.channelRecView.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() {
lifecycleScope.launchWhenCreated {
val response = try {
2022-07-01 14:41:24 +05:30
RetrofitInstance.api.getChannelNextPage(channelId!!, nextPage!!)
2022-02-05 19:39:50 +05:30
} catch (e: IOException) {
2022-07-01 14:41:24 +05:30
binding.channelRefresh.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-07-01 14:41:24 +05:30
binding.channelRefresh.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-07-01 14:41:24 +05:30
binding.channelRefresh.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-05-20 03:52:10 +05:30
}