Skip to content
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
1 change: 1 addition & 0 deletions feature/topic/api/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
-->
<resources>
<string name="feature_topic_api_loading">Loading topic</string>
<string name="feature_topic_api_error_header">Couldn\'t load topic</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ class TopicScreenTest {
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

private lateinit var topicLoading: String
private lateinit var topicErrorHeader: String

@Before
fun setup() {
composeTestRule.activity.apply {
topicLoading = getString(R.string.feature_topic_api_loading)
topicErrorHeader = getString(R.string.feature_topic_api_error_header)
}
}

Expand Down Expand Up @@ -97,6 +99,26 @@ class TopicScreenTest {
.assertExists()
}

@Test
fun errorMessage_whenTopicIsError_isShown() {
composeTestRule.setContent {
TopicScreen(
topicUiState = TopicUiState.Error,
newsUiState = NewsUiState.Loading,
showBackButton = true,
onBackClick = {},
onFollowClick = {},
onTopicClick = {},
onBookmarkChanged = { _, _ -> },
onNewsResourceViewed = {},
)
}

composeTestRule
.onNodeWithText(topicErrorHeader)
.assertExists()
}

@Test
fun news_whenTopicIsLoading_isNotShown() {
composeTestRule.setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ internal fun TopicScreen(
)
}

TopicUiState.Error -> TODO()
TopicUiState.Error -> item {
TopicErrorBody()
}
is TopicUiState.Success -> {
item {
TopicToolbar(
Expand Down Expand Up @@ -177,7 +179,7 @@ private fun topicItemsSize(
topicUiState: TopicUiState,
newsUiState: NewsUiState,
) = when (topicUiState) {
TopicUiState.Error -> 0 // Nothing
TopicUiState.Error -> 1 // Error message
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While updating this to 1 is correct for the new error state, the existing logic for TopicUiState.Success (lines 185-186) appears to be incorrect as it doesn't account for the Toolbar and Header items which are always rendered in that state. This discrepancy will cause the scrollbar to be incorrectly sized or hidden when news is loading or has an error. Consider updating the entire function to ensure the scrollbar accurately reflects the number of items in the LazyColumn.

TopicUiState.Loading -> 1 // Loading bar
is TopicUiState.Success -> when (newsUiState) {
NewsUiState.Error -> 0 // Nothing
Expand All @@ -203,6 +205,22 @@ private fun LazyListScope.topicBody(
userNewsResourceCards(news, onBookmarkChanged, onNewsResourceViewed, onTopicClick)
}

@Composable
private fun TopicErrorBody() {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
Comment on lines +209 to +212
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is a best practice in Compose to have a modifier parameter for all composables to allow for layout adjustments from the caller. This also ensures that the internal Column uses the passed-in modifier as its base.

Suggested change
private fun TopicErrorBody() {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
private fun TopicErrorBody(modifier: Modifier = Modifier) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier

.fillMaxWidth()
.padding(horizontal = 48.dp),
) {
Text(
text = stringResource(id = TopicR.string.feature_topic_api_error_header),
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.padding(vertical = 24.dp),
)
}
}

@Composable
private fun TopicHeader(name: String, description: String, imageUrl: String) {
Column(
Expand Down