Skip to content

Commit e699050

Browse files
authored
fix: Fixed incorrect bookmark status on home screen recomposition (#199)
1 parent 79f9373 commit e699050

File tree

7 files changed

+33
-8
lines changed

7 files changed

+33
-8
lines changed

app/src/main/java/com/rob729/newsfeed/model/state/home/HomeFeedState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ data class HomeFeedState(
99
val showNewsSourceBottomSheet: Boolean = false,
1010
val newsSources: List<NewsSource> = listOf(),
1111
val shouldOpenLinksUsingInAppBrowser: Boolean = true,
12+
val bookmarkedArticleUrls: Set<String> = emptySet()
1213
)

app/src/main/java/com/rob729/newsfeed/ui/components/CommonComponents.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.compose.material3.Icon
1717
import androidx.compose.material3.IconButton
1818
import androidx.compose.material3.Text
1919
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.LaunchedEffect
2021
import androidx.compose.runtime.getValue
2122
import androidx.compose.runtime.mutableStateOf
2223
import androidx.compose.runtime.remember
@@ -41,6 +42,14 @@ fun BottomStrip(
4142
) {
4243
var isBookmarked by remember { mutableStateOf(isArticleBookmarked) }
4344

45+
LaunchedEffect(isArticleBookmarked) {
46+
isBookmarked = isArticleBookmarked
47+
}
48+
49+
val timeSincePublish = remember(publishedAt) {
50+
getHowOldIsArticle(publishedAt)
51+
}
52+
4453
ConstraintLayout(
4554
modifier =
4655
modifier
@@ -84,7 +93,7 @@ fun BottomStrip(
8493
},
8594
)
8695
Text(
87-
text = getHowOldIsArticle(publishedAt),
96+
text = timeSincePublish,
8897
fontWeight = FontWeight.Light,
8998
fontSize = 12.sp,
9099
textAlign = TextAlign.End,

app/src/main/java/com/rob729/newsfeed/ui/components/NewsFeedItem.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import kotlin.time.Instant
4040
fun NewsFeedItem(
4141
modifier: Modifier = Modifier,
4242
newsArticleUiData: NewsArticleUiData,
43-
isArticleBookmarked: Boolean,
43+
isBookmarked: Boolean,
4444
onItemClick: () -> Unit,
4545
onBookmarkClick: (isBookmarked: Boolean) -> Unit,
4646
) {
@@ -107,7 +107,7 @@ fun NewsFeedItem(
107107
newsArticleUiData.source,
108108
newsArticleUiData.publishedAt,
109109
newsArticleUiData.url,
110-
isArticleBookmarked,
110+
isBookmarked,
111111
onBookmarkClick,
112112
Modifier.padding(start = 12.dp, end = 12.dp, bottom = 8.dp),
113113
)
@@ -139,5 +139,5 @@ fun shareArticle(
139139
}
140140

141141
val shareIntent = Intent.createChooser(sendIntent, null)
142-
startActivity(context, shareIntent, null)
142+
context.startActivity(shareIntent, null)
143143
}

app/src/main/java/com/rob729/newsfeed/ui/components/NewsSourceBottomSheetContent.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import androidx.compose.runtime.Composable
1414
import androidx.compose.runtime.LaunchedEffect
1515
import androidx.compose.ui.Alignment
1616
import androidx.compose.ui.Modifier
17-
import androidx.compose.ui.platform.LocalConfiguration
1817
import androidx.compose.ui.platform.LocalContext
18+
import androidx.compose.ui.platform.LocalWindowInfo
1919
import androidx.compose.ui.platform.testTag
2020
import androidx.compose.ui.unit.dp
2121
import com.rob729.newsfeed.AppPreferences
22+
import androidx.compose.ui.platform.LocalResources
2223

2324
private const val VISIBLE_CARDS = 4.25f
2425
private const val ITEM_SPACING = 12
@@ -30,7 +31,8 @@ fun NewsSourceBottomSheetContent(
3031
currentSelectedNewsSource: String,
3132
) {
3233
val vibrator = LocalContext.current.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
33-
val screenWidthDp = LocalConfiguration.current.screenWidthDp
34+
val screenWidthDp =
35+
LocalWindowInfo.current.containerSize.width / LocalResources.current.displayMetrics.density
3436
val itemWidth = (screenWidthDp - (ITEM_SPACING * VISIBLE_CARDS)).div(VISIBLE_CARDS)
3537
val rowState = rememberLazyListState()
3638

app/src/main/java/com/rob729/newsfeed/ui/screen/BookmarkedArticlesScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fun BookmarkedArticlesScreen(
9393
NewsFeedItem(
9494
Modifier.animateItem(),
9595
newsArticleUiData = item,
96-
true,
96+
isBookmarked = true,
9797
{ viewModel.newsFeedItemClicked(item) },
9898
{ isBookmarked ->
9999
viewModel.newsFeedItemBookmarkClicked(

app/src/main/java/com/rob729/newsfeed/ui/screen/HomeScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private fun DisplayNewsFeed(
275275
NewsFeedItem(
276276
Modifier,
277277
newsArticleUiData = item,
278-
false,
278+
newsState.bookmarkedArticleUrls.contains(item.url),
279279
{ viewModel.newsFeedItemClicked(item) },
280280
{ isBookmarked ->
281281
viewModel.newsFeedItemBookmarkClicked(

app/src/main/java/com/rob729/newsfeed/vm/HomeViewModel.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,26 @@ class HomeViewModel(
3333
newsRepository.fetchNewsArticles(state.selectedNewsSource).collectLatest {
3434
this.updateStateFromNewsResource(it)
3535
}
36+
}
37+
38+
intent {
3639
preferenceRepository.getNewsSources().collectLatest {
3740
this.reduce {
3841
state.copy(newsSources = it)
3942
}
4043
}
4144
}
4245

46+
intent {
47+
newsRepository.getBookmarkedNewsArticles().collectLatest { bookmarkedArticles ->
48+
this.reduce {
49+
state.copy(
50+
bookmarkedArticleUrls = bookmarkedArticles.map { it.url }.toSet()
51+
)
52+
}
53+
}
54+
}
55+
4356
intent {
4457
preferenceRepository.shouldOpenLinksUsingInAppBrowser().collectLatest {
4558
this.reduce {

0 commit comments

Comments
 (0)