#1223 - Add option to only show mobile numbers

This commit is contained in:
Moez Bhatti
2018-08-10 22:37:05 -04:00
parent d6bf211d9f
commit caae77a189
10 changed files with 99 additions and 15 deletions
@@ -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)
@@ -138,6 +138,7 @@ class SettingsController : QkController<SettingsView, SettingsState, SettingsPre
systemFont.checkbox.isChecked = state.systemFontEnabled
unicode.checkbox.isChecked = state.stripUnicodeEnabled
mobileOnly.checkbox.isChecked = state.mobileOnly
mmsSize.summary = state.maxMmsSizeSummary
mmsSizeDialog.adapter.selectedItem = state.maxMmsSizeId
@@ -78,6 +78,9 @@ class SettingsPresenter @Inject constructor(
disposables += prefs.unicode.asObservable()
.subscribe { enabled -> newState { copy(stripUnicodeEnabled = enabled) } }
disposables += prefs.mobileOnly.asObservable()
.subscribe { enabled -> newState { copy(mobileOnly = enabled) } }
val mmsSizeLabels = context.resources.getStringArray(R.array.mms_sizes)
val mmsSizeIds = context.resources.getIntArray(R.array.mms_sizes_ids)
disposables += prefs.mmsSize.asObservable()
@@ -130,6 +133,8 @@ class SettingsPresenter @Inject constructor(
R.id.unicode -> prefs.unicode.set(!prefs.unicode.get())
R.id.mobileOnly -> prefs.mobileOnly.set(!prefs.mobileOnly.get())
R.id.mmsSize -> view.showMmsSizePicker()
R.id.sync -> {
@@ -38,6 +38,7 @@ data class SettingsState(
val systemFontEnabled: Boolean = false,
val splitSmsEnabled: Boolean = false,
val stripUnicodeEnabled: Boolean = false,
val mobileOnly: Boolean = false,
val maxMmsSizeSummary: String = "100KB",
val maxMmsSizeId: Int = 100
)
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2017 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/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M17,1.01L7,1c-1.1,0 -2,0.9 -2,2v18c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2V3c0,-1.1 -0.9,-1.99 -2,-1.99zM17,19H7V5h10v14z"/>
</vector>
@@ -153,6 +153,15 @@
app:title="@string/settings_unicode_title"
app:widget="@layout/settings_switch_widget" />
<com.moez.QKSMS.common.widget.PreferenceView
android:id="@+id/mobileOnly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:icon="@drawable/ic_smartphone_black_24dp"
app:summary="@string/settings_mobile_summary"
app:title="@string/settings_mobile_title"
app:widget="@layout/settings_switch_widget" />
<com.moez.QKSMS.common.widget.PreferenceView
android:id="@+id/mmsSize"
android:layout_width="match_parent"
@@ -203,6 +203,8 @@
<string name="settings_delivery_summary">Confirm that messages were sent successfully</string>
<string name="settings_unicode_title">Strip accents</string>
<string name="settings_unicode_summary">Remove accents from characters in outgoing SMS messages</string>
<string name="settings_mobile_title">Mobile numbers only</string>
<string name="settings_mobile_summary">When composing a message, only show mobile numbers</string>
<string name="settings_mms_size_title">Auto-compress MMS attachments</string>
<string name="settings_sync_title">Sync messages</string>
<string name="settings_sync_summary">Re-sync your messages with the native Android SMS database</string>