diff --git a/app/build.gradle b/app/build.gradle index 12035e69..ef995694 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,5 +32,5 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:3.2.15' + implementation 'com.simplemobiletools:commons:3.2.16' } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt index fb3248bc..4b6f36b4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -16,6 +16,7 @@ import com.simplemobiletools.contacts.BuildConfig import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.adapters.ContactsAdapter import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog +import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.openContact import com.simplemobiletools.contacts.extensions.startCallIntent @@ -83,6 +84,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.sort -> showSortingDialog() + R.id.filter -> showFilterDialog() R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java)) R.id.about -> launchAbout() else -> return super.onOptionsItemSelected(item) @@ -96,6 +98,12 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { } } + private fun showFilterDialog() { + FilterContactSourcesDialog(this) { + initContacts() + } + } + private fun launchAbout() { startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_MULTISELECT, BuildConfig.VERSION_NAME) } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt new file mode 100644 index 00000000..9e42c9c1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt @@ -0,0 +1,83 @@ +package com.simplemobiletools.contacts.adapters + +import android.support.v7.widget.RecyclerView +import android.util.SparseArray +import android.view.View +import android.view.ViewGroup +import com.simplemobiletools.commons.interfaces.MyAdapterListener +import com.simplemobiletools.contacts.R +import com.simplemobiletools.contacts.activities.SimpleActivity +import com.simplemobiletools.contacts.extensions.config +import kotlinx.android.synthetic.main.filter_contact_source_view.view.* +import java.util.* + +class FilterContactSourcesAdapter(val activity: SimpleActivity, val contactSources: List, val displayContactSources: Set) : + RecyclerView.Adapter() { + private val itemViews = SparseArray() + private val selectedPositions = HashSet() + + init { + contactSources.forEachIndexed { index, value -> + if ((displayContactSources.size == 1 && displayContactSources.first() == "-1") || displayContactSources.contains(value)) { + selectedPositions.add(index) + } + } + } + + private fun toggleItemSelection(select: Boolean, pos: Int) { + if (select) { + if (itemViews[pos] != null) { + selectedPositions.add(pos) + } + } else { + selectedPositions.remove(pos) + } + + itemViews[pos]?.filter_contact_source_checkbox?.isChecked = select + } + + private val adapterListener = object : MyAdapterListener { + override fun toggleItemSelectionAdapter(select: Boolean, position: Int) { + toggleItemSelection(select, position) + } + + override fun getSelectedPositions() = selectedPositions + + override fun itemLongClicked(position: Int) {} + } + + fun getSelectedItemsSet(): HashSet { + val selectedItemsSet = HashSet(selectedPositions.size) + selectedPositions.forEach { selectedItemsSet.add(contactSources[it]) } + return selectedItemsSet + } + + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { + val view = activity.layoutInflater.inflate(R.layout.filter_contact_source_view, parent, false) + return ViewHolder(view, adapterListener, activity) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val contactSource = contactSources[position] + itemViews.put(position, holder.bindView(contactSource)) + toggleItemSelection(selectedPositions.contains(position), position) + } + + override fun getItemCount() = contactSources.size + + class ViewHolder(view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity) : RecyclerView.ViewHolder(view) { + fun bindView(contactSource: String): View { + itemView.apply { + filter_contact_source_checkbox.setColors(activity.config.textColor, activity.config.primaryColor, activity.config.backgroundColor) + filter_contact_source_checkbox.text = contactSource + filter_contact_source_holder.setOnClickListener { viewClicked(!filter_contact_source_checkbox.isChecked) } + } + + return itemView + } + + private fun viewClicked(select: Boolean) { + adapterListener.toggleItemSelectionAdapter(select, adapterPosition) + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt new file mode 100644 index 00000000..064523b5 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt @@ -0,0 +1,38 @@ +package com.simplemobiletools.contacts.dialogs + +import android.support.v7.app.AlertDialog +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.contacts.R +import com.simplemobiletools.contacts.activities.SimpleActivity +import com.simplemobiletools.contacts.adapters.FilterContactSourcesAdapter +import com.simplemobiletools.contacts.extensions.config +import com.simplemobiletools.contacts.helpers.ContactsHelper +import kotlinx.android.synthetic.main.dialog_filter_contact_sources.view.* + +class FilterContactSourcesDialog(val activity: SimpleActivity, val callback: () -> Unit) { + var dialog: AlertDialog + val view = activity.layoutInflater.inflate(R.layout.dialog_filter_contact_sources, null) + + init { + ContactsHelper(activity).getContactSources { + val selectedSources = activity.config.displayContactSources + view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) + } + + dialog = AlertDialog.Builder(activity) + .setPositiveButton(R.string.ok, { dialogInterface, i -> confirmEventTypes() }) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this) + } + } + + private fun confirmEventTypes() { + val selectedItems = (view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedItemsSet() + if (activity.config.displayContactSources != selectedItems) { + activity.config.displayContactSources = selectedItems + callback() + } + dialog.dismiss() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt index 140d4549..e479738c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt @@ -16,4 +16,8 @@ class Config(context: Context) : BaseConfig(context) { var callContact: Boolean get() = prefs.getBoolean(CALL_CONTACT_ON_CLICK, false) set(callContact) = prefs.edit().putBoolean(CALL_CONTACT_ON_CLICK, callContact).apply() + + var displayContactSources: Set + get() = prefs.getStringSet(DISPLAY_CONTACT_SOURCES, hashSetOf("-1")) + set(displayContactSources) = prefs.edit().remove(DISPLAY_CONTACT_SOURCES).putStringSet(DISPLAY_CONTACT_SOURCES, displayContactSources).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt index 9fc58894..dc352b10 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.helpers // shared prefs val CALL_CONTACT_ON_CLICK = "call_contact_on_click" +val DISPLAY_CONTACT_SOURCES = "display_contact_sources" val SORTING = "sorting" val CONTACT_ID = "contact_id" diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index f73692a1..18facada 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -10,8 +10,30 @@ import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.models.Contact +import java.util.* class ContactsHelper(val activity: SimpleActivity) { + fun getContactSources(callback: (ArrayList) -> Unit) { + val accounts = HashSet() + Thread { + val uri = ContactsContract.RawContacts.CONTENT_URI + val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_NAME) + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + accounts.add(cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + + callback(ArrayList(accounts)) + }.start() + } + fun getContacts(callback: (ArrayList) -> Unit) { val contacts = HashMap() Thread { diff --git a/app/src/main/res/layout/dialog_filter_contact_sources.xml b/app/src/main/res/layout/dialog_filter_contact_sources.xml new file mode 100644 index 00000000..4264f88b --- /dev/null +++ b/app/src/main/res/layout/dialog_filter_contact_sources.xml @@ -0,0 +1,11 @@ + + diff --git a/app/src/main/res/layout/filter_contact_source_view.xml b/app/src/main/res/layout/filter_contact_source_view.xml new file mode 100644 index 00000000..29ded9de --- /dev/null +++ b/app/src/main/res/layout/filter_contact_source_view.xml @@ -0,0 +1,18 @@ + + + + + + diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index b422f365..45de2234 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -6,6 +6,11 @@ android:icon="@drawable/ic_sort" android:title="@string/sort_by" app:showAsAction="ifRoom"/> +