okay place within the development of the contacts feature
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Contacts
|
||||
import ContactsUI
|
||||
|
||||
struct ContactsView: View {
|
||||
var testUsers:[TestUser] = [
|
||||
@@ -16,16 +18,23 @@ struct ContactsView: View {
|
||||
TestUser(_profilePic: "rohan", _firstN: "Rohan", _lastN: "Chadha")
|
||||
]
|
||||
|
||||
@ObservedObject var contactsViewModel = ContactsViewModel()
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
ForEach(testUsers) { user in
|
||||
let fullName = "\(user.firstName) \(user.lastName)"
|
||||
IndividualContactView(name: fullName, imageName: user.profilePictureUrl)
|
||||
}
|
||||
VStack(spacing: 0) {
|
||||
OnboardingTemplateView(imgName: "undraw_grades_re_j7d6", mainLeadingActText: "Add your ", mainHighlightedActText: "closest folks.", mainTrailingActText: "", subActText: "besties, bf or gf, siblings, parents, etc.")
|
||||
|
||||
Rectangle()
|
||||
.strokeBorder(style: StrokeStyle(lineWidth: 4, dash: [10]))
|
||||
|
||||
}
|
||||
.background(NirvanaColor.bgLightGrey)
|
||||
.alert(item: $contactsViewModel.permissionsError) {_ in
|
||||
Alert(title: Text("Permissions Needed"),
|
||||
message: Text(contactsViewModel.permissionsError?.description ?? "unknown error"),
|
||||
dismissButton: .default(Text("Ok"), action: contactsViewModel.openSettings))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
struct ContactsView_Previews: PreviewProvider {
|
||||
@@ -40,7 +49,7 @@ private struct IndividualContactView: View {
|
||||
|
||||
@Namespace private var animation
|
||||
@State var isAddedUser = false
|
||||
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .center, spacing: 0) {
|
||||
Image(imageName)
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// ContactsViewModel.swift
|
||||
// nirvana-ios
|
||||
//
|
||||
// Created by Arjun Patel on 12/15/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Contacts
|
||||
import SwiftUI
|
||||
|
||||
final public class ContactsViewModel : ObservableObject {
|
||||
@Published var contacts : [Contact] = []
|
||||
@Published var permissionsError: PermissionsError? = .none
|
||||
|
||||
init() {
|
||||
self.permissions()
|
||||
}
|
||||
|
||||
func openSettings() {
|
||||
self.permissionsError = .none
|
||||
guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else { return }
|
||||
if UIApplication.shared.canOpenURL(settingsURL) { UIApplication.shared.open(settingsURL)}
|
||||
}
|
||||
|
||||
private func fetchContacts() {
|
||||
//remove all current contacts from vm
|
||||
contacts.removeAll()
|
||||
|
||||
let store = CNContactStore()
|
||||
|
||||
do {
|
||||
let predicate = CNContact.predicateForContacts(matchingName: "sarth")
|
||||
//specific contact details: ex: name, email, etc.
|
||||
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey] as [CNKeyDescriptor]
|
||||
|
||||
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch)
|
||||
|
||||
print("Fetched contacts: \(contacts)")
|
||||
} catch {
|
||||
print("Failed to fetch contacts, error: \(error)")
|
||||
// Handle the error
|
||||
self.permissionsError = .fetchError(error)
|
||||
}
|
||||
}
|
||||
|
||||
private func permissions() {
|
||||
switch CNContactStore.authorizationStatus(for: .contacts) {
|
||||
case.authorized:
|
||||
print("fetching contacts")
|
||||
fetchContacts()
|
||||
case .notDetermined, .restricted, .denied:
|
||||
CNContactStore().requestAccess(for: .contacts) {granted, error in
|
||||
switch granted {
|
||||
case true:
|
||||
self.fetchContacts()
|
||||
case false:
|
||||
DispatchQueue.main.async {
|
||||
print("unable to get contacts")
|
||||
self.permissionsError = .userError
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
fatalError("Unknown Error!")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user