improve home section

This commit is contained in:
Bnyro 2022-11-18 16:23:45 +01:00
parent 42e326f9b8
commit 89be23a858
4 changed files with 79 additions and 58 deletions

View File

@ -60,7 +60,7 @@ class VideosAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideosViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideosViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
return when { return when {
forceMode in listOf(ForceMode.TRENDING, ForceMode.RELATED) -> VideosViewHolder(TrendingRowBinding.inflate(layoutInflater, parent, false)) forceMode in listOf(ForceMode.TRENDING, ForceMode.RELATED, ForceMode.HOME) -> VideosViewHolder(TrendingRowBinding.inflate(layoutInflater, parent, false))
forceMode == ForceMode.CHANNEL -> VideosViewHolder(VideoRowBinding.inflate(layoutInflater, parent, false)) forceMode == ForceMode.CHANNEL -> VideosViewHolder(VideoRowBinding.inflate(layoutInflater, parent, false))
PreferenceHelper.getBoolean( PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT, PreferenceKeys.ALTERNATIVE_VIDEOS_LAYOUT,
@ -83,11 +83,14 @@ class VideosAdapter(
// Trending layout // Trending layout
holder.trendingRowBinding?.apply { holder.trendingRowBinding?.apply {
if (forceMode == ForceMode.RELATED) { // set a fixed width for better visuals
val params = root.layoutParams val params = root.layoutParams
params.width = (180).toDp(root.context.resources).toInt() when (forceMode) {
root.layoutParams = params ForceMode.RELATED -> params.width = (180).toDp(root.context.resources).toInt()
ForceMode.HOME -> params.width = (250).toDp(root.context.resources).toInt()
else -> {}
} }
root.layoutParams = params
textViewTitle.text = video.title textViewTitle.text = video.title
textViewChannel.text = textViewChannel.text =
@ -168,7 +171,8 @@ class VideosAdapter(
TRENDING, TRENDING,
ROW, ROW,
CHANNEL, CHANNEL,
RELATED RELATED,
HOME
} }
fun getLayout(context: Context): LayoutManager { fun getLayout(context: Context): LayoutManager {

View File

@ -1,6 +1,5 @@
package com.github.libretube.ui.fragments package com.github.libretube.ui.fragments
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -51,29 +50,37 @@ class HomeFragment : BaseFragment() {
findNavController().navigate(R.id.libraryFragment) findNavController().navigate(R.id.libraryFragment)
} }
binding.refresh.setOnRefreshListener {
binding.refresh.isRefreshing = true
lifecycleScope.launch(Dispatchers.IO) {
fetchHome(LocaleHelper.getTrendingRegion(requireContext()))
}
}
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
fetchHome(requireContext(), LocaleHelper.getTrendingRegion(requireContext())) fetchHome(LocaleHelper.getTrendingRegion(requireContext()))
} }
} }
private suspend fun fetchHome(context: Context, trendingRegion: String) { private suspend fun fetchHome(trendingRegion: String) {
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()
val appContext = context.applicationContext runOrError {
runOrError(appContext) {
val feed = SubscriptionHelper.getFeed().withMaxSize(20) val feed = SubscriptionHelper.getFeed().withMaxSize(20)
if (feed.isEmpty()) return@runOrError
runOnUiThread { runOnUiThread {
makeVisible(binding.featuredRV, binding.featuredTV) makeVisible(binding.featuredRV, binding.featuredTV)
binding.featuredRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) binding.featuredRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
binding.featuredRV.adapter = VideosAdapter( binding.featuredRV.adapter = VideosAdapter(
feed.toMutableList(), feed.toMutableList(),
childFragmentManager, childFragmentManager,
forceMode = VideosAdapter.Companion.ForceMode.RELATED forceMode = VideosAdapter.Companion.ForceMode.HOME
) )
} }
} }
runOrError(appContext) { runOrError {
val trending = RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10) val trending = RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10)
if (trending.isEmpty()) return@runOrError
runOnUiThread { runOnUiThread {
makeVisible(binding.trendingRV, binding.trendingTV) makeVisible(binding.trendingRV, binding.trendingTV)
binding.trendingRV.layoutManager = GridLayoutManager(context, 2) binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
@ -85,8 +92,9 @@ class HomeFragment : BaseFragment() {
} }
} }
runOrError(appContext) { runOrError {
val playlists = RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20) val playlists = RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20)
if (playlists.isEmpty()) return@runOrError
runOnUiThread { runOnUiThread {
makeVisible(binding.playlistsRV, binding.playlistsTV) makeVisible(binding.playlistsRV, binding.playlistsTV)
binding.playlistsRV.layoutManager = LinearLayoutManager(context) binding.playlistsRV.layoutManager = LinearLayoutManager(context)
@ -105,12 +113,12 @@ class HomeFragment : BaseFragment() {
} }
} }
private fun runOrError(context: Context, action: suspend () -> Unit) { private fun runOrError(action: suspend () -> Unit) {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
try { try {
action.invoke() action.invoke()
} catch (e: Exception) { } catch (e: Exception) {
e.localizedMessage?.let { context.toastFromMainThread(it) } e.localizedMessage?.let { context?.toastFromMainThread(it) }
} }
} }
} }
@ -120,5 +128,7 @@ class HomeFragment : BaseFragment() {
it.visibility = View.VISIBLE it.visibility = View.VISIBLE
} }
binding.progress.visibility = View.GONE binding.progress.visibility = View.GONE
binding.scroll.visibility = View.VISIBLE
binding.refresh.isRefreshing = false
} }
} }

