mirror of
https://github.com/SimpleMobileTools/Simple-Dialer.git
synced 2025-06-05 21:49:23 +02:00
Merge pull request #648 from Merkost/speed_dial_search
Speed dial search implemented
This commit is contained in:
@ -65,7 +65,7 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:2462c80e58'
|
||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:91763fe00f'
|
||||
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
|
||||
implementation 'me.grantland:autofittextview:0.2.1'
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
|
||||
|
@ -217,8 +217,9 @@ class MainActivity : SimpleActivity() {
|
||||
}
|
||||
|
||||
private fun changeColumnCount() {
|
||||
val items = (CONTACTS_GRID_MIN_COLUMNS_COUNT..CONTACTS_GRID_MAX_COLUMNS_COUNT).map {
|
||||
RadioItem(it, resources.getQuantityString(R.plurals.column_counts, it, it))
|
||||
val items = ArrayList<RadioItem>()
|
||||
for (i in 1..CONTACTS_GRID_MAX_COLUMNS_COUNT) {
|
||||
items.add(RadioItem(i, resources.getQuantityString(R.plurals.column_counts, i, i)))
|
||||
}
|
||||
|
||||
val currentColumnCount = config.contactsGridColumnCount
|
||||
|
@ -149,9 +149,9 @@ class ContactsAdapter(
|
||||
|
||||
override fun getItemCount() = contacts.size
|
||||
|
||||
fun updateItems(newItems: ArrayList<Contact>, highlightText: String = "") {
|
||||
fun updateItems(newItems: List<Contact>, highlightText: String = "") {
|
||||
if (newItems.hashCode() != contacts.hashCode()) {
|
||||
contacts = newItems.clone() as ArrayList<Contact>
|
||||
contacts = ArrayList(newItems)
|
||||
textToHighlight = highlightText
|
||||
notifyDataSetChanged()
|
||||
finishActMode()
|
||||
@ -351,7 +351,7 @@ class ContactsAdapter(
|
||||
val layoutManager = recyclerView.layoutManager
|
||||
if (layoutManager is GridLayoutManager) {
|
||||
val currentSpanCount = layoutManager.spanCount
|
||||
val newSpanCount = (currentSpanCount - 1).coerceIn(CONTACTS_GRID_MIN_COLUMNS_COUNT, CONTACTS_GRID_MAX_COLUMNS_COUNT)
|
||||
val newSpanCount = (currentSpanCount - 1).coerceIn(1, CONTACTS_GRID_MAX_COLUMNS_COUNT)
|
||||
layoutManager.spanCount = newSpanCount
|
||||
recyclerView.requestLayout()
|
||||
onSpanCountListener(newSpanCount)
|
||||
@ -362,7 +362,7 @@ class ContactsAdapter(
|
||||
val layoutManager = recyclerView.layoutManager
|
||||
if (layoutManager is GridLayoutManager) {
|
||||
val currentSpanCount = layoutManager.spanCount
|
||||
val newSpanCount = (currentSpanCount + 1).coerceIn(CONTACTS_GRID_MIN_COLUMNS_COUNT, CONTACTS_GRID_MAX_COLUMNS_COUNT)
|
||||
val newSpanCount = (currentSpanCount + 1).coerceIn(1, CONTACTS_GRID_MAX_COLUMNS_COUNT)
|
||||
layoutManager.spanCount = newSpanCount
|
||||
recyclerView.requestLayout()
|
||||
onSpanCountListener(newSpanCount)
|
||||
|
@ -1,20 +1,28 @@
|
||||
package com.simplemobiletools.dialer.dialogs
|
||||
|
||||
import android.graphics.Color
|
||||
import android.view.KeyEvent
|
||||
import android.view.View
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageView
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.reddit.indicatorfastscroll.FastScrollItemIndicator
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.models.contacts.Contact
|
||||
import com.simplemobiletools.commons.views.MySearchMenu
|
||||
import com.simplemobiletools.dialer.R
|
||||
import com.simplemobiletools.dialer.activities.SimpleActivity
|
||||
import com.simplemobiletools.dialer.adapters.ContactsAdapter
|
||||
import kotlinx.android.synthetic.main.dialog_select_contact.view.letter_fastscroller
|
||||
import kotlinx.android.synthetic.main.dialog_select_contact.view.letter_fastscroller_thumb
|
||||
import kotlinx.android.synthetic.main.dialog_select_contact.view.select_contact_list
|
||||
import kotlinx.android.synthetic.main.dialog_select_contact.view.*
|
||||
import java.util.Locale
|
||||
|
||||
class SelectContactDialog(val activity: SimpleActivity, contacts: MutableList<Contact>, val callback: (selectedContact: Contact) -> Unit) {
|
||||
class SelectContactDialog(val activity: SimpleActivity, val contacts: List<Contact>, val callback: (selectedContact: Contact) -> Unit) {
|
||||
private var dialog: AlertDialog? = null
|
||||
private var view = activity.layoutInflater.inflate(R.layout.dialog_select_contact, null)
|
||||
private val searchView = view.contact_search_view
|
||||
private val searchEditText = view.findViewById<EditText>(R.id.top_toolbar_search)
|
||||
private val searchViewAppBarLayout = view.findViewById<View>(R.id.top_app_bar_layout)
|
||||
|
||||
init {
|
||||
view.apply {
|
||||
@ -23,17 +31,10 @@ class SelectContactDialog(val activity: SimpleActivity, contacts: MutableList<Co
|
||||
letter_fastscroller_thumb.textColor = context.getProperPrimaryColor().getContrastColor()
|
||||
letter_fastscroller_thumb.thumbColor = context.getProperPrimaryColor().getColorStateList()
|
||||
|
||||
letter_fastscroller.setupWithRecyclerView(select_contact_list, { position ->
|
||||
try {
|
||||
val name = contacts[position].getNameToDisplay()
|
||||
val character = if (name.isNotEmpty()) name.substring(0, 1) else ""
|
||||
FastScrollItemIndicator.Text(character.uppercase(Locale.getDefault()))
|
||||
} catch (e: Exception) {
|
||||
FastScrollItemIndicator.Text("")
|
||||
}
|
||||
})
|
||||
setupLetterFastScroller(contacts)
|
||||
configureSearchView()
|
||||
|
||||
select_contact_list.adapter = ContactsAdapter(activity, contacts, select_contact_list, allowLongClick = false) {
|
||||
select_contact_list.adapter = ContactsAdapter(activity, contacts.toMutableList(), select_contact_list, allowLongClick = false) {
|
||||
callback(it as Contact)
|
||||
dialog?.dismiss()
|
||||
}
|
||||
@ -41,10 +42,105 @@ class SelectContactDialog(val activity: SimpleActivity, contacts: MutableList<Co
|
||||
|
||||
activity.getAlertDialogBuilder()
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setOnKeyListener { _, i, keyEvent ->
|
||||
if (keyEvent.action == KeyEvent.ACTION_UP && i == KeyEvent.KEYCODE_BACK) {
|
||||
backPressed()
|
||||
}
|
||||
true
|
||||
}
|
||||
.apply {
|
||||
activity.setupDialogStuff(view, this) { alertDialog ->
|
||||
activity.setupDialogStuff(view, this, R.string.choose_contact) { alertDialog ->
|
||||
dialog = alertDialog
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupLetterFastScroller(contacts: List<Contact>) {
|
||||
view.letter_fastscroller.setupWithRecyclerView(view.select_contact_list, { position ->
|
||||
try {
|
||||
val name = contacts[position].getNameToDisplay()
|
||||
val character = if (name.isNotEmpty()) name.substring(0, 1) else ""
|
||||
FastScrollItemIndicator.Text(character.uppercase(Locale.getDefault()))
|
||||
} catch (e: Exception) {
|
||||
FastScrollItemIndicator.Text("")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun configureSearchView() = with(searchView) {
|
||||
updateHintText(context.getString(R.string.search_contacts))
|
||||
searchEditText.imeOptions = EditorInfo.IME_ACTION_DONE
|
||||
|
||||
toggleHideOnScroll(true)
|
||||
setupMenu()
|
||||
setSearchViewListeners()
|
||||
updateSearchViewUi()
|
||||
}
|
||||
|
||||
private fun MySearchMenu.updateSearchViewUi() {
|
||||
getToolbar().beInvisible()
|
||||
updateColors()
|
||||
setBackgroundColor(Color.TRANSPARENT)
|
||||
searchViewAppBarLayout.setBackgroundColor(Color.TRANSPARENT)
|
||||
}
|
||||
|
||||
private fun MySearchMenu.setSearchViewListeners() {
|
||||
onSearchOpenListener = {
|
||||
updateSearchViewLeftIcon(R.drawable.ic_cross_vector)
|
||||
}
|
||||
onSearchClosedListener = {
|
||||
searchEditText.clearFocus()
|
||||
activity.hideKeyboard(searchEditText)
|
||||
updateSearchViewLeftIcon(R.drawable.ic_search_vector)
|
||||
}
|
||||
|
||||
onSearchTextChangedListener = { text ->
|
||||
filterContactListBySearchQuery(text)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateSearchViewLeftIcon(iconResId: Int) = with(view.findViewById<ImageView>(R.id.top_toolbar_search_icon)) {
|
||||
post {
|
||||
setImageResource(iconResId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun filterContactListBySearchQuery(query: String) {
|
||||
val adapter = view.select_contact_list.adapter as? ContactsAdapter
|
||||
var contactsToShow = contacts
|
||||
if (query.isNotEmpty()) {
|
||||
contactsToShow = contacts.filter { it.name.contains(query, true) }
|
||||
}
|
||||
checkPlaceholderVisibility(contactsToShow)
|
||||
|
||||
if (adapter?.contacts != contactsToShow) {
|
||||
adapter?.updateItems(contactsToShow)
|
||||
setupLetterFastScroller(contactsToShow)
|
||||
|
||||
view.select_contact_list.apply {
|
||||
post {
|
||||
scrollToPosition(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkPlaceholderVisibility(contacts: List<Contact>) = with(view) {
|
||||
contacts_empty_placeholder.beVisibleIf(contacts.isEmpty())
|
||||
|
||||
if (contact_search_view.isSearchOpen) {
|
||||
contacts_empty_placeholder.text = context.getString(R.string.no_items_found)
|
||||
}
|
||||
|
||||
letter_fastscroller.beVisibleIf(contacts_empty_placeholder.isGone())
|
||||
letter_fastscroller_thumb.beVisibleIf(contacts_empty_placeholder.isGone())
|
||||
}
|
||||
|
||||
private fun backPressed() {
|
||||
if (searchView.isSearchOpen) {
|
||||
searchView.closeSearch()
|
||||
} else {
|
||||
dialog?.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,33 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<com.simplemobiletools.commons.views.MySearchMenu
|
||||
android:id="@+id/contact_search_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/contacts_empty_placeholder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_search_view"
|
||||
android:alpha="0.8"
|
||||
android:gravity="center_horizontal"
|
||||
android:paddingStart="@dimen/activity_margin"
|
||||
android:paddingTop="@dimen/activity_margin"
|
||||
android:paddingEnd="@dimen/activity_margin"
|
||||
android:text="@string/no_contacts_found"
|
||||
android:textSize="@dimen/bigger_text_size"
|
||||
android:textStyle="italic"
|
||||
android:visibility="gone" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||
android:id="@+id/select_contact_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contacts_empty_placeholder"
|
||||
android:clipToPadding="false"
|
||||
android:scrollbars="none"
|
||||
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
|
||||
@ -17,6 +40,7 @@
|
||||
android:id="@+id/letter_fastscroller"
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contact_search_view"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:paddingTop="@dimen/big_margin"
|
||||
android:paddingBottom="@dimen/big_margin" />
|
||||
@ -29,5 +53,4 @@
|
||||
android:layout_alignBottom="@+id/letter_fastscroller"
|
||||
android:layout_marginEnd="@dimen/activity_margin"
|
||||
android:layout_toStartOf="@+id/letter_fastscroller" />
|
||||
|
||||
</RelativeLayout>
|
||||
|
Reference in New Issue
Block a user