mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 04:10:45 +01:00
insert regular event type into the database on creation
This commit is contained in:
parent
7ae700db34
commit
b1a263d54f
@ -4,6 +4,9 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.helpers.DBHelper
|
||||
import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao
|
||||
import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionsDao
|
||||
import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao
|
||||
@ -12,6 +15,7 @@ import com.simplemobiletools.calendar.pro.models.Event
|
||||
import com.simplemobiletools.calendar.pro.models.EventRepetition
|
||||
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@Database(entities = [Event::class, EventType::class, EventRepetition::class, EventRepetitionException::class], version = 1)
|
||||
abstract class EventsDatabase : RoomDatabase() {
|
||||
@ -32,6 +36,12 @@ abstract class EventsDatabase : RoomDatabase() {
|
||||
synchronized(EventsDatabase::class) {
|
||||
if (db == null) {
|
||||
db = Room.databaseBuilder(context.applicationContext, EventsDatabase::class.java, "events.db")
|
||||
.addCallback(object : Callback() {
|
||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||
super.onCreate(db)
|
||||
insertRegularEventType(context)
|
||||
}
|
||||
})
|
||||
.build()
|
||||
db!!.openHelper.setWriteAheadLoggingEnabled(true)
|
||||
}
|
||||
@ -43,5 +53,13 @@ abstract class EventsDatabase : RoomDatabase() {
|
||||
fun destroyInstance() {
|
||||
db = null
|
||||
}
|
||||
|
||||
private fun insertRegularEventType(context: Context) {
|
||||
Executors.newSingleThreadScheduledExecutor().execute {
|
||||
val regularEvent = context.resources.getString(R.string.regular_event)
|
||||
val eventType = EventType(DBHelper.REGULAR_EVENT_TYPE_ID, regularEvent, context.config.primaryColor)
|
||||
db!!.EventTypesDao().insertOrUpdate(eventType)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,16 @@
|
||||
package com.simplemobiletools.calendar.pro.interfaces
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.simplemobiletools.calendar.pro.models.EventType
|
||||
|
||||
@Dao
|
||||
interface EventTypesDao {
|
||||
@Query("SELECT * FROM event_types ORDER BY title ASC")
|
||||
fun getEventTypes(): List<EventType>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertOrUpdate(eventType: EventType): Long
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user