Store contact’s photoUri

So much less complicated now!
This commit is contained in:
Moez Bhatti
2019-11-24 00:27:39 -05:00
parent a1fcecd79e
commit 573b01ff65
15 changed files with 36 additions and 173 deletions
@@ -41,6 +41,7 @@ class CursorToContactImpl @Inject constructor(
Phone.TYPE,
Phone.LABEL,
Phone.DISPLAY_NAME,
Phone.PHOTO_URI,
Phone.STARRED,
Phone.CONTACT_LAST_UPDATED_TIMESTAMP
)
@@ -52,13 +53,15 @@ class CursorToContactImpl @Inject constructor(
const val COLUMN_TYPE = 4
const val COLUMN_LABEL = 5
const val COLUMN_DISPLAY_NAME = 6
const val COLUMN_STARRED = 7
const val CONTACT_LAST_UPDATED = 8
const val COLUMN_PHOTO_URI = 7
const val COLUMN_STARRED = 8
const val CONTACT_LAST_UPDATED = 9
}
override fun map(from: Cursor) = Contact().apply {
lookupKey = from.getString(COLUMN_LOOKUP_KEY)
name = from.getString(COLUMN_DISPLAY_NAME) ?: ""
photoUri = from.getString(COLUMN_PHOTO_URI)
numbers.add(PhoneNumber(
id = from.getLong(COLUMN_ID),
accountType = from.getString(COLUMN_ACCOUNT_TYPE),
@@ -126,6 +126,7 @@ class QkRealmMigration : RealmMigration {
realm.schema.get("Contact")
?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED)
?.addField("photoUri", String::class.java)
realm.schema.get("PhoneNumber")
?.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
@@ -1,103 +0,0 @@
/*
* 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/>.
*/
package com.moez.QKSMS.util
import android.content.Context
import android.provider.ContactsContract
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.Key
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.data.DataFetcher
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory
import com.moez.QKSMS.repository.ContactRepository
import com.moez.QKSMS.repository.ContactRepositoryImpl
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.io.InputStream
import java.security.MessageDigest
class ContactImageLoader(
private val context: Context,
private val contactRepo: ContactRepository,
private val phoneNumberUtils: PhoneNumberUtils
) : ModelLoader<String, InputStream> {
override fun handles(model: String): Boolean {
return model.startsWith("tel:")
}
override fun buildLoadData(
model: String,
width: Int,
height: Int,
options: Options
): ModelLoader.LoadData<InputStream>? {
return ModelLoader.LoadData(
ContactImageKey(phoneNumberUtils.normalizeNumber(model)),
ContactImageFetcher(context, contactRepo, model))
}
class Factory(
private val context: Context,
private val prefs: Preferences
) : ModelLoaderFactory<String, InputStream> {
override fun build(multiFactory: MultiModelLoaderFactory): ContactImageLoader {
return ContactImageLoader(context, ContactRepositoryImpl(context, prefs), PhoneNumberUtils(context))
}
override fun teardown() {} // nothing to do here
}
class ContactImageKey(private val address: String) : Key {
override fun updateDiskCacheKey(digest: MessageDigest) = digest.update(address.toByteArray())
}
class ContactImageFetcher(
private val context: Context,
private val contactRepo: ContactRepository,
private val address: String
) : DataFetcher<InputStream> {
private var loadPhotoDisposable: Disposable? = null
override fun cleanup() {}
override fun getDataClass() = InputStream::class.java
override fun getDataSource() = DataSource.LOCAL
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
loadPhotoDisposable = contactRepo.findContactUri(address)
.map { uri ->
ContactsContract.Contacts.openContactPhotoInputStream(context.contentResolver, uri, true)
}
.subscribeOn(Schedulers.io())
.subscribe(
{ inputStream -> callback.onDataReady(inputStream) },
{ error -> callback.onLoadFailed(Exception(error)) })
}
override fun cancel() {
loadPhotoDisposable?.dispose()
}
}
}
@@ -19,15 +19,12 @@
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
class GlideAppModule : AppGlideModule() {
@@ -37,10 +34,6 @@ class GlideAppModule : AppGlideModule() {
}
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
// TODO use DI to create the ContactImageLoader.Factory
val rxPrefs = RxSharedPreferences.create(PreferenceManager.getDefaultSharedPreferences(context))
val prefs = Preferences(context, rxPrefs)
registry.append(String::class.java, InputStream::class.java, ContactImageLoader.Factory(context, prefs))
}
}
}