forked from upstream-mirrors/quik
Add ScheduledMessage to realm schema
This commit is contained in:
@@ -26,7 +26,7 @@ import io.realm.RealmMigration
|
||||
class QkRealmMigration : RealmMigration {
|
||||
|
||||
companion object {
|
||||
const val SCHEMA_VERSION: Long = 3
|
||||
const val SCHEMA_VERSION: Long = 4
|
||||
}
|
||||
|
||||
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
|
||||
@@ -53,6 +53,18 @@ class QkRealmMigration : RealmMigration {
|
||||
version++
|
||||
}
|
||||
|
||||
if (version == 3L) {
|
||||
realm.schema.create("ScheduledMessage")
|
||||
.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
|
||||
.addField("date", Long::class.java, FieldAttribute.REQUIRED)
|
||||
.addRealmListField("recipients", String::class.java)
|
||||
.addField("sendAsGroup", Boolean::class.java, FieldAttribute.REQUIRED)
|
||||
.addField("body", String::class.java, FieldAttribute.REQUIRED)
|
||||
.addRealmListField("attachments", String::class.java)
|
||||
|
||||
version++
|
||||
}
|
||||
|
||||
if (version < newVersion) {
|
||||
throw IllegalStateException("Migration missing from v$oldVersion to v$newVersion")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.moez.QKSMS.repository
|
||||
|
||||
import com.moez.QKSMS.model.ScheduledMessage
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmResults
|
||||
|
||||
class ScheduledMessageRepositoryImpl : ScheduledMessageRepository {
|
||||
|
||||
override fun saveScheduledMessage(date: Long, recipients: RealmList<String>, sendAsGroup: Boolean, body: String,
|
||||
attachments: RealmList<String>): Long {
|
||||
|
||||
return Realm.getDefaultInstance().use { realm ->
|
||||
val id = (realm.where(ScheduledMessage::class.java).max("id")?.toLong() ?: -1) + 1
|
||||
val message = ScheduledMessage(id, date, recipients, sendAsGroup, body, attachments)
|
||||
|
||||
realm.executeTransaction { realm.insertOrUpdate(message) }
|
||||
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
override fun getScheduledMessages(): RealmResults<ScheduledMessage> {
|
||||
return Realm.getDefaultInstance()
|
||||
.where(ScheduledMessage::class.java)
|
||||
.sort("date")
|
||||
.findAll()
|
||||
}
|
||||
|
||||
override fun getScheduledMessage(id: Long): ScheduledMessage? {
|
||||
return Realm.getDefaultInstance()
|
||||
.where(ScheduledMessage::class.java)
|
||||
.equalTo("id", id)
|
||||
.findFirst()
|
||||
}
|
||||
|
||||
override fun deleteScheduledMessage(id: Long) {
|
||||
Realm.getDefaultInstance().use { realm ->
|
||||
val message = realm.where(ScheduledMessage::class.java)
|
||||
.equalTo("id", id)
|
||||
.findFirst()
|
||||
|
||||
realm.executeTransaction { message?.deleteFromRealm() }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.moez.QKSMS.model
|
||||
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmObject
|
||||
import io.realm.annotations.PrimaryKey
|
||||
|
||||
open class ScheduledMessage(
|
||||
@PrimaryKey var id: Long = 0,
|
||||
var date: Long = 0,
|
||||
var recipients: RealmList<String> = RealmList(),
|
||||
var sendAsGroup: Boolean = true,
|
||||
var body: String = "",
|
||||
var attachments: RealmList<String> = RealmList()
|
||||
) : RealmObject()
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.moez.QKSMS.repository
|
||||
|
||||
import com.moez.QKSMS.model.ScheduledMessage
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmResults
|
||||
|
||||
interface ScheduledMessageRepository {
|
||||
|
||||
/**
|
||||
* Saves a scheduled message and returns the id if successful
|
||||
*/
|
||||
fun saveScheduledMessage(date: Long, recipients: RealmList<String>, sendAsGroup: Boolean, body: String,
|
||||
attachments: RealmList<String>): Long
|
||||
|
||||
/**
|
||||
* Returns all of the scheduled messages, sorted chronologically
|
||||
*/
|
||||
fun getScheduledMessages(): RealmResults<ScheduledMessage>
|
||||
|
||||
/**
|
||||
* Returns the scheduled message with the given [id]
|
||||
*/
|
||||
fun getScheduledMessage(id: Long): ScheduledMessage?
|
||||
|
||||
/**
|
||||
* Deletes the scheduled message with the given [id]
|
||||
*/
|
||||
fun deleteScheduledMessage(id: Long)
|
||||
|
||||
}
|
||||
@@ -19,10 +19,10 @@
|
||||
package com.moez.QKSMS.injection
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.preference.PreferenceManager
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||
import com.moez.QKSMS.common.ViewModelFactory
|
||||
import com.moez.QKSMS.common.util.NotificationManagerImpl
|
||||
@@ -57,6 +57,8 @@ import com.moez.QKSMS.repository.ImageRepository
|
||||
import com.moez.QKSMS.repository.ImageRepostoryImpl
|
||||
import com.moez.QKSMS.repository.MessageRepository
|
||||
import com.moez.QKSMS.repository.MessageRepositoryImpl
|
||||
import com.moez.QKSMS.repository.ScheduledMessageRepository
|
||||
import com.moez.QKSMS.repository.ScheduledMessageRepositoryImpl
|
||||
import com.moez.QKSMS.repository.SyncRepository
|
||||
import com.moez.QKSMS.repository.SyncRepositoryImpl
|
||||
import dagger.Module
|
||||
@@ -139,6 +141,9 @@ class AppModule(private var application: Application) {
|
||||
@Provides
|
||||
fun provideMessageRepository(repository: MessageRepositoryImpl): MessageRepository = repository
|
||||
|
||||
@Provides
|
||||
fun provideScheduledMessagesRepository(repository: ScheduledMessageRepositoryImpl): ScheduledMessageRepository = repository
|
||||
|
||||
@Provides
|
||||
fun provideSyncRepository(repository: SyncRepositoryImpl): SyncRepository = repository
|
||||
|
||||
|
||||
Reference in New Issue
Block a user