Skip to content

Commit

Permalink
#25 Added SimContactsPermissionsRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
vestrel00 committed Mar 24, 2022
1 parent cff4429 commit 7ce3ee5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 72 deletions.
65 changes: 6 additions & 59 deletions core/src/main/java/contacts/core/entities/SimContact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sealed interface SimContactEntity : Entity {
*/
val number: String?

// TODO We'll eventually support emails in SIM when the system level APIs support it correctly and reliably.
// Support for CRUD operations for emails in SIM cards was implemented in Android 12 (API 31).
// Given that this support is very new and IMO unstable and still incomplete, this library will
// not yet support emails in SIM cards.
Expand All @@ -38,38 +39,17 @@ sealed interface SimContactEntity : Entity {
override fun redactedCopy(): SimContactEntity
}

// TODO Update this if SIM contacts cannot be updated!
/* DEV NOTES: Necessary Abstractions
*
* We only create abstractions when they are necessary!
*
* Apart from SimContactEntity, there is only one interface that extends it; MutableSimContactEntity.
*
* The MutableSimContactEntity interface is used for library constructs that require an SimContactEntity
* that can be mutated whether it is already inserted in the database or not. There are two
* variants of this; MutableSimContact and NewSimContact. With this, we can create constructs that can
* keep a reference to MutableSimContact(s) or NewSimContact(s) through the MutableSimContactEntity
* abstraction/facade.
*
* This is why there are no interfaces for NewSimContactEntity, ExistingSimContactEntity, and
* ImmutableSimContactEntity. There are currently no library functions or constructs that require them.
* This is why there are no interfaces for NewSimContactEntity, ExistingSimContactEntity,
* ImmutableSimContactEntity, and MutableNewSimContactEntity. There are currently no
* library functions or constructs that require them.
*
* Please update this documentation if new abstractions are created.
*/

/**
* A mutable [SimContactEntity]. `
*/
// TODO Remove this if SIM contacts cannot be updated!
sealed interface MutableSimContactEntity : SimContactEntity, MutableEntity {

override var name: String?
override var number: String?

// We have to cast the return type because we are not using recursive generic types.
override fun redactedCopy(): MutableSimContactEntity
}

/**
* An existing immutable [SimContactEntity].
*/
Expand Down Expand Up @@ -98,40 +78,7 @@ data class SimContact internal constructor(

override val isRedacted: Boolean

) : SimContactEntity, ExistingEntity, ImmutableEntityWithMutableType<MutableSimContact> {

override fun mutableCopy() = MutableSimContact(
id = id,

name = name,
number = number,

isRedacted = isRedacted
)

override fun redactedCopy() = copy(
isRedacted = true,

name = name?.redact(),
number = number?.redact()
)
}

/**
* An existing mutable [SimContactEntity].
*/
// TODO Remove this if SIM contacts cannot be updated!
@Parcelize
data class MutableSimContact internal constructor(

override val id: Long,

override var name: String?,
override var number: String?,

override val isRedacted: Boolean

) : SimContactEntity, ExistingEntity, MutableSimContactEntity {
) : SimContactEntity, ExistingEntity, ImmutableEntity {

override fun redactedCopy() = copy(
isRedacted = true,
Expand All @@ -152,7 +99,7 @@ data class NewSimContact @JvmOverloads constructor(

override val isRedacted: Boolean = false

) : SimContactEntity, NewEntity, MutableSimContactEntity {
) : SimContactEntity, NewEntity, MutableEntity {

override fun redactedCopy() = copy(
isRedacted = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import contacts.core.entities.table.Table
*/
internal class SimContactsOperation {

fun insert(simContact: NewSimContact): ContentValues? =
if (simContact.isBlank) {
null
} else {
ContentValues().apply {
put(SimContactsFields.Tag.columnName, simContact.name)
put(SimContactsFields.Number.columnName, simContact.number)
}
fun insert(simContact: NewSimContact): ContentValues? = if (simContact.isBlank) {
null
} else {
ContentValues().apply {
put(SimContactsFields.Tag.columnName, simContact.name)
put(SimContactsFields.Number.columnName, simContact.number)
}
}

/**
* Returns a where clause that uses the [SimContact.name] (tag) and [SimContact.number] to
Expand Down
7 changes: 2 additions & 5 deletions core/src/main/java/contacts/core/sim/SimContacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package contacts.core.sim
import contacts.core.Contacts

/**
* Provides new [SimContactsQuery], [SimContactsInsert], [SimContactsUpdate], and
* [SimContactsDelete] instances.
* Provides new [SimContactsQuery], [SimContactsInsert], and [SimContactsDelete] instances.
*/
interface SimContacts {

Expand All @@ -18,9 +17,7 @@ interface SimContacts {
*/
fun insert(): SimContactsInsert

/**
* Returns a new [SimContactsUpdate] instance.
*/
// TODO We'll eventually support SimContactsUpdate when the system level APIs support it correctly and reliably.
// fun update(): SimContactsUpdate

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package contacts.permissions.sim

import contacts.core.ContactsPermissions
import contacts.core.sim.SimContacts
import contacts.core.sim.SimContactsDelete
import contacts.core.sim.SimContactsInsert
import contacts.core.sim.SimContactsQuery
import contacts.permissions.accounts.requestGetAccountsPermission
import contacts.permissions.requestReadPermission
import contacts.permissions.requestWritePermission

/**
* If [ContactsPermissions.READ_PERMISSION] is not yet granted, suspends the current coroutine,
* requests for the permission, and then returns a new [SimContactsQuery] instance.
*
* If permission is already granted, then immediately returns a new [SimContactsQuery] instance.
*/
suspend fun SimContacts.queryWithPermission(): SimContactsQuery {
if (!contactsApi.permissions.canQuery()) {
contactsApi.applicationContext.requestReadPermission()
}

return query()
}

/**
* If [ContactsPermissions.WRITE_PERMISSION] are not yet granted, suspends the current coroutine,
* requests for the permissions, and then returns a new [SimContactsInsert] instance.
*
* If permissions are already granted, then immediately returns a new [SimContactsInsert] instance.
*/
suspend fun SimContacts.insertWithPermission(): SimContactsInsert {
if (!contactsApi.permissions.canInsert()) {
contactsApi.applicationContext.requestWritePermission()
contactsApi.applicationContext.requestGetAccountsPermission()
}

return insert()
}

/**
* If [ContactsPermissions.WRITE_PERMISSION] is not yet granted, suspends the current coroutine,
* requests for the permissions, and then returns a new [SimContactsDelete] instance.
*
* If permissions are already granted, then immediately returns a new [SimContactsDelete] instance.
*/
suspend fun SimContacts.deleteWithPermission(): SimContactsDelete {
if (!contactsApi.permissions.canUpdateDelete()) {
contactsApi.applicationContext.requestWritePermission()
}

return delete()
}

0 comments on commit 7ce3ee5

Please sign in to comment.