-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#25 Added SimContactsPermissionsRequest
- Loading branch information
Showing
4 changed files
with
68 additions
and
72 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
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
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
53 changes: 53 additions & 0 deletions
53
permissions/src/main/java/contacts/permissions/sim/SimContactsPermissionsRequest.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,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() | ||
} |