Merge pull request #122 from suraj0208/feature/channel-swipe-to-refresh

Add swipe to refresh on channel fragment
This commit is contained in:
Farbod 2022-04-07 22:44:44 -07:00 committed by GitHub
commit 041005f40b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 87 deletions

View File

@ -4,6 +4,7 @@ package com.github.libretube
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.opengl.Visibility
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
@ -17,6 +18,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.libretube.adapters.ChannelAdapter
import com.github.libretube.obj.Subscribe
import com.google.android.material.button.MaterialButton
@ -33,6 +35,7 @@ class ChannelFragment : Fragment() {
var channelAdapter: ChannelAdapter? = null
var isLoading = true
var isSubscribed: Boolean =false
private var refreshLayout: SwipeRefreshLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -58,12 +61,22 @@ class ChannelFragment : Fragment() {
view.findViewById<TextView>(R.id.channel_name).text=channel_id
val recyclerView = view.findViewById<RecyclerView>(R.id.channel_recView)
recyclerView.layoutManager = LinearLayoutManager(context)
fetchChannel(view)
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
if(sharedPref?.getString("token","")!=""){
refreshLayout = view.findViewById(R.id.channel_refresh)
val refreshChannel = {
refreshLayout?.isRefreshing = true
fetchChannel(view)
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val subButton = view.findViewById<MaterialButton>(R.id.channel_subscribe)
isSubscribed(subButton)
if (sharedPref?.getString("token","") != "") {
isSubscribed(subButton)
}
}
refreshChannel()
refreshLayout?.setOnRefreshListener {
refreshChannel()
}
val scrollView = view.findViewById<ScrollView>(R.id.channel_scrollView)
scrollView.viewTreeObserver
.addOnScrollChangedListener {
@ -72,6 +85,7 @@ class ChannelFragment : Fragment() {
//scroll view is at bottom
if(nextPage!=null && !isLoading){
isLoading=true
refreshLayout?.isRefreshing = true;
fetchNextPage()
}
@ -180,16 +194,20 @@ class ChannelFragment : Fragment() {
val response = try {
RetrofitInstance.api.getChannel(channel_id!!)
}catch(e: IOException) {
refreshLayout?.isRefreshing = false;
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
refreshLayout?.isRefreshing = false;
Log.e(TAG, "HttpException, unexpected response")
return@launchWhenCreated
}
nextPage = response.nextpage
isLoading=false
refreshLayout?.isRefreshing = false;
runOnUiThread {
view.findViewById<ScrollView>(R.id.channel_scrollView).visibility = View.VISIBLE
view.findViewById<TextView>(R.id.channel_name).text=response.name
view.findViewById<TextView>(R.id.channel_subs).text=response.subscriberCount.formatShort() + " subscribers"
view.findViewById<TextView>(R.id.channel_description).text=response.description
@ -212,17 +230,19 @@ class ChannelFragment : Fragment() {
val response = try {
RetrofitInstance.api.getChannelNextPage(channel_id!!,nextPage!!)
} catch (e: IOException) {
refreshLayout?.isRefreshing = false;
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
refreshLayout?.isRefreshing = false;
Log.e(TAG, "HttpException, unexpected response,"+e.response())
return@launchWhenCreated
}
nextPage = response.nextpage
channelAdapter?.updateItems(response.relatedStreams!!)
isLoading=false
refreshLayout?.isRefreshing = false;
}
}
run()

View File

@ -1,92 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<com.github.libretube.CustomSwipeToRefresh xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/channel_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ChannelFragment"
android:id="@+id/channel_scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/channel_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/channel_banner"
android:layout_width="match_parent"
android:layout_height="80dp"
/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/channel_image"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ChannelFragment"
android:visibility="invisible">
<TextView
android:id="@+id/channel_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Mia Plays"
android:textSize="20sp"
android:layout_marginTop="7dp"
android:textStyle="bold" />
<com.google.android.material.button.MaterialButton
android:id="@+id/channel_subscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/subscribe"
android:textColor="?attr/colorPrimary"
android:background="?android:attr/selectableItemBackground"
/>
<TextView
android:id="@+id/channel_subs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Description"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:textSize="17sp" />
<TextView
android:id="@+id/channel_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginBottom="5dp"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Videos"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textSize="17sp" />
<RelativeLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/channel_recView"
<ImageView
android:id="@+id/channel_banner"
android:layout_width="match_parent"
android:layout_height="80dp" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/channel_image"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/channel_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="7dp"
android:text="Mia Plays"
android:textSize="20sp"
android:textStyle="bold" />
<com.google.android.material.button.MaterialButton
android:id="@+id/channel_subscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="?android:attr/selectableItemBackground"
android:text="@string/subscribe"
android:textColor="?attr/colorPrimary"
/>
<TextView
android:id="@+id/channel_subs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/app_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="Description"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/channel_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginBottom="5dp"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Videos"
android:textSize="17sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/channel_recView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:nestedScrollingEnabled="false" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</com.github.libretube.CustomSwipeToRefresh>