Skip to content

Add get groups for a specific container/account #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions ios/Classes/SwiftFlutterContactsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,43 @@ public enum FlutterContacts {
}
}

static func getGroupsForAccount(accountId: String) -> [[String: Any]] {
let store = CNContactStore()
let groupsContainer = fetchGroupForAccount(store, accountId)

return groupsContainer.map {
Group(fromGroup: $0).toMap()
}
}

private static func fetchGroupForAccount(_ store: CNContactStore, _ accountId: String) -> [CNGroup] {
let containerPredicate = CNContainer.predicateForContainers(withIdentifiers: [accountId])

var cnContainers: [CNContainer] = []
do {
cnContainers = try store.containers(matching: containerPredicate)
} catch {
print("Error fetching containers")
}

if (cnContainers.count == 0) {
return []
}

var container = cnContainers.first!

let groupPredicate = CNGroup.predicateForGroupsInContainer(withIdentifier: container.identifier)

var cnGroups: [CNGroup] = []
do {
cnGroups = try store.groups(matching: groupPredicate)
} catch {
print("Error fetching groups")
}

return cnGroups
}

private static func clearFields(
_ contact: CNMutableContact,
_ includeNotesOnIos13AndAbove: Bool
Expand Down Expand Up @@ -537,6 +574,15 @@ public class SwiftFlutterContactsPlugin: NSObject, FlutterPlugin, FlutterStreamH
))
}
}
case "getGroupsForAccount":
DispatchQueue.global(qos: .userInteractive).async {
let args = call.arguments as! [Any?]
let accountId = args[0] as! String
let groups = FlutterContacts.getGroupsForAccount(accountId: accountId)
result(groups)
}

break;
case "insertGroup":
DispatchQueue.global(qos: .userInteractive).async {
do {
Expand Down
9 changes: 9 additions & 0 deletions lib/flutter_contacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ class FlutterContacts {
return groups;
}

static Future<List<Group>> getGroupsForAccount(String accountId) async {
List untypedGroups = await _channel.invokeMethod('getGroupsForAccount', [accountId]);
// ignore: omit_local_variable_types
List<Group> groups = untypedGroups
.map((x) => Group.fromJson(Map<String, dynamic>.from(x)))
.toList();
return groups;
}

/// Inserts a new group (or label on Android).
static Future<Group> insertGroup(Group group) async {
return Group.fromJson(Map<String, dynamic>.from(
Expand Down