sub channels rewrite

This commit is contained in:
Bnyro 2022-06-04 17:34:18 +02:00
parent d2c4b41618
commit 83d634bc95
3 changed files with 65 additions and 14 deletions

View File

@ -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
} }

View File

@ -1,19 +1,30 @@
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
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
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 kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
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>() {
val TAG = "SubChannelAdapter"
override fun getItemCount(): Int { override fun getItemCount(): Int {
return subscriptions.size return subscriptions.size
} }
@ -35,6 +46,31 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
val bundle = bundleOf("channel_id" to subscription.url) val bundle = bundleOf("channel_id" 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)
unsubscribeBtn.setOnClickListener {
val channel_id = subscription.url?.replace("/channel/", "")!!
unsubscribe(holder.v.context, channel_id)
}
}
private fun unsubscribe(context: Context, channel_id: 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(channel_id)
)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response")
}
}
}
run()
} }
} }

View File

@ -1,25 +1,39 @@
<?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_margin="8dp"
android:padding="8dp"
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/subs_subscribe"
style="@style/Widget.Material3.Button.ElevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="?attr/colorOnPrimary"
android:layout_alignParentEnd="true"
android:drawableLeft="@drawable/ic_bell"
android:drawableTint="@color/white"
android:textColor="@color/white"
android:text="@string/unsubscribe"
app:cornerRadius="11dp" />
</RelativeLayout>