properly refresh the letters fastscroller if contacts change

This commit is contained in:
tibbi 2020-03-08 18:45:23 +01:00
parent 6db7f548ee
commit 4df2f32780
2 changed files with 31 additions and 31 deletions

View File

@ -61,7 +61,7 @@ dependencies {
implementation 'joda-time:joda-time:2.10.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'com.googlecode.ez-vcard:ez-vcard:0.10.5'
implementation 'com.github.tibbi:IndicatorFastScroll:d615a5c141'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'
kapt "androidx.room:room-compiler:2.2.2"
implementation "androidx.room:room-runtime:2.2.2"

View File

@ -45,7 +45,6 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
var skipHashComparing = false
var forceListRedraw = false
var wasLetterFastScrollerSetup = false
fun setupFragment(activity: SimpleActivity) {
config = activity.config
@ -151,9 +150,6 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
setupContactsFavoritesAdapter(contacts)
contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: ArrayList()
if (!wasLetterFastScrollerSetup) {
wasLetterFastScrollerSetup = true
val states = arrayOf(intArrayOf(android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_enabled),
intArrayOf(-android.R.attr.state_checked),
@ -165,26 +161,12 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
val myList = ColorStateList(states, colors)
letter_fastscroller.textColor = myList
letter_fastscroller.setupWithRecyclerView(fragment_list, { position ->
try {
val name = contacts[position].getNameToDisplay()
var character = if (name.isNotEmpty()) name.substring(0, 1) else ""
if (!character.areLettersOnly()) {
character = "#"
}
FastScrollItemIndicator.Text(character.toUpperCase(Locale.getDefault()))
} catch (e: Exception) {
FastScrollItemIndicator.Text("")
}
})
setupLetterFastscroller(contacts)
letter_fastscroller_thumb.setupWithFastScroller(letter_fastscroller)
letter_fastscroller_thumb.textColor = config.primaryColor.getContrastColor()
}
}
}
private fun setupGroupsAdapter(contacts: ArrayList<Contact>, callback: () -> Unit) {
ContactsHelper(activity!!).getStoredGroups {
@ -270,6 +252,22 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
private fun setupLetterFastscroller(contacts: ArrayList<Contact>) {
letter_fastscroller.setupWithRecyclerView(fragment_list, { position ->
try {
val name = contacts[position].getNameToDisplay()
var character = if (name.isNotEmpty()) name.substring(0, 1) else ""
if (!character.areLettersOnly()) {
character = "#"
}
FastScrollItemIndicator.Text(character.toUpperCase(Locale.getDefault()))
} catch (e: Exception) {
FastScrollItemIndicator.Text("")
}
})
}
fun fontSizeChanged() {
if (this is GroupsFragment) {
(fragment_list.adapter as? GroupsAdapter)?.apply {
@ -321,6 +319,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fragment_placeholder.beVisibleIf(filtered.isEmpty())
(adapter as? ContactsAdapter)?.updateItems(filtered, text.normalizeString())
setupLetterFastscroller(filtered)
} else if (adapter is GroupsAdapter) {
val filtered = groupsIgnoringSearch.filter {
it.title.contains(text, true)
@ -343,6 +342,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fun onSearchClosed() {
if (fragment_list.adapter is ContactsAdapter) {
(fragment_list.adapter as? ContactsAdapter)?.updateItems(contactsIgnoringSearch)
setupLetterFastscroller(contactsIgnoringSearch)
setupViewVisibility(contactsIgnoringSearch.isNotEmpty())
} else if (fragment_list.adapter is GroupsAdapter) {
(fragment_list.adapter as? GroupsAdapter)?.updateItems(groupsIgnoringSearch)