forked from upstream-mirrors/quik
Split contacts to new screen, allow attaching multiple photos
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.feature.contacts
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import com.jakewharton.rxbinding2.widget.editorActions
|
||||
import com.jakewharton.rxbinding2.widget.textChanges
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.ViewModelFactory
|
||||
import com.moez.QKSMS.common.base.QkThemedActivity
|
||||
import com.moez.QKSMS.common.widget.QkDialog
|
||||
import com.moez.QKSMS.extensions.Optional
|
||||
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||
import com.moez.QKSMS.feature.compose.editing.ComposeItemAdapter
|
||||
import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction
|
||||
import com.moez.QKSMS.feature.compose.editing.PhoneNumberPickerAdapter
|
||||
import dagger.android.AndroidInjection
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.contacts_activity.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class ContactsActivity : QkThemedActivity(), ContactsContract {
|
||||
|
||||
companion object {
|
||||
const val ChipsKey = "chips"
|
||||
}
|
||||
|
||||
@Inject lateinit var contactsAdapter: ComposeItemAdapter
|
||||
@Inject lateinit var phoneNumberAdapter: PhoneNumberPickerAdapter
|
||||
@Inject lateinit var viewModelFactory: ViewModelFactory
|
||||
|
||||
override val queryChangedIntent: Observable<CharSequence> by lazy { search.textChanges() }
|
||||
override val queryBackspaceIntent: Observable<*> by lazy { search.backspaces }
|
||||
override val queryEditorActionIntent: Observable<Int> by lazy { search.editorActions() }
|
||||
override val composeItemPressedIntent: Subject<ComposeItem> by lazy { contactsAdapter.clicks }
|
||||
override val composeItemLongPressedIntent: Subject<ComposeItem> by lazy { contactsAdapter.longClicks }
|
||||
override val phoneNumberSelectedIntent: Subject<Optional<Long>> by lazy { phoneNumberAdapter.selectedItemChanges }
|
||||
override val phoneNumberActionIntent: Subject<PhoneNumberAction> = PublishSubject.create()
|
||||
|
||||
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[ContactsViewModel::class.java] }
|
||||
|
||||
private val phoneNumberDialog by lazy {
|
||||
QkDialog(this).apply {
|
||||
titleRes = R.string.compose_number_picker_title
|
||||
adapter = phoneNumberAdapter
|
||||
positiveButton = R.string.compose_number_picker_always
|
||||
positiveButtonListener = { phoneNumberActionIntent.onNext(PhoneNumberAction.ALWAYS) }
|
||||
negativeButton = R.string.compose_number_picker_once
|
||||
negativeButtonListener = { phoneNumberActionIntent.onNext(PhoneNumberAction.JUST_ONCE) }
|
||||
cancelListener = { phoneNumberActionIntent.onNext(PhoneNumberAction.CANCEL) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
AndroidInjection.inject(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.contacts_activity)
|
||||
showBackButton(true)
|
||||
viewModel.bindView(this)
|
||||
|
||||
contacts.itemAnimator = null
|
||||
contacts.adapter = contactsAdapter
|
||||
}
|
||||
|
||||
override fun render(state: ContactsState) {
|
||||
contactsAdapter.data = state.composeItems
|
||||
|
||||
if (state.selectedContact != null && !phoneNumberDialog.isShowing) {
|
||||
phoneNumberAdapter.data = state.selectedContact.numbers
|
||||
phoneNumberDialog.subtitle = state.selectedContact.name
|
||||
phoneNumberDialog.show()
|
||||
} else if (state.selectedContact == null && phoneNumberDialog.isShowing) {
|
||||
phoneNumberDialog.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
override fun finish(result: HashMap<String, String?>) {
|
||||
val intent = Intent().putExtra(ChipsKey, result)
|
||||
setResult(Activity.RESULT_OK, intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.feature.contacts
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.moez.QKSMS.injection.ViewModelKey
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.multibindings.IntoMap
|
||||
|
||||
@Module
|
||||
class ContactsActivityModule {
|
||||
|
||||
@Provides
|
||||
fun provideChips(activity: ContactsActivity): HashMap<String, String?> {
|
||||
return activity.intent.extras?.getSerializable(ContactsActivity.ChipsKey)
|
||||
?.let { serializable -> serializable as? HashMap<String, String?> }
|
||||
?: hashMapOf()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@IntoMap
|
||||
@ViewModelKey(ContactsViewModel::class)
|
||||
fun provideContactsViewModel(viewModel: ContactsViewModel): ViewModel = viewModel
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.feature.contacts
|
||||
|
||||
import com.moez.QKSMS.common.base.QkView
|
||||
import com.moez.QKSMS.extensions.Optional
|
||||
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||
import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.Subject
|
||||
|
||||
interface ContactsContract : QkView<ContactsState> {
|
||||
|
||||
val queryChangedIntent: Observable<CharSequence>
|
||||
val queryBackspaceIntent: Observable<*>
|
||||
val queryEditorActionIntent: Observable<Int>
|
||||
val composeItemPressedIntent: Subject<ComposeItem>
|
||||
val composeItemLongPressedIntent: Subject<ComposeItem>
|
||||
val phoneNumberSelectedIntent: Subject<Optional<Long>>
|
||||
val phoneNumberActionIntent: Subject<PhoneNumberAction>
|
||||
|
||||
fun finish(result: HashMap<String, String?>)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.feature.contacts
|
||||
|
||||
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||
import com.moez.QKSMS.model.Contact
|
||||
|
||||
data class ContactsState(
|
||||
val composeItems: List<ComposeItem> = ArrayList(),
|
||||
val selectedContact: Contact? = null // For phone number picker
|
||||
)
|
||||
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* 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.feature.contacts
|
||||
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import com.moez.QKSMS.common.base.QkViewModel
|
||||
import com.moez.QKSMS.extensions.mapNotNull
|
||||
import com.moez.QKSMS.extensions.removeAccents
|
||||
import com.moez.QKSMS.feature.compose.editing.Chip
|
||||
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||
import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction
|
||||
import com.moez.QKSMS.filter.ContactFilter
|
||||
import com.moez.QKSMS.filter.ContactGroupFilter
|
||||
import com.moez.QKSMS.interactor.ContactSync
|
||||
import com.moez.QKSMS.interactor.SetDefaultPhoneNumber
|
||||
import com.moez.QKSMS.model.Contact
|
||||
import com.moez.QKSMS.model.ContactGroup
|
||||
import com.moez.QKSMS.model.Conversation
|
||||
import com.moez.QKSMS.model.PhoneNumber
|
||||
import com.moez.QKSMS.repository.ContactRepository
|
||||
import com.moez.QKSMS.repository.ConversationRepository
|
||||
import com.moez.QKSMS.util.PhoneNumberUtils
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.autoDisposable
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.rxkotlin.Observables
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.RealmList
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.rx2.awaitFirst
|
||||
import javax.inject.Inject
|
||||
|
||||
class ContactsViewModel @Inject constructor(
|
||||
serializedChips: HashMap<String, String?>,
|
||||
syncContacts: ContactSync,
|
||||
private val contactFilter: ContactFilter,
|
||||
private val contactGroupFilter: ContactGroupFilter,
|
||||
private val contactsRepo: ContactRepository,
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val phoneNumberUtils: PhoneNumberUtils,
|
||||
private val setDefaultPhoneNumber: SetDefaultPhoneNumber
|
||||
) : QkViewModel<ContactsContract, ContactsState>(ContactsState()) {
|
||||
|
||||
private val contactGroups: Observable<List<ContactGroup>> by lazy { contactsRepo.getUnmanagedContactGroups() }
|
||||
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts() }
|
||||
private val recents: Observable<List<Conversation>> by lazy { conversationRepo.getUnmanagedConversations() }
|
||||
private val starredContacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts(true) }
|
||||
|
||||
private val selectedChips = Observable.just(serializedChips)
|
||||
.observeOn(Schedulers.io())
|
||||
.map { hashmap ->
|
||||
hashmap.map { (address, lookupKey) ->
|
||||
Chip(address, lookupKey?.let(contactsRepo::getUnmanagedContact))
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
syncContacts.execute(Unit)
|
||||
}
|
||||
|
||||
override fun bindView(view: ContactsContract) {
|
||||
super.bindView(view)
|
||||
|
||||
// Update the list of contact suggestions based on the query input, while also filtering out any contacts
|
||||
// that have already been selected
|
||||
Observables
|
||||
.combineLatest(
|
||||
view.queryChangedIntent, recents, starredContacts, contactGroups, contacts, selectedChips
|
||||
) { query, recents, starredContacts, contactGroups, contacts, selectedChips ->
|
||||
val composeItems = mutableListOf<ComposeItem>()
|
||||
if (query.isBlank()) {
|
||||
composeItems += recents
|
||||
.filter { conversation ->
|
||||
conversation.recipients.any { recipient ->
|
||||
selectedChips.none { chip ->
|
||||
if (recipient.contact == null) {
|
||||
chip.address == recipient.address
|
||||
} else {
|
||||
chip.contact?.lookupKey == recipient.contact?.lookupKey
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.map(ComposeItem::Recent)
|
||||
|
||||
composeItems += starredContacts
|
||||
.filter { contact -> selectedChips.none { it.contact?.lookupKey == contact.lookupKey } }
|
||||
.map(ComposeItem::Starred)
|
||||
|
||||
composeItems += contactGroups
|
||||
.filter { group ->
|
||||
group.contacts.any { contact ->
|
||||
selectedChips.none { chip -> chip.contact?.lookupKey == contact.lookupKey }
|
||||
}
|
||||
}
|
||||
.map(ComposeItem::Group)
|
||||
|
||||
composeItems += contacts
|
||||
.filter { contact -> selectedChips.none { it.contact?.lookupKey == contact.lookupKey } }
|
||||
.map(ComposeItem::Person)
|
||||
} else {
|
||||
// If the entry is a valid destination, allow it as a recipient
|
||||
if (phoneNumberUtils.isPossibleNumber(query.toString())) {
|
||||
val newAddress = phoneNumberUtils.formatNumber(query)
|
||||
val newContact = Contact(numbers = RealmList(PhoneNumber(address = newAddress)))
|
||||
composeItems += ComposeItem.New(newContact)
|
||||
}
|
||||
|
||||
// Strip the accents from the query. This can be an expensive operation, so
|
||||
// cache the result instead of doing it for each contact
|
||||
val normalizedQuery = query.removeAccents()
|
||||
composeItems += starredContacts
|
||||
.asSequence()
|
||||
.filter { contact -> selectedChips.none { it.contact?.lookupKey == contact.lookupKey } }
|
||||
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
||||
.map(ComposeItem::Starred)
|
||||
|
||||
composeItems += contactGroups
|
||||
.asSequence()
|
||||
.filter { group ->
|
||||
group.contacts.any { contact ->
|
||||
selectedChips.none { chip -> chip.contact?.lookupKey == contact.lookupKey }
|
||||
}
|
||||
}
|
||||
.filter { group -> contactGroupFilter.filter(group, normalizedQuery) }
|
||||
.map(ComposeItem::Group)
|
||||
|
||||
composeItems += contacts
|
||||
.asSequence()
|
||||
.filter { contact -> selectedChips.none { it.contact?.lookupKey == contact.lookupKey } }
|
||||
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
||||
.map(ComposeItem::Person)
|
||||
}
|
||||
|
||||
composeItems
|
||||
}
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { items -> newState { copy(composeItems = items) } }
|
||||
|
||||
// Listen for ComposeItems being selected, and then send them off to the number picker dialog in case
|
||||
// the user needs to select a phone number
|
||||
view.queryEditorActionIntent
|
||||
.filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE }
|
||||
.withLatestFrom(state) { _, state -> state }
|
||||
.mapNotNull { state -> state.composeItems.firstOrNull() }
|
||||
.mergeWith(view.composeItemPressedIntent)
|
||||
.map { composeItem -> composeItem to false }
|
||||
.mergeWith(view.composeItemLongPressedIntent.map { composeItem -> composeItem to true })
|
||||
.observeOn(Schedulers.io())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { (composeItem, force) ->
|
||||
val contacts = composeItem.getContacts()
|
||||
val newChips = contacts.map { contact ->
|
||||
if (contact.numbers.size == 1 || contact.getDefaultNumber() != null && !force) {
|
||||
val number = contact.getDefaultNumber() ?: contact.numbers[0]!!
|
||||
Chip(number.address, contact)
|
||||
} else {
|
||||
runBlocking {
|
||||
newState { copy(selectedContact = contact) }
|
||||
val action = view.phoneNumberActionIntent.awaitFirst()
|
||||
newState { copy(selectedContact = null) }
|
||||
val numberId = view.phoneNumberSelectedIntent.awaitFirst().value
|
||||
val number = contact.numbers.find { number -> number.id == numberId }
|
||||
|
||||
if (action == PhoneNumberAction.CANCEL || number == null) {
|
||||
return@runBlocking null
|
||||
}
|
||||
|
||||
if (action == PhoneNumberAction.ALWAYS) {
|
||||
val params = SetDefaultPhoneNumber.Params(contact.lookupKey, number.id)
|
||||
setDefaultPhoneNumber.execute(params)
|
||||
}
|
||||
|
||||
Chip(number.address, contact)
|
||||
} ?: return@subscribe
|
||||
}
|
||||
}
|
||||
|
||||
view.finish(HashMap(newChips.associate { chip -> chip.address to chip.contact?.lookupKey }))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user