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

178 lines
6.8 KiB
Kotlin
Raw Normal View History

2022-06-08 14:37:47 +05:30
package com.github.libretube.fragments
2021-12-09 18:25:32 +05:30
import android.os.Bundle
2022-02-08 19:57:13 +05:30
import android.util.Log
2021-12-09 18:25:32 +05:30
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2022-05-21 13:32:04 +05:30
import android.widget.ProgressBar
import android.widget.Toast
import androidx.core.view.isVisible
2022-05-20 03:52:10 +05:30
import androidx.fragment.app.Fragment
2022-02-12 20:00:36 +05:30
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
2022-06-04 21:04:18 +05:30
import androidx.recyclerview.widget.LinearLayoutManager
2022-02-12 20:00:36 +05:30
import androidx.recyclerview.widget.RecyclerView
2022-06-08 14:37:47 +05:30
import com.github.libretube.R
2022-02-12 20:00:36 +05:30
import com.github.libretube.adapters.SubscriptionAdapter
import com.github.libretube.adapters.SubscriptionChannelAdapter
2022-07-01 14:41:24 +05:30
import com.github.libretube.databinding.FragmentSubscriptionsBinding
2022-06-26 14:25:05 +05:30
import com.github.libretube.util.PreferenceHelper
2022-06-03 00:40:16 +05:30
import com.github.libretube.util.RetrofitInstance
2022-06-08 14:38:40 +05:30
import retrofit2.HttpException
2022-06-24 20:56:36 +05:30
import java.io.IOException
2021-12-09 18:25:32 +05:30
2022-07-01 14:41:24 +05:30
class SubscriptionsFragment : Fragment() {
2022-02-12 20:00:36 +05:30
val TAG = "SubFragment"
2022-07-01 14:41:24 +05:30
private lateinit var binding: FragmentSubscriptionsBinding
2022-02-12 20:00:36 +05:30
lateinit var token: String
2022-07-01 14:41:24 +05:30
private var isLoaded = false
2022-05-20 03:52:10 +05:30
private var subscriptionAdapter: SubscriptionAdapter? = null
2022-07-01 14:41:24 +05:30
2021-12-09 18:25:32 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
2022-05-20 03:52:10 +05:30
inflater: LayoutInflater,
container: ViewGroup?,
2021-12-09 18:25:32 +05:30
savedInstanceState: Bundle?
2022-07-01 14:41:24 +05:30
): View {
binding = FragmentSubscriptionsBinding.inflate(layoutInflater, container, false)
return binding.root
2021-12-09 18:25:32 +05:30
}
2022-02-07 22:13:30 +05:30
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-06-26 14:25:05 +05:30
token = PreferenceHelper.getToken(requireContext())
2022-07-01 14:41:24 +05:30
if (token != "") {
binding.loginOrRegister.visibility = View.GONE
binding.subRefresh.isEnabled = true
2022-02-12 20:00:36 +05:30
2022-07-01 14:41:24 +05:30
binding.subProgress.visibility = View.VISIBLE
2022-02-12 20:00:36 +05:30
2022-06-26 14:25:05 +05:30
val grid = PreferenceHelper.getString(
requireContext(),
2022-06-24 20:56:36 +05:30
"grid",
resources.getInteger(R.integer.grid_items).toString()
2022-05-21 13:32:04 +05:30
)!!
2022-07-01 14:41:24 +05:30
binding.subFeed.layoutManager = GridLayoutManager(view.context, grid.toInt())
fetchFeed(binding.subFeed, binding.subProgress)
2022-02-12 20:00:36 +05:30
2022-07-01 14:41:24 +05:30
binding.subRefresh.setOnRefreshListener {
fetchChannels(binding.subChannels)
fetchFeed(binding.subFeed, binding.subProgress)
}
2022-07-01 14:41:24 +05:30
binding.toggleSubs.visibility = View.VISIBLE
2022-05-16 21:03:22 +05:30
var loadedSubbedChannels = false
2022-06-18 19:28:41 +05:30
2022-07-01 14:41:24 +05:30
binding.toggleSubs.setOnClickListener {
binding.toggle.animate().rotationBy(180F).setDuration(100).start()
if (!binding.subChannels.isVisible) {
2022-05-16 21:03:22 +05:30
if (!loadedSubbedChannels) {
2022-07-01 14:41:24 +05:30
binding.subChannels.layoutManager = LinearLayoutManager(context)
fetchChannels(binding.subChannels)
2022-05-16 21:03:22 +05:30
loadedSubbedChannels = true
}
2022-07-01 14:41:24 +05:30
binding.subChannels.visibility = View.VISIBLE
binding.subFeed.visibility = View.GONE
2022-05-20 03:52:10 +05:30
} else {
2022-07-01 14:41:24 +05:30
binding.subChannels.visibility = View.GONE
binding.subFeed.visibility = View.VISIBLE
}
2022-05-14 21:44:53 +05:30
}
2022-07-01 14:41:24 +05:30
binding.scrollviewSub.viewTreeObserver
2022-02-12 20:00:36 +05:30
.addOnScrollChangedListener {
2022-07-01 14:41:24 +05:30
if (binding.scrollviewSub.getChildAt(0).bottom
== (binding.scrollviewSub.height + binding.scrollviewSub.scrollY)
2022-05-20 03:52:10 +05:30
) {
// scroll view is at bottom
if (isLoaded) {
2022-07-01 14:41:24 +05:30
binding.subRefresh.isRefreshing = true
2022-02-12 20:00:36 +05:30
subscriptionAdapter?.updateItems()
2022-07-01 14:41:24 +05:30
binding.subRefresh.isRefreshing = false
2022-02-12 20:00:36 +05:30
}
}
}
} else {
2022-07-01 14:41:24 +05:30
binding.subRefresh.isEnabled = false
2022-02-12 20:00:36 +05:30
}
2022-02-07 22:13:30 +05:30
}
2022-07-01 14:41:24 +05:30
private fun fetchFeed(feedRecView: RecyclerView, progressBar: ProgressBar) {
2022-02-12 20:00:36 +05:30
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getFeed(token)
2022-05-20 03:52:10 +05:30
} catch (e: IOException) {
Log.e(TAG, e.toString())
2022-02-12 20:00:36 +05:30
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated
} finally {
2022-07-01 14:41:24 +05:30
binding.subRefresh.isRefreshing = false
2021-12-09 18:25:32 +05:30
}
2022-05-20 03:52:10 +05:30
if (response.isNotEmpty()) {
subscriptionAdapter = SubscriptionAdapter(response, childFragmentManager)
2022-05-21 13:32:04 +05:30
feedRecView.adapter = subscriptionAdapter
2022-02-14 09:54:46 +05:30
subscriptionAdapter?.updateItems()
2022-05-20 03:52:10 +05:30
} else {
2022-04-15 16:56:06 +05:30
runOnUiThread {
2022-07-01 14:41:24 +05:30
with(binding.boogh) {
2022-05-20 03:52:10 +05:30
visibility = View.VISIBLE
2022-04-15 16:56:06 +05:30
setImageResource(R.drawable.ic_list)
}
2022-07-01 14:41:24 +05:30
with(binding.textLike) {
2022-05-20 03:52:10 +05:30
visibility = View.VISIBLE
2022-04-15 16:56:06 +05:30
text = getString(R.string.emptyList)
}
2022-07-01 14:41:24 +05:30
binding.loginOrRegister.visibility = View.VISIBLE
2022-04-15 16:56:06 +05:30
}
2022-02-13 22:43:26 +05:30
}
2022-05-20 03:52:10 +05:30
progressBar.visibility = View.GONE
isLoaded = true
2021-12-09 18:25:32 +05:30
}
2022-02-12 20:00:36 +05:30
}
run()
2021-12-09 18:25:32 +05:30
}
2022-02-12 20:00:36 +05:30
private fun fetchChannels(channelRecView: RecyclerView) {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.subscriptions(token)
2022-05-20 03:52:10 +05:30
} catch (e: IOException) {
Log.e(TAG, e.toString())
2022-02-12 20:00:36 +05:30
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated
} finally {
2022-07-01 14:41:24 +05:30
binding.subRefresh.isRefreshing = false
2022-02-12 20:00:36 +05:30
}
2022-05-20 03:52:10 +05:30
if (response.isNotEmpty()) {
2022-05-21 13:32:04 +05:30
channelRecView.adapter = SubscriptionChannelAdapter(response.toMutableList())
2022-05-20 03:52:10 +05:30
} else {
Toast.makeText(context, R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show()
2022-02-13 22:43:26 +05:30
}
2022-02-12 20:00:36 +05:30
}
}
run()
}
2022-05-21 13:32:04 +05:30
2022-04-15 16:56:06 +05:30
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}