Skip to content

Commit

Permalink
Make all filed from News API optional
Browse files Browse the repository at this point in the history
kirich1409 committed Jun 18, 2024
1 parent cee57a7 commit 866c017
Showing 7 changed files with 53 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import dev.androidbroadcast.news.database.models.Source as SourceDBO
internal fun ArticleDBO.toArticle(): Article {
return Article(
cacheId = id,
source = source.toSource(),
source = source?.toSource(),
author = author,
title = title,
description = description,
@@ -35,7 +35,7 @@ internal fun SourceDTO.toSourceDbo(): SourceDBO {

internal fun ArticleDTO.toArticle(): Article {
return Article(
source = source.toSource(),
source = source?.toSource(),
author = author,
title = title,
description = description,
@@ -48,7 +48,7 @@ internal fun ArticleDTO.toArticle(): Article {

internal fun ArticleDTO.toArticleDbo(): ArticleDBO {
return ArticleDBO(
source = source.toSourceDbo(),
source = source?.toSourceDbo(),
author = author,
title = title,
description = description,
Original file line number Diff line number Diff line change
@@ -4,14 +4,14 @@ import java.util.Date

public data class Article(
val cacheId: Long = ID_NONE,
val source: Source,
val source: Source?,
val author: String?,
val title: String,
val description: String,
val url: String,
val title: String?,
val description: String?,
val url: String?,
val urlToImage: String?,
val publishedAt: Date,
val content: String
val publishedAt: Date?,
val content: String?
) {
public companion object {
/**
Original file line number Diff line number Diff line change
@@ -8,14 +8,14 @@ import java.util.Date

@Entity(tableName = "articles")
data class ArticleDBO(
@Embedded(prefix = "source") val source: Source,
@Embedded(prefix = "source") val source: Source?,
@ColumnInfo("author") val author: String?,
@ColumnInfo("title") val title: String,
@ColumnInfo("description") val description: String,
@ColumnInfo("url") val url: String,
@ColumnInfo("title") val title: String?,
@ColumnInfo("description") val description: String?,
@ColumnInfo("url") val url: String?,
@ColumnInfo("urlToImage") val urlToImage: String?,
@ColumnInfo("publishedAt") val publishedAt: Date,
@ColumnInfo("content") val content: String,
@ColumnInfo("publishedAt") val publishedAt: Date?,
@ColumnInfo("content") val content: String?,
@PrimaryKey(autoGenerate = true) val id: Long = 0
)

Original file line number Diff line number Diff line change
@@ -7,12 +7,12 @@ import java.util.Date

@Serializable
data class ArticleDTO(
@SerialName("source") val source: SourceDTO,
@SerialName("source") val source: SourceDTO?,
@SerialName("author") val author: String?,
@SerialName("title") val title: String,
@SerialName("description") val description: String,
@SerialName("url") val url: String,
@SerialName("title") val title: String?,
@SerialName("description") val description: String?,
@SerialName("url") val url: String?,
@SerialName("urlToImage") val urlToImage: String?,
@[SerialName("publishedAt") Serializable(with = DateTimeUTCSerializer::class)] val publishedAt: Date,
@SerialName("content") val content: String
@[SerialName("publishedAt") Serializable(with = DateTimeUTCSerializer::class)] val publishedAt: Date?,
@SerialName("content") val content: String?
)
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ package dev.androidbroadcast.news.main

public data class ArticleUI(
val id: Long,
val title: String,
val description: String,
val title: String?,
val description: String?,
val imageUrl: String?,
val url: String
val url: String?
)
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ import dev.androidbroadcast.news.NewsTheme
@Composable
internal fun ArticleList(
articleState: State.Success,
modifier: Modifier = Modifier,
modifier: Modifier = Modifier
) {
ArticleList(articles = articleState.articles, modifier)
}
@@ -37,7 +37,7 @@ internal fun ArticleList(
@Composable
internal fun ArticleList(
@PreviewParameter(ArticlesPreviewProvider::class, limit = 1) articles: List<ArticleUI>,
modifier: Modifier = Modifier,
modifier: Modifier = Modifier
) {
LazyColumn(modifier) {
items(articles) { article ->
@@ -52,7 +52,7 @@ internal fun ArticleList(
@Composable
internal fun Article(
@PreviewParameter(ArticlePreviewProvider::class, limit = 1) article: ArticleUI,
modifier: Modifier = Modifier,
modifier: Modifier = Modifier
) {
Row(modifier.padding(bottom = 4.dp)) {
article.imageUrl?.let { imageUrl ->
@@ -73,17 +73,23 @@ internal fun Article(
}
Spacer(modifier = Modifier.size(4.dp))
Column(modifier = Modifier.padding(8.dp)) {
Text(
text = article.title,
style = NewsTheme.typography.headlineMedium,
maxLines = 1
)
val title = article.title
if (title != null) {
Text(
text = title,
style = NewsTheme.typography.headlineMedium,
maxLines = 1
)
}
Spacer(modifier = Modifier.size(4.dp))
Text(
text = article.description,
style = NewsTheme.typography.bodyMedium,
maxLines = 3
)
val description = article.description
if (description != null) {
Text(
text = description,
style = NewsTheme.typography.bodyMedium,
maxLines = 3
)
}
}
}
}
20 changes: 10 additions & 10 deletions schemas/dev.androidbroadcast.news.database.NewsRoomDatabase/1.json
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "713c887a36501d1941c3ed0942f44c8b",
"identityHash": "1700bfbe4426be55ff41583146e48816",
"entities": [
{
"tableName": "articles",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`author` TEXT, `title` TEXT NOT NULL, `description` TEXT NOT NULL, `url` TEXT NOT NULL, `urlToImage` TEXT, `publishedAt` TEXT NOT NULL, `content` TEXT NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `sourceid` TEXT NOT NULL, `sourcename` TEXT NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`author` TEXT, `title` TEXT, `description` TEXT, `url` TEXT, `urlToImage` TEXT, `publishedAt` TEXT, `content` TEXT, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `sourceid` TEXT, `sourcename` TEXT)",
"fields": [
{
"fieldPath": "author",
@@ -18,19 +18,19 @@
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
"notNull": false
},
{
"fieldPath": "description",
"columnName": "description",
"affinity": "TEXT",
"notNull": true
"notNull": false
},
{
"fieldPath": "url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
"notNull": false
},
{
"fieldPath": "urlToImage",
@@ -42,13 +42,13 @@
"fieldPath": "publishedAt",
"columnName": "publishedAt",
"affinity": "TEXT",
"notNull": true
"notNull": false
},
{
"fieldPath": "content",
"columnName": "content",
"affinity": "TEXT",
"notNull": true
"notNull": false
},
{
"fieldPath": "id",
@@ -60,13 +60,13 @@
"fieldPath": "source.id",
"columnName": "sourceid",
"affinity": "TEXT",
"notNull": true
"notNull": false
},
{
"fieldPath": "source.name",
"columnName": "sourcename",
"affinity": "TEXT",
"notNull": true
"notNull": false
}
],
"primaryKey": {
@@ -82,7 +82,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '713c887a36501d1941c3ed0942f44c8b')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1700bfbe4426be55ff41583146e48816')"
]
}
}

0 comments on commit 866c017

Please sign in to comment.