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

183 lines
7.3 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:31:47 +05:30
import androidx.fragment.app.Fragment
2021-12-09 18:25:32 +05:30
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2022-02-12 20:00:36 +05:30
import android.widget.*
import androidx.core.view.isVisible
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.LinearLayoutManager
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
2022-05-16 21:03:22 +05:30
import org.chromium.base.ThreadUtils.runOnUiThread
2022-02-12 20:00:36 +05:30
import retrofit2.HttpException
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
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(
inflater: LayoutInflater, container: ViewGroup?,
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-02-12 20:00:36 +05:30
token = sharedPref?.getString("token","")!!
refreshLayout = view.findViewById(R.id.sub_refresh)
2022-02-12 20:00:36 +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)
progressBar.visibility=View.VISIBLE
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
}
else {
channelRecView.visibility = View.GONE
feedRecView.visibility = View.VISIBLE
}
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
== (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
}
2022-02-07 22:13:30 +05:30
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)
}catch(e: IOException) {
2022-04-01 17:10:46 +05:30
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-02-13 22:43:26 +05:30
if (response.isNotEmpty()){
subscriptionAdapter = SubscriptionAdapter(response)
feedRecView?.adapter= subscriptionAdapter
2022-02-14 09:54:46 +05:30
subscriptionAdapter?.updateItems()
2022-04-15 16:56:06 +05:30
}else{
runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh)){
visibility=View.VISIBLE
setImageResource(R.drawable.ic_list)
}
with(view.findViewById<TextView>(R.id.textLike)){
visibility=View.VISIBLE
text = getString(R.string.emptyList)
}
view.findViewById<RelativeLayout>(R.id.loginOrRegister).visibility=View.VISIBLE
}
2022-02-13 22:43:26 +05:30
}
2022-02-12 20:00:36 +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)
}catch(e: IOException) {
2022-04-01 17:10:46 +05:30
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-02-13 22:43:26 +05:30
if (response.isNotEmpty()){
channelRecView?.adapter=SubscriptionChannelAdapter(response.toMutableList())
}else{
Toast.makeText(context,R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show()
}
2022-02-12 20:00:36 +05:30
}
}
run()
}
override fun onDestroy() {
Log.e(TAG,"Destroyed")
super.onDestroy()
2022-03-30 00:16:34 +05:30
subscriptionAdapter = null
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)
}
2022-02-12 20:00:36 +05:30
}