forked from upstream-mirrors/quik
@@ -23,33 +23,28 @@ import android.database.ContentObserver
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.provider.ContactsContract
|
import android.provider.ContactsContract
|
||||||
import com.moez.QKSMS.repository.SyncRepository
|
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listens for a contact being added, and then syncs it to Realm
|
* Listens for a contact being added, and then syncs it to Realm
|
||||||
*
|
|
||||||
* TODO: Stop listening automatically. Currently, this will only happen if the contact is added
|
|
||||||
*/
|
*/
|
||||||
class ContactAddedListenerImpl @Inject constructor(
|
class ContactAddedListenerImpl @Inject constructor(
|
||||||
private val context: Context,
|
private val context: Context
|
||||||
private val syncRepo: SyncRepository
|
|
||||||
) : ContactAddedListener {
|
) : ContactAddedListener {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
private val URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun listen(address: String): Observable<*> {
|
override fun listen(): Observable<*> {
|
||||||
return ContactContentObserver(context).observable
|
return ContactContentObserver(context).observable
|
||||||
.filter { syncRepo.syncContact(address) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ContactContentObserver(context: Context) : ContentObserver(Handler()) {
|
private class ContactContentObserver(context: Context) : ContentObserver(Handler()) {
|
||||||
|
|
||||||
private val subject = PublishSubject.create<Unit>()
|
private val subject = BehaviorSubject.createDefault<Unit>(Unit)
|
||||||
|
|
||||||
val observable: Observable<Unit> = subject
|
val observable: Observable<Unit> = subject
|
||||||
.doOnSubscribe { context.contentResolver.registerContentObserver(URI, true, this) }
|
.doOnSubscribe { context.contentResolver.registerContentObserver(URI, true, this) }
|
||||||
@@ -66,4 +61,4 @@ class ContactAddedListenerImpl @Inject constructor(
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
) : SyncRepository {
|
) : SyncRepository {
|
||||||
|
|
||||||
override val syncProgress: Subject<SyncRepository.SyncProgress> =
|
override val syncProgress: Subject<SyncRepository.SyncProgress> =
|
||||||
BehaviorSubject.createDefault(SyncRepository.SyncProgress.Idle())
|
BehaviorSubject.createDefault(SyncRepository.SyncProgress.Idle)
|
||||||
|
|
||||||
override fun syncMessages() {
|
override fun syncMessages() {
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
// Only delete this after the sync has successfully completed
|
// Only delete this after the sync has successfully completed
|
||||||
oldBlockedSenders.delete()
|
oldBlockedSenders.delete()
|
||||||
|
|
||||||
syncProgress.onNext(SyncRepository.SyncProgress.Idle())
|
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun syncMessage(uri: Uri): Message? {
|
override fun syncMessage(uri: Uri): Message? {
|
||||||
@@ -259,32 +259,6 @@ class SyncRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun syncContact(address: String): Boolean {
|
|
||||||
// See if there's a contact that matches this phone number
|
|
||||||
var contact = getContacts().find { contact ->
|
|
||||||
contact.numbers.any { number -> phoneNumberUtils.compare(number.address, address) }
|
|
||||||
} ?: return false
|
|
||||||
|
|
||||||
Realm.getDefaultInstance().use { realm ->
|
|
||||||
val recipients = realm.where(Recipient::class.java).findAll().filter { recipient ->
|
|
||||||
contact.numbers.any { number ->
|
|
||||||
phoneNumberUtils.compare(recipient.address, number.address)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
realm.executeTransaction {
|
|
||||||
contact = realm.copyToRealmOrUpdate(contact)
|
|
||||||
|
|
||||||
// Update all the matching recipients with the new contact
|
|
||||||
recipients.forEach { recipient -> recipient.contact = contact }
|
|
||||||
|
|
||||||
realm.insertOrUpdate(recipients)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getContacts(): List<Contact> {
|
private fun getContacts(): List<Contact> {
|
||||||
val defaultNumberIds = Realm.getDefaultInstance().use { realm ->
|
val defaultNumberIds = Realm.getDefaultInstance().use { realm ->
|
||||||
realm.where(PhoneNumber::class.java)
|
realm.where(PhoneNumber::class.java)
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ package com.moez.QKSMS.interactor
|
|||||||
import com.moez.QKSMS.repository.SyncRepository
|
import com.moez.QKSMS.repository.SyncRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class SyncContacts @Inject constructor(private val syncManager: SyncRepository) : Interactor<Unit>() {
|
class SyncContacts @Inject constructor(private val syncManager: SyncRepository) : Interactor<Unit>() {
|
||||||
@@ -30,8 +29,7 @@ class SyncContacts @Inject constructor(private val syncManager: SyncRepository)
|
|||||||
return Flowable.just(System.currentTimeMillis())
|
return Flowable.just(System.currentTimeMillis())
|
||||||
.doOnNext { syncManager.syncContacts() }
|
.doOnNext { syncManager.syncContacts() }
|
||||||
.map { startTime -> System.currentTimeMillis() - startTime }
|
.map { startTime -> System.currentTimeMillis() - startTime }
|
||||||
.map { elapsed -> TimeUnit.MILLISECONDS.toSeconds(elapsed) }
|
.doOnNext { duration -> Timber.v("Completed sync in ${duration}ms") }
|
||||||
.doOnNext { seconds -> Timber.v("Completed sync in $seconds seconds") }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,6 @@ import io.reactivex.Observable
|
|||||||
|
|
||||||
interface ContactAddedListener {
|
interface ContactAddedListener {
|
||||||
|
|
||||||
fun listen(address: String): Observable<*>
|
fun listen(): Observable<*>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import io.reactivex.Observable
|
|||||||
interface SyncRepository {
|
interface SyncRepository {
|
||||||
|
|
||||||
sealed class SyncProgress {
|
sealed class SyncProgress {
|
||||||
class Idle : SyncProgress()
|
object Idle : SyncProgress()
|
||||||
data class Running(val max: Int, val progress: Int, val indeterminate: Boolean) : SyncProgress()
|
data class Running(val max: Int, val progress: Int, val indeterminate: Boolean) : SyncProgress()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,11 +37,4 @@ interface SyncRepository {
|
|||||||
|
|
||||||
fun syncContacts()
|
fun syncContacts()
|
||||||
|
|
||||||
/**
|
}
|
||||||
* Syncs a single contact to the Realm
|
|
||||||
*
|
|
||||||
* Return false if the contact couldn't be found
|
|
||||||
*/
|
|
||||||
fun syncContact(address: String): Boolean
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
|||||||
import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction
|
import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction
|
||||||
import com.moez.QKSMS.filter.ContactFilter
|
import com.moez.QKSMS.filter.ContactFilter
|
||||||
import com.moez.QKSMS.filter.ContactGroupFilter
|
import com.moez.QKSMS.filter.ContactGroupFilter
|
||||||
import com.moez.QKSMS.interactor.SyncContacts
|
|
||||||
import com.moez.QKSMS.interactor.SetDefaultPhoneNumber
|
import com.moez.QKSMS.interactor.SetDefaultPhoneNumber
|
||||||
import com.moez.QKSMS.model.Contact
|
import com.moez.QKSMS.model.Contact
|
||||||
import com.moez.QKSMS.model.ContactGroup
|
import com.moez.QKSMS.model.ContactGroup
|
||||||
@@ -50,7 +49,6 @@ import javax.inject.Inject
|
|||||||
class ContactsViewModel @Inject constructor(
|
class ContactsViewModel @Inject constructor(
|
||||||
sharing: Boolean,
|
sharing: Boolean,
|
||||||
serializedChips: HashMap<String, String?>,
|
serializedChips: HashMap<String, String?>,
|
||||||
syncContacts: SyncContacts,
|
|
||||||
private val contactFilter: ContactFilter,
|
private val contactFilter: ContactFilter,
|
||||||
private val contactGroupFilter: ContactGroupFilter,
|
private val contactGroupFilter: ContactGroupFilter,
|
||||||
private val contactsRepo: ContactRepository,
|
private val contactsRepo: ContactRepository,
|
||||||
@@ -76,10 +74,6 @@ class ContactsViewModel @Inject constructor(
|
|||||||
|
|
||||||
private var shouldOpenKeyboard: Boolean = true
|
private var shouldOpenKeyboard: Boolean = true
|
||||||
|
|
||||||
init {
|
|
||||||
syncContacts.execute(Unit)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun bindView(view: ContactsContract) {
|
override fun bindView(view: ContactsContract) {
|
||||||
super.bindView(view)
|
super.bindView(view)
|
||||||
|
|
||||||
|
|||||||
+3
-13
@@ -26,14 +26,12 @@ import com.moez.QKSMS.extensions.mapNotNull
|
|||||||
import com.moez.QKSMS.interactor.DeleteConversations
|
import com.moez.QKSMS.interactor.DeleteConversations
|
||||||
import com.moez.QKSMS.interactor.MarkArchived
|
import com.moez.QKSMS.interactor.MarkArchived
|
||||||
import com.moez.QKSMS.interactor.MarkUnarchived
|
import com.moez.QKSMS.interactor.MarkUnarchived
|
||||||
import com.moez.QKSMS.listener.ContactAddedListener
|
|
||||||
import com.moez.QKSMS.manager.PermissionManager
|
import com.moez.QKSMS.manager.PermissionManager
|
||||||
import com.moez.QKSMS.model.Conversation
|
import com.moez.QKSMS.model.Conversation
|
||||||
import com.moez.QKSMS.repository.ConversationRepository
|
import com.moez.QKSMS.repository.ConversationRepository
|
||||||
import com.moez.QKSMS.repository.MessageRepository
|
import com.moez.QKSMS.repository.MessageRepository
|
||||||
import com.uber.autodispose.android.lifecycle.scope
|
import com.uber.autodispose.android.lifecycle.scope
|
||||||
import com.uber.autodispose.autoDisposable
|
import com.uber.autodispose.autoDisposable
|
||||||
import io.reactivex.Observable
|
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import io.reactivex.subjects.BehaviorSubject
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
@@ -44,7 +42,6 @@ import javax.inject.Named
|
|||||||
class ConversationInfoPresenter @Inject constructor(
|
class ConversationInfoPresenter @Inject constructor(
|
||||||
@Named("threadId") threadId: Long,
|
@Named("threadId") threadId: Long,
|
||||||
messageRepo: MessageRepository,
|
messageRepo: MessageRepository,
|
||||||
private val contactAddedListener: ContactAddedListener,
|
|
||||||
private val conversationRepo: ConversationRepository,
|
private val conversationRepo: ConversationRepository,
|
||||||
private val deleteConversations: DeleteConversations,
|
private val deleteConversations: DeleteConversations,
|
||||||
private val markArchived: MarkArchived,
|
private val markArchived: MarkArchived,
|
||||||
@@ -105,16 +102,9 @@ class ConversationInfoPresenter @Inject constructor(
|
|||||||
// Add or display the contact
|
// Add or display the contact
|
||||||
view.recipientClicks()
|
view.recipientClicks()
|
||||||
.mapNotNull(conversationRepo::getRecipient)
|
.mapNotNull(conversationRepo::getRecipient)
|
||||||
.flatMap { recipient ->
|
.doOnNext { recipient ->
|
||||||
val lookupKey = recipient.contact?.lookupKey
|
recipient.contact?.lookupKey?.let(navigator::showContact)
|
||||||
if (lookupKey != null) {
|
?: navigator.addContact(recipient.address)
|
||||||
navigator.showContact(lookupKey)
|
|
||||||
Observable.empty<Unit>()
|
|
||||||
} else {
|
|
||||||
// Allow the user to add the contact, then listen for changes
|
|
||||||
navigator.addContact(recipient.address)
|
|
||||||
contactAddedListener.listen(recipient.address)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.autoDisposable(view.scope(Lifecycle.Event.ON_DESTROY)) // ... this should be the default
|
.autoDisposable(view.scope(Lifecycle.Event.ON_DESTROY)) // ... this should be the default
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ data class MainState(
|
|||||||
val drawerOpen: Boolean = false,
|
val drawerOpen: Boolean = false,
|
||||||
val upgraded: Boolean = true,
|
val upgraded: Boolean = true,
|
||||||
val showRating: Boolean = false,
|
val showRating: Boolean = false,
|
||||||
val syncing: SyncRepository.SyncProgress = SyncRepository.SyncProgress.Idle(),
|
val syncing: SyncRepository.SyncProgress = SyncRepository.SyncProgress.Idle,
|
||||||
val defaultSms: Boolean = true,
|
val defaultSms: Boolean = true,
|
||||||
val smsPermission: Boolean = true,
|
val smsPermission: Boolean = true,
|
||||||
val contactPermission: Boolean = true
|
val contactPermission: Boolean = true
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import com.moez.QKSMS.interactor.MarkUnarchived
|
|||||||
import com.moez.QKSMS.interactor.MarkUnpinned
|
import com.moez.QKSMS.interactor.MarkUnpinned
|
||||||
import com.moez.QKSMS.interactor.MarkUnread
|
import com.moez.QKSMS.interactor.MarkUnread
|
||||||
import com.moez.QKSMS.interactor.MigratePreferences
|
import com.moez.QKSMS.interactor.MigratePreferences
|
||||||
|
import com.moez.QKSMS.interactor.SyncContacts
|
||||||
import com.moez.QKSMS.interactor.SyncMessages
|
import com.moez.QKSMS.interactor.SyncMessages
|
||||||
import com.moez.QKSMS.listener.ContactAddedListener
|
import com.moez.QKSMS.listener.ContactAddedListener
|
||||||
import com.moez.QKSMS.manager.ChangelogManager
|
import com.moez.QKSMS.manager.ChangelogManager
|
||||||
@@ -55,10 +56,10 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
class MainViewModel @Inject constructor(
|
class MainViewModel @Inject constructor(
|
||||||
billingManager: BillingManager,
|
billingManager: BillingManager,
|
||||||
|
contactAddedListener: ContactAddedListener,
|
||||||
markAllSeen: MarkAllSeen,
|
markAllSeen: MarkAllSeen,
|
||||||
migratePreferences: MigratePreferences,
|
migratePreferences: MigratePreferences,
|
||||||
syncRepository: SyncRepository,
|
syncRepository: SyncRepository,
|
||||||
private val contactAddedListener: ContactAddedListener,
|
|
||||||
private val changelogManager: ChangelogManager,
|
private val changelogManager: ChangelogManager,
|
||||||
private val conversationRepo: ConversationRepository,
|
private val conversationRepo: ConversationRepository,
|
||||||
private val deleteConversations: DeleteConversations,
|
private val deleteConversations: DeleteConversations,
|
||||||
@@ -72,6 +73,7 @@ class MainViewModel @Inject constructor(
|
|||||||
private val permissionManager: PermissionManager,
|
private val permissionManager: PermissionManager,
|
||||||
private val prefs: Preferences,
|
private val prefs: Preferences,
|
||||||
private val ratingManager: RatingManager,
|
private val ratingManager: RatingManager,
|
||||||
|
private val syncContacts: SyncContacts,
|
||||||
private val syncMessages: SyncMessages
|
private val syncMessages: SyncMessages
|
||||||
) : QkViewModel<MainView, MainState>(MainState(page = Inbox(data = conversationRepo.getConversations()))) {
|
) : QkViewModel<MainView, MainState>(MainState(page = Inbox(data = conversationRepo.getConversations()))) {
|
||||||
|
|
||||||
@@ -81,6 +83,7 @@ class MainViewModel @Inject constructor(
|
|||||||
disposables += markArchived
|
disposables += markArchived
|
||||||
disposables += markUnarchived
|
disposables += markUnarchived
|
||||||
disposables += migratePreferences
|
disposables += migratePreferences
|
||||||
|
disposables += syncContacts
|
||||||
disposables += syncMessages
|
disposables += syncMessages
|
||||||
|
|
||||||
// Show the syncing UI
|
// Show the syncing UI
|
||||||
@@ -109,6 +112,12 @@ class MainViewModel @Inject constructor(
|
|||||||
syncMessages.execute(Unit)
|
syncMessages.execute(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sync contacts when we detect a change
|
||||||
|
disposables += contactAddedListener.listen()
|
||||||
|
.debounce(1, TimeUnit.SECONDS)
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.subscribe { syncContacts.execute(Unit) }
|
||||||
|
|
||||||
ratingManager.addSession()
|
ratingManager.addSession()
|
||||||
markAllSeen.execute(Unit)
|
markAllSeen.execute(Unit)
|
||||||
}
|
}
|
||||||
@@ -287,7 +296,6 @@ class MainViewModel @Inject constructor(
|
|||||||
.map { conversation -> conversation.recipients }
|
.map { conversation -> conversation.recipients }
|
||||||
.mapNotNull { recipients -> recipients[0]?.address?.takeIf { recipients.size == 1 } }
|
.mapNotNull { recipients -> recipients[0]?.address?.takeIf { recipients.size == 1 } }
|
||||||
.doOnNext(navigator::addContact)
|
.doOnNext(navigator::addContact)
|
||||||
.flatMap(contactAddedListener::listen)
|
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|
||||||
|
|||||||
@@ -42,5 +42,5 @@ data class SettingsState(
|
|||||||
val mobileOnly: Boolean = false,
|
val mobileOnly: Boolean = false,
|
||||||
val maxMmsSizeSummary: String = "100KB",
|
val maxMmsSizeSummary: String = "100KB",
|
||||||
val maxMmsSizeId: Int = 100,
|
val maxMmsSizeId: Int = 100,
|
||||||
val syncProgress: SyncRepository.SyncProgress = SyncRepository.SyncProgress.Idle()
|
val syncProgress: SyncRepository.SyncProgress = SyncRepository.SyncProgress.Idle
|
||||||
)
|
)
|
||||||
Reference in New Issue
Block a user