implement logic

This commit is contained in:
Bnyro 2022-11-17 18:58:53 +01:00
parent 79b08c2e5a
commit 214aaf6612
5 changed files with 123 additions and 36 deletions

View File

@ -0,0 +1,5 @@
package com.github.libretube.ui.extensions
fun <T> List<T>.withMaxSize(maxSize: Int): List<T> {
return this.filterIndexed { index, _ -> index < maxSize }
}

View File

@ -7,8 +7,13 @@ import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.databinding.FragmentHomeBinding
import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.models.HomeModel
import com.github.libretube.util.LocaleHelper
@ -47,7 +52,48 @@ class HomeFragment : BaseFragment() {
viewModel.fetchHome(requireContext(), LocaleHelper.getTrendingRegion(requireContext()))
}
viewModel.feed.observe(viewLifecycleOwner) {
binding.featuredTV.visibility = View.VISIBLE
binding.featuredRV.visibility = View.VISIBLE
binding.progress.visibility = View.GONE
binding.featuredRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
binding.featuredRV.adapter = VideosAdapter(
it.toMutableList(),
childFragmentManager,
forceMode = VideosAdapter.Companion.ForceMode.RELATED
)
}
viewModel.trending.observe(viewLifecycleOwner) {
if (it.isEmpty()) return@observe
binding.trendingTV.visibility = View.VISIBLE
binding.trendingRV.visibility = View.VISIBLE
binding.progress.visibility = View.GONE
binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
binding.trendingRV.adapter = VideosAdapter(
it.toMutableList(),
childFragmentManager,
forceMode = VideosAdapter.Companion.ForceMode.TRENDING
)
}
viewModel.playlists.observe(viewLifecycleOwner) {
if (it.isEmpty()) return@observe
binding.playlistsRV.visibility = View.VISIBLE
binding.playlistsTV.visibility = View.VISIBLE
binding.progress.visibility = View.GONE
binding.playlistsRV.layoutManager = LinearLayoutManager(context)
binding.playlistsRV.adapter = PlaylistsAdapter(it.toMutableList(), childFragmentManager)
binding.playlistsRV.adapter?.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
if (itemCount == 0) {
binding.playlistsRV.visibility = View.GONE
binding.playlistsTV.visibility = View.GONE
}
}
})
}
}
}

View File

@ -7,6 +7,7 @@ import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Playlists
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.ui.extensions.withMaxSize
import com.github.libretube.util.PreferenceHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -21,15 +22,26 @@ class HomeModel : ViewModel() {
val token = PreferenceHelper.getToken()
val appContext = context.applicationContext
runOrError(appContext) {
trending.postValue(RetrofitInstance.api.getTrending(trendingRegion))
if (trending.value.isNullOrEmpty()) {
trending.postValue(
RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(20)
)
}
}
runOrError(appContext) {
feed.postValue(RetrofitInstance.authApi.getFeed(token))
if (feed.value.isNullOrEmpty()) {
feed.postValue(
RetrofitInstance.authApi.getFeed(token).withMaxSize(20)
)
}
}
runOrError(appContext) {
playlists.postValue(RetrofitInstance.authApi.getUserPlaylists(token))
if (token == "" || !playlists.value.isNullOrEmpty()) return@runOrError
playlists.postValue(
RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20)
)
}
}

View File

@ -1,40 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="match_parent">
<TextView
android:id="@+id/featuredTV"
style="@style/HomeCategoryTitle"
android:text="@string/featured" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/featuredRV"
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="210dp"
android:layout_marginHorizontal="10dp" />
android:layout_height="match_parent"
android:layout_gravity="center" />
<TextView
android:id="@+id/trendingTV"
style="@style/HomeCategoryTitle"
android:text="@string/trending" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/trendingRV"
<ScrollView
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginHorizontal="10dp" />
android:layout_height="wrap_content">
<TextView
android:id="@+id/playlistsTV"
style="@style/HomeCategoryTitle"
android:text="@string/playlists" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlistsRV"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginHorizontal="10dp" />
<TextView
android:id="@+id/featuredTV"
style="@style/HomeCategoryTitle"
android:text="@string/featured" />
</LinearLayout>
<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" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/trendingRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:nestedScrollingEnabled="false"
android:visibility="gone" />
<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>
</FrameLayout>

View File

@ -213,8 +213,8 @@
<item name="android:textSize">20sp</item>
<item name="android:textColor">?attr/colorControlNormal</item>
<item name="android:padding">15dp</item>
<item name="background">?attr/selectableItemBackground</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:background">?attr/selectableItemBackground</item>
<item name="android:visibility">gone</item>
</style>