adding a database for storing clips

This commit is contained in:
tibbi 2022-01-27 10:40:55 +01:00
parent c8011a8f60
commit f1e045c69c
6 changed files with 75 additions and 1 deletions

View File

@ -65,4 +65,8 @@ android {
dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:db3561b188'
kapt 'androidx.room:room-compiler:2.3.0'
implementation 'androidx.room:room-runtime:2.3.0'
annotationProcessor 'androidx.room:room-compiler:2.3.0'
}

View File

@ -0,0 +1,34 @@
package com.simplemobiletools.keyboard.databases
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.simplemobiletools.keyboard.interfaces.ClipsDao
import com.simplemobiletools.keyboard.models.Clip
@Database(entities = [Clip::class], version = 1)
abstract class ClipsDatabase : RoomDatabase() {
abstract fun ClipsDao(): ClipsDao
companion object {
private var db: ClipsDatabase? = null
fun getInstance(context: Context): ClipsDatabase {
if (db == null) {
synchronized(ClipsDatabase::class) {
if (db == null) {
db = Room.databaseBuilder(context.applicationContext, ClipsDatabase::class.java, "clips.db").build()
db!!.openHelper.setWriteAheadLoggingEnabled(true)
}
}
}
return db!!
}
fun destroyInstance() {
db = null
}
}
}

View File

@ -1,6 +1,10 @@
package com.simplemobiletools.keyboard.extensions
import android.content.Context
import com.simplemobiletools.keyboard.databases.ClipsDatabase
import com.simplemobiletools.keyboard.helpers.Config
import com.simplemobiletools.keyboard.interfaces.ClipsDao
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.clipsDB: ClipsDao get() = ClipsDatabase.getInstance(applicationContext).ClipsDao()

View File

@ -0,0 +1,16 @@
package com.simplemobiletools.keyboard.interfaces
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.keyboard.models.Clip
@Dao
interface ClipsDao {
@Query("SELECT * FROM clips ORDER BY id")
fun getClips(): List<Clip>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(clip: Clip): Long
}

View File

@ -0,0 +1,12 @@
package com.simplemobiletools.keyboard.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
@Entity(tableName = "clips", indices = [(Index(value = ["id"], unique = true))])
data class Clip(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "value") var value: String
)

View File

@ -355,7 +355,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mClipboardManagerHolder!!.apply {
clipboard_manager_close.setOnClickListener {
vibrateIfNeeded()
mClipboardManagerHolder!!.clipboard_manager_holder.beGone()
openClipboardManager()
}
}
}
@ -1295,6 +1295,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
return true
}
private fun openClipboardManager() {
mClipboardManagerHolder!!.clipboard_manager_holder.beGone()
}
private fun closing() {
if (mPreviewPopup.isShowing) {
mPreviewPopup.dismiss()