diff --git a/app/src/main/java/com/github/libretube/Subscriptions.kt b/app/src/main/java/com/github/libretube/Subscriptions.kt index bc26e717a..56ee74638 100644 --- a/app/src/main/java/com/github/libretube/Subscriptions.kt +++ b/app/src/main/java/com/github/libretube/Subscriptions.kt @@ -20,6 +20,7 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.github.libretube.adapters.SubscriptionAdapter @@ -82,7 +83,7 @@ class Subscriptions : Fragment() { toggleSubs.setOnClickListener { if (!channelRecView.isVisible) { if (!loadedSubbedChannels) { - channelRecView?.layoutManager = GridLayoutManager(context, 4) + channelRecView?.layoutManager = LinearLayoutManager(context) fetchChannels(channelRecView) loadedSubbedChannels = true } diff --git a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt index 7721a00c1..f2d75cd99 100644 --- a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt @@ -15,7 +15,7 @@ import com.github.libretube.obj.Comment import com.squareup.picasso.Picasso class CommentsAdapter(private val comments: MutableList) : - RecyclerView.Adapter() { + RecyclerView.Adapter() { fun updateItems(newItems: List) { var commentsSize = comments.size @@ -23,13 +23,13 @@ class CommentsAdapter(private val comments: MutableList) : notifyItemRangeInserted(commentsSize, newItems.size) } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentsViewHolder { var commentsView = LayoutInflater.from(parent.context).inflate(R.layout.comments_row, parent, false) - return ViewHolder(commentsView) + return CommentsViewHolder(commentsView) } - override fun onBindViewHolder(holder: ViewHolder, position: Int) { + override fun onBindViewHolder(holder: CommentsViewHolder, position: Int) { holder.v.findViewById(R.id.comment_infos).text = comments[position].author.toString() + " • " + comments[position].commentedTime.toString() @@ -68,7 +68,7 @@ class CommentsAdapter(private val comments: MutableList) : } } -class ViewHolder(val v: View) : RecyclerView.ViewHolder(v) { +class CommentsViewHolder(val v: View) : RecyclerView.ViewHolder(v) { init { } } 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 ced50de1d..f37c09928 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -23,7 +23,7 @@ class SearchAdapter( private val searchItems: MutableList, private val childFragmentManager: FragmentManager ) : - RecyclerView.Adapter() { + RecyclerView.Adapter() { fun updateItems(newItems: List) { var searchItemsSize = searchItems.size @@ -35,7 +35,7 @@ class SearchAdapter( return searchItems.size } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder1 { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder { val layout = when (viewType) { 0 -> R.layout.video_search_row 1 -> R.layout.channel_search_row @@ -44,10 +44,10 @@ class SearchAdapter( } val layoutInflater = LayoutInflater.from(parent.context) val cell = layoutInflater.inflate(layout, parent, false) - return CustomViewHolder1(cell, childFragmentManager) + return SearchViewHolder(cell, childFragmentManager) } - override fun onBindViewHolder(holder: CustomViewHolder1, position: Int) { + override fun onBindViewHolder(holder: SearchViewHolder, position: Int) { holder.bind(searchItems[position]) } @@ -61,7 +61,7 @@ class SearchAdapter( } } -class CustomViewHolder1( +class SearchViewHolder( private val v: View, private val childFragmentManager: FragmentManager ) : RecyclerView.ViewHolder(v) { diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index a122db98b..ca8c24bef 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -1,5 +1,7 @@ package com.github.libretube.adapters +import android.content.Context +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -9,11 +11,21 @@ import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.libretube.MainActivity import com.github.libretube.R +import com.github.libretube.obj.Subscribe import com.github.libretube.obj.Subscription +import com.github.libretube.util.RetrofitInstance import com.squareup.picasso.Picasso +import java.io.IOException +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import retrofit2.HttpException class SubscriptionChannelAdapter(private val subscriptions: MutableList) : RecyclerView.Adapter() { + val TAG = "SubChannelAdapter" + private var subscribed = true + private var isLoading = false override fun getItemCount(): Int { return subscriptions.size } @@ -32,9 +44,72 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList( + R.id.subscription_subscribe + ) + subscribeBtn.setOnClickListener { + if (!isLoading) { + isLoading = true + val channelId = subscription.url?.replace("/channel/", "")!! + if (subscribed) { + unsubscribe(holder.v.context, channelId) + subscribeBtn.text = holder.v.context.getString(R.string.subscribe) + } else { + subscribe(holder.v.context, channelId) + subscribeBtn.text = holder.v.context.getString(R.string.unsubscribe) + } + } + } + } + + private fun subscribe(context: Context, channelId: String) { + fun run() { + CoroutineScope(Dispatchers.IO).launch { + val response = try { + val sharedPref = context + .getSharedPreferences("token", Context.MODE_PRIVATE) + RetrofitInstance.api.subscribe( + 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 = true + isLoading = false + } + } + run() + } + + private fun unsubscribe(context: Context, channelId: 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(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() } } diff --git a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt index 6845eeca5..7d317ba48 100644 --- a/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/TrendingAdapter.kt @@ -23,18 +23,18 @@ import com.squareup.picasso.Picasso class TrendingAdapter( private val videoFeed: List, private val childFragmentManager: FragmentManager -) : RecyclerView.Adapter() { +) : RecyclerView.Adapter() { override fun getItemCount(): Int { return videoFeed.size } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrendingViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val cell = layoutInflater.inflate(R.layout.trending_row, parent, false) - return CustomViewHolder(cell) + return TrendingViewHolder(cell) } - override fun onBindViewHolder(holder: CustomViewHolder, position: Int) { + override fun onBindViewHolder(holder: TrendingViewHolder, position: Int) { val trending = videoFeed[position] holder.v.findViewById(R.id.textView_title).text = trending.title holder.v.findViewById(R.id.textView_channel).text = @@ -89,7 +89,7 @@ class TrendingAdapter( } } -class CustomViewHolder(val v: View) : RecyclerView.ViewHolder(v) { +class TrendingViewHolder(val v: View) : RecyclerView.ViewHolder(v) { init { } } diff --git a/app/src/main/res/layout/channel_subscription_row.xml b/app/src/main/res/layout/channel_subscription_row.xml index 1f2628518..899a81d3c 100644 --- a/app/src/main/res/layout/channel_subscription_row.xml +++ b/app/src/main/res/layout/channel_subscription_row.xml @@ -1,25 +1,40 @@ - + android:layout_centerVertical="true" + android:layout_width="40dp" + android:layout_height="40dp" /> - \ No newline at end of file + android:maxLines="1" + android:text="Channel Name" + android:textSize="16dp" /> + + + \ No newline at end of file