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

153 lines
5.7 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
2022-02-07 22:13:30 +05:30
import android.media.Image
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.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
import com.github.libretube.adapters.TrendingAdapter
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","")!!
Log.e(TAG,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)
channelRecView?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
fetchChannels(channelRecView)
var feedRecView = view.findViewById<RecyclerView>(R.id.sub_feed)
feedRecView.layoutManager = GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items))
fetchFeed(feedRecView, progressBar)
refreshLayout?.setOnRefreshListener {
fetchChannels(channelRecView)
fetchFeed(feedRecView, progressBar)
}
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-02-12 20:00:36 +05:30
private fun fetchFeed(feedRecView: RecyclerView, progressBar: ProgressBar) {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getFeed(token)
}catch(e: IOException) {
println(e)
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-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) {
println(e)
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 onStop() {
Log.e(TAG,"Stopped")
subscriptionAdapter = null
view?.findViewById<RecyclerView>(R.id.sub_feed)?.adapter=null
super.onStop()
}
override fun onDestroy() {
Log.e(TAG,"Destroyed")
super.onDestroy()
}
}