Skip to content

Commit

Permalink
add a new field, note type
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed Nov 14, 2016
1 parent afbf579 commit 6c67016
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/src/main/kotlin/com/simplemobiletools/notes/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ val FONT_SIZE_SMALL = 0
val FONT_SIZE_MEDIUM = 1
val FONT_SIZE_LARGE = 2
val FONT_SIZE_EXTRA_LARGE = 3

// note types
val TYPE_NOTE = 0
val TYPE_CHECKLIST = 1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.simplemobiletools.notes.MyWidgetProvider
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.TYPE_NOTE
import com.simplemobiletools.notes.Utils
import com.simplemobiletools.notes.databases.DBHelper
import com.simplemobiletools.notes.dialogs.OpenNoteDialog
Expand Down Expand Up @@ -139,7 +140,7 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener {
toast(R.string.title_taken)
} else {
saveText()
val newNote = Note(0, title, "")
val newNote = Note(0, title, "", TYPE_NOTE)
val id = mDb.insertNote(newNote)
updateSelectedNote(id)
dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.simplemobiletools.notes.PREFS_KEY
import com.simplemobiletools.notes.TEXT
import com.simplemobiletools.notes.TYPE_NOTE
import com.simplemobiletools.notes.models.Note
import java.util.*

Expand All @@ -15,13 +16,14 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe

companion object {
private val DB_NAME = "notes.db"
private val DB_VERSION = 1
private val DB_VERSION = 2
private val TABLE_NAME = "notes"
private val NOTE = "General note"

private val COL_ID = "id"
private val COL_TITLE = "title"
private val COL_VALUE = "value"
private val COL_TYPE = "type"

fun newInstance(context: Context) = DBHelper(context)
}
Expand All @@ -31,18 +33,18 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
}

override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT)")
db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT, $COL_TYPE INTEGER DEFAULT 0)")
insertFirstNote(db)
}

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {

db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COL_TYPE INTEGER DEFAULT 0")
}

private fun insertFirstNote(db: SQLiteDatabase) {
val prefs = mContext.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
val text = prefs.getString(TEXT, "")
val note = Note(1, NOTE, text)
val note = Note(1, NOTE, text, TYPE_NOTE)
insertNote(note, db)
}

Expand All @@ -60,6 +62,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
return ContentValues().apply {
put(COL_TITLE, note.title)
put(COL_VALUE, note.value)
put(COL_TYPE, 0)
}
}

Expand All @@ -79,7 +82,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe

fun getNotes(): List<Note> {
val notes = ArrayList<Note>()
val cols = arrayOf(COL_ID, COL_TITLE, COL_VALUE)
val cols = arrayOf(COL_ID, COL_TITLE, COL_VALUE, COL_TYPE)
var cursor: Cursor? = null
try {
cursor = mDb.query(TABLE_NAME, cols, null, null, null, null, "$COL_TITLE COLLATE NOCASE ASC")
Expand All @@ -88,7 +91,8 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
val id = cursor.getInt(cursor.getColumnIndex(COL_ID))
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE))
val value = cursor.getString(cursor.getColumnIndex(COL_VALUE))
val note = Note(id, title, value)
val type = cursor.getInt(cursor.getColumnIndex(COL_TYPE))
val note = Note(id, title, value, type)
notes.add(note)
} while (cursor.moveToNext())
}
Expand All @@ -100,7 +104,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
}

fun getNote(id: Int): Note? {
val cols = arrayOf(COL_TITLE, COL_VALUE)
val cols = arrayOf(COL_TITLE, COL_VALUE, COL_TYPE)
val selection = "$COL_ID = ?"
val selectionArgs = arrayOf(id.toString())
var note: Note? = null
Expand All @@ -110,7 +114,8 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
if (cursor != null && cursor.moveToFirst()) {
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE))
val value = cursor.getString(cursor.getColumnIndex(COL_VALUE))
note = Note(id, title, value)
val type = cursor.getInt(cursor.getColumnIndex(COL_TYPE))
note = Note(id, title, value, type)
}
} finally {
cursor?.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.simplemobiletools.notes.models

class Note(var id: Int, var title: String, var value: String) {
class Note(var id: Int, var title: String, var value: String, val type: Int) {

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

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

0 comments on commit 6c67016

Please sign in to comment.