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

194 lines
7.3 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
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-07-16 01:07:44 +05:30
import com.github.libretube.util.ConnectionHelper
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
2022-08-03 17:11:45 +05:30
import com.github.libretube.util.SubscriptionHelper
2022-06-10 18:33:48 +05:30
import com.github.libretube.util.formatShort
2022-07-29 12:30:13 +05:30
import com.github.libretube.util.toID
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
private var channelName: 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
2022-08-03 17:11:45 +05:30
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-29 12:30:13 +05:30
channelId = it.getString("channel_id").toID()
channelName = it.getString("channel_name")
?.replace("/c/", "")
?.replace("/user/", "")
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
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-08-03 17:11:45 +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-08-01 12:25:38 +05:30
fetchChannelNextPage()
2022-02-08 14:58:50 +05:30
}
}
}
2022-02-13 17:23:04 +05:30
}
2022-07-11 19:55:47 +05:30
private fun isSubscribed() {
2022-08-03 17:11:45 +05:30
lifecycleScope.launchWhenCreated {
isSubscribed = SubscriptionHelper.isSubscribed(channelId!!)
if (isSubscribed == null) return@launchWhenCreated
runOnUiThread {
if (isSubscribed == true) {
binding.channelSubscribe.text = getString(R.string.unsubscribe)
2022-02-13 17:23:04 +05:30
}
2022-08-03 17:11:45 +05:30
binding.channelSubscribe.setOnClickListener {
binding.channelSubscribe.text = if (isSubscribed == true) {
SubscriptionHelper.unsubscribe(channelId!!)
isSubscribed = false
getString(R.string.subscribe)
} else {
SubscriptionHelper.subscribe(channelId!!)
2022-05-20 03:52:10 +05:30
isSubscribed = true
2022-08-03 17:11:45 +05:30
getString(R.string.unsubscribe)
2022-05-20 03:52:10 +05:30
}
2022-02-13 17:23:04 +05:30
}
}
}
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 {
if (channelId != null) RetrofitInstance.api.getChannel(channelId!!)
else RetrofitInstance.api.getChannelByName(channelName!!)
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
2022-07-16 01:07:44 +05:30
ConnectionHelper.loadImage(response.bannerUrl, binding.channelBanner)
ConnectionHelper.loadImage(response.avatarUrl, binding.channelImage)
// recyclerview of the videos by the channel
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-08-01 12:25:38 +05:30
private fun fetchChannelNextPage() {
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
}