Migrate blocked conversations to qk blocking manager

This commit is contained in:
moezbhatti
2019-09-08 20:51:12 -04:00
parent 3aba92825b
commit 4c9130e11a
8 changed files with 77 additions and 22 deletions
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.migration
import android.content.Context
import com.moez.QKSMS.blocking.QksmsBlockingClient
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject
class QkMigration @Inject constructor(
context: Context,
private val conversationRepo: ConversationRepository,
private val prefs: Preferences,
private val qksmsBlockingClient: QksmsBlockingClient
) {
init {
val oldVersion = prefs.version.get()
val newVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionCode
if (oldVersion < 2199) {
upgradeTo370()
}
prefs.changelogVersion.set(newVersion)
}
private fun upgradeTo370() = GlobalScope.launch {
// Migrate changelog version
prefs.changelogVersion.set(prefs.version.get())
// Migrate from old SIA preference to blocking manager preference
if (prefs.sia.get()) {
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_SIA)
prefs.sia.delete()
}
// Migrate blocked conversations into QK blocking client
val addresses = conversationRepo.getBlockedConversations()
.flatMap { conversation -> conversation.recipients }
.map { recipient -> recipient.address }
.distinct()
qksmsBlockingClient.block(addresses).blockingAwait()
}
}