Skip to content

Commit 6c67016

Browse files
committed
add a new field, note type
1 parent afbf579 commit 6c67016

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/Constants.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ val FONT_SIZE_SMALL = 0
1717
val FONT_SIZE_MEDIUM = 1
1818
val FONT_SIZE_LARGE = 2
1919
val FONT_SIZE_EXTRA_LARGE = 3
20+
21+
// note types
22+
val TYPE_NOTE = 0
23+
val TYPE_CHECKLIST = 1

app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodManager
1515
import android.widget.EditText
1616
import com.simplemobiletools.notes.MyWidgetProvider
1717
import com.simplemobiletools.notes.R
18+
import com.simplemobiletools.notes.TYPE_NOTE
1819
import com.simplemobiletools.notes.Utils
1920
import com.simplemobiletools.notes.databases.DBHelper
2021
import com.simplemobiletools.notes.dialogs.OpenNoteDialog
@@ -139,7 +140,7 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener {
139140
toast(R.string.title_taken)
140141
} else {
141142
saveText()
142-
val newNote = Note(0, title, "")
143+
val newNote = Note(0, title, "", TYPE_NOTE)
143144
val id = mDb.insertNote(newNote)
144145
updateSelectedNote(id)
145146
dismiss()

app/src/main/kotlin/com/simplemobiletools/notes/databases/DBHelper.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
77
import android.database.sqlite.SQLiteOpenHelper
88
import com.simplemobiletools.notes.PREFS_KEY
99
import com.simplemobiletools.notes.TEXT
10+
import com.simplemobiletools.notes.TYPE_NOTE
1011
import com.simplemobiletools.notes.models.Note
1112
import 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()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.simplemobiletools.notes.models
22

3-
class Note(var id: Int, var title: String, var value: String) {
3+
class Note(var id: Int, var title: String, var value: String, val type: Int) {
44

55
override fun equals(other: Any?) = other != null && this.toString() == other.toString()
66

7-
override fun toString() = "Note {id=$id, title=$title, value=$value}"
7+
override fun toString() = "Note {id=$id, title=$title, value=$value, type=$type}"
88
}

0 commit comments

Comments
 (0)