Skip to content

Instantly share code, notes, and snippets.

@MaxMichel2
Last active June 14, 2024 13:49
Show Gist options
  • Save MaxMichel2/3b78b26f8a07fb42bb55d172fddb560a to your computer and use it in GitHub Desktop.
Save MaxMichel2/3b78b26f8a07fb42bb55d172fddb560a to your computer and use it in GitHub Desktop.
The actual implementation of the ForYouScreen (from the Now In Android application)
@Composable
fun ForYouScreen(
modifier: Modifier = Modifier,
viewModel: ForYouViewModel = hiltViewModel()
) {
val state = viewModel.state.collectAsStateWithLifecycle()
val effect = rememberFlowWithLifecycle(viewModel.effect)
val context = LocalContext.current
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
LaunchedEffect(effect) {
effect.collect { action ->
when (action) {
is ForYouEffect.NavigateToTopic -> {
// This effect would result in a navigation to another screen of the application
// with the topicId as a parameter.
Log.d("ForYouScreen", "Navigate to topic with id: ${action.topicId}")
}
is ForYouEffect.NavigateToNews -> launchCustomChromeTab(
context,
Uri.parse(action.newsUrl),
backgroundColor
)
}
}
}
ForYouScreenContent(
modifier = modifier,
topicsLoading = state.value.topicsLoading,
topics = state.value.topics,
topicsVisible = state.value.topicsVisible,
newsLoading = state.value.newsLoading,
news = state.value.news,
onTopicCheckedChanged = { topicId, isChecked ->
viewModel.sendEvent(
event = ForYouScreenReducer.ForYouEvent.UpdateTopicIsFollowed(
topicId = topicId,
isFollowed = isChecked,
)
)
},
onTopicClick = viewModel::onTopicClick,
saveFollowedTopics = {
viewModel.sendEvent(
event = ForYouScreenReducer.ForYouEvent.UpdateTopicsVisible(
isVisible = false
)
)
},
onNewsResourcesCheckedChanged = { newsResourceId, isChecked ->
viewModel.sendEvent(
event = ForYouScreenReducer.ForYouEvent.UpdateNewsIsSaved(
newsId = newsResourceId,
isSaved = isChecked,
)
)
},
onNewsResourceViewed = { newsResourceId ->
viewModel.sendEvent(
event = ForYouScreenReducer.ForYouEvent.UpdateNewsIsViewed(
newsId = newsResourceId,
isViewed = true,
)
)
},
)
}
@Composable
fun ForYouScreenContent(
topicsLoading: Boolean,
topics: List<FollowableTopic>,
topicsVisible: Boolean,
newsLoading: Boolean,
news: List<UserNewsResource>,
onTopicCheckedChanged: (String, Boolean) -> Unit,
onTopicClick: (String) -> Unit,
saveFollowedTopics: () -> Unit,
onNewsResourcesCheckedChanged: (String, Boolean) -> Unit,
onNewsResourceViewed: (String) -> Unit,
modifier: Modifier = Modifier,
) {
// The actual implementation is omitted as it adds no value here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment