forked from upstream-mirrors/quik
Persist archived and blocked state across sync
This commit is contained in:
@@ -18,8 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package repository
|
package repository
|
||||||
|
|
||||||
|
import android.content.ContentResolver
|
||||||
import android.content.ContentUris
|
import android.content.ContentUris
|
||||||
import android.content.Context
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.telephony.PhoneNumberUtils
|
import android.telephony.PhoneNumberUtils
|
||||||
@@ -44,7 +44,7 @@ import javax.inject.Singleton
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class SyncRepositoryImpl @Inject constructor(
|
class SyncRepositoryImpl @Inject constructor(
|
||||||
private val context: Context,
|
private val contentResolver: ContentResolver,
|
||||||
private val messageRepo: MessageRepository,
|
private val messageRepo: MessageRepository,
|
||||||
private val cursorToConversation: CursorToConversation,
|
private val cursorToConversation: CursorToConversation,
|
||||||
private val cursorToMessage: CursorToMessage,
|
private val cursorToMessage: CursorToMessage,
|
||||||
@@ -57,7 +57,11 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
class Running : Status()
|
class Running : Status()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val contentResolver = context.contentResolver
|
/**
|
||||||
|
* Holds data that should be persisted across full syncs
|
||||||
|
*/
|
||||||
|
private data class PersistedData(val id: Long, val archived: Boolean, val blocked: Boolean)
|
||||||
|
|
||||||
private var status: Status = Status.Idle()
|
private var status: Status = Status.Idle()
|
||||||
|
|
||||||
override fun syncMessages(fullSync: Boolean) {
|
override fun syncMessages(fullSync: Boolean) {
|
||||||
@@ -69,7 +73,20 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
realm.beginTransaction()
|
realm.beginTransaction()
|
||||||
|
|
||||||
|
var persistedData: List<PersistedData>? = null
|
||||||
|
|
||||||
if (fullSync) {
|
if (fullSync) {
|
||||||
|
persistedData = realm.where(Conversation::class.java)
|
||||||
|
.beginGroup()
|
||||||
|
.equalTo("archived", true)
|
||||||
|
.or()
|
||||||
|
.equalTo("blocked", true)
|
||||||
|
.endGroup()
|
||||||
|
.findAll()
|
||||||
|
.map { conversation ->
|
||||||
|
PersistedData(conversation.id, conversation.archived, conversation.blocked)
|
||||||
|
}
|
||||||
|
|
||||||
realm.delete(Conversation::class.java)
|
realm.delete(Conversation::class.java)
|
||||||
realm.delete(Message::class.java)
|
realm.delete(Message::class.java)
|
||||||
realm.delete(MmsPart::class.java)
|
realm.delete(MmsPart::class.java)
|
||||||
@@ -82,7 +99,15 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
// Sync conversations
|
// Sync conversations
|
||||||
val conversationCursor = cursorToConversation.getConversationsCursor()
|
val conversationCursor = cursorToConversation.getConversationsCursor()
|
||||||
val conversations = conversationCursor.map { cursor -> cursorToConversation.map(cursor) }
|
val conversations = conversationCursor
|
||||||
|
.map { cursor -> cursorToConversation.map(cursor) }
|
||||||
|
|
||||||
|
persistedData?.forEach { data ->
|
||||||
|
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
|
||||||
|
conversation?.archived = data.archived
|
||||||
|
conversation?.blocked = data.blocked
|
||||||
|
}
|
||||||
|
|
||||||
realm.insertOrUpdate(conversations)
|
realm.insertOrUpdate(conversations)
|
||||||
conversationCursor.close()
|
conversationCursor.close()
|
||||||
|
|
||||||
@@ -136,7 +161,7 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
else -> ContentUris.withAppendedId(Telephony.Sms.CONTENT_URI, id)
|
else -> ContentUris.withAppendedId(Telephony.Sms.CONTENT_URI, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
val cursor = context.contentResolver.query(stableUri, null, null, null, null)
|
val cursor = contentResolver.query(stableUri, null, null, null, null)
|
||||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||||
|
|
||||||
// Map the cursor to a message
|
// Map the cursor to a message
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
package injection
|
package injection
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.ContentResolver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||||
@@ -61,6 +62,11 @@ class AppModule(private var application: Application) {
|
|||||||
return application
|
return application
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
fun provideContentResolver(context: Context): ContentResolver {
|
||||||
|
return context.contentResolver
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideRxPreferences(context: Context): RxSharedPreferences {
|
fun provideRxPreferences(context: Context): RxSharedPreferences {
|
||||||
|
|||||||
Reference in New Issue
Block a user