kotlin Android:仓库模式中的模型Map

gab6jxml  于 2023-10-23  发布在  Kotlin
关注(0)|答案(1)|浏览(91)

我想从远程获取新闻并将网络响应Map到实体类以存储在存储库层的房间数据库中。我正在使用远程和数据库单独的数据类
下面是我正在使用的数据类....
如何添加类似下面的Map逻辑?

fun NewsRemote.toLocal() = NewsLocal(
       ..............
)

网络响应数据类

data class NewsRemote(
    val articles: List<ArticlesItem?>? = null,
    val status: String? = null,
) {
    data class ArticlesItem(
        val publishedAt: String? = null,
        val author: String? = null,
        val urlToImage: String? = null,
        val description: String? = null,
        val source: Source? = null,
        val title: String? = null,
        val url: String? = null,
        val content: String? = null
    ) {
        data class Source(
            val name: String? = null,
            val id: String? = null
        )
    }
}

数据库数据类

@Entity(tableName = "news")
data class NewsLocal(
    @PrimaryKey
    var id: Int = Random().nextInt(),
    var publishedAt: String? = null,
    var author: String? = null,
    var urlToImage: String? = null,
    var description: String? = null,
    var source: Source? = null,
    var title: String? = null,
    var url: String? = null,
    var content: String? = null
)

数据类External

data class Headlines(
    val status: String? = null,
    val publishedAt: String? = null,
    val author: String? = null,
    val urlToImage: String? = null,
    val description: String? = null,
    val source: NewsSource? = null,
    val title: String? = null,
    val url: String? = null,
    val content: String? = null
)

data class NewsSource(
    val name: String? = null,
    val id: String? = null
)

我试图添加一些像下面,但不知道如何准确地Map

fun NewsRemote.toLocal() = NewsLocal(
       ..............
)
sqxo8psd

sqxo8psd1#

标记实体或POJO的字段以允许嵌套字段(即注解字段类的字段),以便在SQL查询中直接引用。
如果容器是实体,这些子字段将是实体数据库表中的列。

源类新增嵌入实体。

您的实体

@Entity(tableName = "news")
data class NewsLocal(
    @PrimaryKey(autoGenerate = true) 
    val id: Int,
    val publishedAt: String?,
    val author: String?,
    val urlToImage: String?,
    val description: String?,
    @Embedded 
    val source: Source?,
    val title: String?,
    val url: String?,
    val content: String?
)

@Entity(tableName = "source")
data class Source(
    @PrimaryKey(autoGenerate = true)
    val id: String,
    val name: String
)

Map

fun NewsRemote.ArticlesItem.toLocalNews(): NewsLocal {
   val sourceObj = Source(
        id = this.source?.id ?: "",
        name = this.source?.name ?: ""
    )

    return NewsLocal(
        id = 0,
        publishedAt = this.publishedAt,
        author = this.author,
        urlToImage = this.urlToImage,
        description = this.description,
        source = sourceObj,
        title = this.title,
        url = this.url,
        content = this.content
    )
}

fun NewsRemote.toLocalNewsList(): List<NewsLocal> {
    
   val newsLocalList: MutableList<NewsLocal> = mutableListOf()
   this.articles?.forEach { articleItem ->
        
        if (articleItem != null) {
            val localNews = articleItem.toLocalNews()
            newsLocalList.add(localNews)
        }
    }
    return newsLocalList
}

相关问题