Skip to content

Commit 0eca15e

Browse files
committed
Added database implementation
1 parent 6371f9a commit 0eca15e

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ apply plugin: 'kotlin-android'
44

55
apply plugin: 'kotlin-android-extensions'
66

7+
apply plugin: "kotlin-kapt"
8+
9+
710
android {
811
compileSdkVersion 28
912
defaultConfig {
@@ -32,4 +35,6 @@ dependencies {
3235
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
3336
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
3437
implementation 'com.google.android.material:material:1.1.0-alpha04'
38+
implementation "android.arch.persistence.room:runtime:1.1.0"
39+
kapt "android.arch.persistence.room:compiler:1.1.0"
3540
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hackware.mormont.notebook.db
2+
3+
import android.content.Context
4+
import androidx.room.Database
5+
import androidx.room.Room
6+
import androidx.room.RoomDatabase
7+
import com.hackware.mormont.notebook.db.dao.NotesDataDao
8+
import com.hackware.mormont.notebook.db.entity.NotesData
9+
10+
@Database(entities = [NotesData::class], version = 1)
11+
abstract class NotesDataBase: RoomDatabase() {
12+
13+
abstract fun notesDataDao(): NotesDataDao
14+
15+
companion object {
16+
private var INSTANCE: NotesDataBase? = null
17+
18+
fun getInstance(context: Context) : NotesDataBase? {
19+
if (INSTANCE == null) {
20+
synchronized(NotesDataBase::class){
21+
INSTANCE = Room.databaseBuilder(context.applicationContext,
22+
NotesDataBase::class.java, "notes.db").build()
23+
}
24+
}
25+
return INSTANCE
26+
}
27+
28+
fun destroyInstance(){
29+
INSTANCE = null
30+
}
31+
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hackware.mormont.notebook.db.dao
2+
3+
import androidx.room.*
4+
import com.hackware.mormont.notebook.db.entity.NotesData
5+
6+
@Dao
7+
interface NotesDataDao{
8+
9+
@Query("SELECT * from NotesData")
10+
fun getAll() : List<NotesData>
11+
12+
@Query ("SELECT * from NotesData WHERE id= :id LIMIT 1")
13+
fun loadNoteWithId(id: Int): NotesData
14+
15+
@Insert(onConflict = OnConflictStrategy.REPLACE)
16+
fun insertNote(notesdata: NotesData)
17+
18+
@Update
19+
fun updateNote(note: NotesData)
20+
21+
@Delete
22+
fun deleteNote(note: NotesData)
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.hackware.mormont.notebook.db.entity
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
7+
@Entity(tableName= "NotesData")
8+
data class NotesData(
9+
@PrimaryKey(autoGenerate = true) var id: Long?,
10+
@ColumnInfo(name = "note_id") var noteId: Int,
11+
@ColumnInfo(name = "created_date") var createdDate:Int,
12+
@ColumnInfo(name = "modified_date") var modifiedDate: Int,
13+
@ColumnInfo(name = "str_content") var strContent: String,
14+
@ColumnInfo(name = "content") var rawContent: String
15+
16+
){
17+
constructor():this(null, 0, 0,0,"","")
18+
}

0 commit comments

Comments
 (0)