LibreTube/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt

119 lines
4.4 KiB
Kotlin
Raw Normal View History

2022-02-12 20:00:36 +05:30
package com.github.libretube.adapters
2022-06-04 21:04:18 +05:30
import android.content.Context
import android.util.Log
2022-02-12 20:00:36 +05:30
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView
2022-07-01 20:24:20 +05:30
import com.github.libretube.activities.MainActivity
2022-02-12 20:00:36 +05:30
import com.github.libretube.R
import com.github.libretube.databinding.ChannelSubscriptionRowBinding
2022-06-04 21:04:18 +05:30
import com.github.libretube.obj.Subscribe
2022-02-12 20:00:36 +05:30
import com.github.libretube.obj.Subscription
2022-06-26 14:38:10 +05:30
import com.github.libretube.util.PreferenceHelper
2022-06-04 21:04:18 +05:30
import com.github.libretube.util.RetrofitInstance
2022-02-12 20:00:36 +05:30
import com.squareup.picasso.Picasso
2022-06-04 21:04:18 +05:30
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import retrofit2.HttpException
2022-06-24 20:56:36 +05:30
import java.io.IOException
2022-02-12 20:00:36 +05:30
2022-05-21 13:32:04 +05:30
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
2022-06-04 21:04:18 +05:30
val TAG = "SubChannelAdapter"
private lateinit var binding: ChannelSubscriptionRowBinding
2022-06-04 22:35:27 +05:30
private var subscribed = true
private var isLoading = false
2022-02-12 20:00:36 +05:30
override fun getItemCount(): Int {
return subscriptions.size
}
2022-05-21 13:32:04 +05:30
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
SubscriptionChannelViewHolder {
2022-02-12 20:00:36 +05:30
val layoutInflater = LayoutInflater.from(parent.context)
binding = ChannelSubscriptionRowBinding.inflate(layoutInflater, parent, false)
return SubscriptionChannelViewHolder(binding.root)
2022-02-12 20:00:36 +05:30
}
override fun onBindViewHolder(holder: SubscriptionChannelViewHolder, position: Int) {
val subscription = subscriptions[position]
binding.apply {
subscriptionChannelName.text = subscription.name
Picasso.get().load(subscription.avatar).into(subscriptionChannelImage)
root.setOnClickListener {
val activity = holder.v.context as MainActivity
val bundle = bundleOf("channel_id" to subscription.url)
activity.navController.navigate(R.id.channel, bundle)
}
subscriptionSubscribe.setOnClickListener {
if (!isLoading) {
isLoading = true
val channelId = subscription.url?.replace("/channel/", "")!!
if (subscribed) {
unsubscribe(root.context, channelId)
subscriptionSubscribe.text = holder.v.context.getString(R.string.subscribe)
} else {
subscribe(root.context, channelId)
2022-07-01 15:41:30 +05:30
subscriptionSubscribe.text =
holder.v.context.getString(R.string.unsubscribe)
}
2022-06-04 22:35:27 +05:30
}
}
}
}
2022-06-04 22:43:11 +05:30
private fun subscribe(context: Context, channelId: String) {
2022-06-04 22:35:27 +05:30
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
2022-06-26 14:38:10 +05:30
val token = PreferenceHelper.getToken(context)
2022-06-04 22:35:27 +05:30
RetrofitInstance.api.subscribe(
2022-06-26 14:38:10 +05:30
token,
2022-06-04 22:35:27 +05:30
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
}
2022-06-04 21:04:18 +05:30
}
2022-06-04 22:35:27 +05:30
run()
2022-06-04 21:04:18 +05:30
}
2022-06-04 22:43:11 +05:30
private fun unsubscribe(context: Context, channelId: String) {
2022-06-04 21:04:18 +05:30
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
2022-06-26 14:38:10 +05:30
val token = PreferenceHelper.getToken(context)
2022-06-04 21:04:18 +05:30
RetrofitInstance.api.unsubscribe(
2022-06-26 14:38:10 +05:30
token,
2022-06-04 22:35:27 +05:30
Subscribe(channelId)
2022-06-04 21:04:18 +05:30
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
}
2022-06-04 22:35:27 +05:30
subscribed = false
isLoading = false
2022-06-04 21:04:18 +05:30
}
}
run()
2022-02-12 20:00:36 +05:30
}
}
2022-05-21 13:32:04 +05:30
2022-05-20 03:52:10 +05:30
class SubscriptionChannelViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
2022-02-12 20:00:36 +05:30
init {
}
2022-05-20 03:52:10 +05:30
}