Skip to content

Commit

Permalink
create a table for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed Nov 7, 2018
1 parent e7ff0c6 commit d0e731f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class WidgetConfigureActivity : SimpleActivity() {
val views = RemoteViews(packageName, R.layout.activity_main)
views.setBackgroundColor(R.id.notes_view, mBgColor)
AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views)
val widget = Widget(mWidgetId, mCurrentNoteId)
val widget = Widget(null, mWidgetId, mCurrentNoteId)
dbHelper.insertWidget(widget)

storeWidgetBackground()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.simplemobiletools.notes.pro.interfaces.NotesDao
import com.simplemobiletools.notes.pro.interfaces.WidgetsDao
import com.simplemobiletools.notes.pro.models.Note
import com.simplemobiletools.notes.pro.models.Widget
import com.simplemobiletools.notes.pro.objects.MyExecutor
import java.util.concurrent.Executors

@Database(entities = [Note::class], version = 1)
@Database(entities = [Note::class, Widget::class], version = 1)
abstract class NotesDatabase : RoomDatabase() {

abstract fun NotesDao(): NotesDao

abstract fun WidgetsDao(): WidgetsDao

companion object {
private var db: NotesDatabase? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.databases.NotesDatabase
import com.simplemobiletools.notes.pro.helpers.*
import com.simplemobiletools.notes.pro.interfaces.NotesDao
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()

fun Context.getTextSize() = when (config.fontSize) {
FONT_SIZE_SMALL -> resources.getDimension(R.dimen.smaller_text_size)
FONT_SIZE_LARGE -> resources.getDimension(R.dimen.big_text_size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
do {
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
val noteId = cursor.getIntValue(COL_NOTE_ID)
val widget = Widget(widgetId, noteId)
val widget = Widget(0, widgetId, noteId)
widgets.add(widget)
} while (cursor.moveToNext())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.simplemobiletools.notes.pro.interfaces

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.notes.pro.models.Widget

@Dao
interface WidgetsDao {
@Query("SELECT * FROM widgets")
fun getWidgets(): List<Widget>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(widget: Widget): Long
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package com.simplemobiletools.notes.pro.models

data class Widget(var widgetId: Int, var noteId: Int)
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity(tableName = "widgets", indices = [(Index(value = ["widget_id"], unique = true))])
data class Widget(
@PrimaryKey(autoGenerate = true) var id: Int?,
@ColumnInfo(name = "widget_id") var widgetId: Int,
@ColumnInfo(name = "note_id") var noteId: Int)

0 comments on commit d0e731f

Please sign in to comment.