Skip to content

Commit 452225d

Browse files
miapuffianaveensingh
authored andcommitted
Added lazy loading
1 parent 9f4d926 commit 452225d

1 file changed

Lines changed: 49 additions & 21 deletions

File tree

app/src/main/kotlin/org/fossify/phone/activities/DialpadActivity.kt

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class DialpadActivity : SimpleActivity(), CachedContacts {
4848
private val longPressHandler = Handler(Looper.getMainLooper())
4949
private val pressedKeys = mutableSetOf<Char>()
5050
override var cachedContacts = ArrayList<Contact>()
51+
private var cachedRecentCalls = emptyList<RecentCall>()
5152

5253
override fun onCreate(savedInstanceState: Bundle?) {
5354
super.onCreate(savedInstanceState)
@@ -187,7 +188,9 @@ class DialpadActivity : SimpleActivity(), CachedContacts {
187188
binding.dialpadClearChar.applyColorFilter(getProperTextColor())
188189
updateNavigationBarColor(getProperBackgroundColor())
189190
setupToolbar(binding.dialpadToolbar, NavigationIcon.Arrow)
190-
handleGetItems()
191+
refreshCallItems {
192+
refreshCallItems(true)
193+
}
191194
}
192195

193196
private fun setupOptionsMenu() {
@@ -234,33 +237,54 @@ class DialpadActivity : SimpleActivity(), CachedContacts {
234237
binding.dialpadInput.setText("")
235238
}
236239

237-
private fun handleGetItems() {
240+
private fun refreshCallItems(loadAllRecents: Boolean = false, callback: (() -> Unit)? = null) {
238241
val newItems = ArrayList<DialpadItem>()
239-
newItems.add(DialpadItem("Contacts", true))
240242

241-
ContactsHelper(this).getContacts(showOnlyContactsWithNumbers = true) { contacts ->
242-
newItems.addAll(contacts.map { DialpadItem(it) })
243-
newItems.add(DialpadItem("Call History", false))
244-
RecentsHelper(this).getRecentCalls(queryLimit = Int.MAX_VALUE) { recentCalls ->
245-
val recentCallsNonContact = filterContactsInRecentCalls(recentCalls.distinctBy { it.phoneNumber }, contacts)
246-
recentCallsNonContact.forEach { newItems.add(DialpadItem(it)) }
243+
getContacts { contacts ->
244+
if (contacts.isNotEmpty()) {
245+
newItems.add(DialpadItem("Contacts", true))
246+
newItems.addAll(contacts.map { DialpadItem(it) })
247+
}
247248

248-
gotContacts(newItems)
249+
getRecents(contacts, loadAllRecents) { recentCalls ->
250+
if (recentCalls.isNotEmpty()) {
251+
newItems.add(DialpadItem("Call History", false))
252+
newItems.addAll(recentCalls.map { DialpadItem(it) })
253+
}
249254
}
250255
}
251-
}
252256

253-
private fun gotContacts(newItems: ArrayList<DialpadItem>) {
254257
allCallItems = newItems
258+
}
255259

256-
val privateContacts = MyContactsContentProvider.getContacts(this, privateCursor)
257-
if (privateContacts.isNotEmpty()) {
258-
allCallItems.addAll(privateContacts.map { DialpadItem(it) })
260+
private fun getContacts(callback: (List<Contact>) -> Unit) {
261+
ContactsHelper(this).getContacts(showOnlyContactsWithNumbers = true) { contacts ->
262+
ensureBackgroundThread {
263+
callback(contacts)
264+
}
259265
}
266+
}
267+
268+
private fun getRecents(contacts: List<Contact>, loadAllRecents: Boolean, callback: (List<RecentCall>) -> Unit) {
269+
val queryCount = if (loadAllRecents) Int.MAX_VALUE else RecentsHelper.QUERY_LIMIT
260270

261-
runOnUiThread {
262-
if (!checkDialIntent() && binding.dialpadInput.value.isEmpty()) {
263-
dialpadValueChanged("")
271+
RecentsHelper(this).getRecentCalls(cachedRecentCalls, queryCount) { recentCalls ->
272+
ensureBackgroundThread {
273+
cachedRecentCalls = recentCalls
274+
val recentCallsNonContact = filterContactsInRecentCalls(recentCalls.distinctBy { it.phoneNumber }, contacts)
275+
276+
val privateContacts = MyContactsContentProvider.getContacts(this, privateCursor)
277+
if (privateContacts.isNotEmpty()) {
278+
allCallItems.addAll(privateContacts.map { DialpadItem(it) })
279+
}
280+
281+
runOnUiThread {
282+
if (!checkDialIntent() && binding.dialpadInput.value.isEmpty()) {
283+
dialpadValueChanged("")
284+
}
285+
}
286+
287+
callback(recentCallsNonContact)
264288
}
265289
}
266290
}
@@ -307,10 +331,14 @@ class DialpadActivity : SimpleActivity(), CachedContacts {
307331
}
308332

309333
DialpadItem.DialpadItemType.RECENTCALL -> {
310-
val recentCall = item.recentCall!!
311-
val fixedText = text.trim().replace("\\s+".toRegex(), " ")
334+
if (len > 0) {
335+
val recentCall = item.recentCall!!
336+
val fixedText = text.trim().replace("\\s+".toRegex(), " ")
312337

313-
recentCall.name.contains(fixedText, true) || recentCall.doesContainPhoneNumber(fixedText)
338+
recentCall.name.contains(fixedText, true) || recentCall.doesContainPhoneNumber(fixedText)
339+
} else {
340+
false
341+
}
314342
}
315343
}
316344
}

0 commit comments

Comments
 (0)