Migrate blocked preferences from 2.7.3

This commit is contained in:
Moez Bhatti
2018-04-09 02:29:32 -04:00
parent ecc040a603
commit afdf577e4a
2 changed files with 17 additions and 4 deletions
@@ -23,6 +23,7 @@ import android.content.ContentUris
import android.net.Uri import android.net.Uri
import android.provider.Telephony import android.provider.Telephony
import android.telephony.PhoneNumberUtils import android.telephony.PhoneNumberUtils
import com.f2prateek.rx.preferences2.RxSharedPreferences
import io.realm.Realm import io.realm.Realm
import mapper.CursorToContact import mapper.CursorToContact
import mapper.CursorToConversation import mapper.CursorToConversation
@@ -48,7 +49,8 @@ class SyncRepositoryImpl @Inject constructor(
private val cursorToConversation: CursorToConversation, private val cursorToConversation: CursorToConversation,
private val cursorToMessage: CursorToMessage, private val cursorToMessage: CursorToMessage,
private val cursorToRecipient: CursorToRecipient, private val cursorToRecipient: CursorToRecipient,
private val cursorToContact: CursorToContact private val cursorToContact: CursorToContact,
private val rxPrefs: RxSharedPreferences
) : SyncRepository { ) : SyncRepository {
sealed class Status { sealed class Status {
@@ -72,10 +74,10 @@ class SyncRepositoryImpl @Inject constructor(
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
realm.beginTransaction() realm.beginTransaction()
var persistedData: List<PersistedData>? = null var persistedData: List<PersistedData> = listOf()
if (fullSync) { if (fullSync) {
persistedData = realm.where(Conversation::class.java) persistedData += realm.where(Conversation::class.java)
.beginGroup() .beginGroup()
.equalTo("archived", true) .equalTo("archived", true)
.or() .or()
@@ -106,13 +108,19 @@ class SyncRepositoryImpl @Inject constructor(
realm.insertOrUpdate(messages) realm.insertOrUpdate(messages)
} }
// Migrate blocked conversations from 2.7.3
val oldBlockedSenders = rxPrefs.getStringSet("pref_key_blocked_senders")
persistedData += oldBlockedSenders.get()
.map { threadIdString -> threadIdString.toLong() }
.filter { threadId -> persistedData.none { it.id == threadId } }
.map { threadId -> PersistedData(threadId, false, true) }
// Sync conversations // Sync conversations
cursorToConversation.getConversationsCursor()?.use { conversationCursor -> cursorToConversation.getConversationsCursor()?.use { conversationCursor ->
val conversations = conversationCursor val conversations = conversationCursor
.map { cursor -> cursorToConversation.map(cursor) } .map { cursor -> cursorToConversation.map(cursor) }
persistedData?.forEach { data -> persistedData.forEach { data ->
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id } val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
conversation?.archived = data.archived conversation?.archived = data.archived
conversation?.blocked = data.blocked conversation?.blocked = data.blocked
@@ -131,6 +139,9 @@ class SyncRepositoryImpl @Inject constructor(
realm.commitTransaction() realm.commitTransaction()
realm.close() realm.close()
// Only delete this after the sync has successfully completed
oldBlockedSenders.delete()
syncContacts() syncContacts()
status = Status.Idle() status = Status.Idle()
@@ -26,6 +26,8 @@ import javax.inject.Inject
/** /**
* When upgrading from 2.7.3 to 3.0, migrate the preferences * When upgrading from 2.7.3 to 3.0, migrate the preferences
*
* Blocked conversations will be migrated in SyncManager
*/ */
class MigratePreferences @Inject constructor( class MigratePreferences @Inject constructor(
private val nightModeManager: NightModeManager, private val nightModeManager: NightModeManager,