couple database handling tweaks

This commit is contained in:
tibbi 2017-01-06 19:58:35 +01:00
parent ba2cb16893
commit ca16d12112
3 changed files with 17 additions and 22 deletions

View File

@ -61,9 +61,11 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
invalidateOptionsMenu() invalidateOptionsMenu()
pager_title_strip.setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize()) pager_title_strip.apply {
pager_title_strip.setGravity(Gravity.CENTER_VERTICAL) setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize())
pager_title_strip.setNonPrimaryAlpha(0.4f) setGravity(Gravity.CENTER_VERTICAL)
setNonPrimaryAlpha(0.4f)
}
} }
override fun onDestroy() { override fun onDestroy() {

View File

@ -5,17 +5,15 @@ import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import com.simplemobiletools.notes.helpers.PREFS_KEY
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.helpers.TEXT
import com.simplemobiletools.notes.helpers.TYPE_NOTE
import com.simplemobiletools.notes.extensions.getIntValue import com.simplemobiletools.notes.extensions.getIntValue
import com.simplemobiletools.notes.extensions.getStringValue import com.simplemobiletools.notes.extensions.getStringValue
import com.simplemobiletools.notes.helpers.TYPE_NOTE
import com.simplemobiletools.notes.models.Note import com.simplemobiletools.notes.models.Note
import java.util.* import java.util.*
class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DBHelper.DB_NAME, null, DBHelper.DB_VERSION) { class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DBHelper.DB_NAME, null, DBHelper.DB_VERSION) {
private val mDb: SQLiteDatabase private val mDb: SQLiteDatabase = writableDatabase
companion object { companion object {
private val DB_NAME = "notes.db" private val DB_NAME = "notes.db"
@ -31,10 +29,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
fun newInstance(context: Context) = DBHelper(context) fun newInstance(context: Context) = DBHelper(context)
} }
init {
mDb = writableDatabase
}
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT, $COL_TYPE INTEGER DEFAULT 0, $COL_PATH TEXT)") db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT, $COL_TYPE INTEGER DEFAULT 0, $COL_PATH TEXT)")
insertFirstNote(db) insertFirstNote(db)
@ -50,9 +44,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
private fun insertFirstNote(db: SQLiteDatabase) { private fun insertFirstNote(db: SQLiteDatabase) {
val generalNote = mContext.resources.getString(R.string.general_note) val generalNote = mContext.resources.getString(R.string.general_note)
val prefs = mContext.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) val note = Note(1, generalNote, "", TYPE_NOTE)
val text = prefs.getString(TEXT, "")
val note = Note(1, generalNote, text, TYPE_NOTE)
insertNote(note, db) insertNote(note, db)
} }
@ -82,10 +74,13 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
val cols = arrayOf(COL_ID) val cols = arrayOf(COL_ID)
val selection = "$COL_TITLE = ?" val selection = "$COL_TITLE = ?"
val selectionArgs = arrayOf(title) val selectionArgs = arrayOf(title)
val cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null) ?: return false var cursor: Cursor? = null
val cnt = cursor.count try {
cursor.close() cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null)
return cnt == 1 return cursor.count == 1
} finally {
cursor?.close()
}
} }
fun getNotes(): List<Note> { fun getNotes(): List<Note> {
@ -94,7 +89,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = mDb.query(TABLE_NAME, cols, null, null, null, null, "$COL_TITLE COLLATE NOCASE ASC") cursor = mDb.query(TABLE_NAME, cols, null, null, null, null, "$COL_TITLE COLLATE NOCASE ASC")
if (cursor != null && cursor.moveToFirst()) { if (cursor?.moveToFirst() == true) {
do { do {
val id = cursor.getIntValue(COL_ID) val id = cursor.getIntValue(COL_ID)
val title = cursor.getStringValue(COL_TITLE) val title = cursor.getStringValue(COL_TITLE)
@ -119,7 +114,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
var cursor: Cursor? = null var cursor: Cursor? = null
try { try {
cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null) cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null)
if (cursor != null && cursor.moveToFirst()) { if (cursor?.moveToFirst() == true) {
val title = cursor.getStringValue(COL_TITLE) val title = cursor.getStringValue(COL_TITLE)
val value = cursor.getStringValue(COL_VALUE) val value = cursor.getStringValue(COL_VALUE)
val type = cursor.getIntValue(COL_TYPE) val type = cursor.getIntValue(COL_TYPE)

View File

@ -5,8 +5,6 @@ val NOTE_ID = "note_id"
// shared preferences // shared preferences
val PREFS_KEY = "Notes" val PREFS_KEY = "Notes"
val IS_FIRST_RUN = "is_first_run"
val IS_DARK_THEME = "is_dark_theme"
val CURRENT_NOTE_ID = "current_note_id" val CURRENT_NOTE_ID = "current_note_id"
val WIDGET_NOTE_ID = "widget_note_id" val WIDGET_NOTE_ID = "widget_note_id"
val FONT_SIZE = "font_size" val FONT_SIZE = "font_size"