Merge pull request #476 from android/caren/resource_type

Display resource type for each news resource
pull/489/head
Caren 2 years ago committed by GitHub
commit 65fdb0c09c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,6 +17,7 @@
package com.google.samples.apps.nowinandroid.core.model.data package com.google.samples.apps.nowinandroid.core.model.data
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
@ -83,5 +84,16 @@ val previewNewsResources = listOf(
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video, type = Video,
topics = listOf(previewTopics[2]) topics = listOf(previewTopics[2])
),
NewsResource(
id = "4",
title = "New Jetpack Release",
content = "New Jetpack release includes updates to libraries such as CameraX, Benchmark, and" +
"more!",
url = "https://developer.android.com/jetpack/androidx/versions/all-channel",
headerImageUrl = "",
publishDate = Instant.parse("2022-10-01T00:00:00.000Z"),
type = Unknown,
topics = listOf(previewTopics[2])
) )
) )

@ -20,6 +20,9 @@ plugins {
} }
android { android {
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
namespace = "com.google.samples.apps.nowinandroid.core.ui" namespace = "com.google.samples.apps.nowinandroid.core.ui"
} }
@ -45,4 +48,6 @@ dependencies {
api(libs.androidx.compose.runtime.livedata) api(libs.androidx.compose.runtime.livedata)
api(libs.androidx.metrics) api(libs.androidx.metrics)
api(libs.androidx.tracing.ktx) api(libs.androidx.tracing.ktx)
}
androidTestImplementation(project(":core:testing"))
}

@ -0,0 +1,78 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.ui
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import org.junit.Rule
import org.junit.Test
class NewsResourceCardTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testMetaDataDisplay_withCodelabResource() {
val newsWithKnownResourceType = previewNewsResources[0]
var dateFormatted = ""
composeTestRule.setContent {
NewsResourceCardExpanded(
newsResource = newsWithKnownResourceType,
isBookmarked = false,
onToggleBookmark = {},
onClick = {}
)
dateFormatted = dateFormatted(publishDate = newsWithKnownResourceType.publishDate)
}
composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(
R.string.card_meta_data_text,
dateFormatted,
newsWithKnownResourceType.type.displayText
)
)
.assertExists()
}
@Test
fun testMetaDataDisplay_withUnknownResource() {
val newsWithUnknownResourceType = previewNewsResources[3]
var dateFormatted = ""
composeTestRule.setContent {
NewsResourceCardExpanded(
newsResource = newsWithUnknownResourceType,
isBookmarked = false,
onToggleBookmark = {},
onClick = {}
)
dateFormatted = dateFormatted(publishDate = newsWithUnknownResourceType.publishDate)
}
composeTestRule
.onNodeWithText(dateFormatted)
.assertIsDisplayed()
}
}

@ -57,6 +57,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopic
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import java.time.ZoneId import java.time.ZoneId
@ -109,7 +110,7 @@ fun NewsResourceCardExpanded(
BookmarkButton(isBookmarked, onToggleBookmark) BookmarkButton(isBookmarked, onToggleBookmark)
} }
Spacer(modifier = Modifier.height(12.dp)) Spacer(modifier = Modifier.height(12.dp))
NewsResourceDate(newsResource.publishDate) NewsResourceMetaData(newsResource.publishDate, newsResource.type)
Spacer(modifier = Modifier.height(12.dp)) Spacer(modifier = Modifier.height(12.dp))
NewsResourceShortDescription(newsResource.content) NewsResourceShortDescription(newsResource.content)
Spacer(modifier = Modifier.height(12.dp)) Spacer(modifier = Modifier.height(12.dp))
@ -175,7 +176,7 @@ fun BookmarkButton(
} }
@Composable @Composable
private fun dateFormatted(publishDate: Instant): String { fun dateFormatted(publishDate: Instant): String {
var zoneId by remember { mutableStateOf(ZoneId.systemDefault()) } var zoneId by remember { mutableStateOf(ZoneId.systemDefault()) }
val context = LocalContext.current val context = LocalContext.current
@ -195,10 +196,19 @@ private fun dateFormatted(publishDate: Instant): String {
} }
@Composable @Composable
fun NewsResourceDate( fun NewsResourceMetaData(
publishDate: Instant publishDate: Instant,
resourceType: NewsResourceType
) { ) {
Text(dateFormatted(publishDate), style = MaterialTheme.typography.labelSmall) val formattedDate = dateFormatted(publishDate)
Text(
if (resourceType != NewsResourceType.Unknown) {
stringResource(R.string.card_meta_data_text, formattedDate, resourceType.displayText)
} else {
formattedDate
},
style = MaterialTheme.typography.labelSmall
)
} }
@Composable @Composable

@ -20,4 +20,5 @@
<string name="back">Back</string> <string name="back">Back</string>
<string name="card_tap_action">Open Resource Link</string> <string name="card_tap_action">Open Resource Link</string>
<string name="card_meta_data_text">%1$s • %2$s</string>
</resources> </resources>

Loading…
Cancel
Save