enter image description here]1
class NewsRepository( private val newsService: NewsService, private val articleDatabase: ArticleDatabase, private val applicationContext: Context ) { private val newsLiveData = MutableLiveData() val news: LiveData get() = newsLiveData
suspend fun getNews(country: String) {
if (Network.isInternetThere(applicationContext)) {
val result = newsService.getNews(country)
if ( result.body()!=null) {
articleDatabase.ArticleDao().insertNews(result.body()!!.articles)
newsLiveData.postValue(result.body())
}
}
}
}
@Dao
interface ArticleDao {
@Insert
suspend fun insertNews(articles:List<Article>):List<Long>
}
@Entity(tableName="newsTable")
data class Article(
@PrimaryKey(autoGenerate = true)
val newsid:Int,
val urlToImage: String,
var author: String,
val content: String,
val description: String,
val publishedAt: String,
val title: String,
val url: String,
)