Merge pull request #902 from SimonMarquis/resource-type-to-string

Replace `NewsResourceType` enum with simpler String type
pull/909/head
Don Turner 1 year ago committed by GitHub
commit 6645ec9fce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,6 @@ package com.google.samples.apps.nowinandroid.core.data
import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository
import com.google.samples.apps.nowinandroid.core.data.repository.NewsResourceQuery import com.google.samples.apps.nowinandroid.core.data.repository.NewsResourceQuery
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.Video
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.mapToUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.mapToUserNewsResources
import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository
@ -164,7 +163,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/-fJ6poHQrjM", url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(sampleTopic1), topics = listOf(sampleTopic1),
), ),
NewsResource( NewsResource(
@ -176,7 +175,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/ZARz0pjm5YM", url = "https://youtu.be/ZARz0pjm5YM",
headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(sampleTopic1, sampleTopic2), topics = listOf(sampleTopic1, sampleTopic2),
), ),
NewsResource( NewsResource(
@ -186,7 +185,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/r5JgIyS3t3s", url = "https://youtu.be/r5JgIyS3t3s",
headerImageUrl = "https://i.ytimg.com/vi/r5JgIyS3t3s/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/r5JgIyS3t3s/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-08T00:00:00.000Z"), publishDate = Instant.parse("2021-11-08T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(sampleTopic2), topics = listOf(sampleTopic2),
), ),
) )

