Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Room implementation #3

Merged
merged 4 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added room implementation
  • Loading branch information
MuhammedEdrees committed Sep 4, 2023
commit f2f49731c239d4fc4459f15315e9b0ce45a82bff
13 changes: 13 additions & 0 deletions app/src/main/java/com/edrees/newsapp/dao/ArticleDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.edrees.newsapp.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import com.edrees.newsapp.model.Article

@Dao
interface ArticleDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertArticle(vararg articles: Article)
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/edrees/newsapp/db/NewsDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.edrees.newsapp.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.edrees.newsapp.dao.ArticleDao
import com.edrees.newsapp.model.Article

@Database(entities = [Article::class], version = 1)
abstract class NewsDatabase : RoomDatabase(){
abstract fun articleDao(): ArticleDao
companion object{
@Volatile
private var INSTANCE: NewsDatabase? = null
fun getInstance(context: Context): NewsDatabase{
return INSTANCE?: synchronized(this){
INSTANCE?: Room.databaseBuilder(context, NewsDatabase::class.java, "news_db")
.fallbackToDestructiveMigration()
.build()
.also { INSTANCE = it}
}
}
}
}
11 changes: 8 additions & 3 deletions app/src/main/java/com/edrees/newsapp/model/Article.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.edrees.newsapp.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity("articles")
data class Article(
val author: String,
val content: String,
val description: String,
val publishedAt: String,
@ColumnInfo(name="puplished_at")val publishedAt: String,
val source: Source,
val title: String,
val url: String,
val urlToImage: String
@PrimaryKey val url: String,
@ColumnInfo(name="url_to_image")val urlToImage: String
)