Skip to content

Commit

Permalink
Simplify determineItemViewMode()
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Aug 11, 2024
1 parent 6f5187b commit 03b47b8
Showing 1 changed file with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,20 @@ fun ItemList(

@Composable
private fun determineItemViewMode(): ItemViewMode {
val listMode = PreferenceManager.getDefaultSharedPreferences(LocalContext.current)
.getString(
stringResource(R.string.list_view_mode_key),
stringResource(R.string.list_view_mode_value)
)
val prefValue = PreferenceManager.getDefaultSharedPreferences(LocalContext.current)
.getString(stringResource(R.string.list_view_mode_key), null)
val viewMode = prefValue?.let { ItemViewMode.valueOf(it.uppercase()) } ?: ItemViewMode.AUTO

return when (listMode) {
stringResource(R.string.list_view_mode_list_key) -> ItemViewMode.LIST
stringResource(R.string.list_view_mode_grid_key) -> ItemViewMode.GRID
stringResource(R.string.list_view_mode_card_key) -> ItemViewMode.CARD
else -> {
// Auto mode - evaluate whether to use Grid based on screen real estate.
return when (viewMode) {
ItemViewMode.AUTO -> {
// Evaluate whether to use Grid based on screen real estate.
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
if (windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.EXPANDED) {
ItemViewMode.GRID
} else {
ItemViewMode.LIST
}
}
else -> viewMode
}
}

0 comments on commit 03b47b8

Please sign in to comment.