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.ImageView
|
|
|
|
import android.widget.ProgressBar
|
|
|
|
import android.widget.RelativeLayout
|
|
|
|
import android.widget.ScrollView
|
|
|
|
import android.widget.TextView
|
|
|
|
import android.widget.Toast
|
2022-05-15 12:04:43 +05:30
|
|
|
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-03-17 22:38:23 +05:30
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
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-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
|
|
|
|
|
|
|
class Subscriptions : Fragment() {
|
2022-02-12 20:00:36 +05:30
|
|
|
val TAG = "SubFragment"
|
|
|
|
lateinit var token: String
|
|
|
|
var isLoaded = false
|
2022-05-20 03:52:10 +05:30
|
|
|
private var subscriptionAdapter: SubscriptionAdapter? = null
|
2022-03-17 22:38:23 +05:30
|
|
|
private var refreshLayout: SwipeRefreshLayout? = null
|
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?
|
|
|
|
): View? {
|
|
|
|
// Inflate the layout for this fragment
|
|
|
|
return inflater.inflate(R.layout.fragment_subscriptions, container, false)
|
|
|
|
}
|
|
|
|
|
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-03-17 22:38:23 +05:30
|
|
|
refreshLayout = view.findViewById(R.id.sub_refresh)
|
2022-05-20 03:52:10 +05:30
|
|
|
if (token != "") {
|
|
|
|
view.findViewById<RelativeLayout>(R.id.loginOrRegister).visibility = View.GONE
|
2022-03-17 22:38:23 +05:30
|
|
|
refreshLayout?.isEnabled = true
|
|
|
|
|
2022-02-12 20:00:36 +05:30
|
|
|
var progressBar = view.findViewById<ProgressBar>(R.id.sub_progress)
|
2022-05-20 03:52:10 +05:30
|
|
|
progressBar.visibility = View.VISIBLE
|
2022-02-12 20:00:36 +05:30
|
|
|
|
|
|
|
var channelRecView = view.findViewById<RecyclerView>(R.id.sub_channels)
|
|
|
|
|
|
|
|
var feedRecView = view.findViewById<RecyclerView>(R.id.sub_feed)
|
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-03-30 00:16:34 +05:30
|
|
|
feedRecView.layoutManager = GridLayoutManager(view.context, grid.toInt())
|
2022-04-15 16:56:06 +05:30
|
|
|
fetchFeed(feedRecView, progressBar, view)
|
2022-02-12 20:00:36 +05:30
|
|
|
|
2022-03-17 22:38:23 +05:30
|
|
|
refreshLayout?.setOnRefreshListener {
|
|
|
|
fetchChannels(channelRecView)
|
2022-04-15 16:56:06 +05:30
|
|
|
fetchFeed(feedRecView, progressBar, view)
|
2022-03-17 22:38:23 +05:30
|
|
|
}
|
|
|
|
|
2022-05-14 21:44:53 +05:30
|
|
|
var toggleSubs = view.findViewById<RelativeLayout>(R.id.toggle_subs)
|
2022-06-18 19:28:41 +05:30
|
|
|
val arrowImageView = view.findViewById<ImageView>(R.id.toggle)
|
|
|
|
|
2022-05-15 12:04:43 +05:30
|
|
|
toggleSubs.visibility = View.VISIBLE
|
2022-05-16 21:03:22 +05:30
|
|
|
var loadedSubbedChannels = false
|
2022-06-18 19:28:41 +05:30
|
|
|
|
2022-05-14 21:44:53 +05:30
|
|
|
toggleSubs.setOnClickListener {
|
2022-06-18 19:28:41 +05:30
|
|
|
arrowImageView.animate().rotationBy(180F).setDuration(100).start()
|
2022-05-15 12:04:43 +05:30
|
|
|
if (!channelRecView.isVisible) {
|
2022-05-16 21:03:22 +05:30
|
|
|
if (!loadedSubbedChannels) {
|
2022-06-04 21:04:18 +05:30
|
|
|
channelRecView?.layoutManager = LinearLayoutManager(context)
|
2022-05-16 21:03:22 +05:30
|
|
|
fetchChannels(channelRecView)
|
|
|
|
loadedSubbedChannels = true
|
|
|
|
}
|
2022-05-15 12:04:43 +05:30
|
|
|
channelRecView.visibility = View.VISIBLE
|
|
|
|
feedRecView.visibility = View.GONE
|
2022-05-20 03:52:10 +05:30
|
|
|
} else {
|
2022-05-15 12:04:43 +05:30
|
|
|
channelRecView.visibility = View.GONE
|
|
|
|
feedRecView.visibility = View.VISIBLE
|
2022-05-20 19:12:45 +05:30
|
|
|
|
2022-05-21 13:32:04 +05:30
|
|
|
// toggle button
|
2022-05-20 19:12:45 +05:30
|
|
|
val image = view.findViewById<ImageView>(R.id.toggle)
|
|
|
|
image.clearAnimation()
|
2022-05-15 12:04:43 +05:30
|
|
|
}
|
2022-05-14 21:44:53 +05:30
|
|
|
}
|
|
|
|
|
2022-02-12 20:00:36 +05:30
|
|
|
val scrollView = view.findViewById<ScrollView>(R.id.scrollview_sub)
|
|
|
|
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 (isLoaded) {
|
2022-03-17 22:38:23 +05:30
|
|
|
refreshLayout?.isRefreshing = true
|
2022-02-12 20:00:36 +05:30
|
|
|
subscriptionAdapter?.updateItems()
|
2022-03-17 22:38:23 +05:30
|
|
|
refreshLayout?.isRefreshing = false
|
2022-02-12 20:00:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 22:38:23 +05:30
|
|
|
} else {
|
|
|
|
refreshLayout?.isEnabled = false
|
2022-02-12 20:00:36 +05:30
|
|
|
}
|
2022-02-07 22:13:30 +05:30
|
|
|
}
|
|
|
|
|
2022-04-15 16:56:06 +05:30
|
|
|
private fun fetchFeed(feedRecView: RecyclerView, progressBar: ProgressBar, view: View) {
|
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
|
2022-03-17 22:38:23 +05:30
|
|
|
} finally {
|
|
|
|
refreshLayout?.isRefreshing = false
|
2021-12-09 18:25:32 +05:30
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
if (response.isNotEmpty()) {
|
2022-05-27 01:20:30 +05:30
|
|
|
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-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<ImageView>(R.id.boogh)) {
|
|
|
|
visibility = View.VISIBLE
|
2022-04-15 16:56:06 +05:30
|
|
|
setImageResource(R.drawable.ic_list)
|
|
|
|
}
|
2022-05-20 03:52:10 +05:30
|
|
|
with(view.findViewById<TextView>(R.id.textLike)) {
|
|
|
|
visibility = View.VISIBLE
|
2022-04-15 16:56:06 +05:30
|
|
|
text = getString(R.string.emptyList)
|
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
view.findViewById<RelativeLayout>(R.id.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
|
2022-03-17 22:38:23 +05:30
|
|
|
} finally {
|
|
|
|
refreshLayout?.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-02-12 20:00:36 +05:30
|
|
|
override fun onDestroy() {
|
2022-05-20 03:52:10 +05:30
|
|
|
Log.e(TAG, "Destroyed")
|
2022-02-12 20:00:36 +05:30
|
|
|
super.onDestroy()
|
2022-03-30 00:16:34 +05:30
|
|
|
subscriptionAdapter = null
|
2022-05-20 03:52:10 +05:30
|
|
|
view?.findViewById<RecyclerView>(R.id.sub_feed)?.adapter = null
|
2022-02-12 20:00:36 +05:30
|
|
|
}
|
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)
|
|
|
|
}
|
2022-03-17 22:38:23 +05:30
|
|
|
}
|