@@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
7
7
import android.database.sqlite.SQLiteOpenHelper
8
8
import com.simplemobiletools.notes.PREFS_KEY
9
9
import com.simplemobiletools.notes.TEXT
10
+ import com.simplemobiletools.notes.TYPE_NOTE
10
11
import com.simplemobiletools.notes.models.Note
11
12
import java.util.*
12
13
@@ -15,13 +16,14 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
15
16
16
17
companion object {
17
18
private val DB_NAME = " notes.db"
18
- private val DB_VERSION = 1
19
+ private val DB_VERSION = 2
19
20
private val TABLE_NAME = " notes"
20
21
private val NOTE = " General note"
21
22
22
23
private val COL_ID = " id"
23
24
private val COL_TITLE = " title"
24
25
private val COL_VALUE = " value"
26
+ private val COL_TYPE = " type"
25
27
26
28
fun newInstance (context : Context ) = DBHelper (context)
27
29
}
@@ -31,18 +33,18 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
31
33
}
32
34
33
35
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 )" )
35
37
insertFirstNote(db)
36
38
}
37
39
38
40
override fun onUpgrade (db : SQLiteDatabase , oldVersion : Int , newVersion : Int ) {
39
-
41
+ db.execSQL( " ALTER TABLE $TABLE_NAME ADD COLUMN $COL_TYPE INTEGER DEFAULT 0 " )
40
42
}
41
43
42
44
private fun insertFirstNote (db : SQLiteDatabase ) {
43
45
val prefs = mContext.getSharedPreferences(PREFS_KEY , Context .MODE_PRIVATE )
44
46
val text = prefs.getString(TEXT , " " )
45
- val note = Note (1 , NOTE , text)
47
+ val note = Note (1 , NOTE , text, TYPE_NOTE )
46
48
insertNote(note, db)
47
49
}
48
50
@@ -60,6 +62,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
60
62
return ContentValues ().apply {
61
63
put(COL_TITLE , note.title)
62
64
put(COL_VALUE , note.value)
65
+ put(COL_TYPE , 0 )
63
66
}
64
67
}
65
68
@@ -79,7 +82,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
79
82
80
83
fun getNotes (): List <Note > {
81
84
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 )
83
86
var cursor: Cursor ? = null
84
87
try {
85
88
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
88
91
val id = cursor.getInt(cursor.getColumnIndex(COL_ID ))
89
92
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE ))
90
93
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)
92
96
notes.add(note)
93
97
} while (cursor.moveToNext())
94
98
}
@@ -100,7 +104,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
100
104
}
101
105
102
106
fun getNote (id : Int ): Note ? {
103
- val cols = arrayOf(COL_TITLE , COL_VALUE )
107
+ val cols = arrayOf(COL_TITLE , COL_VALUE , COL_TYPE )
104
108
val selection = " $COL_ID = ?"
105
109
val selectionArgs = arrayOf(id.toString())
106
110
var note: Note ? = null
@@ -110,7 +114,8 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
110
114
if (cursor != null && cursor.moveToFirst()) {
111
115
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE ))
112
116
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)
114
119
}
115
120
} finally {
116
121
cursor?.close()
0 commit comments