From d41b7cb73d99a71e10558e1bb6ebb4fc5fabe10f Mon Sep 17 00:00:00 2001 From: Yash-Garg Date: Tue, 7 Nov 2023 18:29:11 +0530 Subject: [PATCH] feat: add option to change column size in a particular library --- .../jellyfin/fragments/LibraryFragment.kt | 37 ++++++++++++++++--- .../jellyfin/dialogs/SortDialogFragment.kt | 16 ++++++++ .../jellyfin/viewmodels/LibraryViewModel.kt | 11 ++++++ core/src/main/res/menu/library_menu.xml | 5 +++ core/src/main/res/values/string_arrays.xml | 7 ++++ core/src/main/res/values/strings.xml | 1 + .../dev/jdtech/jellyfin/AppPreferences.kt | 7 ++++ .../java/dev/jdtech/jellyfin/Constants.kt | 1 + 8 files changed, 79 insertions(+), 6 deletions(-) diff --git a/app/phone/src/main/java/dev/jdtech/jellyfin/fragments/LibraryFragment.kt b/app/phone/src/main/java/dev/jdtech/jellyfin/fragments/LibraryFragment.kt index c5a3c2577b..a64eea50f3 100644 --- a/app/phone/src/main/java/dev/jdtech/jellyfin/fragments/LibraryFragment.kt +++ b/app/phone/src/main/java/dev/jdtech/jellyfin/fragments/LibraryFragment.kt @@ -18,6 +18,7 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import androidx.paging.LoadState +import androidx.recyclerview.widget.GridLayoutManager import dagger.hilt.android.AndroidEntryPoint import dev.jdtech.jellyfin.AppPreferences import dev.jdtech.jellyfin.adapters.ViewItemPagingAdapter @@ -61,6 +62,7 @@ class LibraryFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + val gridLayoutManager = GridLayoutManager(context, preferences.columnSize) val menuHost: MenuHost = requireActivity() menuHost.addMenuProvider( object : MenuProvider { @@ -94,6 +96,18 @@ class LibraryFragment : Fragment() { ) true } + CoreR.id.action_column_size -> { + SortDialogFragment( + args.libraryId, + args.libraryType, + viewModel, + "columnSize", + ).show( + parentFragmentManager, + "sortdialog", + ) + true + } else -> false } } @@ -113,12 +127,15 @@ class LibraryFragment : Fragment() { ) } - binding.itemsRecyclerView.adapter = - ViewItemPagingAdapter( - { item -> - navigateToItem(item) - }, - ) + binding.itemsRecyclerView.apply { + layoutManager = gridLayoutManager + adapter = + ViewItemPagingAdapter( + { item -> + navigateToItem(item) + }, + ) + } (binding.itemsRecyclerView.adapter as ViewItemPagingAdapter).addLoadStateListener { when (it.refresh) { @@ -135,6 +152,14 @@ class LibraryFragment : Fragment() { } } + viewLifecycleOwner.lifecycleScope.launch { + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + viewModel.columnFlow.collect { + gridLayoutManager.spanCount = it + } + } + } + viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.uiState.collect { uiState -> diff --git a/core/src/main/java/dev/jdtech/jellyfin/dialogs/SortDialogFragment.kt b/core/src/main/java/dev/jdtech/jellyfin/dialogs/SortDialogFragment.kt index 7a93955a03..42bc3e5446 100644 --- a/core/src/main/java/dev/jdtech/jellyfin/dialogs/SortDialogFragment.kt +++ b/core/src/main/java/dev/jdtech/jellyfin/dialogs/SortDialogFragment.kt @@ -89,6 +89,22 @@ class SortDialogFragment( dialog.dismiss() } } + "columnSize" -> { + val columnSizeValues = resources.getIntArray(R.array.column_size_options) + val columnSizeOptions = columnSizeValues.map { o -> o.toString() }.toTypedArray() + + builder + .setTitle(getString(R.string.column_size)) + .setSingleChoiceItems( + columnSizeOptions, + columnSizeValues.indexOf(appPreferences.columnSize), + ) { dialog, which -> + val columnSize = columnSizeValues[which] + appPreferences.columnSize = columnSize + viewModel.setColumnCount(columnSize) + dialog.dismiss() + } + } } builder.create() } ?: throw IllegalStateException("Activity cannot be null") diff --git a/core/src/main/java/dev/jdtech/jellyfin/viewmodels/LibraryViewModel.kt b/core/src/main/java/dev/jdtech/jellyfin/viewmodels/LibraryViewModel.kt index 3ccc04e012..ff3e8e8b09 100644 --- a/core/src/main/java/dev/jdtech/jellyfin/viewmodels/LibraryViewModel.kt +++ b/core/src/main/java/dev/jdtech/jellyfin/viewmodels/LibraryViewModel.kt @@ -10,7 +10,9 @@ import dev.jdtech.jellyfin.models.FindroidItem import dev.jdtech.jellyfin.models.SortBy import dev.jdtech.jellyfin.repository.JellyfinRepository import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import org.jellyfin.sdk.model.api.BaseItemKind @@ -28,6 +30,9 @@ constructor( private val _uiState = MutableStateFlow(UiState.Loading) val uiState = _uiState.asStateFlow() + private val _columnFlow = MutableSharedFlow() + val columnFlow = _columnFlow.asSharedFlow() + var itemsloaded = false sealed class UiState { @@ -36,6 +41,12 @@ constructor( data class Error(val error: Exception) : UiState() } + fun setColumnCount(count: Int) { + viewModelScope.launch { + _columnFlow.emit(count) + } + } + fun loadItems( parentId: UUID, libraryType: CollectionType, diff --git a/core/src/main/res/menu/library_menu.xml b/core/src/main/res/menu/library_menu.xml index b425ffee51..6fb5f3b695 100644 --- a/core/src/main/res/menu/library_menu.xml +++ b/core/src/main/res/menu/library_menu.xml @@ -12,4 +12,9 @@ android:id="@+id/action_sort_order" android:title="@string/sort_order" app:showAsAction="collapseActionView" /> + + \ No newline at end of file diff --git a/core/src/main/res/values/string_arrays.xml b/core/src/main/res/values/string_arrays.xml index bc768c8a5f..7d58f2a1be 100644 --- a/core/src/main/res/values/string_arrays.xml +++ b/core/src/main/res/values/string_arrays.xml @@ -12,6 +12,13 @@ @string/ascending @string/descending + + 2 + 3 + 4 + 5 + 6 + no mediacodec diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index c78f6962c1..20f6fda93f 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -94,6 +94,7 @@ Hide Sort by Sort order + Column size Close Share %1$s poster diff --git a/preferences/src/main/java/dev/jdtech/jellyfin/AppPreferences.kt b/preferences/src/main/java/dev/jdtech/jellyfin/AppPreferences.kt index 6a97e827fe..81f3f809e9 100644 --- a/preferences/src/main/java/dev/jdtech/jellyfin/AppPreferences.kt +++ b/preferences/src/main/java/dev/jdtech/jellyfin/AppPreferences.kt @@ -41,6 +41,13 @@ constructor( putBoolean(Constants.PREF_DISPLAY_EXTRA_INFO, value) } } + var columnSize: Int + get() = sharedPreferences.getInt(Constants.PREF_COLUMN_SIZE, 1) + set(value) { + sharedPreferences.edit { + putInt(Constants.PREF_COLUMN_SIZE, value) + } + } // Player val playerGestures get() = sharedPreferences.getBoolean(Constants.PREF_PLAYER_GESTURES, true) diff --git a/preferences/src/main/java/dev/jdtech/jellyfin/Constants.kt b/preferences/src/main/java/dev/jdtech/jellyfin/Constants.kt index a9f5dda016..efe0deb9d7 100644 --- a/preferences/src/main/java/dev/jdtech/jellyfin/Constants.kt +++ b/preferences/src/main/java/dev/jdtech/jellyfin/Constants.kt @@ -44,6 +44,7 @@ object Constants { const val PREF_SORT_BY = "pref_sort_by" const val PREF_SORT_ORDER = "pref_sort_order" const val PREF_DISPLAY_EXTRA_INFO = "pref_display_extra_info" + const val PREF_COLUMN_SIZE = "pref_column_size" // caching const val DEFAULT_CACHE_SIZE = 20