Skip to content

Commit

Permalink
feat: add option to change column size in a particular library
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Garg committed Nov 7, 2023
1 parent a4499f5 commit d41b7cb
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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) {
Expand All @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/res/menu/library_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
Expand Up @@ -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
Expand Down

0 comments on commit d41b7cb

Please sign in to comment.