@@ -5,17 +5,15 @@ import android.content.Context
55import android.database.Cursor
66import android.database.sqlite.SQLiteDatabase
77import android.database.sqlite.SQLiteOpenHelper
8- import com.simplemobiletools.notes.helpers.PREFS_KEY
98import com.simplemobiletools.notes.R
10- import com.simplemobiletools.notes.helpers.TEXT
11- import com.simplemobiletools.notes.helpers.TYPE_NOTE
129import com.simplemobiletools.notes.extensions.getIntValue
1310import com.simplemobiletools.notes.extensions.getStringValue
11+ import com.simplemobiletools.notes.helpers.TYPE_NOTE
1412import com.simplemobiletools.notes.models.Note
1513import java.util.*
1614
1715class DBHelper private constructor(private val mContext : Context ) : SQLiteOpenHelper(mContext, DBHelper .DB_NAME , null , DBHelper .DB_VERSION ) {
18- private val mDb: SQLiteDatabase
16+ private val mDb: SQLiteDatabase = writableDatabase
1917
2018 companion object {
2119 private val DB_NAME = " notes.db"
@@ -31,10 +29,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
3129 fun newInstance (context : Context ) = DBHelper (context)
3230 }
3331
34- init {
35- mDb = writableDatabase
36- }
37-
3832 override fun onCreate (db : SQLiteDatabase ) {
3933 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)" )
4034 insertFirstNote(db)
@@ -50,9 +44,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
5044
5145 private fun insertFirstNote (db : SQLiteDatabase ) {
5246 val generalNote = mContext.resources.getString(R .string.general_note)
53- val prefs = mContext.getSharedPreferences(PREFS_KEY , Context .MODE_PRIVATE )
54- val text = prefs.getString(TEXT , " " )
55- val note = Note (1 , generalNote, text, TYPE_NOTE )
47+ val note = Note (1 , generalNote, " " , TYPE_NOTE )
5648 insertNote(note, db)
5749 }
5850
@@ -82,10 +74,13 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
8274 val cols = arrayOf(COL_ID )
8375 val selection = " $COL_TITLE = ?"
8476 val selectionArgs = arrayOf(title)
85- val cursor = mDb.query(TABLE_NAME , cols, selection, selectionArgs, null , null , null ) ? : return false
86- val cnt = cursor.count
87- cursor.close()
88- return cnt == 1
77+ var cursor: Cursor ? = null
78+ try {
79+ cursor = mDb.query(TABLE_NAME , cols, selection, selectionArgs, null , null , null )
80+ return cursor.count == 1
81+ } finally {
82+ cursor?.close()
83+ }
8984 }
9085
9186 fun getNotes (): List <Note > {
@@ -94,7 +89,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
9489 var cursor: Cursor ? = null
9590 try {
9691 cursor = mDb.query(TABLE_NAME , cols, null , null , null , null , " $COL_TITLE COLLATE NOCASE ASC" )
97- if (cursor != null && cursor .moveToFirst()) {
92+ if (cursor? .moveToFirst() == true ) {
9893 do {
9994 val id = cursor.getIntValue(COL_ID )
10095 val title = cursor.getStringValue(COL_TITLE )
@@ -119,7 +114,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
119114 var cursor: Cursor ? = null
120115 try {
121116 cursor = mDb.query(TABLE_NAME , cols, selection, selectionArgs, null , null , null )
122- if (cursor != null && cursor .moveToFirst()) {
117+ if (cursor? .moveToFirst() == true ) {
123118 val title = cursor.getStringValue(COL_TITLE )
124119 val value = cursor.getStringValue(COL_VALUE )
125120 val type = cursor.getIntValue(COL_TYPE )
0 commit comments