mirror of
https://github.com/SimpleMobileTools/Simple-Dialer.git
synced 2025-02-12 09:30:51 +01:00
Merge pull request #558 from ismailnurudeen/feat/filtering
feat: implemented filtering similar to simple contact.
This commit is contained in:
commit
a7fa1f9fe4
@ -25,6 +25,7 @@ import com.simplemobiletools.dialer.BuildConfig
|
|||||||
import com.simplemobiletools.dialer.R
|
import com.simplemobiletools.dialer.R
|
||||||
import com.simplemobiletools.dialer.adapters.ViewPagerAdapter
|
import com.simplemobiletools.dialer.adapters.ViewPagerAdapter
|
||||||
import com.simplemobiletools.dialer.dialogs.ChangeSortingDialog
|
import com.simplemobiletools.dialer.dialogs.ChangeSortingDialog
|
||||||
|
import com.simplemobiletools.dialer.dialogs.FilterContactSourcesDialog
|
||||||
import com.simplemobiletools.dialer.extensions.config
|
import com.simplemobiletools.dialer.extensions.config
|
||||||
import com.simplemobiletools.dialer.extensions.launchCreateNewContactIntent
|
import com.simplemobiletools.dialer.extensions.launchCreateNewContactIntent
|
||||||
import com.simplemobiletools.dialer.fragments.FavoritesFragment
|
import com.simplemobiletools.dialer.fragments.FavoritesFragment
|
||||||
@ -181,6 +182,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
R.id.clear_call_history -> clearCallHistory()
|
R.id.clear_call_history -> clearCallHistory()
|
||||||
R.id.create_new_contact -> launchCreateNewContactIntent()
|
R.id.create_new_contact -> launchCreateNewContactIntent()
|
||||||
R.id.sort -> showSortingDialog(showCustomSorting = getCurrentFragment() is FavoritesFragment)
|
R.id.sort -> showSortingDialog(showCustomSorting = getCurrentFragment() is FavoritesFragment)
|
||||||
|
R.id.filter -> showFilterDialog()
|
||||||
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
|
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
|
||||||
R.id.settings -> launchSettings()
|
R.id.settings -> launchSettings()
|
||||||
R.id.about -> launchAbout()
|
R.id.about -> launchAbout()
|
||||||
@ -519,7 +521,21 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fun showFilterDialog() {
|
||||||
|
FilterContactSourcesDialog(this) {
|
||||||
|
favorites_fragment?.refreshItems {
|
||||||
|
if (main_menu.isSearchOpen) {
|
||||||
|
getCurrentFragment()?.onSearchQueryChanged(main_menu.getCurrentQuery())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contacts_fragment?.refreshItems {
|
||||||
|
if (main_menu.isSearchOpen) {
|
||||||
|
getCurrentFragment()?.onSearchQueryChanged(main_menu.getCurrentQuery())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
fun cacheContacts(contacts: List<Contact>) {
|
fun cacheContacts(contacts: List<Contact>) {
|
||||||
try {
|
try {
|
||||||
cachedContacts.clear()
|
cachedContacts.clear()
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
package com.simplemobiletools.dialer.adapters
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.simplemobiletools.commons.extensions.getProperBackgroundColor
|
||||||
|
import com.simplemobiletools.commons.extensions.getProperPrimaryColor
|
||||||
|
import com.simplemobiletools.commons.extensions.getProperTextColor
|
||||||
|
import com.simplemobiletools.commons.helpers.SMT_PRIVATE
|
||||||
|
import com.simplemobiletools.commons.models.contacts.ContactSource
|
||||||
|
import com.simplemobiletools.dialer.R
|
||||||
|
import com.simplemobiletools.dialer.activities.SimpleActivity
|
||||||
|
import kotlinx.android.synthetic.main.item_filter_contact_source.view.*
|
||||||
|
|
||||||
|
class FilterContactSourcesAdapter(
|
||||||
|
val activity: SimpleActivity,
|
||||||
|
private val contactSources: List<ContactSource>,
|
||||||
|
private val displayContactSources: ArrayList<String>
|
||||||
|
) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
private val selectedKeys = HashSet<Int>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
contactSources.forEachIndexed { index, contactSource ->
|
||||||
|
if (displayContactSources.contains(contactSource.name)) {
|
||||||
|
selectedKeys.add(contactSource.hashCode())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contactSource.type == SMT_PRIVATE && displayContactSources.contains(SMT_PRIVATE)) {
|
||||||
|
selectedKeys.add(contactSource.hashCode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toggleItemSelection(select: Boolean, contactSource: ContactSource, position: Int) {
|
||||||
|
if (select) {
|
||||||
|
selectedKeys.add(contactSource.hashCode())
|
||||||
|
} else {
|
||||||
|
selectedKeys.remove(contactSource.hashCode())
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyItemChanged(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getSelectedContactSources() = contactSources.filter { selectedKeys.contains(it.hashCode()) }
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
|
val view = activity.layoutInflater.inflate(R.layout.item_filter_contact_source, parent, false)
|
||||||
|
return ViewHolder(view)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
val contactSource = contactSources[position]
|
||||||
|
holder.bindView(contactSource)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount() = contactSources.size
|
||||||
|
|
||||||
|
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
fun bindView(contactSource: ContactSource): View {
|
||||||
|
val isSelected = selectedKeys.contains(contactSource.hashCode())
|
||||||
|
itemView.apply {
|
||||||
|
filter_contact_source_checkbox.isChecked = isSelected
|
||||||
|
filter_contact_source_checkbox.setColors(activity.getProperTextColor(), activity.getProperPrimaryColor(), activity.getProperBackgroundColor())
|
||||||
|
val countText = if (contactSource.count >= 0) " (${contactSource.count})" else ""
|
||||||
|
val displayName = "${contactSource.publicName}$countText"
|
||||||
|
filter_contact_source_checkbox.text = displayName
|
||||||
|
filter_contact_source_holder.setOnClickListener { viewClicked(!isSelected, contactSource) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemView
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun viewClicked(select: Boolean, contactSource: ContactSource) {
|
||||||
|
toggleItemSelection(select, contactSource, adapterPosition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,86 @@
|
|||||||
|
package com.simplemobiletools.dialer.dialogs
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
|
||||||
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||||
|
import com.simplemobiletools.commons.helpers.SMT_PRIVATE
|
||||||
|
import com.simplemobiletools.commons.extensions.getVisibleContactSources
|
||||||
|
import com.simplemobiletools.commons.helpers.ContactsHelper
|
||||||
|
import com.simplemobiletools.commons.models.contacts.*
|
||||||
|
import com.simplemobiletools.dialer.R
|
||||||
|
import com.simplemobiletools.dialer.activities.SimpleActivity
|
||||||
|
import com.simplemobiletools.dialer.adapters.FilterContactSourcesAdapter
|
||||||
|
import com.simplemobiletools.dialer.extensions.config
|
||||||
|
import kotlinx.android.synthetic.main.dialog_filter_contact_sources.view.*
|
||||||
|
|
||||||
|
class FilterContactSourcesDialog(val activity: SimpleActivity, private val callback: () -> Unit) {
|
||||||
|
private var dialog: AlertDialog? = null
|
||||||
|
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_contact_sources, null)
|
||||||
|
private var contactSources = ArrayList<ContactSource>()
|
||||||
|
private var contacts = ArrayList<Contact>()
|
||||||
|
private var isContactSourcesReady = false
|
||||||
|
private var isContactsReady = false
|
||||||
|
|
||||||
|
init {
|
||||||
|
val contactHelper = ContactsHelper(activity)
|
||||||
|
contactHelper.getContactSources { contactSources ->
|
||||||
|
contactSources.mapTo(this@FilterContactSourcesDialog.contactSources) { it.copy() }
|
||||||
|
isContactSourcesReady = true
|
||||||
|
processDataIfReady()
|
||||||
|
}
|
||||||
|
|
||||||
|
contactHelper.getContacts(getAll = true) { contacts ->
|
||||||
|
contacts.mapTo(this@FilterContactSourcesDialog.contacts) { it.copy() }
|
||||||
|
isContactsReady = true
|
||||||
|
processDataIfReady()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun processDataIfReady() {
|
||||||
|
if (!isContactSourcesReady) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val contactSourcesWithCount = ArrayList<ContactSource>()
|
||||||
|
for (contactSource in contactSources) {
|
||||||
|
val count = if (isContactsReady) {
|
||||||
|
contacts.filter { it.source == contactSource.name }.count()
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
}
|
||||||
|
contactSourcesWithCount.add(contactSource.copy(count = count))
|
||||||
|
}
|
||||||
|
|
||||||
|
contactSources.clear()
|
||||||
|
contactSources.addAll(contactSourcesWithCount)
|
||||||
|
|
||||||
|
activity.runOnUiThread {
|
||||||
|
val selectedSources = activity.getVisibleContactSources()
|
||||||
|
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, selectedSources)
|
||||||
|
|
||||||
|
if (dialog == null) {
|
||||||
|
activity.getAlertDialogBuilder()
|
||||||
|
.setPositiveButton(R.string.ok) { dialogInterface, i -> confirmContactSources() }
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.apply {
|
||||||
|
activity.setupDialogStuff(view, this) { alertDialog ->
|
||||||
|
dialog = alertDialog
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun confirmContactSources() {
|
||||||
|
val selectedContactSources = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
|
||||||
|
val ignoredContactSources = contactSources.filter { !selectedContactSources.contains(it) }.map {
|
||||||
|
if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.getFullIdentifier()
|
||||||
|
}.toHashSet()
|
||||||
|
|
||||||
|
if (activity.getVisibleContactSources() != ignoredContactSources) {
|
||||||
|
activity.config.ignoredContactSources = ignoredContactSources
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
dialog?.dismiss()
|
||||||
|
}
|
||||||
|
}
|
10
app/src/main/res/layout/dialog_filter_contact_sources.xml
Normal file
10
app/src/main/res/layout/dialog_filter_contact_sources.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.simplemobiletools.commons.views.MyRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/filter_contact_sources_list"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:clipToPadding="false"
|
||||||
|
android:overScrollMode="never"
|
||||||
|
android:paddingTop="@dimen/medium_margin"
|
||||||
|
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
|
21
app/src/main/res/layout/item_filter_contact_source.xml
Normal file
21
app/src/main/res/layout/item_filter_contact_source.xml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/filter_contact_source_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?attr/selectableItemBackground"
|
||||||
|
android:paddingStart="@dimen/normal_margin"
|
||||||
|
android:paddingTop="@dimen/small_margin"
|
||||||
|
android:paddingEnd="@dimen/normal_margin"
|
||||||
|
android:paddingBottom="@dimen/small_margin">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
|
||||||
|
android:id="@+id/filter_contact_source_checkbox"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="@dimen/medium_margin"
|
||||||
|
android:background="@null"
|
||||||
|
android:clickable="false"
|
||||||
|
android:paddingStart="@dimen/small_margin" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
@ -9,6 +9,11 @@
|
|||||||
android:showAsAction="always"
|
android:showAsAction="always"
|
||||||
android:title="@string/sort_by"
|
android:title="@string/sort_by"
|
||||||
app:showAsAction="always" />
|
app:showAsAction="always" />
|
||||||
|
<item
|
||||||
|
android:id="@+id/filter"
|
||||||
|
android:icon="@drawable/ic_filter_vector"
|
||||||
|
android:title="@string/filter"
|
||||||
|
app:showAsAction="always" />
|
||||||
<item
|
<item
|
||||||
android:id="@+id/create_new_contact"
|
android:id="@+id/create_new_contact"
|
||||||
android:icon="@drawable/ic_plus_vector"
|
android:icon="@drawable/ic_plus_vector"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user