@ -19,7 +19,6 @@ package com.google.samples.apps.nowinandroid.core.data
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.FOLLOW_SYSTEM import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.FOLLOW_SYSTEM
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
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.Article
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand.DEFAULT import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand.DEFAULT
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.UserData import com.google.samples.apps.nowinandroid.core.model.data.UserData
@ -45,7 +44,7 @@ class UserNewsResourceTest {
url = "Test URL", url = "Test URL",
headerImageUrl = "Test image URL", headerImageUrl = "Test image URL",
publishDate = Clock.System.now(), publishDate = Clock.System.now(),
type = Article, type = "Article 📚",
topics = listOf( topics = listOf(
Topic( Topic(
id = "T1", id = "T1",

@ -16,7 +16,6 @@
package com.google.samples.apps.nowinandroid.core.data.model package com.google.samples.apps.nowinandroid.core.data.model
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Article
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResourceExpanded import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResourceExpanded
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
@ -56,7 +55,7 @@ class NetworkEntityKtTest {
url = "url", url = "url",
headerImageUrl = "headerImageUrl", headerImageUrl = "headerImageUrl",
publishDate = Instant.fromEpochMilliseconds(1), publishDate = Instant.fromEpochMilliseconds(1),
type = Article, type = "Article 📚",
) )
val entity = networkModel.asEntity() val entity = networkModel.asEntity()
@ -66,7 +65,7 @@ class NetworkEntityKtTest {
assertEquals("url", entity.url) assertEquals("url", entity.url)
assertEquals("headerImageUrl", entity.headerImageUrl) assertEquals("headerImageUrl", entity.headerImageUrl)
assertEquals(Instant.fromEpochMilliseconds(1), entity.publishDate) assertEquals(Instant.fromEpochMilliseconds(1), entity.publishDate)
assertEquals(Article, entity.type) assertEquals("Article 📚", entity.type)
val expandedNetworkModel = val expandedNetworkModel =
NetworkNewsResourceExpanded( NetworkNewsResourceExpanded(
@ -76,7 +75,7 @@ class NetworkEntityKtTest {
url = "url", url = "url",
headerImageUrl = "headerImageUrl", headerImageUrl = "headerImageUrl",
publishDate = Instant.fromEpochMilliseconds(1), publishDate = Instant.fromEpochMilliseconds(1),
type = Article, type = "Article 📚",
) )
val entityFromExpanded = expandedNetworkModel.asEntity() val entityFromExpanded = expandedNetworkModel.asEntity()
@ -87,6 +86,6 @@ class NetworkEntityKtTest {
assertEquals("url", entityFromExpanded.url) assertEquals("url", entityFromExpanded.url)
assertEquals("headerImageUrl", entityFromExpanded.headerImageUrl) assertEquals("headerImageUrl", entityFromExpanded.headerImageUrl)
assertEquals(Instant.fromEpochMilliseconds(1), entityFromExpanded.publishDate) assertEquals(Instant.fromEpochMilliseconds(1), entityFromExpanded.publishDate)
assertEquals(Article, entityFromExpanded.type) assertEquals("Article 📚", entityFromExpanded.type)
} }
} }

@ -17,7 +17,6 @@
package com.google.samples.apps.nowinandroid.core.database.model package com.google.samples.apps.nowinandroid.core.database.model
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.Video
import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.Topic
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import org.junit.Test import org.junit.Test
@ -33,7 +32,7 @@ class PopulatedNewsResourceKtTest {
content = "Hilt", content = "Hilt",
url = "url", url = "url",
headerImageUrl = "headerImageUrl", headerImageUrl = "headerImageUrl",
type = Video, type = "Video 📺",
publishDate = Instant.fromEpochMilliseconds(1), publishDate = Instant.fromEpochMilliseconds(1),
), ),
topics = listOf( topics = listOf(
@ -56,7 +55,7 @@ class PopulatedNewsResourceKtTest {
content = "Hilt", content = "Hilt",
url = "url", url = "url",
headerImageUrl = "headerImageUrl", headerImageUrl = "headerImageUrl",
type = Video, type = "Video 📺",
publishDate = Instant.fromEpochMilliseconds(1), publishDate = Instant.fromEpochMilliseconds(1),
topics = listOf( topics = listOf(
Topic( Topic(

@ -1,96 +0,0 @@
/*
* 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.database.util
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import org.junit.Test
import kotlin.test.assertEquals
class NewsResourceTypeConverterTest {
@Test
fun test_room_news_resource_type_converter_for_video() {
assertEquals(
NewsResourceType.Video,
NewsResourceTypeConverter().stringToNewsResourceType("Video 📺"),
)
}
@Test
fun test_room_news_resource_type_converter_for_article() {
assertEquals(
NewsResourceType.Article,
NewsResourceTypeConverter().stringToNewsResourceType("Article 📚"),
)
}
@Test
fun test_room_news_resource_type_converter_for_api_change() {
assertEquals(
NewsResourceType.APIChange,
NewsResourceTypeConverter().stringToNewsResourceType("API change"),
)
}
@Test
fun test_room_news_resource_type_converter_for_codelab() {
assertEquals(
NewsResourceType.Codelab,
NewsResourceTypeConverter().stringToNewsResourceType("Codelab"),
)
}
@Test
fun test_room_news_resource_type_converter_for_podcast() {
assertEquals(
NewsResourceType.Podcast,
NewsResourceTypeConverter().stringToNewsResourceType("Podcast 🎙"),
)
}
@Test
fun test_room_news_resource_type_converter_for_docs() {
assertEquals(
NewsResourceType.Docs,
NewsResourceTypeConverter().stringToNewsResourceType("Docs 📑"),
)
}
@Test
fun test_room_news_resource_type_converter_for_event() {
assertEquals(
NewsResourceType.Event,
NewsResourceTypeConverter().stringToNewsResourceType("Event 📆"),
)
}
@Test
fun test_room_news_resource_type_converter_for_dac() {
assertEquals(
NewsResourceType.DAC,
NewsResourceTypeConverter().stringToNewsResourceType("DAC"),
)
}
@Test
fun test_room_news_resource_type_converter_for_umm() {
assertEquals(
NewsResourceType.Unknown,
NewsResourceTypeConverter().stringToNewsResourceType("umm"),
)
}
}

@ -24,7 +24,6 @@ import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEnti
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
@ -305,5 +304,5 @@ private fun testNewsResource(
url = "", url = "",
headerImageUrl = "", headerImageUrl = "",
publishDate = Instant.fromEpochMilliseconds(millisSinceEpoch), publishDate = Instant.fromEpochMilliseconds(millisSinceEpoch),
type = NewsResourceType.DAC, type = "Article 📚",
) )

@ -32,7 +32,6 @@ import com.google.samples.apps.nowinandroid.core.database.model.RecentSearchQuer
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import com.google.samples.apps.nowinandroid.core.database.model.TopicFtsEntity import com.google.samples.apps.nowinandroid.core.database.model.TopicFtsEntity
import com.google.samples.apps.nowinandroid.core.database.util.InstantConverter import com.google.samples.apps.nowinandroid.core.database.util.InstantConverter
import com.google.samples.apps.nowinandroid.core.database.util.NewsResourceTypeConverter
@Database( @Database(
entities = [ entities = [
@ -63,7 +62,6 @@ import com.google.samples.apps.nowinandroid.core.database.util.NewsResourceTypeC
) )
@TypeConverters( @TypeConverters(
InstantConverter::class, InstantConverter::class,
NewsResourceTypeConverter::class,
) )
abstract class NiaDatabase : RoomDatabase() { abstract class NiaDatabase : RoomDatabase() {
abstract fun topicDao(): TopicDao abstract fun topicDao(): TopicDao

@ -20,7 +20,6 @@ import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
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 kotlinx.datetime.Instant import kotlinx.datetime.Instant
/** /**
@ -39,7 +38,7 @@ data class NewsResourceEntity(
val headerImageUrl: String?, val headerImageUrl: String?,
@ColumnInfo(name = "publish_date") @ColumnInfo(name = "publish_date")
val publishDate: Instant, val publishDate: Instant,
val type: NewsResourceType, val type: String,
) )
fun NewsResourceEntity.asExternalModel() = NewsResource( fun NewsResourceEntity.asExternalModel() = NewsResource(

@ -17,8 +17,6 @@
package com.google.samples.apps.nowinandroid.core.database.util package com.google.samples.apps.nowinandroid.core.database.util
import androidx.room.TypeConverter import androidx.room.TypeConverter
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import com.google.samples.apps.nowinandroid.core.model.data.asNewsResourceType
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
class InstantConverter { class InstantConverter {
@ -30,13 +28,3 @@ class InstantConverter {
fun instantToLong(instant: Instant?): Long? = fun instantToLong(instant: Instant?): Long? =
instant?.toEpochMilliseconds() instant?.toEpochMilliseconds()
} }
class NewsResourceTypeConverter {
@TypeConverter
fun newsResourceTypeToString(value: NewsResourceType?): String? =
value?.let(NewsResourceType::serializedName)
@TypeConverter
fun stringToNewsResourceType(serializedName: String?): NewsResourceType =
serializedName.asNewsResourceType()
}

@ -28,6 +28,6 @@ data class NewsResource(
val url: String, val url: String,
val headerImageUrl: String?, val headerImageUrl: String?,
val publishDate: Instant, val publishDate: Instant,
val type: NewsResourceType, val type: String,
val topics: List<Topic>, val topics: List<Topic>,
) )

@ -1,80 +0,0 @@
/*
* 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.model.data
/**
* Type for [NewsResource]
*/
enum class NewsResourceType(
val serializedName: String,
val displayText: String,
// TODO: descriptions should probably be string resources
val description: String,
) {
Video(
serializedName = "Video 📺",
displayText = "Video 📺",
description = "A video published on YouTube",
),
APIChange(
serializedName = "API change",
displayText = "API change",
description = "An addition, deprecation or change to the Android platform APIs.",
),
Article(
serializedName = "Article 📚",
displayText = "Article 📚",
description = "An article, typically on Medium or the official Android blog",
),
Codelab(
serializedName = "Codelab",
displayText = "Codelab",
description = "A new or updated codelab",
),
Podcast(
serializedName = "Podcast 🎙",
displayText = "Podcast 🎙",
description = "A podcast",
),
Docs(
serializedName = "Docs 📑",
displayText = "Docs 📑",
description = "A new or updated piece of documentation",
),
Event(
serializedName = "Event 📆",
displayText = "Event 📆",
description = "Information about a developer event e.g. Android Developer Summit",
),
DAC(
serializedName = "DAC",
displayText = "DAC",
description = "Android version features - Information about features in an Android",
),
Unknown(
serializedName = "Unknown",
displayText = "Unknown",
description = "Unknown",
),
}
fun String?.asNewsResourceType() = when (this) {
null -> NewsResourceType.Unknown
else -> NewsResourceType.values()
.firstOrNull { type -> type.serializedName == this }
?: NewsResourceType.Unknown
}

@ -29,7 +29,7 @@ data class UserNewsResource internal constructor(
val url: String, val url: String,
val headerImageUrl: String?, val headerImageUrl: String?,
val publishDate: Instant, val publishDate: Instant,
val type: NewsResourceType, val type: String,
val followableTopics: List<FollowableTopic>, val followableTopics: List<FollowableTopic>,
val isSaved: Boolean, val isSaved: Boolean,
val hasBeenViewed: Boolean, val hasBeenViewed: Boolean,

@ -17,9 +17,7 @@
package com.google.samples.apps.nowinandroid.core.network.model package com.google.samples.apps.nowinandroid.core.network.model
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.network.model.util.InstantSerializer import com.google.samples.apps.nowinandroid.core.network.model.util.InstantSerializer
import com.google.samples.apps.nowinandroid.core.network.model.util.NewsResourceTypeSerializer
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -35,8 +33,7 @@ data class NetworkNewsResource(
val headerImageUrl: String, val headerImageUrl: String,
@Serializable(InstantSerializer::class) @Serializable(InstantSerializer::class)
val publishDate: Instant, val publishDate: Instant,
@Serializable(NewsResourceTypeSerializer::class) val type: String,
val type: NewsResourceType,
val topics: List<String> = listOf(), val topics: List<String> = listOf(),
) )
@ -52,7 +49,6 @@ data class NetworkNewsResourceExpanded(
val headerImageUrl: String, val headerImageUrl: String,
@Serializable(InstantSerializer::class) @Serializable(InstantSerializer::class)
val publishDate: Instant, val publishDate: Instant,
@Serializable(NewsResourceTypeSerializer::class) val type: String,
val type: NewsResourceType,
val topics: List<NetworkTopic> = listOf(), val topics: List<NetworkTopic> = listOf(),
) )

@ -1,39 +0,0 @@
/*
* 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.network.model.util
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import com.google.samples.apps.nowinandroid.core.model.data.asNewsResourceType
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind.STRING
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
object NewsResourceTypeSerializer : KSerializer<NewsResourceType> {
override fun deserialize(decoder: Decoder): NewsResourceType =
decoder.decodeString().asNewsResourceType()
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
serialName = "type",
kind = STRING,
)
override fun serialize(encoder: Encoder, value: NewsResourceType) =
encoder.encodeString(value.serializedName)
}

@ -17,7 +17,6 @@
package com.google.samples.apps.nowinandroid.core.network.fake package com.google.samples.apps.nowinandroid.core.network.fake
import JvmUnitTestFakeAssetManager import JvmUnitTestFakeAssetManager
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.StandardTestDispatcher
@ -81,7 +80,7 @@ class FakeNiaNetworkDataSourceTest {
second = 0, second = 0,
nanosecond = 0, nanosecond = 0,
).toInstant(TimeZone.UTC), ).toInstant(TimeZone.UTC),
type = Codelab, type = "Codelab",
topics = listOf("2", "3", "10"), topics = listOf("2", "3", "10"),
), ),
/* ktlint-enable max-line-length */ /* ktlint-enable max-line-length */

@ -1,106 +0,0 @@
/*
* 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.network.model.util
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import kotlinx.serialization.json.Json
import org.junit.Test
import kotlin.test.assertEquals
class NewsResourceTypeSerializerTest {
@Test
fun test_news_resource_serializer_video() {
assertEquals(
NewsResourceType.Video,
Json.decodeFromString(NewsResourceTypeSerializer, """"Video 📺""""),
)
}
@Test
fun test_news_resource_serializer_article() {
assertEquals(
NewsResourceType.Article,
Json.decodeFromString(NewsResourceTypeSerializer, """"Article 📚""""),
)
}
@Test
fun test_news_resource_serializer_api_change() {
assertEquals(
NewsResourceType.APIChange,
Json.decodeFromString(NewsResourceTypeSerializer, """"API change""""),
)
}
@Test
fun test_news_resource_serializer_codelab() {
assertEquals(
NewsResourceType.Codelab,
Json.decodeFromString(NewsResourceTypeSerializer, """"Codelab""""),
)
}
@Test
fun test_news_resource_serializer_podcast() {
assertEquals(
NewsResourceType.Podcast,
Json.decodeFromString(NewsResourceTypeSerializer, """"Podcast 🎙""""),
)
}
@Test
fun test_news_resource_serializer_docs() {
assertEquals(
NewsResourceType.Docs,
Json.decodeFromString(NewsResourceTypeSerializer, """"Docs 📑""""),
)
}
@Test
fun test_news_resource_serializer_event() {
assertEquals(
NewsResourceType.Event,
Json.decodeFromString(NewsResourceTypeSerializer, """"Event 📆""""),
)
}
@Test
fun test_news_resource_serializer_dac() {
assertEquals(
NewsResourceType.DAC,
Json.decodeFromString(NewsResourceTypeSerializer, """"DAC""""),
)
}
@Test
fun test_news_resource_serializer_unknown() {
assertEquals(
NewsResourceType.Unknown,
Json.decodeFromString(NewsResourceTypeSerializer, """"umm""""),
)
}
@Test
fun test_serialize_and_deserialize() {
val json = Json.encodeToString(NewsResourceTypeSerializer, NewsResourceType.Video)
assertEquals(
NewsResourceType.Video,
Json.decodeFromString(NewsResourceTypeSerializer, json),
)
}
}

@ -17,9 +17,6 @@
package com.google.samples.apps.nowinandroid.core.testing.data package com.google.samples.apps.nowinandroid.core.testing.data
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.Codelab
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
/* ktlint-disable max-line-length */ /* ktlint-disable max-line-length */
@ -31,7 +28,7 @@ val newsResourcesTestData: List<NewsResource> = listOf(
url = "https://android-developers.googleblog.com/2022/05/new-android-basics-with-compose-course.html", url = "https://android-developers.googleblog.com/2022/05/new-android-basics-with-compose-course.html",
headerImageUrl = "https://developer.android.com/images/hero-assets/android-basics-compose.svg", headerImageUrl = "https://developer.android.com/images/hero-assets/android-basics-compose.svg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Codelab, type = "Codelab",
topics = listOf(topicsTestData[1]), topics = listOf(topicsTestData[1]),
), ),
NewsResource( NewsResource(
@ -44,7 +41,7 @@ val newsResourcesTestData: List<NewsResource> = listOf(
url = "https://youtu.be/-fJ6poHQrjM", url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(topicsTestData[0], topicsTestData[1]), topics = listOf(topicsTestData[0], topicsTestData[1]),
), ),
NewsResource( NewsResource(
@ -57,7 +54,7 @@ val newsResourcesTestData: List<NewsResource> = listOf(
url = "https://youtu.be/ZARz0pjm5YM", url = "https://youtu.be/ZARz0pjm5YM",
headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(topicsTestData[2]), topics = listOf(topicsTestData[2]),
), ),
NewsResource( NewsResource(
@ -68,7 +65,7 @@ val newsResourcesTestData: List<NewsResource> = listOf(
url = "https://developer.android.com/jetpack/androidx/versions/all-channel", url = "https://developer.android.com/jetpack/androidx/versions/all-channel",
headerImageUrl = "", headerImageUrl = "",
publishDate = Instant.parse("2022-10-01T00:00:00.000Z"), publishDate = Instant.parse("2022-10-01T00:00:00.000Z"),
type = Unknown, type = "",
topics = listOf(topicsTestData[2]), topics = listOf(topicsTestData[2]),
), ),
) )

@ -18,9 +18,6 @@ package com.google.samples.apps.nowinandroid.core.testing.data
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
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.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.ThemeBrand import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
import com.google.samples.apps.nowinandroid.core.model.data.UserData import com.google.samples.apps.nowinandroid.core.model.data.UserData
import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
@ -56,7 +53,7 @@ val userNewsResourcesTestData: List<UserNewsResource> = UserData(
second = 0, second = 0,
nanosecond = 0, nanosecond = 0,
).toInstant(TimeZone.UTC), ).toInstant(TimeZone.UTC),
type = Codelab, type = "Codelab",
topics = listOf(topicsTestData[2]), topics = listOf(topicsTestData[2]),
), ),
userData = userData, userData = userData,
@ -72,7 +69,7 @@ val userNewsResourcesTestData: List<UserNewsResource> = UserData(
url = "https://youtu.be/-fJ6poHQrjM", url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = topicsTestData.take(2), topics = topicsTestData.take(2),
), ),
userData = userData, userData = userData,
@ -88,7 +85,7 @@ val userNewsResourcesTestData: List<UserNewsResource> = UserData(
url = "https://youtu.be/ZARz0pjm5YM", url = "https://youtu.be/ZARz0pjm5YM",
headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(topicsTestData[2]), topics = listOf(topicsTestData[2]),
), ),
userData = userData, userData = userData,
@ -102,7 +99,7 @@ val userNewsResourcesTestData: List<UserNewsResource> = UserData(
url = "https://developer.android.com/jetpack/androidx/versions/all-channel", url = "https://developer.android.com/jetpack/androidx/versions/all-channel",
headerImageUrl = "", headerImageUrl = "",
publishDate = Instant.parse("2022-10-01T00:00:00.000Z"), publishDate = Instant.parse("2022-10-01T00:00:00.000Z"),
type = Unknown, type = "",
topics = listOf(topicsTestData[2]), topics = listOf(topicsTestData[2]),
), ),
userData = userData, userData = userData,

@ -34,7 +34,7 @@ class NewsResourceCardTest {
@Test @Test
fun testMetaDataDisplay_withCodelabResource() { fun testMetaDataDisplay_withCodelabResource() {
val newsWithKnownResourceType = userNewsResourcesTestData[0] val newsWithKnownResourceType = userNewsResourcesTestData[0]
var dateFormatted = "" lateinit var dateFormatted: String
composeTestRule.setContent { composeTestRule.setContent {
NewsResourceCardExpanded( NewsResourceCardExpanded(
@ -54,20 +54,20 @@ class NewsResourceCardTest {
composeTestRule.activity.getString( composeTestRule.activity.getString(
R.string.card_meta_data_text, R.string.card_meta_data_text,
dateFormatted, dateFormatted,
newsWithKnownResourceType.type.displayText, newsWithKnownResourceType.type,
), ),
) )
.assertExists() .assertExists()
} }
@Test @Test
fun testMetaDataDisplay_withUnknownResource() { fun testMetaDataDisplay_withEmptyResourceType() {
val newsWithUnknownResourceType = userNewsResourcesTestData[3] val newsWithEmptyResourceType = userNewsResourcesTestData[3]
var dateFormatted = "" lateinit var dateFormatted: String
composeTestRule.setContent { composeTestRule.setContent {
NewsResourceCardExpanded( NewsResourceCardExpanded(
userNewsResource = newsWithUnknownResourceType, userNewsResource = newsWithEmptyResourceType,
isBookmarked = false, isBookmarked = false,
hasBeenViewed = false, hasBeenViewed = false,
onToggleBookmark = {}, onToggleBookmark = {},
@ -75,7 +75,7 @@ class NewsResourceCardTest {
onTopicClick = {}, onTopicClick = {},
) )
dateFormatted = dateFormatted(publishDate = newsWithUnknownResourceType.publishDate) dateFormatted = dateFormatted(publishDate = newsWithEmptyResourceType.publishDate)
} }
composeTestRule composeTestRule

@ -68,7 +68,6 @@ 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.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
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.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import kotlinx.datetime.toJavaInstant import kotlinx.datetime.toJavaInstant
@ -264,12 +263,12 @@ fun dateFormatted(publishDate: Instant): String {
@Composable @Composable
fun NewsResourceMetaData( fun NewsResourceMetaData(
publishDate: Instant, publishDate: Instant,
resourceType: NewsResourceType, resourceType: String,
) { ) {
val formattedDate = dateFormatted(publishDate) val formattedDate = dateFormatted(publishDate)
Text( Text(
if (resourceType != NewsResourceType.Unknown) { if (resourceType.isNotBlank()) {
stringResource(R.string.card_meta_data_text, formattedDate, resourceType.displayText) stringResource(R.string.card_meta_data_text, formattedDate, resourceType)
} else { } else {
formattedDate formattedDate
}, },

@ -19,8 +19,6 @@ package com.google.samples.apps.nowinandroid.core.ui
import androidx.compose.ui.tooling.preview.PreviewParameterProvider import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
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.NewsResourceType.Video
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
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.UserData import com.google.samples.apps.nowinandroid.core.model.data.UserData
@ -97,7 +95,7 @@ object PreviewParameterData {
second = 0, second = 0,
nanosecond = 0, nanosecond = 0,
).toInstant(TimeZone.UTC), ).toInstant(TimeZone.UTC),
type = NewsResourceType.Codelab, type = "Codelab",
topics = listOf(topics[2]), topics = listOf(topics[2]),
), ),
userData = userData, userData = userData,
@ -113,7 +111,7 @@ object PreviewParameterData {
url = "https://youtu.be/-fJ6poHQrjM", url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = topics.take(2), topics = topics.take(2),
), ),
userData = userData, userData = userData,
@ -129,7 +127,7 @@ object PreviewParameterData {
url = "https://youtu.be/ZARz0pjm5YM", url = "https://youtu.be/ZARz0pjm5YM",
headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf(topics[2]), topics = listOf(topics[2]),
), ),
userData = userData, userData = userData,

@ -23,7 +23,6 @@ import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNe
import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
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.Video
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.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.mapToUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.mapToUserNewsResources
@ -545,7 +544,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/-fJ6poHQrjM", url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf( topics = listOf(
Topic( Topic(
id = "0", id = "0",
@ -566,7 +565,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/ZARz0pjm5YM", url = "https://youtu.be/ZARz0pjm5YM",
headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf( topics = listOf(
Topic( Topic(
id = "1", id = "1",
@ -585,7 +584,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/r5JgIyS3t3s", url = "https://youtu.be/r5JgIyS3t3s",
headerImageUrl = "https://i.ytimg.com/vi/r5JgIyS3t3s/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/r5JgIyS3t3s/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-08T00:00:00.000Z"), publishDate = Instant.parse("2021-11-08T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf( topics = listOf(
Topic( Topic(
id = "1", id = "1",

@ -20,7 +20,6 @@ import androidx.lifecycle.SavedStateHandle
import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
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.Video
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.testing.repository.TestNewsRepository import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository
@ -258,7 +257,7 @@ private val sampleNewsResources = listOf(
url = "https://youtu.be/-fJ6poHQrjM", url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video, type = "Video 📺",
topics = listOf( topics = listOf(
Topic( Topic(
id = "0", id = "0",

Loading…
Cancel
Save