Skip to content

Commit

Permalink
refactor: rename and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmikius committed Mar 28, 2024
1 parent f19bad5 commit 151ac0e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

`FSContactStore` is a Swift package that provides a convenient and easy-to-use interface for interacting with Apple's `Contacts` framework on iOS and macOS.

## Features:
## Features

* Simplified authorization handling for requesting access to contacts.
* Flexible fetching options for retrieving contacts with filtering, sorting, and specifying properties to retrieve.
* Support for unified contacts, which combine information from multiple sources.
* Shared instance for quick access to common functionalities.
* Customizable save requests to allow tailoring logic for adding, updating, and deleting contacts.

## Installation:
## Installation

Add the package to your Package.swift file:

Expand All @@ -35,7 +35,7 @@ Import the package in your Swift files:
import ContactStore
```

## Usage:
## Usage

1. Import the library:

Expand Down Expand Up @@ -120,7 +120,7 @@ try store.delete(contact)

By default, `ContactStore` uses a standard `CNSaveRequest` instance.
You can modify the static closure `ContactStore.makeCNSaveRequest` to inject custom logic or provide different request implementations.
**Important:** To modify the ContactStore.makeCNSaveRequest closure, you must use the use(_:) method.
**Important:** To modify the `ContactStore.makeCNSaveRequest` closure, you must use the `use(_:)` method.

```swift
ContactStore.use { request in
Expand All @@ -133,7 +133,33 @@ ContactStore.use { request in
This code updates the `ContactStore.makeCNSaveRequest` closure to accept a `CNSaveRequest` instance as an argument,
make any necessary changes to it, and then return the updated request.

## Contributions:
7. Customizing Authorization Status (Optional):

By default, `ContactStore` uses the actual authorization status from the system's `CNContactStore`.
However, you can override this behavior with a custom closure to inject specific authorization statuses for testing or other purposes.

Here's how to customize the authorization status:

**Use the `use(_:)` method:**

- Call the `use(_:)` method on `ContactStore` to provide a custom closure for creating `CNAuthorizationStatus` instances.
- The closure accepts no arguments and returns a `CNAuthorizationStatus` value.

**Implement the closure logic:**

- Within the closure, specify the desired authorization status to be returned.
You can return a fixed value or implement more complex logic based on your needs.

```swift
ContactStore.use {
return .denied // Simulate denied authorization status
}

let currentStatus = ContactStore.authorizationStatus()
// currentStatus will now be .denied
```

## Contributions

We welcome contributions to this project! Please feel free to open issues or pull requests to help improve the package.

Expand Down
8 changes: 4 additions & 4 deletions Sources/ContactStore/ContactStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public final class ContactStore: ContactStoreProtocol {
CNSaveRequest()
}

/// A closure for creating `CNAuthorizationStatus` instances, allowing for custom implementations.
private static var makeAuthorizationStatus: () -> CNAuthorizationStatus = {
/// A closure for creating `CNAuthorizationStatus` instances, allowing set needed status.
private static var makeCNAuthorizationStatus: () -> CNAuthorizationStatus = {
CNContactStore.authorizationStatus(for: .contacts)
}

Expand All @@ -55,7 +55,7 @@ public final class ContactStore: ContactStoreProtocol {
///
/// - Returns: The current authorization status (e.g., .authorized, .denied).
public func authorizationStatus() -> CNAuthorizationStatus {
let status = Self.makeAuthorizationStatus
let status = Self.makeCNAuthorizationStatus
return status()
}

Expand Down Expand Up @@ -202,6 +202,6 @@ public final class ContactStore: ContactStoreProtocol {
///
/// - Parameter make: The closure that creates a `CNAuthorizationStatus` instance.
func use(_ make: @escaping () -> CNAuthorizationStatus) {
Self.makeAuthorizationStatus = make
Self.makeCNAuthorizationStatus = make
}
}

0 comments on commit 151ac0e

Please sign in to comment.