mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
creating a widgets table for holding individual widget info
This commit is contained in:
@ -336,7 +336,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
|||||||
toast(R.string.invalid_file_format)
|
toast(R.string.invalid_file_format)
|
||||||
} else if (file.length() > 10 * 1000 * 1000) {
|
} else if (file.length() > 10 * 1000 * 1000) {
|
||||||
toast(R.string.file_too_large)
|
toast(R.string.file_too_large)
|
||||||
} else if (checkTitle && dbHelper.doesTitleExist(path.getFilenameFromPath())) {
|
} else if (checkTitle && dbHelper.doesNoteTitleExist(path.getFilenameFromPath())) {
|
||||||
toast(R.string.title_taken)
|
toast(R.string.title_taken)
|
||||||
} else {
|
} else {
|
||||||
onChecksPassed(file)
|
onChecksPassed(file)
|
||||||
@ -367,7 +367,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
|||||||
private fun openPath(path: String) {
|
private fun openPath(path: String) {
|
||||||
openFile(path, false) {
|
openFile(path, false) {
|
||||||
var title = path.getFilenameFromPath()
|
var title = path.getFilenameFromPath()
|
||||||
if (dbHelper.doesTitleExist(title))
|
if (dbHelper.doesNoteTitleExist(title))
|
||||||
title += " (file)"
|
title += " (file)"
|
||||||
|
|
||||||
val note = Note(0, title, "", TYPE_NOTE, path)
|
val note = Note(0, title, "", TYPE_NOTE, path)
|
||||||
|
@ -44,7 +44,7 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
|
|||||||
file.isDirectory -> false
|
file.isDirectory -> false
|
||||||
filename.isMediaFile() -> false
|
filename.isMediaFile() -> false
|
||||||
file.length() > 10 * 1000 * 1000 -> false
|
file.length() > 10 * 1000 * 1000 -> false
|
||||||
activity.dbHelper.doesTitleExist(filename) -> false
|
activity.dbHelper.doesNoteTitleExist(filename) -> false
|
||||||
else -> true
|
else -> true
|
||||||
}
|
}
|
||||||
}.forEach {
|
}.forEach {
|
||||||
|
@ -25,7 +25,7 @@ class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title:
|
|||||||
val title = view.note_name.value
|
val title = view.note_name.value
|
||||||
when {
|
when {
|
||||||
title.isEmpty() -> activity.toast(R.string.no_title)
|
title.isEmpty() -> activity.toast(R.string.no_title)
|
||||||
db.doesTitleExist(title) -> activity.toast(R.string.title_taken)
|
db.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken)
|
||||||
else -> {
|
else -> {
|
||||||
callback(title)
|
callback(title)
|
||||||
dismiss()
|
dismiss()
|
||||||
|
@ -26,7 +26,7 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (
|
|||||||
val title = view.note_name.value
|
val title = view.note_name.value
|
||||||
when {
|
when {
|
||||||
title.isEmpty() -> activity.toast(R.string.no_title)
|
title.isEmpty() -> activity.toast(R.string.no_title)
|
||||||
activity.dbHelper.doesTitleExist(title) -> activity.toast(R.string.title_taken)
|
activity.dbHelper.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken)
|
||||||
else -> {
|
else -> {
|
||||||
note.title = title
|
note.title = title
|
||||||
val path = note.path
|
val path = note.path
|
||||||
|
@ -48,7 +48,7 @@ class NoteFragment : Fragment() {
|
|||||||
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
|
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
|
||||||
noteId = arguments!!.getInt(NOTE_ID)
|
noteId = arguments!!.getInt(NOTE_ID)
|
||||||
db = context!!.dbHelper
|
db = context!!.dbHelper
|
||||||
note = db.getNote(noteId) ?: return view
|
note = db.getNoteWithId(noteId) ?: return view
|
||||||
retainInstance = true
|
retainInstance = true
|
||||||
|
|
||||||
val layoutToInflate = if (context!!.config.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable
|
val layoutToInflate = if (context!!.config.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.simplemobiletools.notes.helpers
|
package com.simplemobiletools.notes.helpers
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
|
import android.content.ComponentName
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
@ -9,7 +11,9 @@ import android.database.sqlite.SQLiteOpenHelper
|
|||||||
import com.simplemobiletools.commons.extensions.getIntValue
|
import com.simplemobiletools.commons.extensions.getIntValue
|
||||||
import com.simplemobiletools.commons.extensions.getStringValue
|
import com.simplemobiletools.commons.extensions.getStringValue
|
||||||
import com.simplemobiletools.notes.R
|
import com.simplemobiletools.notes.R
|
||||||
|
import com.simplemobiletools.notes.extensions.config
|
||||||
import com.simplemobiletools.notes.models.Note
|
import com.simplemobiletools.notes.models.Note
|
||||||
|
import com.simplemobiletools.notes.models.Widget
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@ -18,8 +22,9 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val DB_NAME = "notes.db"
|
private const val DB_NAME = "notes.db"
|
||||||
private const val DB_VERSION = 3
|
private const val DB_VERSION = 4
|
||||||
private const val TABLE_NAME = "notes"
|
private const val NOTES_TABLE_NAME = "notes"
|
||||||
|
private const val WIDGETS_TABLE_NAME = "widgets"
|
||||||
|
|
||||||
private const val COL_ID = "id"
|
private const val COL_ID = "id"
|
||||||
private const val COL_TITLE = "title"
|
private const val COL_TITLE = "title"
|
||||||
@ -27,21 +32,30 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
private const val COL_TYPE = "type"
|
private const val COL_TYPE = "type"
|
||||||
private const val COL_PATH = "path"
|
private const val COL_PATH = "path"
|
||||||
|
|
||||||
|
private const val COL_WIDGET_ID = "widget_id"
|
||||||
|
private const val COL_NOTE_ID = "note_id"
|
||||||
|
|
||||||
fun newInstance(context: Context) = DBHelper(context)
|
fun newInstance(context: Context) = DBHelper(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(db: SQLiteDatabase) {
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT, $COL_TYPE INTEGER DEFAULT 0, $COL_PATH TEXT)")
|
db.execSQL("CREATE TABLE $NOTES_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT, $COL_TYPE INTEGER DEFAULT 0, $COL_PATH TEXT)")
|
||||||
|
db.execSQL("CREATE TABLE $WIDGETS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_WIDGET_ID INTEGER DEFAULT 0, $COL_NOTE_ID INTEGER DEFAULT 0)")
|
||||||
insertFirstNote(db)
|
insertFirstNote(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||||
if (oldVersion < 2) {
|
if (oldVersion < 2) {
|
||||||
db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COL_TYPE INTEGER DEFAULT 0")
|
db.execSQL("ALTER TABLE $NOTES_TABLE_NAME ADD COLUMN $COL_TYPE INTEGER DEFAULT 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldVersion < 3) {
|
if (oldVersion < 3) {
|
||||||
db.execSQL("ALTER TABLE $TABLE_NAME ADD COLUMN $COL_PATH TEXT DEFAULT ''")
|
db.execSQL("ALTER TABLE $NOTES_TABLE_NAME ADD COLUMN $COL_PATH TEXT DEFAULT ''")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldVersion < 4) {
|
||||||
|
db.execSQL("CREATE TABLE $WIDGETS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_WIDGET_ID INTEGER DEFAULT 0, $COL_NOTE_ID INTEGER DEFAULT 0)")
|
||||||
|
insertFirstWidget(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,17 +65,36 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
insertNote(note, db)
|
insertNote(note, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if a user has exactly 1 widget active, prefill it. Can happen only at upgrading from older app versions
|
||||||
|
private fun insertFirstWidget(db: SQLiteDatabase) {
|
||||||
|
val widgetIDs = AppWidgetManager.getInstance(mContext).getAppWidgetIds(ComponentName(mContext, MyWidgetProvider::class.java))
|
||||||
|
if (widgetIDs.size == 1) {
|
||||||
|
val widget = Widget(widgetIDs.first(), mContext.config.widgetNoteId)
|
||||||
|
insertWidget(widget, db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun insertNote(note: Note, db: SQLiteDatabase) {
|
private fun insertNote(note: Note, db: SQLiteDatabase) {
|
||||||
val values = fillContentValues(note)
|
val values = fillNoteContentValues(note)
|
||||||
db.insert(TABLE_NAME, null, values)
|
db.insert(NOTES_TABLE_NAME, null, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun insertWidget(widget: Widget, db: SQLiteDatabase) {
|
||||||
|
val values = fillWidgetContentValues(widget)
|
||||||
|
db.insert(WIDGETS_TABLE_NAME, null, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun insertNote(note: Note): Int {
|
fun insertNote(note: Note): Int {
|
||||||
val values = fillContentValues(note)
|
val values = fillNoteContentValues(note)
|
||||||
return mDb.insertWithOnConflict(TABLE_NAME, null, values, CONFLICT_IGNORE).toInt()
|
return mDb.insertWithOnConflict(NOTES_TABLE_NAME, null, values, CONFLICT_IGNORE).toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fillContentValues(note: Note): ContentValues {
|
fun insertWidget(widget: Widget): Int {
|
||||||
|
val values = fillWidgetContentValues(widget)
|
||||||
|
return mDb.insertWithOnConflict(NOTES_TABLE_NAME, null, values, CONFLICT_IGNORE).toInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fillNoteContentValues(note: Note): ContentValues {
|
||||||
return ContentValues().apply {
|
return ContentValues().apply {
|
||||||
put(COL_TITLE, note.title)
|
put(COL_TITLE, note.title)
|
||||||
put(COL_VALUE, note.value)
|
put(COL_VALUE, note.value)
|
||||||
@ -70,17 +103,24 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteNote(id: Int) {
|
private fun fillWidgetContentValues(widget: Widget): ContentValues {
|
||||||
mDb.delete(TABLE_NAME, "$COL_ID = $id", null)
|
return ContentValues().apply {
|
||||||
|
put(COL_WIDGET_ID, widget.widgetId)
|
||||||
|
put(COL_NOTE_ID, widget.noteId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doesTitleExist(title: String): Boolean {
|
fun deleteNote(id: Int) {
|
||||||
|
mDb.delete(NOTES_TABLE_NAME, "$COL_ID = $id", null)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun doesNoteTitleExist(title: String): Boolean {
|
||||||
val cols = arrayOf(COL_ID)
|
val cols = arrayOf(COL_ID)
|
||||||
val selection = "$COL_TITLE = ? COLLATE NOCASE"
|
val selection = "$COL_TITLE = ? COLLATE NOCASE"
|
||||||
val selectionArgs = arrayOf(title)
|
val selectionArgs = arrayOf(title)
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||||
return cursor.count == 1
|
return cursor.count == 1
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
@ -92,7 +132,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
val cols = arrayOf(COL_ID, COL_TITLE, COL_VALUE, COL_TYPE, COL_PATH)
|
val cols = arrayOf(COL_ID, COL_TITLE, COL_VALUE, COL_TYPE, COL_PATH)
|
||||||
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(NOTES_TABLE_NAME, cols, null, null, null, null, "$COL_TITLE COLLATE NOCASE ASC")
|
||||||
if (cursor?.moveToFirst() == true) {
|
if (cursor?.moveToFirst() == true) {
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
@ -120,14 +160,14 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
return notes
|
return notes
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getNote(id: Int): Note? {
|
fun getNoteWithId(id: Int): Note? {
|
||||||
val cols = arrayOf(COL_TITLE, COL_VALUE, COL_TYPE, COL_PATH)
|
val cols = arrayOf(COL_TITLE, COL_VALUE, COL_TYPE, COL_PATH)
|
||||||
val selection = "$COL_ID = ?"
|
val selection = "$COL_ID = ?"
|
||||||
val selectionArgs = arrayOf(id.toString())
|
val selectionArgs = arrayOf(id.toString())
|
||||||
var note: Note? = null
|
var note: Note? = null
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||||
if (cursor?.moveToFirst() == true) {
|
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)
|
||||||
@ -147,7 +187,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
val selectionArgs = arrayOf(path)
|
val selectionArgs = arrayOf(path)
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||||
if (cursor?.moveToFirst() == true) {
|
if (cursor?.moveToFirst() == true) {
|
||||||
return cursor.getIntValue(COL_ID)
|
return cursor.getIntValue(COL_ID)
|
||||||
}
|
}
|
||||||
@ -175,7 +215,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
|||||||
private fun updateNote(id: Int, values: ContentValues) {
|
private fun updateNote(id: Int, values: ContentValues) {
|
||||||
val selection = "$COL_ID = ?"
|
val selection = "$COL_ID = ?"
|
||||||
val selectionArgs = arrayOf(id.toString())
|
val selectionArgs = arrayOf(id.toString())
|
||||||
mDb.update(TABLE_NAME, values, selection, selectionArgs)
|
mDb.update(NOTES_TABLE_NAME, values, selection, selectionArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isValidId(id: Int) = id > 0
|
fun isValidId(id: Int) = id > 0
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.simplemobiletools.notes.models
|
||||||
|
|
||||||
|
data class Widget(var widgetId: Int, var noteId: Int)
|
Reference in New Issue
Block a user