diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/EventsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/EventsDatabase.kt new file mode 100644 index 000000000..016e27302 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/EventsDatabase.kt @@ -0,0 +1,47 @@ +package com.simplemobiletools.calendar.pro + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao +import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionsDao +import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao +import com.simplemobiletools.calendar.pro.interfaces.EventsDao +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 + +@Database(entities = [Event::class, EventType::class, EventRepetition::class, EventRepetitionException::class], version = 1) +abstract class EventsDatabase : RoomDatabase() { + + abstract fun EventsDao(): EventsDao + + abstract fun EventTypesDao(): EventTypesDao + + abstract fun EventRepetitionsDao(): EventRepetitionsDao + + abstract fun EventRepetitionExceptionsDao(): EventRepetitionExceptionsDao + + companion object { + private var db: EventsDatabase? = null + + fun getInstance(context: Context): EventsDatabase { + if (db == null) { + synchronized(EventsDatabase::class) { + if (db == null) { + db = Room.databaseBuilder(context.applicationContext, EventsDatabase::class.java, "events.db") + .build() + db!!.openHelper.setWriteAheadLoggingEnabled(true) + } + } + } + return db!! + } + + fun destroyInstance() { + db = null + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index f13c9ffc4..0aab1b0bd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -21,12 +21,17 @@ import android.widget.LinearLayout import android.widget.TextView import androidx.core.app.AlarmManagerCompat import androidx.core.app.NotificationCompat +import com.simplemobiletools.calendar.pro.EventsDatabase import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.activities.EventActivity import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.activities.SnoozeReminderActivity import com.simplemobiletools.calendar.pro.helpers.* import com.simplemobiletools.calendar.pro.helpers.Formatter +import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao +import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionsDao +import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao +import com.simplemobiletools.calendar.pro.interfaces.EventsDao import com.simplemobiletools.calendar.pro.models.* import com.simplemobiletools.calendar.pro.receivers.CalDAVSyncReceiver import com.simplemobiletools.calendar.pro.receivers.NotificationReceiver @@ -43,6 +48,14 @@ import java.util.* val Context.config: Config get() = Config.newInstance(applicationContext) +val Context.eventsDB: EventsDao get() = EventsDatabase.getInstance(applicationContext).EventsDao() + +val Context.eventTypesDB: EventTypesDao get() = EventsDatabase.getInstance(applicationContext).EventTypesDao() + +val Context.eventRepetitionssDB: EventRepetitionsDao get() = EventsDatabase.getInstance(applicationContext).EventRepetitionsDao() + +val Context.eventRepetitionExceptionsDB: EventRepetitionExceptionsDao get() = EventsDatabase.getInstance(applicationContext).EventRepetitionExceptionsDao() + val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext) fun Context.updateWidgets() { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventRepetitionExceptionsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventRepetitionExceptionsDao.kt new file mode 100644 index 000000000..5c29a654a --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventRepetitionExceptionsDao.kt @@ -0,0 +1,8 @@ +package com.simplemobiletools.calendar.pro.interfaces + +import androidx.room.Dao + +@Dao +interface EventRepetitionExceptionsDao { + +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventRepetitionsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventRepetitionsDao.kt new file mode 100644 index 000000000..c1ddb95da --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventRepetitionsDao.kt @@ -0,0 +1,8 @@ +package com.simplemobiletools.calendar.pro.interfaces + +import androidx.room.Dao + +@Dao +interface EventRepetitionsDao { + +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt new file mode 100644 index 000000000..a56435eea --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventTypesDao.kt @@ -0,0 +1,8 @@ +package com.simplemobiletools.calendar.pro.interfaces + +import androidx.room.Dao + +@Dao +interface EventTypesDao { + +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt new file mode 100644 index 000000000..847c1137f --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -0,0 +1,8 @@ +package com.simplemobiletools.calendar.pro.interfaces + +import androidx.room.Dao + +@Dao +interface EventsDao { + +}