Skip to content

Commit

Permalink
Disable mark all as read button if there are no unread posts
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Jul 28, 2024
1 parent af736cc commit 092f69c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,32 @@ class HomePresenter(
.onEach { hasFeeds -> _state.update { it.copy(hasFeeds = hasFeeds) } }
.launchIn(coroutineScope)

combine(observableActiveSource.activeSource, settingsRepository.postsType) {
activeSource,
postsType ->
Pair(activeSource, postsType)
}
.flatMapLatest { (activeSource, postsType) ->
val postsAfter =
when (postsType) {
PostsType.ALL,
PostsType.UNREAD -> Instant.DISTANT_PAST
PostsType.TODAY -> {
getTodayStartInstant()
}
PostsType.LAST_24_HOURS -> {
getLast24HourStart()
}
}

rssRepository.hasUnreadPosts(
sourceId = activeSource?.id,
postsAfter = postsAfter,
)
}
.onEach { hasUnreadPosts -> _state.update { it.copy(hasUnreadPosts = hasUnreadPosts) } }
.launchIn(coroutineScope)

settingsRepository.enableFeaturedItemBlur
.onEach { value -> _state.update { it.copy(featuredItemBlurEnabled = value) } }
.launchIn(coroutineScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ internal data class HomeState(
val activeSource: Source?,
val featuredItemBlurEnabled: Boolean,
val hasFeeds: Boolean?,
val postsType: PostsType
val postsType: PostsType,
val hasUnreadPosts: Boolean
) {

companion object {
Expand All @@ -51,7 +52,8 @@ internal data class HomeState(
activeSource = null,
featuredItemBlurEnabled = true,
hasFeeds = null,
postsType = PostsType.ALL
postsType = PostsType.ALL,
hasUnreadPosts = false,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ internal fun HomeScreen(homePresenter: HomePresenter, modifier: Modifier = Modif
postsType = state.postsType,
listState = listState,
hasFeeds = hasFeeds,
hasUnreadPosts = state.hasUnreadPosts,
onSearchClicked = { homePresenter.dispatch(HomeEvent.SearchClicked) },
onBookmarksClicked = { homePresenter.dispatch(HomeEvent.BookmarksClicked) },
onSettingsClicked = { homePresenter.dispatch(HomeEvent.SettingsClicked) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package dev.sasikanth.rss.reader.home.ui

import androidx.compose.animation.animateColor
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -41,9 +43,11 @@ import androidx.compose.material.icons.rounded.MoreVert
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -77,6 +81,7 @@ internal fun HomeTopAppBar(
postsType: PostsType,
listState: LazyListState,
hasFeeds: Boolean?,
hasUnreadPosts: Boolean,
modifier: Modifier = Modifier,
onSearchClicked: () -> Unit,
onBookmarksClicked: () -> Unit,
Expand Down Expand Up @@ -138,7 +143,11 @@ internal fun HomeTopAppBar(
) {
PostsFilterButton(postsType = postsType, onPostTypeChanged = onPostTypeChanged)

MarkPostsAsReadButton(source = source, onMarkPostsAsRead = onMarkPostsAsRead)
MarkPostsAsReadButton(
source = source,
onMarkPostsAsRead = onMarkPostsAsRead,
enabled = hasUnreadPosts,
)
}
}
}
Expand Down Expand Up @@ -223,29 +232,41 @@ private fun MarkPostsAsReadButton(
source: Source?,
onMarkPostsAsRead: (Source?) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
) {
val transition = updateTransition(enabled, "button_enable_state")
val backgroundColor by
transition.animateColor {
if (it) Color.White.copy(alpha = 0.12f) else Color.White.copy(alpha = 0f)
}
val contentColor by
transition.animateColor {
if (it) AppTheme.colorScheme.textEmphasisHigh
else AppTheme.colorScheme.onSurface.copy(alpha = 0.38f)
}

Row(
modifier =
modifier
.clip(RoundedCornerShape(8.dp))
.clickable(onClick = { onMarkPostsAsRead(source) })
.background(color = Color.White.copy(alpha = 0.12f))
.clickable(enabled = enabled, onClick = { onMarkPostsAsRead(source) })
.background(color = backgroundColor)
.padding(vertical = 4.dp)
.padding(start = 8.dp, end = 12.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Text(
text = LocalStrings.current.markAsRead,
style = MaterialTheme.typography.bodyMedium,
color = AppTheme.colorScheme.textEmphasisHigh,
)
CompositionLocalProvider(LocalContentColor provides contentColor) {
Text(
text = LocalStrings.current.markAsRead,
style = MaterialTheme.typography.bodyMedium,
)

Icon(
imageVector = Icons.Filled.DoneAll,
contentDescription = null,
modifier = Modifier.requiredSize(20.dp),
tint = AppTheme.colorScheme.textEmphasisHigh,
)
Icon(
imageVector = Icons.Filled.DoneAll,
contentDescription = null,
modifier = Modifier.requiredSize(20.dp),
)
}
}
}

Expand Down

0 comments on commit 092f69c

Please sign in to comment.