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

[JetNews] feature/favorite developed. #1519

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions JetNews/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose)
id("androidx.room")
id("com.google.devtools.ksp")
}

android {
Expand Down Expand Up @@ -69,6 +71,10 @@ android {
compose = true
}

room {
schemaDirectory("$projectDir/schemas")
}

packaging.resources {
// Multiple dependency bring these files in. Exclude them to enable
// our test APK to build (has no effect on our AARs)
Expand Down Expand Up @@ -110,6 +116,12 @@ dependencies {
implementation(libs.androidx.glance.appwidget)
implementation(libs.androidx.glance.material3)

implementation(libs.androidx.room.ktx)
implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)

implementation("com.google.code.gson:gson:2.10.1")

implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.lifecycle.viewmodel.savedstate)
implementation(libs.androidx.lifecycle.livedata.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ interface PostsRepository {
*/
fun observeFavorites(): Flow<Set<String>>

/**
* Observe the current marks
*/
fun observeMarks(): Flow<Set<String>>

/**
* Observe the posts feed.
*/
Expand All @@ -50,4 +55,9 @@ interface PostsRepository {
* Toggle a postId to be a favorite or not.
*/
suspend fun toggleFavorite(postId: String)

/**
* Toggle a postId to be a mark or not.
*/
suspend fun toggleMark(postId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class BlockingFakePostsRepository : PostsRepository {
// for now, keep the favorites in memory
private val favorites = MutableStateFlow<Set<String>>(setOf())

private val marks = MutableStateFlow<Set<String>>(setOf())

private val postsFeed = MutableStateFlow<PostsFeed?>(null)

override suspend fun getPost(postId: String?): Result<Post> {
Expand All @@ -57,9 +59,16 @@ class BlockingFakePostsRepository : PostsRepository {
}

override fun observeFavorites(): Flow<Set<String>> = favorites

override fun observeMarks(): Flow<Set<String>> = marks

override fun observePostsFeed(): Flow<PostsFeed?> = postsFeed

override suspend fun toggleFavorite(postId: String) {
favorites.update { it.addOrRemove(postId) }
}

override suspend fun toggleMark(postId: String) {
marks.update { it.addOrRemove(postId) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class FakePostsRepository : PostsRepository {
// for now, store these in memory
private val favorites = MutableStateFlow<Set<String>>(setOf())

private val marks = MutableStateFlow<Set<String>>(setOf())

private val postsFeed = MutableStateFlow<PostsFeed?>(null)

// Used to make suspend functions that read and update state safe to call from any thread
Expand Down Expand Up @@ -65,6 +67,7 @@ class FakePostsRepository : PostsRepository {
}

override fun observeFavorites(): Flow<Set<String>> = favorites
override fun observeMarks(): Flow<Set<String>> = marks
override fun observePostsFeed(): Flow<PostsFeed?> = postsFeed

override suspend fun toggleFavorite(postId: String) {
Expand All @@ -73,6 +76,12 @@ class FakePostsRepository : PostsRepository {
}
}

override suspend fun toggleMark(postId: String) {
marks.update {
it.addOrRemove(postId)
}
}

// used to drive "random" failure in a predictable pattern, making the first request always
// succeed
private var requestCount = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ListAlt
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.ListAlt
import androidx.compose.material3.DrawerState
Expand Down Expand Up @@ -67,7 +68,7 @@ fun AppDrawer(
)
NavigationDrawerItem(
label = { Text(stringResource(id = R.string.interests_title)) },
icon = { Icon(Icons.Filled.ListAlt, null) },
icon = { Icon(Icons.AutoMirrored.Filled.ListAlt, null) },
selected = currentRoute == JetnewsDestinations.INTERESTS_ROUTE,
onClick = { navigateToInterests(); closeDrawer() },
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.BottomAppBar
Expand Down Expand Up @@ -86,6 +87,8 @@ fun ArticleScreen(
onBack: () -> Unit,
isFavorite: Boolean,
onToggleFavorite: () -> Unit,
isMarked: Boolean,
onToggleMark: () -> Unit,
modifier: Modifier = Modifier,
lazyListState: LazyListState = rememberLazyListState()
) {
Expand All @@ -103,7 +106,7 @@ fun ArticleScreen(
if (!isExpandedScreen) {
IconButton(onClick = onBack) {
Icon(
imageVector = Icons.Filled.ArrowBack,
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = stringResource(R.string.cd_navigate_up),
tint = MaterialTheme.colorScheme.primary
)
Expand All @@ -115,7 +118,7 @@ fun ArticleScreen(
if (!isExpandedScreen) {
BottomAppBar(
actions = {
FavoriteButton(onClick = { showUnimplementedActionDialog = true })
FavoriteButton(isFavorite = isMarked, onClick = onToggleMark)
BookmarkButton(isBookmarked = isFavorite, onClick = onToggleFavorite)
ShareButton(onClick = { sharePost(post, context) })
TextSettingsButton(onClick = { showUnimplementedActionDialog = true })
Expand Down Expand Up @@ -248,7 +251,7 @@ fun PreviewArticleDrawer() {
val post = runBlocking {
(BlockingFakePostsRepository().getPost(post3.id) as Result.Success).data
}
ArticleScreen(post, false, {}, false, {})
ArticleScreen(post, false, {}, false, {}, false, {})
}
}

Expand All @@ -265,6 +268,6 @@ fun PreviewArticleNavRail() {
val post = runBlocking {
(BlockingFakePostsRepository().getPost(post3.id) as Result.Success).data
}
ArticleScreen(post, true, {}, false, {})
ArticleScreen(post, true, {}, false, {}, false, {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.res.Configuration
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ListAlt
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.ListAlt
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -66,7 +67,7 @@ fun AppNavRail(
NavigationRailItem(
selected = currentRoute == JetnewsDestinations.INTERESTS_ROUTE,
onClick = navigateToInterests,
icon = { Icon(Icons.Filled.ListAlt, stringResource(R.string.interests_title)) },
icon = { Icon(Icons.AutoMirrored.Filled.ListAlt, stringResource(R.string.interests_title)) },
label = { Text(stringResource(R.string.interests_title)) },
alwaysShowLabel = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fun HomeRoute(
uiState = uiState,
isExpandedScreen = isExpandedScreen,
onToggleFavorite = { homeViewModel.toggleFavourite(it) },
onToggleMark = { homeViewModel.toggleMark(it) },
onSelectPost = { homeViewModel.selectArticle(it) },
onRefreshPosts = { homeViewModel.refreshPosts() },
onErrorDismiss = { homeViewModel.errorShown(it) },
Expand Down Expand Up @@ -87,6 +88,7 @@ fun HomeRoute(
uiState: HomeUiState,
isExpandedScreen: Boolean,
onToggleFavorite: (String) -> Unit,
onToggleMark: (String) -> Unit,
onSelectPost: (String) -> Unit,
onRefreshPosts: () -> Unit,
onErrorDismiss: (Long) -> Unit,
Expand Down Expand Up @@ -116,6 +118,7 @@ fun HomeRoute(
uiState = uiState,
showTopAppBar = !isExpandedScreen,
onToggleFavorite = onToggleFavorite,
onToggleMark = onToggleMark,
onSelectPost = onSelectPost,
onRefreshPosts = onRefreshPosts,
onErrorDismiss = onErrorDismiss,
Expand Down Expand Up @@ -146,6 +149,7 @@ fun HomeRoute(
// Guaranteed by above condition for home screen type
check(uiState is HomeUiState.HasPosts)

// TODO: This
ArticleScreen(
post = uiState.selectedPost,
isExpandedScreen = isExpandedScreen,
Expand All @@ -154,6 +158,10 @@ fun HomeRoute(
onToggleFavorite = {
onToggleFavorite(uiState.selectedPost.id)
},
isMarked = uiState.marks.contains(uiState.selectedPost.id),
onToggleMark = {
onToggleMark(uiState.selectedPost.id)
},
lazyListState = articleDetailLazyListStates.getValue(
uiState.selectedPost.id
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
Expand Down Expand Up @@ -124,6 +125,7 @@ fun HomeFeedWithArticleDetailsScreen(
uiState: HomeUiState,
showTopAppBar: Boolean,
onToggleFavorite: (String) -> Unit,
onToggleMark: (String) -> Unit,
onSelectPost: (String) -> Unit,
onRefreshPosts: () -> Unit,
onErrorDismiss: (Long) -> Unit,
Expand Down Expand Up @@ -193,6 +195,8 @@ fun HomeFeedWithArticleDetailsScreen(
PostTopBar(
isFavorite = hasPostsUiState.favorites.contains(detailPost.id),
onToggleFavorite = { onToggleFavorite(detailPost.id) },
isMark = hasPostsUiState.marks.contains(detailPost.id),
onToggleMark = { onToggleMark(detailPost.id) },
onSharePost = { sharePost(detailPost, context) },
modifier = Modifier
.windowInsetsPadding(WindowInsets.safeDrawing)
Expand Down Expand Up @@ -582,7 +586,7 @@ private fun PostListHistorySection(
*/
@Composable
private fun PostListDivider() {
Divider(
HorizontalDivider(
modifier = Modifier.padding(horizontal = 14.dp),
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.08f)
)
Expand Down Expand Up @@ -649,6 +653,8 @@ private fun submitSearch(
private fun PostTopBar(
isFavorite: Boolean,
onToggleFavorite: () -> Unit,
isMark: Boolean,
onToggleMark: () -> Unit,
onSharePost: () -> Unit,
modifier: Modifier = Modifier
) {
Expand All @@ -658,7 +664,7 @@ private fun PostTopBar(
modifier = modifier.padding(end = 16.dp)
) {
Row(Modifier.padding(horizontal = 8.dp)) {
FavoriteButton(onClick = { /* Functionality not available */ })
FavoriteButton(isFavorite = isMark, onClick = onToggleMark)
BookmarkButton(isBookmarked = isFavorite, onClick = onToggleFavorite)
ShareButton(onClick = onSharePost)
TextSettingsButton(onClick = { /* Functionality not available */ })
Expand Down Expand Up @@ -732,6 +738,7 @@ fun PreviewHomeListDrawerScreen() {
selectedPost = postsFeed.highlightedPost,
isArticleOpen = false,
favorites = emptySet(),
marks = emptySet(),
isLoading = false,
errorMessages = emptyList(),
searchInput = ""
Expand Down Expand Up @@ -768,6 +775,7 @@ fun PreviewHomeListNavRailScreen() {
selectedPost = postsFeed.highlightedPost,
isArticleOpen = false,
favorites = emptySet(),
marks = emptySet(),
isLoading = false,
errorMessages = emptyList(),
searchInput = ""
Expand Down Expand Up @@ -800,12 +808,14 @@ fun PreviewHomeListDetailScreen() {
selectedPost = postsFeed.highlightedPost,
isArticleOpen = false,
favorites = emptySet(),
marks = emptySet(),
isLoading = false,
errorMessages = emptyList(),
searchInput = ""
),
showTopAppBar = true,
onToggleFavorite = {},
onToggleMark = {},
onSelectPost = {},
onRefreshPosts = {},
onErrorDismiss = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sealed interface HomeUiState {
val selectedPost: Post,
val isArticleOpen: Boolean,
val favorites: Set<String>,
val marks: Set<String>,
override val isLoading: Boolean,
override val errorMessages: List<ErrorMessage>,
override val searchInput: String
Expand All @@ -81,6 +82,7 @@ private data class HomeViewModelState(
val selectedPostId: String? = null, // TODO back selectedPostId in a SavedStateHandle
val isArticleOpen: Boolean = false,
val favorites: Set<String> = emptySet(),
val marks: Set<String> = emptySet(),
val isLoading: Boolean = false,
val errorMessages: List<ErrorMessage> = emptyList(),
val searchInput: String = "",
Expand Down Expand Up @@ -108,6 +110,7 @@ private data class HomeViewModelState(
} ?: postsFeed.highlightedPost,
isArticleOpen = isArticleOpen,
favorites = favorites,
marks = marks,
isLoading = isLoading,
errorMessages = errorMessages,
searchInput = searchInput
Expand Down Expand Up @@ -149,6 +152,12 @@ class HomeViewModel(
viewModelState.update { it.copy(favorites = favorites) }
}
}

viewModelScope.launch {
postsRepository.observeMarks().collect { marks ->
viewModelState.update { it.copy(marks = marks) }
}
}
}

/**
Expand Down Expand Up @@ -184,6 +193,15 @@ class HomeViewModel(
}
}

/**
* Toggle mark of a post
*/
fun toggleMark(postId: String) {
viewModelScope.launch {
postsRepository.toggleMark(postId)
}
}

/**
* Selects the given article to view more information about it.
*/
Expand Down
Loading