From 9c7b0e6b1dcadbc9fbcb65dd95428aad0cccea41 Mon Sep 17 00:00:00 2001 From: rimthekid Date: Sat, 12 Feb 2022 18:30:36 +0400 Subject: [PATCH] subscription --- .../java/com/github/libretube/MainActivity.kt | 4 +- .../com/github/libretube/Subscriptions.kt | 129 +++++++++++++----- .../libretube/adapters/SubscriptionAdapter.kt | 80 +++++++++++ .../adapters/SubscriptionChannelAdapter.kt | 45 ++++++ app/src/main/res/drawable/ic_login.xml | 10 ++ .../res/layout/channel_subscription_row.xml | 24 ++++ .../res/layout/fragment_subscriptions.xml | 56 ++++++-- app/src/main/res/values/strings.xml | 1 + 8 files changed, 303 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt create mode 100644 app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt create mode 100644 app/src/main/res/drawable/ic_login.xml create mode 100644 app/src/main/res/layout/channel_subscription_row.xml diff --git a/app/src/main/java/com/github/libretube/MainActivity.kt b/app/src/main/java/com/github/libretube/MainActivity.kt index 15858e107..701fa4383 100644 --- a/app/src/main/java/com/github/libretube/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/MainActivity.kt @@ -129,10 +129,10 @@ class MainActivity : AppCompatActivity() { val orientation = newConfig.orientation if (orientation == Configuration.ORIENTATION_PORTRAIT) { println("Portrait") - unsetFullscreen() + //unsetFullscreen() } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { println("Landscape") - setFullscreen() + //setFullscreen() } } private fun setFullscreen() { diff --git a/app/src/main/java/com/github/libretube/Subscriptions.kt b/app/src/main/java/com/github/libretube/Subscriptions.kt index 91638549f..87afd3ea5 100644 --- a/app/src/main/java/com/github/libretube/Subscriptions.kt +++ b/app/src/main/java/com/github/libretube/Subscriptions.kt @@ -8,28 +8,26 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.ImageView +import android.widget.* +import androidx.lifecycle.lifecycleScope +import androidx.preference.PreferenceManager +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.adapters.SubscriptionAdapter +import com.github.libretube.adapters.SubscriptionChannelAdapter +import com.github.libretube.adapters.TrendingAdapter +import retrofit2.HttpException +import java.io.IOException -// TODO: Rename parameter arguments, choose names that match -// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER -private const val ARG_PARAM1 = "param1" -private const val ARG_PARAM2 = "param2" - -/** - * A simple [Fragment] subclass. - * Use the [Subscriptions.newInstance] factory method to - * create an instance of this fragment. - */ class Subscriptions : Fragment() { - // TODO: Rename and change types of parameters - private var param1: String? = null - private var param2: String? = null - + val TAG = "SubFragment" + lateinit var token: String + var isLoaded = false + private var subscriptionAdapter: SubscriptionAdapter? =null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { - param1 = it.getString(ARG_PARAM1) - param2 = it.getString(ARG_PARAM2) } } @@ -44,27 +42,86 @@ class Subscriptions : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) - Log.e("dafaq",sharedPref?.getString("token","")!!) + token = sharedPref?.getString("token","")!! + Log.e(TAG,token) + if(token!=""){ + view.findViewById(R.id.loginOrRegister).visibility=View.GONE + var progressBar = view.findViewById(R.id.sub_progress) + progressBar.visibility=View.VISIBLE - } + var channelRecView = view.findViewById(R.id.sub_channels) + channelRecView?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) + fetchChannels(channelRecView) - companion object { - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment Subscriptions. - */ - // TODO: Rename and change types and number of parameters - @JvmStatic - fun newInstance(param1: String, param2: String) = - Subscriptions().apply { - arguments = Bundle().apply { - putString(ARG_PARAM1, param1) - putString(ARG_PARAM2, param2) + var feedRecView = view.findViewById(R.id.sub_feed) + feedRecView.layoutManager = GridLayoutManager(view.context, resources.getInteger(R.integer.grid_items)) + fetchFeed(feedRecView, progressBar) + + val scrollView = view.findViewById(R.id.scrollview_sub) + scrollView.viewTreeObserver + .addOnScrollChangedListener { + if (scrollView.getChildAt(0).bottom + == (scrollView.height + scrollView.scrollY)) { + //scroll view is at bottom + if(isLoaded){ + subscriptionAdapter?.updateItems() + } + + } } - } + } } + + private fun fetchFeed(feedRecView: RecyclerView, progressBar: ProgressBar) { + fun run() { + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.api.getFeed(token) + }catch(e: IOException) { + println(e) + Log.e(TAG, "IOException, you might not have internet connection") + return@launchWhenCreated + } catch (e: HttpException) { + Log.e(TAG, "HttpException, unexpected response") + return@launchWhenCreated + } + subscriptionAdapter = SubscriptionAdapter(response) + feedRecView?.adapter= subscriptionAdapter + progressBar.visibility=View.GONE + isLoaded=true + } + } + run() + } + + private fun fetchChannels(channelRecView: RecyclerView) { + fun run() { + lifecycleScope.launchWhenCreated { + val response = try { + RetrofitInstance.api.subscriptions(token) + }catch(e: IOException) { + println(e) + Log.e(TAG, "IOException, you might not have internet connection") + return@launchWhenCreated + } catch (e: HttpException) { + Log.e(TAG, "HttpException, unexpected response") + return@launchWhenCreated + } + channelRecView?.adapter=SubscriptionChannelAdapter(response.toMutableList()) + } + } + run() + } + + override fun onStop() { + Log.e(TAG,"Stopped") + subscriptionAdapter = null + view?.findViewById(R.id.sub_feed)?.adapter=null + super.onStop() + } + override fun onDestroy() { + Log.e(TAG,"Destroyed") + super.onDestroy() + } + } \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt new file mode 100644 index 000000000..3ae808233 --- /dev/null +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt @@ -0,0 +1,80 @@ +package com.github.libretube.adapters + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.constraintlayout.motion.widget.MotionLayout +import androidx.core.os.bundleOf +import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.MainActivity +import com.squareup.picasso.Picasso +import com.github.libretube.PlayerFragment +import com.github.libretube.R +import com.github.libretube.obj.StreamItem +import com.github.libretube.videoViews + +class SubscriptionAdapter(private val videoFeed: List): RecyclerView.Adapter() { + //private var limitedVideoFeed: MutableList = [""].toMutableList() + var i = 10 + override fun getItemCount(): Int { + return i + } + + fun updateItems(){ + //limitedVideoFeed.add("") + i += 10 + //println("suck another dick: "+newItems[0].title) + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubscriptionViewHolder { + val layoutInflater = LayoutInflater.from(parent.context) + val cell = layoutInflater.inflate(R.layout.trending_row,parent,false) + return SubscriptionViewHolder(cell) + } + + override fun onBindViewHolder(holder: SubscriptionViewHolder, position: Int) { + val trending = videoFeed[position] + holder.v.findViewById(R.id.textView_title).text = trending.title + holder.v.findViewById(R.id.textView_channel).text = trending.uploaderName +" • "+ trending.views.videoViews()+" • "+trending.uploadedDate + val thumbnailImage = holder.v.findViewById(R.id.thumbnail) + val channelImage = holder.v.findViewById(R.id.channel_image) + channelImage.setOnClickListener{ + val activity = holder.v.context as MainActivity + val bundle = bundleOf("channel_id" to trending.uploaderUrl) + activity.navController.navigate(R.id.channel, bundle) + try { + val mainMotionLayout = activity.findViewById(R.id.mainMotionLayout) + if (mainMotionLayout.progress == 0.toFloat()) { + mainMotionLayout.transitionToEnd() + activity.findViewById(R.id.playerMotionLayout).transitionToEnd() + } + }catch (e: Exception){ + + } + } + Picasso.get().load(trending.thumbnail).into(thumbnailImage) + Picasso.get().load(trending.uploaderAvatar).into(channelImage) + holder.v.setOnClickListener{ + var bundle = Bundle() + bundle.putString("videoId",trending.url!!.replace("/watch?v=","")) + var frag = PlayerFragment() + frag.arguments = bundle + val activity = holder.v.context as AppCompatActivity + activity.supportFragmentManager.beginTransaction() + .remove(PlayerFragment()) + .commit() + activity.supportFragmentManager.beginTransaction() + .replace(R.id.container, frag) + .commitNow() + } + } +} +class SubscriptionViewHolder(val v: View): RecyclerView.ViewHolder(v){ + init { + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt new file mode 100644 index 000000000..6c357fc3c --- /dev/null +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -0,0 +1,45 @@ +package com.github.libretube.adapters + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.core.os.bundleOf +import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.MainActivity +import com.github.libretube.PlayerFragment +import com.github.libretube.R +import com.github.libretube.obj.Subscription +import com.squareup.picasso.Picasso +import org.w3c.dom.Text + +class SubscriptionChannelAdapter(private val subscriptions: MutableList): RecyclerView.Adapter() { + override fun getItemCount(): Int { + return subscriptions.size + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubscriptionChannelViewHolder { + val layoutInflater = LayoutInflater.from(parent.context) + val cell = layoutInflater.inflate(R.layout.channel_subscription_row,parent,false) + return SubscriptionChannelViewHolder(cell) + } + + override fun onBindViewHolder(holder: SubscriptionChannelViewHolder, position: Int) { + val subscription = subscriptions[position] + holder.v.findViewById(R.id.subscription_channel_name).text=subscription.name + val avatar = holder.v.findViewById(R.id.subscription_channel_image) + Picasso.get().load(subscription.avatar).into(avatar) + holder.v.setOnClickListener{ + val activity = holder.v.context as MainActivity + val bundle = bundleOf("channel_id" to subscription.url) + activity.navController.navigate(R.id.channel,bundle) + } + } +} +class SubscriptionChannelViewHolder(val v: View): RecyclerView.ViewHolder(v){ + init { + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_login.xml b/app/src/main/res/drawable/ic_login.xml new file mode 100644 index 000000000..0d9837d72 --- /dev/null +++ b/app/src/main/res/drawable/ic_login.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/channel_subscription_row.xml b/app/src/main/res/layout/channel_subscription_row.xml new file mode 100644 index 000000000..c9d1df987 --- /dev/null +++ b/app/src/main/res/layout/channel_subscription_row.xml @@ -0,0 +1,24 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_subscriptions.xml b/app/src/main/res/layout/fragment_subscriptions.xml index d1ef4398a..401303438 100644 --- a/app/src/main/res/layout/fragment_subscriptions.xml +++ b/app/src/main/res/layout/fragment_subscriptions.xml @@ -1,14 +1,22 @@ - + @@ -19,17 +27,49 @@ android:layout_height="100dp" android:layout_centerInParent="true" android:layout_marginBottom="16dp" - android:src="@drawable/ic_campaign" /> + android:src="@drawable/ic_login" /> + + android:textStyle="bold" /> + + + - \ No newline at end of file + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5ed12f887..73a71d524 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,4 +22,5 @@ Add a custom instance Choose a region Login/Register + Please Login or Register from the settings to show your Subscriptions! \ No newline at end of file