LibreTube/app/src/main/java/com/github/libretube/Subscriptions.kt

204 lines
8.1 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube
2021-12-09 18:25:32 +05:30
2022-02-08 19:57:13 +05:30
import android.content.Context
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-20 19:12:45 +05:30
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
2022-02-12 20:00:36 +05:30
import android.widget.*
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.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2022-02-12 20:00:36 +05:30
import com.github.libretube.adapters.SubscriptionAdapter
import com.github.libretube.adapters.SubscriptionChannelAdapter
import retrofit2.HttpException
2022-05-20 19:12:45 +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
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-02-08 19:57:13 +05:30
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
2022-05-20 03:52:10 +05:30
token = sharedPref?.getString("token", "")!!
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
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-03-30 00:16:34 +05:30
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val grid = sharedPreferences.getString("grid", resources.getInteger(R.integer.grid_items).toString())!!
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
refreshLayout?.setOnRefreshListener {
fetchChannels(channelRecView)
2022-04-15 16:56:06 +05:30
fetchFeed(feedRecView, progressBar, view)
}
2022-05-14 21:44:53 +05:30
var toggleSubs = view.findViewById<RelativeLayout>(R.id.toggle_subs)
toggleSubs.visibility = View.VISIBLE
2022-05-16 21:03:22 +05:30
var loadedSubbedChannels = false
2022-05-14 21:44:53 +05:30
toggleSubs.setOnClickListener {
if (!channelRecView.isVisible) {
2022-05-16 21:03:22 +05:30
if (!loadedSubbedChannels) {
channelRecView?.layoutManager = GridLayoutManager(context, 4)
fetchChannels(channelRecView)
loadedSubbedChannels = true
}
channelRecView.visibility = View.VISIBLE
feedRecView.visibility = View.GONE
2022-05-20 19:12:45 +05:30
//toggle button
val rotate = RotateAnimation(
0F,
180F,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f
)
rotate.duration = 100
rotate.interpolator = LinearInterpolator()
rotate.fillAfter = true
val image = view.findViewById<ImageView>(R.id.toggle)
image.startAnimation(rotate)
2022-05-20 03:52:10 +05:30
} else {
channelRecView.visibility = View.GONE
feedRecView.visibility = View.VISIBLE
2022-05-20 19:12:45 +05:30
//toggle button
val image = view.findViewById<ImageView>(R.id.toggle)
image.clearAnimation()
}
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) {
refreshLayout?.isRefreshing = true
2022-02-12 20:00:36 +05:30
subscriptionAdapter?.updateItems()
refreshLayout?.isRefreshing = false
2022-02-12 20:00:36 +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
} finally {
refreshLayout?.isRefreshing = false
2021-12-09 18:25:32 +05:30
}
2022-05-20 03:52:10 +05:30
if (response.isNotEmpty()) {
2022-02-13 22:43:26 +05:30
subscriptionAdapter = SubscriptionAdapter(response)
2022-05-20 03:52:10 +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-20 03:52:10 +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
} finally {
refreshLayout?.isRefreshing = false
2022-02-12 20:00:36 +05:30
}
2022-05-20 03:52:10 +05:30
if (response.isNotEmpty()) {
channelRecView?.adapter = SubscriptionChannelAdapter(response.toMutableList())
} 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()
}
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-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)
}
}