|
|
|
@ -52,6 +52,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaFilte
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.SaveableNewsResource
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.previewTopics
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews
|
|
|
|
@ -66,24 +67,27 @@ fun TopicRoute(
|
|
|
|
|
modifier: Modifier = Modifier,
|
|
|
|
|
viewModel: TopicViewModel = hiltViewModel(),
|
|
|
|
|
) {
|
|
|
|
|
val uiState: TopicScreenUiState by viewModel.uiState.collectAsStateWithLifecycle()
|
|
|
|
|
val topicUiState: TopicUiState by viewModel.topicUiState.collectAsStateWithLifecycle()
|
|
|
|
|
val newsUiState: NewsUiState by viewModel.newUiState.collectAsStateWithLifecycle()
|
|
|
|
|
|
|
|
|
|
TopicScreen(
|
|
|
|
|
topicState = uiState.topicState,
|
|
|
|
|
newsState = uiState.newsState,
|
|
|
|
|
topicUiState = topicUiState,
|
|
|
|
|
newsUiState = newsUiState,
|
|
|
|
|
modifier = modifier,
|
|
|
|
|
onBackClick = onBackClick,
|
|
|
|
|
onFollowClick = viewModel::followTopicToggle,
|
|
|
|
|
onBookmarkChanged = viewModel::bookmarkNews,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
@Composable
|
|
|
|
|
internal fun TopicScreen(
|
|
|
|
|
topicState: TopicUiState,
|
|
|
|
|
newsState: NewsUiState,
|
|
|
|
|
topicUiState: TopicUiState,
|
|
|
|
|
newsUiState: NewsUiState,
|
|
|
|
|
onBackClick: () -> Unit,
|
|
|
|
|
onFollowClick: (Boolean) -> Unit,
|
|
|
|
|
onBookmarkChanged: (String, Boolean) -> Unit,
|
|
|
|
|
modifier: Modifier = Modifier,
|
|
|
|
|
) {
|
|
|
|
|
LazyColumn(
|
|
|
|
@ -93,7 +97,7 @@ internal fun TopicScreen(
|
|
|
|
|
item {
|
|
|
|
|
Spacer(Modifier.windowInsetsTopHeight(WindowInsets.safeDrawing))
|
|
|
|
|
}
|
|
|
|
|
when (topicState) {
|
|
|
|
|
when (topicUiState) {
|
|
|
|
|
Loading -> item {
|
|
|
|
|
NiaLoadingWheel(
|
|
|
|
|
modifier = modifier,
|
|
|
|
@ -106,14 +110,15 @@ internal fun TopicScreen(
|
|
|
|
|
TopicToolbar(
|
|
|
|
|
onBackClick = onBackClick,
|
|
|
|
|
onFollowClick = onFollowClick,
|
|
|
|
|
uiState = topicState.followableTopic,
|
|
|
|
|
uiState = topicUiState.followableTopic,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
TopicBody(
|
|
|
|
|
name = topicState.followableTopic.topic.name,
|
|
|
|
|
description = topicState.followableTopic.topic.longDescription,
|
|
|
|
|
news = newsState,
|
|
|
|
|
imageUrl = topicState.followableTopic.topic.imageUrl
|
|
|
|
|
name = topicUiState.followableTopic.topic.name,
|
|
|
|
|
description = topicUiState.followableTopic.topic.longDescription,
|
|
|
|
|
news = newsUiState,
|
|
|
|
|
imageUrl = topicUiState.followableTopic.topic.imageUrl,
|
|
|
|
|
onBookmarkChanged = onBookmarkChanged
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -127,14 +132,15 @@ private fun LazyListScope.TopicBody(
|
|
|
|
|
name: String,
|
|
|
|
|
description: String,
|
|
|
|
|
news: NewsUiState,
|
|
|
|
|
imageUrl: String
|
|
|
|
|
imageUrl: String,
|
|
|
|
|
onBookmarkChanged: (String, Boolean) -> Unit
|
|
|
|
|
) {
|
|
|
|
|
// TODO: Show icon if available
|
|
|
|
|
item {
|
|
|
|
|
TopicHeader(name, description, imageUrl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TopicCards(news)
|
|
|
|
|
TopicCards(news, onBookmarkChanged)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
@ -161,14 +167,17 @@ private fun TopicHeader(name: String, description: String, imageUrl: String) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun LazyListScope.TopicCards(news: NewsUiState) {
|
|
|
|
|
private fun LazyListScope.TopicCards(
|
|
|
|
|
news: NewsUiState,
|
|
|
|
|
onBookmarkChanged: (String, Boolean) -> Unit
|
|
|
|
|
) {
|
|
|
|
|
when (news) {
|
|
|
|
|
is NewsUiState.Success -> {
|
|
|
|
|
newsResourceCardItems(
|
|
|
|
|
items = news.news,
|
|
|
|
|
newsResourceMapper = { it },
|
|
|
|
|
isBookmarkedMapper = { /* TODO */ false },
|
|
|
|
|
onToggleBookmark = { /* TODO */ },
|
|
|
|
|
newsResourceMapper = { it.newsResource },
|
|
|
|
|
isBookmarkedMapper = { it.isSaved },
|
|
|
|
|
onToggleBookmark = { onBookmarkChanged(it.newsResource.id, !it.isSaved) },
|
|
|
|
|
itemModifier = Modifier.padding(24.dp)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
@ -188,7 +197,7 @@ private fun TopicBodyPreview() {
|
|
|
|
|
LazyColumn {
|
|
|
|
|
TopicBody(
|
|
|
|
|
"Jetpack Compose", "Lorem ipsum maximum",
|
|
|
|
|
NewsUiState.Success(emptyList()), ""
|
|
|
|
|
NewsUiState.Success(emptyList()), "", { _, _ -> }
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -237,10 +246,18 @@ fun TopicScreenPopulated() {
|
|
|
|
|
NiaTheme {
|
|
|
|
|
NiaBackground {
|
|
|
|
|
TopicScreen(
|
|
|
|
|
topicState = TopicUiState.Success(FollowableTopic(previewTopics[0], false)),
|
|
|
|
|
newsState = NewsUiState.Success(previewNewsResources),
|
|
|
|
|
topicUiState = TopicUiState.Success(FollowableTopic(previewTopics[0], false)),
|
|
|
|
|
newsUiState = NewsUiState.Success(
|
|
|
|
|
previewNewsResources.mapIndexed { index, newsResource ->
|
|
|
|
|
SaveableNewsResource(
|
|
|
|
|
newsResource = newsResource,
|
|
|
|
|
isSaved = index % 2 == 0,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
onBackClick = {},
|
|
|
|
|
onFollowClick = {}
|
|
|
|
|
onFollowClick = {},
|
|
|
|
|
onBookmarkChanged = { _, _ -> },
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -252,10 +269,11 @@ fun TopicScreenLoading() {
|
|
|
|
|
NiaTheme {
|
|
|
|
|
NiaBackground {
|
|
|
|
|
TopicScreen(
|
|
|
|
|
topicState = TopicUiState.Loading,
|
|
|
|
|
newsState = NewsUiState.Loading,
|
|
|
|
|
topicUiState = TopicUiState.Loading,
|
|
|
|
|
newsUiState = NewsUiState.Loading,
|
|
|
|
|
onBackClick = {},
|
|
|
|
|
onFollowClick = {}
|
|
|
|
|
onFollowClick = {},
|
|
|
|
|
onBookmarkChanged = { _, _ -> },
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|