Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preference to edit the home tab content #3866

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object PreferenceKeys {
const val NEW_VIDEOS_BADGE = "new_videos_badge"
const val PLAYLISTS_ORDER = "playlists_order"
const val PLAYLIST_SORT_ORDER = "playlist_sort_order"
const val HOME_TAB_CONTENT = "home_tab_content"

/**
* Instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ object PreferenceHelper {
return settings.getFloat(key, defValue)
}

fun getStringSet(key: String?, defValue: Set<String>): Set<String> {
return settings.getStringSet(key, defValue).orEmpty()
}

fun clearPreferences() {
editor.clear().apply()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.libretube.ui.base

import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.MultiSelectListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.R
Expand Down Expand Up @@ -42,6 +43,24 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
.setNegativeButton(R.string.cancel, null)
.show()
}
is MultiSelectListPreference -> {
val selectedItems = preference.entryValues.map {
preference.values.contains(it)
}.toBooleanArray()
MaterialAlertDialogBuilder(requireContext())
.setTitle(preference.title)
.setMultiChoiceItems(preference.entries, selectedItems) { dialog, _, _ ->
val newValues = preference.entryValues
.filterIndexed { index, _ -> selectedItems[index] }
.map { it.toString() }
.toMutableSet()
if (preference.callChangeListener(newValues)) {
preference.values = newValues
}
}
.setNegativeButton(R.string.cancel, null)
.show()
}
is EditTextPreference -> {
val binding = DialogTextPreferenceBinding.inflate(layoutInflater)
binding.input.setText(preference.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ class HomeFragment : Fragment() {
private fun fetchHomeFeed() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val defaultItems = resources.getStringArray(R.array.homeTabItemsValues)
val visibleItems = PreferenceHelper
.getStringSet(PreferenceKeys.HOME_TAB_CONTENT, defaultItems.toSet())
awaitAll(
async { loadTrending() },
async { loadBookmarks() },
async { loadFeed() },
async { loadPlaylists() },
async { if (visibleItems.contains(TRENDING)) loadTrending() },
async { if (visibleItems.contains(BOOKMARKS)) loadBookmarks() },
async { if (visibleItems.contains(FEATURED)) loadFeed() },
async { if (visibleItems.contains(PLAYLISTS)) loadPlaylists() },
)
}
}
Expand Down Expand Up @@ -194,4 +197,12 @@ class HomeFragment : Fragment() {
binding.scroll.visibility = View.VISIBLE
binding.refresh.isRefreshing = false
}

companion object {
// The values of the preference entries for the home tab content
private const val FEATURED = "featured"
private const val TRENDING = "trending"
private const val BOOKMARKS = "bookmarks"
private const val PLAYLISTS = "playlists"
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_awesome.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,9l1.25,-2.75L23,5l-2.75,-1.25L19,1l-1.25,2.75L15,5l2.75,1.25L19,9zM11.5,9.5L9,4 6.5,9.5 1,12l5.5,2.5L9,20l2.5,-5.5L17,12l-5.5,-2.5zM19,15l-1.25,2.75L15,19l2.75,1.25L19,23l1.25,-2.75L23,19l-2.75,-1.25L19,15z" />
</vector>
14 changes: 14 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,18 @@
<item>never</item>
</string-array>

<string-array name="homeTabItems">
<item>@string/featured</item>
<item>@string/trending</item>
<item>@string/bookmarks</item>
<item>@string/playlists</item>
</string-array>

<string-array name="homeTabItemsValues">
<item>featured</item>
<item>trending</item>
<item>bookmarks</item>
<item>playlists</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
<string name="import_format_newpipe">NewPipe</string>
<string name="import_format_freetube">FreeTube</string>
<string name="import_format_youtube_csv">YouTube (CSV)</string>
<string name="home_tab_content">Home tab content</string>

<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/appearance_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@

<PreferenceCategory app:title="@string/layout">

<MultiSelectListPreference
android:icon="@drawable/ic_awesome"
app:defaultValue="@array/homeTabItemsValues"
android:title="@string/home_tab_content"
android:key="home_tab_content"
android:entryValues="@array/homeTabItemsValues"
android:entries="@array/homeTabItems" />

<SwitchPreferenceCompat
app:defaultValue="false"
app:icon="@drawable/ic_list"
Expand Down