added resub

This commit is contained in:
Bnyro 2022-06-04 19:05:27 +02:00
parent 83d634bc95
commit 8e6e5b734f
2 changed files with 53 additions and 17 deletions

View File

@ -5,7 +5,6 @@ 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
import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
@ -25,6 +24,8 @@ import java.io.IOException
class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) : class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscription>) :
RecyclerView.Adapter<SubscriptionChannelViewHolder>() { RecyclerView.Adapter<SubscriptionChannelViewHolder>() {
val TAG = "SubChannelAdapter" 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
} }
@ -43,24 +44,33 @@ 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 unsubscribeBtn = holder.v.findViewById<com.google.android.material.button.MaterialButton>(R.id.subs_subscribe) val subscribeBtn = holder.v.findViewById<com.google.android.material.button.MaterialButton>(R.id.subscription_subscribe)
unsubscribeBtn.setOnClickListener { subscribeBtn.setOnClickListener {
val channel_id = subscription.url?.replace("/channel/", "")!! if (!isLoading) {
unsubscribe(holder.v.context, channel_id) isLoading = true
val channelId = subscription.url?.replace("/channel/", "")!!
if (subscribed) {
unsubscribe(holder.v, channelId)
subscribeBtn.text = holder.v.context.getString(R.string.subscribe)
} else {
subscribe(holder.v, channelId)
subscribeBtn.text = holder.v.context.getString(R.string.unsubscribe)
}
}
} }
} }
private fun unsubscribe(context: Context, channel_id: String) { private fun subscribe(view: View, channelId: String) {
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = view.context.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.unsubscribe( RetrofitInstance.api.subscribe(
sharedPref?.getString("token", "")!!, sharedPref?.getString("token", "")!!,
Subscribe(channel_id) Subscribe(channelId)
) )
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
@ -68,6 +78,31 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response") Log.e(TAG, "HttpException, unexpected response")
} }
subscribed = true
isLoading = false
}
}
run()
}
private fun unsubscribe(view: View, channelId: String) {
fun run() {
CoroutineScope(Dispatchers.IO).launch {
val response = try {
val sharedPref =
view.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() run()

View File

@ -3,7 +3,10 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="8dp" android:layout_marginLeft="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
@ -25,15 +28,13 @@
android:textSize="16dp" /> android:textSize="16dp" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/subs_subscribe" android:id="@+id/subscription_subscribe"
style="@style/Widget.Material3.Button.ElevatedButton" style="@style/Widget.Material3.Button.ElevatedButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:backgroundTint="?attr/colorOnPrimary"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:drawableLeft="@drawable/ic_bell" android:backgroundTint="?attr/colorOnPrimary"
android:drawableTint="@color/white"
android:textColor="@color/white"
android:text="@string/unsubscribe" android:text="@string/unsubscribe"
app:cornerRadius="11dp" /> android:textColor="@color/white"
app:cornerRadius="20dp" />
</RelativeLayout> </RelativeLayout>