mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Merge pull request #372 from Bnyro/master
Channels in Subscriptions Page Rework
This commit is contained in:
commit
67a28f8645
@ -20,6 +20,7 @@ import androidx.fragment.app.Fragment
|
|||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
import com.github.libretube.adapters.SubscriptionAdapter
|
import com.github.libretube.adapters.SubscriptionAdapter
|
||||||
@ -82,7 +83,7 @@ class Subscriptions : Fragment() {
|
|||||||
toggleSubs.setOnClickListener {
|
toggleSubs.setOnClickListener {
|
||||||
if (!channelRecView.isVisible) {
|
if (!channelRecView.isVisible) {
|
||||||
if (!loadedSubbedChannels) {
|
if (!loadedSubbedChannels) {
|
||||||
channelRecView?.layoutManager = GridLayoutManager(context, 4)
|
channelRecView?.layoutManager = LinearLayoutManager(context)
|
||||||
fetchChannels(channelRecView)
|
fetchChannels(channelRecView)
|
||||||
loadedSubbedChannels = true
|
loadedSubbedChannels = true
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import com.github.libretube.obj.Comment
|
|||||||
import com.squareup.picasso.Picasso
|
import com.squareup.picasso.Picasso
|
||||||
|
|
||||||
class CommentsAdapter(private val comments: MutableList<Comment>) :
|
class CommentsAdapter(private val comments: MutableList<Comment>) :
|
||||||
RecyclerView.Adapter<ViewHolder>() {
|
RecyclerView.Adapter<CommentsViewHolder>() {
|
||||||
|
|
||||||
fun updateItems(newItems: List<Comment>) {
|
fun updateItems(newItems: List<Comment>) {
|
||||||
var commentsSize = comments.size
|
var commentsSize = comments.size
|
||||||
@ -23,13 +23,13 @@ class CommentsAdapter(private val comments: MutableList<Comment>) :
|
|||||||
notifyItemRangeInserted(commentsSize, newItems.size)
|
notifyItemRangeInserted(commentsSize, newItems.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentsViewHolder {
|
||||||
var commentsView =
|
var commentsView =
|
||||||
LayoutInflater.from(parent.context).inflate(R.layout.comments_row, parent, false)
|
LayoutInflater.from(parent.context).inflate(R.layout.comments_row, parent, false)
|
||||||
return ViewHolder(commentsView)
|
return CommentsViewHolder(commentsView)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: CommentsViewHolder, position: Int) {
|
||||||
holder.v.findViewById<TextView>(R.id.comment_infos).text =
|
holder.v.findViewById<TextView>(R.id.comment_infos).text =
|
||||||
comments[position].author.toString() +
|
comments[position].author.toString() +
|
||||||
" • " + comments[position].commentedTime.toString()
|
" • " + comments[position].commentedTime.toString()
|
||||||
@ -68,7 +68,7 @@ class CommentsAdapter(private val comments: MutableList<Comment>) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
|
class CommentsViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
|
||||||
init {
|
init {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ class SearchAdapter(
|
|||||||
private val searchItems: MutableList<SearchItem>,
|
private val searchItems: MutableList<SearchItem>,
|
||||||
private val childFragmentManager: FragmentManager
|
private val childFragmentManager: FragmentManager
|
||||||
) :
|
) :
|
||||||
RecyclerView.Adapter<CustomViewHolder1>() {
|
RecyclerView.Adapter<SearchViewHolder>() {
|
||||||
|
|
||||||
fun updateItems(newItems: List<SearchItem>) {
|
fun updateItems(newItems: List<SearchItem>) {
|
||||||
var searchItemsSize = searchItems.size
|
var searchItemsSize = searchItems.size
|
||||||
@ -35,7 +35,7 @@ class SearchAdapter(
|
|||||||
return searchItems.size
|
return searchItems.size
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder1 {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
|
||||||
val layout = when (viewType) {
|
val layout = when (viewType) {
|
||||||
0 -> R.layout.video_search_row
|
0 -> R.layout.video_search_row
|
||||||
1 -> R.layout.channel_search_row
|
1 -> R.layout.channel_search_row
|
||||||
@ -44,10 +44,10 @@ class SearchAdapter(
|
|||||||
}
|
}
|
||||||
val layoutInflater = LayoutInflater.from(parent.context)
|
val layoutInflater = LayoutInflater.from(parent.context)
|
||||||
val cell = layoutInflater.inflate(layout, parent, false)
|
val cell = layoutInflater.inflate(layout, parent, false)
|
||||||
return CustomViewHolder1(cell, childFragmentManager)
|
return SearchViewHolder(cell, childFragmentManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: CustomViewHolder1, position: Int) {
|
override fun onBindViewHolder(holder: SearchViewHolder, position: Int) {
|
||||||
holder.bind(searchItems[position])
|
holder.bind(searchItems[position])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ class SearchAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomViewHolder1(
|
class SearchViewHolder(
|
||||||
private val v: View,
|
private val v: View,
|
||||||
private val childFragmentManager: FragmentManager
|
private val childFragmentManager: FragmentManager
|
||||||
) : RecyclerView.ViewHolder(v) {
|
) : RecyclerView.ViewHolder(v) {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.github.libretube.adapters
|
package com.github.libretube.adapters
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@ -9,11 +11,21 @@ import androidx.core.os.bundleOf
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.libretube.MainActivity
|
import com.github.libretube.MainActivity
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
|
import com.github.libretube.obj.Subscribe
|
||||||
import com.github.libretube.obj.Subscription
|
import com.github.libretube.obj.Subscription
|
||||||
|
import com.github.libretube.util.RetrofitInstance
|
||||||
import com.squareup.picasso.Picasso
|
import com.squareup.picasso.Picasso
|
||||||
|
import java.io.IOException
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import retrofit2.HttpException
|
||||||
|
|
||||||
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
|
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
|
||||||
RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
|
RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
|
||||||
|
val TAG = "SubChannelAdapter"
|
||||||
|
private var subscribed = true
|
||||||
|
private var isLoading = false
|
||||||
override fun getItemCount(): Int {
|
override fun getItemCount(): Int {
|
||||||
return subscriptions.size
|
return subscriptions.size
|
||||||
}
|
}
|
||||||
@ -32,9 +44,72 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
|
|||||||
Picasso.get().load(subscription.avatar).into(avatar)
|
Picasso.get().load(subscription.avatar).into(avatar)
|
||||||
holder.v.setOnClickListener {
|
holder.v.setOnClickListener {
|
||||||
val activity = holder.v.context as MainActivity
|
val activity = holder.v.context as MainActivity
|
||||||
val bundle = bundleOf("channel_id" to subscription.url)
|
val bundle = bundleOf("channelId" to subscription.url)
|
||||||
activity.navController.navigate(R.id.channel, bundle)
|
activity.navController.navigate(R.id.channel, bundle)
|
||||||
}
|
}
|
||||||
|
val subscribeBtn = holder.v
|
||||||
|
.findViewById<com.google.android.material.button.MaterialButton>(
|
||||||
|
R.id.subscription_subscribe
|
||||||
|
)
|
||||||
|
subscribeBtn.setOnClickListener {
|
||||||
|
if (!isLoading) {
|
||||||
|
isLoading = true
|
||||||
|
val channelId = subscription.url?.replace("/channel/", "")!!
|
||||||
|
if (subscribed) {
|
||||||
|
unsubscribe(holder.v.context, channelId)
|
||||||
|
subscribeBtn.text = holder.v.context.getString(R.string.subscribe)
|
||||||
|
} else {
|
||||||
|
subscribe(holder.v.context, channelId)
|
||||||
|
subscribeBtn.text = holder.v.context.getString(R.string.unsubscribe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun subscribe(context: Context, channelId: String) {
|
||||||
|
fun run() {
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
val response = try {
|
||||||
|
val sharedPref = context
|
||||||
|
.getSharedPreferences("token", Context.MODE_PRIVATE)
|
||||||
|
RetrofitInstance.api.subscribe(
|
||||||
|
sharedPref?.getString("token", "")!!,
|
||||||
|
Subscribe(channelId)
|
||||||
|
)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
println(e)
|
||||||
|
Log.e(TAG, "IOException, you might not have internet connection")
|
||||||
|
} catch (e: HttpException) {
|
||||||
|
Log.e(TAG, "HttpException, unexpected response")
|
||||||
|
}
|
||||||
|
subscribed = true
|
||||||
|
isLoading = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
run()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun unsubscribe(context: Context, channelId: String) {
|
||||||
|
fun run() {
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
val response = try {
|
||||||
|
val sharedPref =
|
||||||
|
context.getSharedPreferences("token", Context.MODE_PRIVATE)
|
||||||
|
RetrofitInstance.api.unsubscribe(
|
||||||
|
sharedPref?.getString("token", "")!!,
|
||||||
|
Subscribe(channelId)
|
||||||
|
)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
println(e)
|
||||||
|
Log.e(TAG, "IOException, you might not have internet connection")
|
||||||
|
} catch (e: HttpException) {
|
||||||
|
Log.e(TAG, "HttpException, unexpected response")
|
||||||
|
}
|
||||||
|
subscribed = false
|
||||||
|
isLoading = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,18 +23,18 @@ import com.squareup.picasso.Picasso
|
|||||||
class TrendingAdapter(
|
class TrendingAdapter(
|
||||||
private val videoFeed: List<StreamItem>,
|
private val videoFeed: List<StreamItem>,
|
||||||
private val childFragmentManager: FragmentManager
|
private val childFragmentManager: FragmentManager
|
||||||
) : RecyclerView.Adapter<CustomViewHolder>() {
|
) : RecyclerView.Adapter<TrendingViewHolder>() {
|
||||||
override fun getItemCount(): Int {
|
override fun getItemCount(): Int {
|
||||||
return videoFeed.size
|
return videoFeed.size
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrendingViewHolder {
|
||||||
val layoutInflater = LayoutInflater.from(parent.context)
|
val layoutInflater = LayoutInflater.from(parent.context)
|
||||||
val cell = layoutInflater.inflate(R.layout.trending_row, parent, false)
|
val cell = layoutInflater.inflate(R.layout.trending_row, parent, false)
|
||||||
return CustomViewHolder(cell)
|
return TrendingViewHolder(cell)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: TrendingViewHolder, position: Int) {
|
||||||
val trending = videoFeed[position]
|
val trending = videoFeed[position]
|
||||||
holder.v.findViewById<TextView>(R.id.textView_title).text = trending.title
|
holder.v.findViewById<TextView>(R.id.textView_title).text = trending.title
|
||||||
holder.v.findViewById<TextView>(R.id.textView_channel).text =
|
holder.v.findViewById<TextView>(R.id.textView_channel).text =
|
||||||
@ -89,7 +89,7 @@ class TrendingAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
|
class TrendingViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
|
||||||
init {
|
init {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,40 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_margin="8dp"
|
android:layout_marginLeft="8dp"
|
||||||
android:padding="8dp"
|
android:layout_marginRight="8dp"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:layout_marginBottom="4dp"
|
||||||
android:background="?android:attr/selectableItemBackground">
|
android:background="?android:attr/selectableItemBackground">
|
||||||
|
|
||||||
<de.hdodenhof.circleimageview.CircleImageView
|
<de.hdodenhof.circleimageview.CircleImageView
|
||||||
android:id="@+id/subscription_channel_image"
|
android:id="@+id/subscription_channel_image"
|
||||||
android:layout_width="50dp"
|
android:layout_centerVertical="true"
|
||||||
android:layout_height="50dp"
|
android:layout_width="40dp"
|
||||||
android:layout_gravity="center"
|
android:layout_height="40dp" />
|
||||||
android:layout_marginBottom="4dp" />
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:text="kirrrrrrrrrrrrrrrrrrrrrrrrrrfgdfg"
|
|
||||||
android:id="@+id/subscription_channel_name"
|
android:id="@+id/subscription_channel_name"
|
||||||
android:layout_width="60dp"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="center"
|
android:layout_centerVertical="true"
|
||||||
|
android:layout_marginLeft="10dp"
|
||||||
|
android:layout_toEndOf="@id/subscription_channel_image"
|
||||||
android:ellipsize="end"
|
android:ellipsize="end"
|
||||||
android:maxLines="1" />
|
android:maxLines="1"
|
||||||
</LinearLayout>
|
android:text="Channel Name"
|
||||||
|
android:textSize="16dp" />
|
||||||
|
|
||||||
|
<com.google.android.material.button.MaterialButton
|
||||||
|
android:id="@+id/subscription_subscribe"
|
||||||
|
style="@style/Widget.Material3.Button.ElevatedButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentEnd="true"
|
||||||
|
android:backgroundTint="?attr/colorOnPrimary"
|
||||||
|
android:text="@string/unsubscribe"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
app:cornerRadius="20dp" />
|
||||||
|
</RelativeLayout>
|
Loading…
Reference in New Issue
Block a user