mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
adding widget support too
This commit is contained in:
@ -1,101 +0,0 @@
|
||||
package com.simplemobiletools.notes.pro.helpers
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import com.simplemobiletools.commons.extensions.getIntValue
|
||||
import com.simplemobiletools.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.models.Note
|
||||
import com.simplemobiletools.notes.pro.models.Widget
|
||||
import java.util.*
|
||||
|
||||
class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DB_NAME, null, DB_VERSION) {
|
||||
private val mDb = writableDatabase
|
||||
|
||||
companion object {
|
||||
private const val DB_NAME = "notes_old.db"
|
||||
private const val DB_VERSION = 4
|
||||
private const val NOTES_TABLE_NAME = "notes"
|
||||
private const val WIDGETS_TABLE_NAME = "widgets"
|
||||
|
||||
private const val COL_ID = "id"
|
||||
private const val COL_TITLE = "title"
|
||||
private const val COL_VALUE = "value"
|
||||
private const val COL_TYPE = "type"
|
||||
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)
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
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)
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
|
||||
|
||||
private fun insertFirstNote(db: SQLiteDatabase) {
|
||||
val generalNote = mContext.resources.getString(R.string.general_note)
|
||||
val note = Note(1, generalNote, "", TYPE_NOTE)
|
||||
insertNote(note, db)
|
||||
}
|
||||
|
||||
private fun insertNote(note: Note, db: SQLiteDatabase) {
|
||||
val values = fillNoteContentValues(note)
|
||||
db.insert(NOTES_TABLE_NAME, null, values)
|
||||
}
|
||||
|
||||
fun insertWidget(widget: Widget): Int {
|
||||
val values = fillWidgetContentValues(widget)
|
||||
return mDb.insertWithOnConflict(WIDGETS_TABLE_NAME, null, values, CONFLICT_IGNORE).toInt()
|
||||
}
|
||||
|
||||
private fun fillNoteContentValues(note: Note): ContentValues {
|
||||
return ContentValues().apply {
|
||||
put(COL_TITLE, note.title)
|
||||
put(COL_VALUE, note.value)
|
||||
put(COL_PATH, note.path)
|
||||
put(COL_TYPE, TYPE_NOTE)
|
||||
}
|
||||
}
|
||||
|
||||
private fun fillWidgetContentValues(widget: Widget): ContentValues {
|
||||
return ContentValues().apply {
|
||||
put(COL_WIDGET_ID, widget.widgetId)
|
||||
put(COL_NOTE_ID, widget.noteId)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteNote(id: Long) {
|
||||
mDb.delete(NOTES_TABLE_NAME, "$COL_ID = $id", null)
|
||||
mDb.delete(WIDGETS_TABLE_NAME, "$COL_NOTE_ID = $id", null)
|
||||
}
|
||||
|
||||
fun getWidgets(): ArrayList<Widget> {
|
||||
val widgets = ArrayList<Widget>()
|
||||
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(WIDGETS_TABLE_NAME, cols, null, null, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
do {
|
||||
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
|
||||
val noteId = cursor.getIntValue(COL_NOTE_ID)
|
||||
val widget = Widget(0, widgetId, noteId)
|
||||
widgets.add(widget)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
return widgets
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ import com.simplemobiletools.commons.extensions.setBackgroundColor
|
||||
import com.simplemobiletools.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.activities.SplashActivity
|
||||
import com.simplemobiletools.notes.pro.extensions.config
|
||||
import com.simplemobiletools.notes.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.pro.extensions.widgetsDB
|
||||
import com.simplemobiletools.notes.pro.models.Widget
|
||||
import com.simplemobiletools.notes.pro.services.WidgetService
|
||||
|
||||
@ -26,8 +26,7 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
val widgets = context.dbHelper.getWidgets()
|
||||
widgets.forEach {
|
||||
context.widgetsDB.getWidgets().forEach {
|
||||
val views = RemoteViews(context.packageName, R.layout.widget)
|
||||
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
|
||||
setupAppOpenIntent(context, views, R.id.notes_widget_holder, it)
|
||||
|
Reference in New Issue
Block a user