mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-04-13 23:12:02 +02:00
adding widget support too
This commit is contained in:
parent
66821397e9
commit
213cbd80fe
@ -27,10 +27,7 @@ import com.simplemobiletools.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter
|
||||
import com.simplemobiletools.notes.pro.databases.NotesDatabase
|
||||
import com.simplemobiletools.notes.pro.dialogs.*
|
||||
import com.simplemobiletools.notes.pro.extensions.config
|
||||
import com.simplemobiletools.notes.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.pro.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.pro.extensions.updateWidgets
|
||||
import com.simplemobiletools.notes.pro.extensions.*
|
||||
import com.simplemobiletools.notes.pro.helpers.MIME_TEXT_PLAIN
|
||||
import com.simplemobiletools.notes.pro.helpers.NotesHelper
|
||||
import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID
|
||||
@ -576,7 +573,14 @@ class MainActivity : SimpleActivity() {
|
||||
}
|
||||
|
||||
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
|
||||
dbHelper.deleteNote(mCurrentNote.id!!)
|
||||
Thread {
|
||||
notesDB.deleteNote(note)
|
||||
widgetsDB.deleteNoteWidgets(note.id!!)
|
||||
refreshNotes(note, deleteFile)
|
||||
}.start()
|
||||
}
|
||||
|
||||
private fun refreshNotes(note: Note, deleteFile: Boolean) {
|
||||
NotesHelper(this).getNotes {
|
||||
mNotes = it
|
||||
val firstNoteId = mNotes[0].id
|
||||
|
@ -15,8 +15,8 @@ import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
|
||||
import com.simplemobiletools.commons.models.RadioItem
|
||||
import com.simplemobiletools.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.extensions.config
|
||||
import com.simplemobiletools.notes.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.pro.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.pro.extensions.widgetsDB
|
||||
import com.simplemobiletools.notes.pro.helpers.MyWidgetProvider
|
||||
import com.simplemobiletools.notes.pro.helpers.NotesHelper
|
||||
import com.simplemobiletools.notes.pro.models.Note
|
||||
@ -29,7 +29,7 @@ class WidgetConfigureActivity : SimpleActivity() {
|
||||
private var mBgColor = 0
|
||||
private var mBgColorWithoutTransparency = 0
|
||||
private var mTextColor = 0
|
||||
private var mCurrentNoteId = 0
|
||||
private var mCurrentNoteId = 0L
|
||||
private var mIsCustomizingColors = false
|
||||
private var mNotes = ArrayList<Note>()
|
||||
|
||||
@ -95,14 +95,14 @@ class WidgetConfigureActivity : SimpleActivity() {
|
||||
items.add(RadioItem(it.id!!.toInt(), it.title))
|
||||
}
|
||||
|
||||
RadioGroupDialog(this, items, mCurrentNoteId) {
|
||||
RadioGroupDialog(this, items, mCurrentNoteId.toInt()) {
|
||||
val selectedId = it as Int
|
||||
updateCurrentNote(mNotes.first { it.id!!.toInt() == selectedId })
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateCurrentNote(note: Note) {
|
||||
mCurrentNoteId = note.id!!.toInt()
|
||||
mCurrentNoteId = note.id!!
|
||||
notes_picker_value.text = note.title
|
||||
val sampleValue = if (note.value.isEmpty() || mIsCustomizingColors) getString(R.string.widget_config) else note.value
|
||||
notes_view.text = sampleValue
|
||||
@ -113,7 +113,9 @@ class WidgetConfigureActivity : SimpleActivity() {
|
||||
views.setBackgroundColor(R.id.notes_view, mBgColor)
|
||||
AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views)
|
||||
val widget = Widget(null, mWidgetId, mCurrentNoteId)
|
||||
dbHelper.insertWidget(widget)
|
||||
Thread {
|
||||
widgetsDB.insertOrUpdate(widget)
|
||||
}.start()
|
||||
|
||||
storeWidgetBackground()
|
||||
requestWidgetUpdate()
|
||||
|
@ -12,8 +12,6 @@ import com.simplemobiletools.notes.pro.interfaces.WidgetsDao
|
||||
|
||||
val Context.config: Config get() = Config.newInstance(applicationContext)
|
||||
|
||||
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
|
||||
|
||||
val Context.notesDB: NotesDao get() = NotesDatabase.getInstance(applicationContext).NotesDao()
|
||||
|
||||
val Context.widgetsDB: WidgetsDao get() = NotesDatabase.getInstance(applicationContext).WidgetsDao()
|
||||
|
@ -20,7 +20,6 @@ import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.notes.pro.R
|
||||
import com.simplemobiletools.notes.pro.activities.MainActivity
|
||||
import com.simplemobiletools.notes.pro.extensions.config
|
||||
import com.simplemobiletools.notes.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.pro.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.pro.extensions.updateWidgets
|
||||
import com.simplemobiletools.notes.pro.helpers.*
|
||||
@ -43,12 +42,10 @@ class NoteFragment : androidx.fragment.app.Fragment() {
|
||||
private var note: Note? = null
|
||||
|
||||
lateinit var view: ViewGroup
|
||||
private lateinit var db: DBHelper
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
|
||||
noteId = arguments!!.getInt(NOTE_ID)
|
||||
db = context!!.dbHelper
|
||||
retainInstance = true
|
||||
|
||||
val layoutToInflate = if (config!!.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable
|
||||
|
@ -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)
|
||||
|
@ -13,4 +13,7 @@ interface WidgetsDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertOrUpdate(widget: Widget): Long
|
||||
|
||||
@Query("DELETE FROM widgets WHERE note_id = :noteId")
|
||||
fun deleteNoteWidgets(noteId: Long)
|
||||
}
|
||||
|
@ -9,4 +9,4 @@ import androidx.room.PrimaryKey
|
||||
data class Widget(
|
||||
@PrimaryKey(autoGenerate = true) var id: Int?,
|
||||
@ColumnInfo(name = "widget_id") var widgetId: Int,
|
||||
@ColumnInfo(name = "note_id") var noteId: Int)
|
||||
@ColumnInfo(name = "note_id") var noteId: Long)
|
||||
|
Loading…
x
Reference in New Issue
Block a user