@@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
77import android.database.sqlite.SQLiteOpenHelper
88import com.simplemobiletools.notes.PREFS_KEY
99import com.simplemobiletools.notes.TEXT
10+ import com.simplemobiletools.notes.TYPE_NOTE
1011import com.simplemobiletools.notes.models.Note
1112import java.util.*
1213
@@ -15,13 +16,14 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
1516
1617 companion object {
1718 private val DB_NAME = " notes.db"
18- private val DB_VERSION = 1
19+ private val DB_VERSION = 2
1920 private val TABLE_NAME = " notes"
2021 private val NOTE = " General note"
2122
2223 private val COL_ID = " id"
2324 private val COL_TITLE = " title"
2425 private val COL_VALUE = " value"
26+ private val COL_TYPE = " type"
2527
2628 fun newInstance (context : Context ) = DBHelper (context)
2729 }
@@ -31,18 +33,18 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
3133 }
3234
3335 override fun onCreate (db : SQLiteDatabase ) {
34- db.execSQL(" CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT)" )
36+ db.execSQL(" CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT, $COL_TYPE INTEGER DEFAULT 0 )" )
3537 insertFirstNote(db)
3638 }
3739
3840 override fun onUpgrade (db : SQLiteDatabase , oldVersion : Int , newVersion : Int ) {
39-
41+ db.execSQL( " ALTER TABLE $TABLE_NAME ADD COLUMN $COL_TYPE INTEGER DEFAULT 0 " )
4042 }
4143
4244 private fun insertFirstNote (db : SQLiteDatabase ) {
4345 val prefs = mContext.getSharedPreferences(PREFS_KEY , Context .MODE_PRIVATE )
4446 val text = prefs.getString(TEXT , " " )
45- val note = Note (1 , NOTE , text)
47+ val note = Note (1 , NOTE , text, TYPE_NOTE )
4648 insertNote(note, db)
4749 }
4850
@@ -60,6 +62,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
6062 return ContentValues ().apply {
6163 put(COL_TITLE , note.title)
6264 put(COL_VALUE , note.value)
65+ put(COL_TYPE , 0 )
6366 }
6467 }
6568
@@ -79,7 +82,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
7982
8083 fun getNotes (): List <Note > {
8184 val notes = ArrayList <Note >()
82- val cols = arrayOf(COL_ID , COL_TITLE , COL_VALUE )
85+ val cols = arrayOf(COL_ID , COL_TITLE , COL_VALUE , COL_TYPE )
8386 var cursor: Cursor ? = null
8487 try {
8588 cursor = mDb.query(TABLE_NAME , cols, null , null , null , null , " $COL_TITLE COLLATE NOCASE ASC" )
@@ -88,7 +91,8 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
8891 val id = cursor.getInt(cursor.getColumnIndex(COL_ID ))
8992 val title = cursor.getString(cursor.getColumnIndex(COL_TITLE ))
9093 val value = cursor.getString(cursor.getColumnIndex(COL_VALUE ))
91- val note = Note (id, title, value)
94+ val type = cursor.getInt(cursor.getColumnIndex(COL_TYPE ))
95+ val note = Note (id, title, value, type)
9296 notes.add(note)
9397 } while (cursor.moveToNext())
9498 }
@@ -100,7 +104,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
100104 }
101105
102106 fun getNote (id : Int ): Note ? {
103- val cols = arrayOf(COL_TITLE , COL_VALUE )
107+ val cols = arrayOf(COL_TITLE , COL_VALUE , COL_TYPE )
104108 val selection = " $COL_ID = ?"
105109 val selectionArgs = arrayOf(id.toString())
106110 var note: Note ? = null
@@ -110,7 +114,8 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
110114 if (cursor != null && cursor.moveToFirst()) {
111115 val title = cursor.getString(cursor.getColumnIndex(COL_TITLE ))
112116 val value = cursor.getString(cursor.getColumnIndex(COL_VALUE ))
113- note = Note (id, title, value)
117+ val type = cursor.getInt(cursor.getColumnIndex(COL_TYPE ))
118+ note = Note (id, title, value, type)
114119 }
115120 } finally {
116121 cursor?.close()
0 commit comments