This commit is contained in:
Bnyro 2022-06-04 19:13:11 +02:00
parent 8e6e5b734f
commit ed35a28346

View File

@ -15,11 +15,11 @@ 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.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.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) : class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
RecyclerView.Adapter<SubscriptionChannelViewHolder>() { RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
@ -47,27 +47,31 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
val bundle = bundleOf("channelId" 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) val subscribeBtn = holder.v
.findViewById<com.google.android.material.button.MaterialButton>(
R.id.subscription_subscribe
)
subscribeBtn.setOnClickListener { subscribeBtn.setOnClickListener {
if (!isLoading) { if (!isLoading) {
isLoading = true isLoading = true
val channelId = subscription.url?.replace("/channel/", "")!! val channelId = subscription.url?.replace("/channel/", "")!!
if (subscribed) { if (subscribed) {
unsubscribe(holder.v, channelId) unsubscribe(holder.v.context, channelId)
subscribeBtn.text = holder.v.context.getString(R.string.subscribe) subscribeBtn.text = holder.v.context.getString(R.string.subscribe)
} else { } else {
subscribe(holder.v, channelId) subscribe(holder.v.context, channelId)
subscribeBtn.text = holder.v.context.getString(R.string.unsubscribe) subscribeBtn.text = holder.v.context.getString(R.string.unsubscribe)
} }
} }
} }
} }
private fun subscribe(view: View, channelId: String) { private fun subscribe(context: Context, channelId: String) {
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
val sharedPref = view.context.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context
.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.subscribe( RetrofitInstance.api.subscribe(
sharedPref?.getString("token", "")!!, sharedPref?.getString("token", "")!!,
Subscribe(channelId) Subscribe(channelId)
@ -85,12 +89,12 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
run() run()
} }
private fun unsubscribe(view: View, channelId: String) { private fun unsubscribe(context: Context, channelId: String) {
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
val sharedPref = val sharedPref =
view.context.getSharedPreferences("token", Context.MODE_PRIVATE) context.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.unsubscribe( RetrofitInstance.api.unsubscribe(
sharedPref?.getString("token", "")!!, sharedPref?.getString("token", "")!!,
Subscribe(channelId) Subscribe(channelId)