Skip to content

Commit ca16d12

Browse files
committed
couple database handling tweaks
1 parent ba2cb16 commit ca16d12

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
6161
override fun onResume() {
6262
super.onResume()
6363
invalidateOptionsMenu()
64-
pager_title_strip.setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize())
65-
pager_title_strip.setGravity(Gravity.CENTER_VERTICAL)
66-
pager_title_strip.setNonPrimaryAlpha(0.4f)
64+
pager_title_strip.apply {
65+
setTextSize(TypedValue.COMPLEX_UNIT_PX, getTextSize())
66+
setGravity(Gravity.CENTER_VERTICAL)
67+
setNonPrimaryAlpha(0.4f)
68+
}
6769
}
6870

6971
override fun onDestroy() {

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import android.content.Context
55
import android.database.Cursor
66
import android.database.sqlite.SQLiteDatabase
77
import android.database.sqlite.SQLiteOpenHelper
8-
import com.simplemobiletools.notes.helpers.PREFS_KEY
98
import com.simplemobiletools.notes.R
10-
import com.simplemobiletools.notes.helpers.TEXT
11-
import com.simplemobiletools.notes.helpers.TYPE_NOTE
129
import com.simplemobiletools.notes.extensions.getIntValue
1310
import com.simplemobiletools.notes.extensions.getStringValue
11+
import com.simplemobiletools.notes.helpers.TYPE_NOTE
1412
import com.simplemobiletools.notes.models.Note
1513
import java.util.*
1614

1715
class 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)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ val NOTE_ID = "note_id"
55

66
// shared preferences
77
val PREFS_KEY = "Notes"
8-
val IS_FIRST_RUN = "is_first_run"
9-
val IS_DARK_THEME = "is_dark_theme"
108
val CURRENT_NOTE_ID = "current_note_id"
119
val WIDGET_NOTE_ID = "widget_note_id"
1210
val FONT_SIZE = "font_size"

0 commit comments

Comments
 (0)