From 7b30d6efd5ed54e705d7e1a06d6346b8756f896f Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 5 Feb 2020 15:57:55 +0100 Subject: [PATCH] adding an initial implementation of fastscroller with letters --- app/build.gradle | 1 + .../contacts/pro/extensions/String.kt | 2 ++ .../pro/fragments/MyViewPagerFragment.kt | 28 +++++++++++++++++++ .../res/layout/fragment_letters_layout.xml | 7 +++++ 4 files changed, 38 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 4969c33a..91d75d4b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -61,6 +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.reddit:indicator-fast-scroll:1.2.1' kapt "androidx.room:room-compiler:2.2.2" implementation "androidx.room:room-runtime:2.2.2" diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt index beda894f..095a92e8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt @@ -35,3 +35,5 @@ fun String.getDateTimeFromDateString(viewToUpdate: TextView? = null): DateTime { } fun String.normalizeNumber() = PhoneNumberUtils.normalizeNumber(this) + +fun String.areLettersOnly() = matches(Regex("[a-zA-Z]+")) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt index 0c46067c..ff762823 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.util.AttributeSet import android.view.ViewGroup import androidx.coordinatorlayout.widget.CoordinatorLayout +import com.reddit.indicatorfastscroll.FastScrollItemIndicator import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME @@ -16,6 +17,7 @@ import com.simplemobiletools.contacts.pro.activities.MainActivity import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter import com.simplemobiletools.contacts.pro.adapters.GroupsAdapter +import com.simplemobiletools.contacts.pro.extensions.areLettersOnly import com.simplemobiletools.contacts.pro.extensions.config import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources import com.simplemobiletools.contacts.pro.helpers.* @@ -23,6 +25,14 @@ import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.pro.models.Contact import com.simplemobiletools.contacts.pro.models.Group import kotlinx.android.synthetic.main.fragment_layout.view.* +import kotlinx.android.synthetic.main.fragment_layout.view.fragment_fab +import kotlinx.android.synthetic.main.fragment_layout.view.fragment_list +import kotlinx.android.synthetic.main.fragment_layout.view.fragment_placeholder +import kotlinx.android.synthetic.main.fragment_layout.view.fragment_placeholder_2 +import kotlinx.android.synthetic.main.fragment_layout.view.fragment_wrapper +import kotlinx.android.synthetic.main.fragment_letters_layout.view.* +import java.util.* +import kotlin.collections.ArrayList abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { protected var activity: SimpleActivity? = null @@ -35,6 +45,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) var skipHashComparing = false var forceListRedraw = false + var wasLetterFastScrollerSetup = false fun setupFragment(activity: SimpleActivity) { config = activity.config @@ -139,6 +150,23 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } else { setupContactsFavoritesAdapter(contacts) contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: ArrayList() + + if (!wasLetterFastScrollerSetup) { + wasLetterFastScrollerSetup = true + 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("") + } + }) + } } } diff --git a/app/src/main/res/layout/fragment_letters_layout.xml b/app/src/main/res/layout/fragment_letters_layout.xml index e8a508a9..1c50bd85 100644 --- a/app/src/main/res/layout/fragment_letters_layout.xml +++ b/app/src/main/res/layout/fragment_letters_layout.xml @@ -42,6 +42,13 @@ android:scrollbars="none" app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"/> + +