From 5500d9469e635fe14055abc47f7d686c239f55f8 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 11 Jul 2022 15:00:33 +0200 Subject: [PATCH] add sub button to search channels --- .../libretube/adapters/SearchAdapter.kt | 78 +++++++++++++++++++ .../main/res/layout/channel_search_row.xml | 9 +++ 2 files changed, 87 insertions(+) diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index 55748f397..808255a5b 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -3,6 +3,7 @@ package com.github.libretube.adapters import android.os.Bundle import android.text.format.DateUtils import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf @@ -17,8 +18,15 @@ import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.fragments.PlayerFragment import com.github.libretube.obj.SearchItem +import com.github.libretube.obj.Subscribe +import com.github.libretube.preferences.PreferenceHelper +import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.formatShort import com.squareup.picasso.Picasso +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import java.io.IOException class SearchAdapter( private val searchItems: MutableList, @@ -134,6 +142,76 @@ class SearchAdapter( val bundle = bundleOf("channel_id" to item.url) activity.navController.navigate(R.id.channelFragment, bundle) } + val channelId = item.url?.replace("/channel/", "")!! + val token = PreferenceHelper.getToken(root.context) + + // only show subscribe button if logged in + if (token != "") isSubscribed(channelId, token, binding) + } + } + + private fun isSubscribed(channelId: String, token: String, binding: ChannelSearchRowBinding) { + var isSubscribed = false + + // check whether the user subscribed to the channel + CoroutineScope(Dispatchers.Main).launch { + val response = try { + RetrofitInstance.authApi.isSubscribed( + channelId, + token + ) + } catch (e: Exception) { + return@launch + } + + // if subscribed change text to unsubscribe + if (response.subscribed == true) { + isSubscribed = true + binding.searchSubButton.text = binding.root.context.getString(R.string.unsubscribe) + } + + // make sub button visible and set the on click listeners to (un)subscribe + if (response.subscribed != null) { + binding.searchSubButton.visibility = View.VISIBLE + + binding.searchSubButton.setOnClickListener { + if (!isSubscribed) { + subscribe(token, channelId) + binding.searchSubButton.text = binding.root.context.getString(R.string.unsubscribe) + isSubscribed = true + } else { + unsubscribe(token, channelId) + binding.searchSubButton.text = binding.root.context.getString(R.string.subscribe) + isSubscribed = false + } + } + } + } + } + + private fun subscribe(token: String, channelId: String) { + CoroutineScope(Dispatchers.IO).launch { + try { + RetrofitInstance.authApi.subscribe( + token, + Subscribe(channelId) + ) + } catch (e: Exception) { + return@launch + } + } + } + + private fun unsubscribe(token: String, channelId: String) { + CoroutineScope(Dispatchers.IO).launch { + try { + RetrofitInstance.authApi.unsubscribe( + token, + Subscribe(channelId) + ) + } catch (e: IOException) { + return@launch + } } } diff --git a/app/src/main/res/layout/channel_search_row.xml b/app/src/main/res/layout/channel_search_row.xml index c3165df7a..5c527e94d 100644 --- a/app/src/main/res/layout/channel_search_row.xml +++ b/app/src/main/res/layout/channel_search_row.xml @@ -36,6 +36,15 @@ android:id="@+id/search_views" android:layout_width="wrap_content" android:layout_height="wrap_content" /> + + \ No newline at end of file