forked from upstream-mirrors/quik
#1223 - Add option to only show mobile numbers
This commit is contained in:
@@ -22,9 +22,12 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.ContactsContract
|
||||
import android.provider.ContactsContract.CommonDataKinds.Email
|
||||
import android.provider.ContactsContract.CommonDataKinds.Phone
|
||||
import com.moez.QKSMS.extensions.asFlowable
|
||||
import com.moez.QKSMS.extensions.mapNotNull
|
||||
import com.moez.QKSMS.model.Contact
|
||||
import com.moez.QKSMS.util.Preferences
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
@@ -35,13 +38,16 @@ import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class ContactRepositoryImpl @Inject constructor(val context: Context) : ContactRepository {
|
||||
class ContactRepositoryImpl @Inject constructor(
|
||||
private val context: Context,
|
||||
private val prefs: Preferences
|
||||
) : ContactRepository {
|
||||
|
||||
override fun findContactUri(address: String): Single<Uri> {
|
||||
return Flowable.just(address)
|
||||
.map {
|
||||
when {
|
||||
address.contains('@') -> Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(address))
|
||||
address.contains('@') -> Uri.withAppendedPath(Email.CONTENT_FILTER_URI, Uri.encode(address))
|
||||
else -> Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address))
|
||||
}
|
||||
}
|
||||
@@ -61,15 +67,41 @@ class ContactRepositoryImpl @Inject constructor(val context: Context) : ContactR
|
||||
|
||||
override fun getUnmanagedContacts(): Flowable<List<Contact>> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Contact::class.java)
|
||||
.sort("name")
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.map { realm.copyFromRealm(it) }
|
||||
.subscribeOn(AndroidSchedulers.mainThread())
|
||||
.observeOn(Schedulers.io())
|
||||
|
||||
val mobileLabel by lazy {
|
||||
Phone.getTypeLabel(context.resources, Phone.TYPE_MOBILE, "Mobile").toString()
|
||||
}
|
||||
|
||||
return when (prefs.mobileOnly.get()) {
|
||||
true -> realm.where(Contact::class.java)
|
||||
.contains("numbers.type", mobileLabel)
|
||||
.sort("name")
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.map { realm.copyFromRealm(it) }
|
||||
.subscribeOn(AndroidSchedulers.mainThread())
|
||||
.observeOn(Schedulers.io())
|
||||
.map { contacts ->
|
||||
contacts.map { contact ->
|
||||
val filteredNumbers = contact.numbers.filter { number -> number.type == mobileLabel }
|
||||
contact.numbers.clear()
|
||||
contact.numbers.addAll(filteredNumbers)
|
||||
contact
|
||||
}
|
||||
}
|
||||
|
||||
false -> realm.where(Contact::class.java)
|
||||
.sort("name")
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.map { realm.copyFromRealm(it) }
|
||||
.subscribeOn(AndroidSchedulers.mainThread())
|
||||
.observeOn(Schedulers.io())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,7 +37,10 @@ import java.io.InputStream
|
||||
import java.security.MessageDigest
|
||||
|
||||
|
||||
class ContactImageLoader(private val context: Context, private val contactRepo: ContactRepository) : ModelLoader<String, InputStream> {
|
||||
class ContactImageLoader(
|
||||
private val context: Context,
|
||||
private val contactRepo: ContactRepository
|
||||
) : ModelLoader<String, InputStream> {
|
||||
|
||||
override fun handles(model: String): Boolean {
|
||||
return PhoneNumberUtils.isGlobalPhoneNumber(model)
|
||||
@@ -47,8 +50,8 @@ class ContactImageLoader(private val context: Context, private val contactRepo:
|
||||
return ModelLoader.LoadData(ContactImageKey(model), ContactImageFetcher(context, contactRepo, model))
|
||||
}
|
||||
|
||||
class Factory(val context: Context) : ModelLoaderFactory<String, InputStream> {
|
||||
override fun build(multiFactory: MultiModelLoaderFactory) = ContactImageLoader(context, ContactRepositoryImpl(context))
|
||||
class Factory(val context: Context, val prefs: Preferences) : ModelLoaderFactory<String, InputStream> {
|
||||
override fun build(multiFactory: MultiModelLoaderFactory) = ContactImageLoader(context, ContactRepositoryImpl(context, prefs))
|
||||
override fun teardown() {} // nothing to do here
|
||||
}
|
||||
|
||||
|
||||
@@ -19,12 +19,14 @@
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
import android.content.Context
|
||||
import android.preference.PreferenceManager
|
||||
import android.util.Log
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.GlideBuilder
|
||||
import com.bumptech.glide.Registry
|
||||
import com.bumptech.glide.annotation.GlideModule
|
||||
import com.bumptech.glide.module.AppGlideModule
|
||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||
import java.io.InputStream
|
||||
|
||||
@GlideModule
|
||||
@@ -35,7 +37,8 @@ class GlideAppModule : AppGlideModule() {
|
||||
}
|
||||
|
||||
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
|
||||
registry.append(String::class.java, InputStream::class.java, ContactImageLoader.Factory(context))
|
||||
// TODO use DI to create the ContactImageLoader.Factory
|
||||
registry.append(String::class.java, InputStream::class.java, ContactImageLoader.Factory(context, Preferences(RxSharedPreferences.create(PreferenceManager.getDefaultSharedPreferences(context)))))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -83,6 +83,7 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
|
||||
val autoEmoji = rxPrefs.getBoolean("autoEmoji", true)
|
||||
val delivery = rxPrefs.getBoolean("delivery", false)
|
||||
val unicode = rxPrefs.getBoolean("unicode", false)
|
||||
val mobileOnly = rxPrefs.getBoolean("mobileOnly", false)
|
||||
val mmsSize = rxPrefs.getInteger("mmsSize", 300)
|
||||
val logging = rxPrefs.getBoolean("logging", false)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user