Skip to content

Commit

Permalink
Make Timeline tab filters persistent (fixes #138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinokuni committed Sep 22, 2024
1 parent 9b1f733 commit 84a5422
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 46 deletions.
108 changes: 68 additions & 40 deletions app/src/main/java/com/readrops/app/timelime/TimelineScreenModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,49 @@ class TimelineScreenModel(
}

screenModelScope.launch(dispatcher) {
combine(
preferences.timelineItemSize.flow,
preferences.scrollRead.flow,
preferences.displayNotificationsPermission.flow
) { a, b, c -> Triple(a, b, c) }
.collect { (itemSize, scrollRead, notificationPermission) ->
getTimelinePreferences()
.collect { preferences ->
_timelineState.update {
it.copy(
itemSize = when (itemSize) {
"compact" -> TimelineItemSize.COMPACT
"regular" -> TimelineItemSize.REGULAR
else -> TimelineItemSize.LARGE
},
markReadOnScroll = scrollRead,
displayNotificationsPermission = notificationPermission
preferences = preferences,
filters = updateFilters {
it.filters.copy(
showReadItems = preferences.showReadItems,
orderField = preferences.orderField,
orderType = preferences.orderType
)
}
)
}
}
}
}

private fun getTimelinePreferences(): Flow<TimelinePreferences> {
return combine(
preferences.timelineItemSize.flow,
preferences.scrollRead.flow,
preferences.displayNotificationsPermission.flow,
preferences.showReadItems.flow,
preferences.orderField.flow,
preferences.orderType.flow,
transform = {
TimelinePreferences(
itemSize = when (it[0]) {
"compact" -> TimelineItemSize.COMPACT
"regular" -> TimelineItemSize.REGULAR
else -> TimelineItemSize.LARGE
},
markReadOnScroll = it[1] as Boolean,
displayNotificationsPermission = it[2] as Boolean,
showReadItems = it[3] as Boolean,
orderField = OrderField.valueOf(it[4] as String),
orderType = OrderType.valueOf(it[5] as String)
)
}
)
}

private fun buildPager(empty: Boolean = false) {
val query = ItemsQueryBuilder.buildItemsQuery(
filters.value,
Expand Down Expand Up @@ -364,38 +386,36 @@ class TimelineScreenModel(
}

fun setShowReadItemsState(showReadItems: Boolean) {
_timelineState.update {
it.copy(
filters = updateFilters {
it.filters.copy(
showReadItems = showReadItems
)
}
)
screenModelScope.launch {
preferences.showReadItems.write(showReadItems)

_timelineState.update {
it.copy(
filters = it.filters.copy(showReadItems = showReadItems)
)
}
}
}

fun setOrderFieldState(orderField: OrderField) {
_timelineState.update {
it.copy(
filters = updateFilters {
it.filters.copy(
orderField = orderField
)
}
)
screenModelScope.launch {
preferences.orderField.write(orderField.name)

_timelineState.update {
it.copy(
filters = it.filters.copy(orderField = orderField)
)
}
}
}

fun setOrderTypeState(orderType: OrderType) {
_timelineState.update {
it.copy(
filters = updateFilters {
it.filters.copy(
orderType = orderType
)
}
)
screenModelScope.launch {
preferences.orderType.write(orderType.name)

_timelineState.update {
it.copy(filters = it.filters.copy(orderType = orderType))
}
}
}

Expand Down Expand Up @@ -437,16 +457,24 @@ data class TimelineState(
val dialog: DialogState? = null,
val isAccountLocal: Boolean = false,
val hideReadAllFAB: Boolean = false,
val itemSize: TimelineItemSize = TimelineItemSize.LARGE,
val markReadOnScroll: Boolean = false,
val displayNotificationsPermission: Boolean = false
val preferences: TimelinePreferences = TimelinePreferences()
) {

val showSubtitle = filters.subFilter != SubFilter.ALL

val displayRefreshScreen = isRefreshing && isAccountLocal
}

@Stable
data class TimelinePreferences(
val itemSize: TimelineItemSize = TimelineItemSize.LARGE,
val markReadOnScroll: Boolean = false,
val displayNotificationsPermission: Boolean = false,
val showReadItems: Boolean = true,
val orderField: OrderField = OrderField.DATE,
val orderType: OrderType = OrderType.DESC
)

sealed interface DialogState {
data object ConfirmDialog : DialogState
data object FilterSheet : DialogState
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/readrops/app/timelime/TimelineTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ object TimelineTab : Tab {

val screenModel = getScreenModel<TimelineScreenModel>()
val state by screenModel.timelineState.collectAsStateWithLifecycle()
val preferences = state.preferences
val items = state.itemState.collectAsLazyPagingItems()

val lazyListState = rememberLazyListState()
Expand All @@ -105,9 +106,9 @@ object TimelineTab : Tab {
screenModel.disableDisplayNotificationsPermission()
}

LaunchedEffect(state.displayNotificationsPermission) {
LaunchedEffect(preferences.displayNotificationsPermission) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU
&& state.displayNotificationsPermission
&& preferences.displayNotificationsPermission
) {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
Expand Down Expand Up @@ -372,21 +373,21 @@ object TimelineTab : Tab {
MarkItemsRead(
lazyListState = lazyListState,
items = items,
markReadOnScroll = state.markReadOnScroll,
markReadOnScroll = preferences.markReadOnScroll,
screenModel = screenModel
)

LazyColumn(
state = lazyListState,
contentPadding = PaddingValues(
vertical = if (state.itemSize == TimelineItemSize.COMPACT) {
vertical = if (preferences.itemSize == TimelineItemSize.COMPACT) {
0.dp
} else {
MaterialTheme.spacing.shortSpacing
}
),
verticalArrangement = Arrangement.spacedBy(
if (state.itemSize == TimelineItemSize.COMPACT) {
if (preferences.itemSize == TimelineItemSize.COMPACT) {
0.dp
} else
MaterialTheme.spacing.shortSpacing
Expand Down Expand Up @@ -417,7 +418,7 @@ object TimelineTab : Tab {
onSetReadState = {
screenModel.updateItemReadState(itemWithFeed.item)
},
size = state.itemSize
size = preferences.itemSize
)
}
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/readrops/app/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ class Preferences(
key = intPreferencesKey("last_version_code"),
default = 0
)

val showReadItems = Preference(
dataStore = dataStore,
key = booleanPreferencesKey("show_read_items"),
default = true
)

val orderField = Preference(
dataStore = dataStore,
key = stringPreferencesKey("order_field"),
default = "DATE" // or "ID", uppercase important, used with Enum.valueOf()
)

val orderType = Preference(
dataStore = dataStore,
key = stringPreferencesKey("order_type"),
default = "DESC" // or "ASC", uppercase important, used with Enum.valueOf()
)
}


Expand Down

0 comments on commit 84a5422

Please sign in to comment.