|
| 1 | +/* |
| 2 | + * Copyright 2024 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + * |
| 8 | + * See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md |
| 9 | + */ |
| 10 | +package org.mifospay.feature.send.money |
| 11 | + |
| 12 | +import android.content.ContentResolver |
| 13 | +import android.provider.ContactsContract |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.remember |
| 16 | +import androidx.compose.ui.platform.LocalContext |
| 17 | + |
| 18 | +@Composable |
| 19 | +actual fun rememberContactRepository(): ContactRepository { |
| 20 | + val context = LocalContext.current |
| 21 | + return remember { AndroidContactRepository(context.contentResolver) } |
| 22 | +} |
| 23 | + |
| 24 | +class AndroidContactRepository( |
| 25 | + private val contentResolver: ContentResolver, |
| 26 | +) : ContactRepository { |
| 27 | + |
| 28 | + override suspend fun getContacts(): List<Contact> { |
| 29 | + return queryContacts(null) |
| 30 | + } |
| 31 | + |
| 32 | + override suspend fun searchContacts(query: String): List<Contact> { |
| 33 | + val selection = "${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} LIKE ? OR ${ContactsContract.CommonDataKinds.Phone.NUMBER} LIKE ?" |
| 34 | + val selectionArgs = arrayOf("%$query%", "%$query%") |
| 35 | + return queryContacts(selection, selectionArgs) |
| 36 | + } |
| 37 | + |
| 38 | + private fun queryContacts(selection: String? = null, selectionArgs: Array<String>? = null): List<Contact> { |
| 39 | + val contacts = mutableListOf<Contact>() |
| 40 | + |
| 41 | + val projection = arrayOf( |
| 42 | + ContactsContract.CommonDataKinds.Phone.CONTACT_ID, |
| 43 | + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, |
| 44 | + ContactsContract.CommonDataKinds.Phone.NUMBER, |
| 45 | + ContactsContract.CommonDataKinds.Phone.TYPE, |
| 46 | + ) |
| 47 | + |
| 48 | + val sortOrder = "${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} ASC" |
| 49 | + |
| 50 | + contentResolver.query( |
| 51 | + ContactsContract.CommonDataKinds.Phone.CONTENT_URI, |
| 52 | + projection, |
| 53 | + selection, |
| 54 | + selectionArgs, |
| 55 | + sortOrder, |
| 56 | + )?.use { cursor -> |
| 57 | + val idColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID) |
| 58 | + val nameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) |
| 59 | + val numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) |
| 60 | + |
| 61 | + while (cursor.moveToNext()) { |
| 62 | + val id = cursor.getString(idColumn) |
| 63 | + val name = cursor.getString(nameColumn) ?: "Unknown" |
| 64 | + val number = cursor.getString(numberColumn) ?: "" |
| 65 | + |
| 66 | + val contact = Contact( |
| 67 | + id = id, |
| 68 | + name = name, |
| 69 | + phoneNumber = number, |
| 70 | + upiId = null, |
| 71 | + ) |
| 72 | + |
| 73 | + if (!contacts.any { it.name == name && it.phoneNumber == number }) { |
| 74 | + contacts.add(contact) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return contacts |
| 80 | + } |
| 81 | +} |
0 commit comments