-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move logic from SearchDeviceContacts into repository
- Loading branch information
Adam Jodlowski
committed
May 23, 2024
1 parent
6efb5a9
commit b018889
Showing
5 changed files
with
163 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...ta/src/main/kotlin/ch/protonmail/android/mailcontact/data/DeviceContactsRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2022 Proton Technologies AG | ||
* This file is part of Proton Technologies AG and Proton Mail. | ||
* | ||
* Proton Mail is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Proton Mail is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Proton Mail. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ch.protonmail.android.mailcontact.data | ||
|
||
import android.content.Context | ||
import android.provider.ContactsContract | ||
import arrow.core.Either | ||
import arrow.core.left | ||
import arrow.core.right | ||
import ch.protonmail.android.mailcontact.domain.model.DeviceContact | ||
import ch.protonmail.android.mailcontact.domain.repository.DeviceContactsRepository | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.withContext | ||
import me.proton.core.util.kotlin.DispatcherProvider | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class DeviceContactsRepositoryImpl @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
private val dispatcherProvider: DispatcherProvider | ||
) : DeviceContactsRepository { | ||
|
||
override suspend fun getDeviceContacts( | ||
query: String | ||
): Either<DeviceContactsRepository.DeviceContactsErrors, List<DeviceContact>> { | ||
|
||
val contentResolver = context.contentResolver | ||
|
||
val selectionArgs = arrayOf("%$query%", "%$query%", "%$query%") | ||
|
||
@Suppress("SwallowedException") | ||
val contactEmails = try { | ||
withContext(dispatcherProvider.Io) { | ||
contentResolver.query( | ||
ContactsContract.CommonDataKinds.Email.CONTENT_URI, | ||
ANDROID_PROJECTION, | ||
ANDROID_SELECTION, | ||
selectionArgs, | ||
ANDROID_ORDER_BY | ||
) | ||
} | ||
} catch (e: SecurityException) { | ||
Timber.d("SearchDeviceContacts: contact permission is not granted") | ||
null | ||
} ?: return DeviceContactsRepository.DeviceContactsErrors.PermissionDenied.left() | ||
|
||
val deviceContacts = mutableListOf<DeviceContact>() | ||
|
||
val displayNameColumnIndex = contactEmails.getColumnIndex( | ||
ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY | ||
).takeIf { | ||
it >= 0 | ||
} ?: 0 | ||
|
||
val emailColumnIndex = contactEmails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS).takeIf { | ||
it >= 0 | ||
} ?: 0 | ||
|
||
contactEmails.use { cursor -> | ||
for (position in 0 until cursor.count) { | ||
cursor.moveToPosition(position) | ||
deviceContacts.add( | ||
DeviceContact( | ||
name = contactEmails.getString(displayNameColumnIndex), | ||
email = contactEmails.getString(emailColumnIndex) | ||
) | ||
) | ||
} | ||
} | ||
|
||
return deviceContacts.right() | ||
} | ||
|
||
companion object { | ||
|
||
private const val ANDROID_ORDER_BY = ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY + " ASC" | ||
|
||
@Suppress("MaxLineLength") | ||
private const val ANDROID_SELECTION = | ||
"${ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY} LIKE ? OR ${ContactsContract.CommonDataKinds.Email.ADDRESS} LIKE ? OR ${ContactsContract.CommonDataKinds.Email.DATA} LIKE ?" | ||
|
||
private val ANDROID_PROJECTION = arrayOf( | ||
ContactsContract.CommonDataKinds.Email.DISPLAY_NAME_PRIMARY, | ||
ContactsContract.CommonDataKinds.Email.ADDRESS, | ||
ContactsContract.CommonDataKinds.Email.DATA | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...in/kotlin/ch/protonmail/android/mailcontact/domain/repository/DeviceContactsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2022 Proton Technologies AG | ||
* This file is part of Proton Technologies AG and Proton Mail. | ||
* | ||
* Proton Mail is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Proton Mail is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Proton Mail. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ch.protonmail.android.mailcontact.domain.repository | ||
|
||
import arrow.core.Either | ||
import ch.protonmail.android.mailcontact.domain.model.DeviceContact | ||
|
||
interface DeviceContactsRepository { | ||
|
||
suspend fun getDeviceContacts(query: String): Either<DeviceContactsErrors, List<DeviceContact>> | ||
|
||
sealed class DeviceContactsErrors { | ||
data object PermissionDenied : DeviceContactsErrors() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters