Skip to content

Commit

Permalink
Merge pull request #763 from mrpmohiburrahman/mrp/add-group-functiona…
Browse files Browse the repository at this point in the history
…lity

feat: add group functionality
  • Loading branch information
morenoh149 authored Nov 17, 2024
2 parents 6ef00ab + cd982b1 commit 1d5eae7
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ If you'd like to read/write the contact's notes, call the `iosEnableNotesUsage(t
* `requestPermission()`: Promise<string> - request permission to access Contacts _ios only_
* `writePhotoToPath()` - writes the contact photo to a given path _android only_

### ios group specific functions
* `getGroups()`: Promise - returns an array of all groups. Each group contains `{ identifier: string; name: string;}`
* `getGroup: (identifier: string)`: Promise - returns the group matching the provided group identifier.
* `deleteGroup(identifier: string)`: Promise - deletes a group by group identifier.
* `updateGroup(identifier: string, groupData: Pick<Group, 'name'>`: Promise - updates an existing group's details. You can only change the group name.
* `addGroup(group: Pick<Group, 'name'>)`: Promise - adds a new group. Group name should be provided.
* `contactsInGroup(identifier: string)`: Promise - retrieves all contacts within a specified group.
* `addContactsToGroup(groupIdentifier: string, contactIdentifiers: string[])`: Promise - adds contacts to a group. Only contacts with id that has `:ABperson` as suffix can be added.
* `removeContactsFromGroup(groupIdentifier: string, contactIdentifiers: string[])`: Promise - removes specified contacts from a group.

## Example Contact Record
```js
{
Expand Down
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ export function requestPermission(): Promise<'authorized' | 'denied' | 'undefine
export function writePhotoToPath(contactId: string, file: string): Promise<boolean>;
export function iosEnableNotesUsage(enabled: boolean): void;

export function getGroups(): Promise<Group[]>;
export function getGroup(identifier: string): Promise<Group | null>;
export function deleteGroup(identifier: string): Promise<boolean>;
export function updateGroup(identifier: string, groupData: Pick<Group, 'name'>): Promise<Group>;
export function addGroup(group: Pick<Group, 'name'>): Promise<Group>;
export function contactsInGroup(identifier: string): Promise<Contact[]>;
export function addContactsToGroup(groupIdentifier: string, contactIdentifiers: string[]): Promise<boolean>;
export function removeContactsFromGroup(groupIdentifier: string, contactIdentifiers: string[]): Promise<boolean>;
export interface Group {
identifier: string;
name: string;
}
export interface EmailAddress {
label: string;
email: string;
Expand Down
35 changes: 34 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NativeModules } from "react-native";
import NativeContacts from "./src/NativeContacts";
import { Contact, PermissionType } from "./type";
import { Contact, Group, PermissionType } from "./type";

const isTurboModuleEnabled = global.__turboModuleProxy != null;
const Contacts = isTurboModuleEnabled ? NativeContacts : NativeModules.Contacts;
Expand Down Expand Up @@ -87,6 +87,31 @@ async function writePhotoToPath(
): Promise<boolean> {
return Contacts.writePhotoToPath(contactId, file);
}

async function getGroups(): Promise<Group[]> {
return Contacts.getGroups();
}
async function getGroup(identifier: string): Promise<Group | null> {
return Contacts.getGroup(identifier);
}
async function deleteGroup(identifier: string): Promise<boolean> {
return Contacts.deleteGroup(identifier);
}
async function updateGroup(identifier: string, groupData: Pick<Group, 'name'>): Promise<Group> {
return Contacts.updateGroup(identifier, groupData);
}
async function addGroup(group: Pick<Group, 'name'>): Promise<Group>{
return Contacts.addGroup(group);
}
async function contactsInGroup(identifier: string): Promise<Contact[]> {
return Contacts.contactsInGroup(identifier);
}
async function addContactsToGroup(groupIdentifier: string, contactIdentifiers: string[]): Promise<boolean> {
return Contacts.addContactsToGroup(groupIdentifier, contactIdentifiers);
}
async function removeContactsFromGroup(groupIdentifier: string, contactIdentifiers: string[]): Promise<boolean> {
return Contacts.removeContactsFromGroup(groupIdentifier, contactIdentifiers);
}
export default {
getAll,
getAllWithoutPhotos,
Expand All @@ -106,4 +131,12 @@ export default {
checkPermission,
requestPermission,
writePhotoToPath,
getGroups,
getGroup,
deleteGroup,
updateGroup,
addGroup,
contactsInGroup,
addContactsToGroup,
removeContactsFromGroup
};
Loading

0 comments on commit 1d5eae7

Please sign in to comment.