View File

@ -3,7 +3,6 @@ package com.github.libretube.ui.models
import android.util.Log import android.util.Log
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Subscription import com.github.libretube.api.obj.Subscription

View File

@ -9,64 +9,72 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center" /> android:layout_gravity="center" />
<ScrollView <com.github.libretube.ui.views.CustomSwipeToRefresh
android:id="@+id/scroll" android:id="@+id/refresh"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<LinearLayout <ScrollView
android:id="@+id/scroll"
android:visibility="gone"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:orientation="vertical"
android:paddingBottom="10dp">
<TextView <LinearLayout
android:id="@+id/featuredTV"
style="@style/HomeCategoryTitle"
android:text="@string/featured" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/featuredRV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:nestedScrollingEnabled="false"
android:visibility="gone" />
<TextView
android:id="@+id/trendingTV"
style="@style/HomeCategoryTitle"
android:text="@string/trending" />
<RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"> android:orientation="vertical"
android:paddingBottom="10dp">
<TextView
android:id="@+id/featuredTV"
style="@style/HomeCategoryTitle"
android:text="@string/featured" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/trendingRV" android:id="@+id/featuredRV"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp" android:layout_marginHorizontal="10dp"
android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false"
android:visibility="gone" /> android:visibility="gone" />
</RelativeLayout> <TextView
android:id="@+id/trendingTV"
style="@style/HomeCategoryTitle"
android:text="@string/trending" />
<TextView <RelativeLayout
android:id="@+id/playlistsTV" android:layout_width="match_parent"
style="@style/HomeCategoryTitle" android:layout_height="wrap_content"
android:text="@string/playlists" /> android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlistsRV" android:id="@+id/trendingRV"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:nestedScrollingEnabled="false" android:layout_marginHorizontal="10dp"
android:visibility="gone" /> android:nestedScrollingEnabled="false"
android:visibility="gone" />
</LinearLayout> </RelativeLayout>
</ScrollView> <TextView
android:id="@+id/playlistsTV"
style="@style/HomeCategoryTitle"
android:text="@string/playlists" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlistsRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
</com.github.libretube.ui.views.CustomSwipeToRefresh>
</FrameLayout> </FrameLayout>