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

feat: add option to change column size in a particular library #565

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
@@ -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 ->
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -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>(UiState.Loading)
val uiState = _uiState.asStateFlow()

private val _columnFlow = MutableSharedFlow<Int>()
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,
5 changes: 5 additions & 0 deletions core/src/main/res/menu/library_menu.xml
Original file line number Diff line number Diff line change
@@ -12,4 +12,9 @@
android:id="@+id/action_sort_order"
android:title="@string/sort_order"
app:showAsAction="collapseActionView" />

<item
android:id="@+id/action_column_size"
android:title="@string/column_size"
app:showAsAction="collapseActionView" />
</menu>
7 changes: 7 additions & 0 deletions core/src/main/res/values/string_arrays.xml
Original file line number Diff line number Diff line change
@@ -12,6 +12,13 @@
<item>@string/ascending</item>
<item>@string/descending</item>
</string-array>
<integer-array name="column_size_options">
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</integer-array>
<string-array name="mpv_hwdec">
<item>no</item>
<item>mediacodec</item>
1 change: 1 addition & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -94,6 +94,7 @@
<string name="hide">Hide</string>
<string name="sort_by">Sort by</string>
<string name="sort_order">Sort order</string>
<string name="column_size">Column size</string>
<string name="close">Close</string>
<string name="share">Share</string>
<string name="image_description_poster">%1$s poster</string>
Original file line number Diff line number Diff line change
@@ -41,6 +41,13 @@ constructor(
putBoolean(Constants.PREF_DISPLAY_EXTRA_INFO, value)
}
}
var columnSize: Int
get() = sharedPreferences.getInt(Constants.PREF_COLUMN_SIZE, 2)
set(value) {
sharedPreferences.edit {
putInt(Constants.PREF_COLUMN_SIZE, value)
}
}

// Player
val playerGestures get() = sharedPreferences.getBoolean(Constants.PREF_PLAYER_GESTURES, true)
1 change: 1 addition & 0 deletions preferences/src/main/java/dev/jdtech/jellyfin/Constants.kt
Original file line number Diff line number Diff line change
@@ -42,6 +42,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