Sort contacts with letter names first

This commit is contained in:
moezbhatti
2019-10-27 13:24:48 -04:00
committed by Moez Bhatti
parent fdf2e2dbcb
commit 28cad9bbc9
2 changed files with 17 additions and 5 deletions
@@ -77,10 +77,9 @@ class ContactRepositoryImpl @Inject constructor(
Phone.getTypeLabel(context.resources, Phone.TYPE_MOBILE, "Mobile").toString()
}
return when (prefs.mobileOnly.get()) {
val contactsFlowable = when (prefs.mobileOnly.get()) {
true -> realm.where(Contact::class.java)
.contains("numbers.type", mobileLabel)
.sort("name")
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
@@ -98,7 +97,6 @@ class ContactRepositoryImpl @Inject constructor(
}
false -> realm.where(Contact::class.java)
.sort("name")
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
@@ -107,6 +105,20 @@ class ContactRepositoryImpl @Inject constructor(
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
}
return contactsFlowable.map { contacts ->
contacts.sortedWith(Comparator { c1, c2 ->
val initial = c1.name.firstOrNull()
val other = c2.name.firstOrNull()
if (initial?.isLetter() == true && other?.isLetter() != true) {
-1
} else if (initial?.isLetter() != true && other?.isLetter() == true) {
1
} else {
c1.name.compareTo(c2.name, ignoreCase = true)
}
})
}
}
}
}