add alternative trending layout

This commit is contained in:
Bnyro 2022-09-10 11:37:30 +02:00
parent ec306f83dc
commit b60c63d48f
4 changed files with 36 additions and 10 deletions

View File

@ -33,6 +33,7 @@ object PreferenceKeys {
const val APP_ICON = "icon_change" const val APP_ICON = "icon_change"
const val LEGACY_SUBSCRIPTIONS = "legacy_subscriptions" const val LEGACY_SUBSCRIPTIONS = "legacy_subscriptions"
const val LEGACY_SUBSCRIPTIONS_COLUMNS = "legacy_subscriptions_columns" const val LEGACY_SUBSCRIPTIONS_COLUMNS = "legacy_subscriptions_columns"
const val ALTERNATIVE_TRENDING_LAYOUT = "trending_layout"
/** /**
* Instance * Instance

View File

@ -8,7 +8,9 @@ import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.ChannelAdapter
import com.github.libretube.adapters.TrendingAdapter import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
@ -41,11 +43,6 @@ class HomeFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val grid = PreferenceHelper.getString(
PreferenceKeys.GRID_COLUMNS,
resources.getInteger(R.integer.grid_items).toString()
)
val regionPref = PreferenceHelper.getString(PreferenceKeys.REGION, "sys") val regionPref = PreferenceHelper.getString(PreferenceKeys.REGION, "sys")
// get the system default country if auto region selected // get the system default country if auto region selected
@ -57,15 +54,14 @@ class HomeFragment : BaseFragment() {
regionPref regionPref
} }
binding.recview.layoutManager = GridLayoutManager(view.context, grid.toInt()) fetchTrending()
fetchJson()
binding.homeRefresh.isEnabled = true binding.homeRefresh.isEnabled = true
binding.homeRefresh.setOnRefreshListener { binding.homeRefresh.setOnRefreshListener {
fetchJson() fetchTrending()
} }
} }
private fun fetchJson() { private fun fetchTrending() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
@ -84,10 +80,32 @@ class HomeFragment : BaseFragment() {
} }
runOnUiThread { runOnUiThread {
binding.progressBar.visibility = View.GONE binding.progressBar.visibility = View.GONE
if (
PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_TRENDING_LAYOUT,
false
)
) {
binding.recview.layoutManager = LinearLayoutManager(context)
binding.recview.adapter = ChannelAdapter(
response.toMutableList(),
childFragmentManager
)
} else {
binding.recview.layoutManager = GridLayoutManager(
context,
PreferenceHelper.getString(
PreferenceKeys.GRID_COLUMNS,
resources.getInteger(R.integer.grid_items).toString()
).toInt()
)
binding.recview.adapter = TrendingAdapter(response, childFragmentManager) binding.recview.adapter = TrendingAdapter(response, childFragmentManager)
} }
} }
} }
}
run() run()
} }
} }

View File

@ -315,4 +315,5 @@
<string name="device_info">Device Info</string> <string name="device_info">Device Info</string>
<string name="audio_video_summary">Quality and format</string> <string name="audio_video_summary">Quality and format</string>
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="trending_layout">Alternative trending layout</string>
</resources> </resources>

View File

@ -94,6 +94,12 @@
app:key="legacy_subscriptions_columns" app:key="legacy_subscriptions_columns"
app:title="@string/grid" /> app:title="@string/grid" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_trending"
app:defaultValue="false"
app:key="trending_layout"
app:title="@string/trending_layout" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>