| | |
| | | package com.example.firstapp.database.repository |
| | | |
| | | import androidx.annotation.WorkerThread |
| | | import com.example.firstapp.database.dao.KeywordDao |
| | | import com.example.firstapp.database.entity.KeywordConfig |
| | | import com.example.firstapp.database.entity.KeywordEntity |
| | |
| | | return try { |
| | | // 从网络获取配置 |
| | | val response = apiService.getKeywords() |
| | | if (response.status == 1) { |
| | | if (response.code == "0") { |
| | | // 保存到本地数据库作为缓存 |
| | | saveToLocal(response.data) |
| | | response.data |
| | | keywordDao.getAllKeywords() |
| | | } else { |
| | | // 如果接口请求失败,使用本地缓存 |
| | | keywordDao.getAllKeywords() |
| | |
| | | } |
| | | } |
| | | |
| | | private suspend fun saveToLocal(keywords: List<KeywordEntity>) { |
| | | true |
| | | //keywordDao.insertAll(keywords.map { it.toEntity() }) |
| | | private suspend fun saveToLocal(keywords: List<KeywordConfig>) { |
| | | // keywords.map { it.toEntity() } |
| | | // keywordDao.insertAll(keywords) |
| | | val keywordEntities = keywords.map { it.toEntity() } |
| | | keywordDao.insertAll(keywordEntities) |
| | | } |
| | | |
| | | @WorkerThread |
| | | fun deleteAll() = keywordDao.deleteAll() |
| | | |
| | | |
| | | } |
| | | |