From 38ea0f8a78645b6625c02fa12f9a40728fdafc1c Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Mon, 26 Mar 2018 01:49:21 -0400 Subject: [PATCH] Persist archived and blocked state across sync --- .../java/repository/SyncRepositoryImpl.kt | 35 ++++++++++++++++--- .../src/main/java/injection/AppModule.kt | 6 ++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/data/src/main/java/repository/SyncRepositoryImpl.kt b/data/src/main/java/repository/SyncRepositoryImpl.kt index b053aff4..8b951c5c 100644 --- a/data/src/main/java/repository/SyncRepositoryImpl.kt +++ b/data/src/main/java/repository/SyncRepositoryImpl.kt @@ -18,8 +18,8 @@ */ package repository +import android.content.ContentResolver import android.content.ContentUris -import android.content.Context import android.net.Uri import android.provider.Telephony import android.telephony.PhoneNumberUtils @@ -44,7 +44,7 @@ import javax.inject.Singleton @Singleton class SyncRepositoryImpl @Inject constructor( - private val context: Context, + private val contentResolver: ContentResolver, private val messageRepo: MessageRepository, private val cursorToConversation: CursorToConversation, private val cursorToMessage: CursorToMessage, @@ -57,7 +57,11 @@ class SyncRepositoryImpl @Inject constructor( 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() override fun syncMessages(fullSync: Boolean) { @@ -69,7 +73,20 @@ class SyncRepositoryImpl @Inject constructor( val realm = Realm.getDefaultInstance() realm.beginTransaction() + var persistedData: List? = null + 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(Message::class.java) realm.delete(MmsPart::class.java) @@ -82,7 +99,15 @@ class SyncRepositoryImpl @Inject constructor( // Sync conversations 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) conversationCursor.close() @@ -136,7 +161,7 @@ class SyncRepositoryImpl @Inject constructor( 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) // Map the cursor to a message diff --git a/presentation/src/main/java/injection/AppModule.kt b/presentation/src/main/java/injection/AppModule.kt index f7865d55..8ba91fa7 100644 --- a/presentation/src/main/java/injection/AppModule.kt +++ b/presentation/src/main/java/injection/AppModule.kt @@ -19,6 +19,7 @@ package injection import android.app.Application +import android.content.ContentResolver import android.content.Context import android.preference.PreferenceManager import com.f2prateek.rx.preferences2.RxSharedPreferences @@ -61,6 +62,11 @@ class AppModule(private var application: Application) { return application } + @Provides + fun provideContentResolver(context: Context): ContentResolver { + return context.contentResolver + } + @Provides @Singleton fun provideRxPreferences(context: Context): RxSharedPreferences {