From e2f8d9d1e28a6a32dfd3b4284747f1e55dce62b7 Mon Sep 17 00:00:00 2001 From: talksik Date: Mon, 20 Dec 2021 04:27:18 -0800 Subject: [PATCH] nice sheet for adding contacts --- .../Views/Contacts/ContactsPickerView.swift | 18 +++++++------ nirvana-ios/Views/Contacts/ContactsView.swift | 2 +- .../Views/Contacts/ContactsViewModel.swift | 25 ++++++++----------- .../Views/InnerCircle/CircleGridView.swift | 2 +- .../InnerCircle/CircleNavigationView.swift | 7 ++++-- .../Views/InnerCircle/InnerCircleView.swift | 18 ++++++++++++- 6 files changed, 45 insertions(+), 27 deletions(-) diff --git a/nirvana-ios/Views/Contacts/ContactsPickerView.swift b/nirvana-ios/Views/Contacts/ContactsPickerView.swift index bb89b93..07af937 100644 --- a/nirvana-ios/Views/Contacts/ContactsPickerView.swift +++ b/nirvana-ios/Views/Contacts/ContactsPickerView.swift @@ -8,10 +8,11 @@ import SwiftUI import Contacts -struct ContactPickerView: View { - @Binding var showPicker: Bool +struct ContactsPickerView: View { + @Environment(\.dismiss) var dismiss + // updates the parent view for it to do something with the new contact - @Binding var selectedContact: CNContact? + @State var selectedContact: CNContact? var contacts: [String: [CNContact]] = loadContactsGrouped() @@ -22,14 +23,17 @@ struct ContactPickerView: View { ForEach(keys.sorted(), id: \.self) { key in Section(header: Text(key)) { ForEach(contacts[key] ?? [CNContact](), id: \.identifier) { contact in - ContactRow(contact: contact, showPicker: self.$showPicker, selectedContact: self.$selectedContact) + ContactRow(contact: contact, selectedContact: self.$selectedContact) } } } } .listStyle(GroupedListStyle()) .navigationTitle("All Contacts".localized) - .navigationBarItems(trailing: Button(action: {self.showPicker = false}, label: { + .navigationBarItems(trailing: Button(action: { + dismiss() + + }, label: { Text("Cancel".localized) })) } @@ -43,6 +47,8 @@ struct ContactPickerView: View { return String(contact.familyName.first ?? "?") } + print(group) + return group } } @@ -50,7 +56,6 @@ struct ContactPickerView: View { struct ContactRow: View { var contact: CNContact - @Binding var showPicker: Bool @Binding var selectedContact: CNContact? var body: some View { @@ -67,7 +72,6 @@ struct ContactRow: View { func selectContact() { self.selectedContact = self.contact - self.showPicker = false } } diff --git a/nirvana-ios/Views/Contacts/ContactsView.swift b/nirvana-ios/Views/Contacts/ContactsView.swift index f376d14..b704ba1 100644 --- a/nirvana-ios/Views/Contacts/ContactsView.swift +++ b/nirvana-ios/Views/Contacts/ContactsView.swift @@ -53,7 +53,7 @@ struct ContactsView: View { Spacer() } .sheet(isPresented: self.$showPicker) { - ContactPickerView(showPicker: self.$showPicker, selectedContact: self.$selectedContact) +// ContactPickerView(showPicker: self.$showPicker, selectedContact: self.$selectedContact) } .padding() .background(NirvanaColor.bgLightGrey) diff --git a/nirvana-ios/Views/Contacts/ContactsViewModel.swift b/nirvana-ios/Views/Contacts/ContactsViewModel.swift index f792014..2873d21 100644 --- a/nirvana-ios/Views/Contacts/ContactsViewModel.swift +++ b/nirvana-ios/Views/Contacts/ContactsViewModel.swift @@ -12,17 +12,11 @@ import SwiftUI class ContactsViewModel : ObservableObject { @Published var showPermissionAlert = false - let store: CNContactStore - let keys: [CNKeyDescriptor] - - init() { - store = CNContactStore() - - keys = [CNContactImageDataKey as CNKeyDescriptor, - CNContactPhoneNumbersKey as CNKeyDescriptor, - CNContactEmailAddressesKey as CNKeyDescriptor, - CNContactFormatter.descriptorForRequiredKeys(for: .fullName)] - } + let store: CNContactStore = CNContactStore() + let keys: [CNKeyDescriptor] = [CNContactImageDataKey as CNKeyDescriptor, + CNContactPhoneNumbersKey as CNKeyDescriptor, + CNContactEmailAddressesKey as CNKeyDescriptor, + CNContactFormatter.descriptorForRequiredKeys(for: .fullName)] func openSettings() { guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else { return } @@ -30,19 +24,20 @@ class ContactsViewModel : ObservableObject { } func requestAccess() { - store.requestAccess(for: .contacts) { - (granted, error) in + + store.requestAccess(for: .contacts) {(granted, error) in + // TODO: do something useful with completionHandler return } - // TODO do something useful with completionHandler } func fetchContacts(sortOrder: CNContactSortOrder = .userDefault) -> [CNContact] { let authStatus = CNContactStore.authorizationStatus(for: .contacts) + if authStatus == .notDetermined { requestAccess() } else if authStatus == .restricted { - // TODO prompt user that app is useless without access to contacts + // TODO: prompt user that app is useless without access to contacts self.showPermissionAlert = true } diff --git a/nirvana-ios/Views/InnerCircle/CircleGridView.swift b/nirvana-ios/Views/InnerCircle/CircleGridView.swift index c51751b..96ffa5b 100644 --- a/nirvana-ios/Views/InnerCircle/CircleGridView.swift +++ b/nirvana-ios/Views/InnerCircle/CircleGridView.swift @@ -16,7 +16,7 @@ struct CircleGridView: View { // magic variables for grid // TODO: make the top left person or one horizontal person be in the center of screen...makes the top honeycomb pop - private static var numberOfItems: Int = 59 + private static var numberOfItems: Int = 12 private static let size: CGFloat = UIScreen.main.bounds.height*0.15 // scaling with screen size private static let spacingBetweenColumns: CGFloat = 0 private static let spacingBetweenRows: CGFloat = 0 diff --git a/nirvana-ios/Views/InnerCircle/CircleNavigationView.swift b/nirvana-ios/Views/InnerCircle/CircleNavigationView.swift index dfdc447..7c96176 100644 --- a/nirvana-ios/Views/InnerCircle/CircleNavigationView.swift +++ b/nirvana-ios/Views/InnerCircle/CircleNavigationView.swift @@ -12,6 +12,8 @@ struct CircleNavigationView: View { @EnvironmentObject var navigationStack: NavigationStack @EnvironmentObject var authSessionStore: AuthSessionStore + @Binding var sheetView: SheetView? + var body: some View { HStack(alignment: .center) { Menu { @@ -23,7 +25,8 @@ struct CircleNavigationView: View { } Button { - self.navigationStack.push(ContactsView()) + print("going to show contacts now") + self.sheetView = .contacts } label: { Label("add peeps", systemImage: "person.2.wave.2") .foregroundColor(NirvanaColor.teal) @@ -141,6 +144,6 @@ struct CircleNavigationView: View { struct CircleNavigationView_Previews: PreviewProvider { static var previews: some View { - CircleNavigationView().environmentObject(AuthSessionStore(isPreview: true)) + CircleNavigationView(sheetView: .constant(SheetView.contacts)).environmentObject(AuthSessionStore(isPreview: true)) } } diff --git a/nirvana-ios/Views/InnerCircle/InnerCircleView.swift b/nirvana-ios/Views/InnerCircle/InnerCircleView.swift index e99c19b..7e3f3c7 100644 --- a/nirvana-ios/Views/InnerCircle/InnerCircleView.swift +++ b/nirvana-ios/Views/InnerCircle/InnerCircleView.swift @@ -8,10 +8,18 @@ import SwiftUI import NavigationStack +enum SheetView : Identifiable { + var id: Self { self } + case contacts + case inbox +} + struct InnerCircleView: View { @EnvironmentObject var authSessionStore: AuthSessionStore @EnvironmentObject var navigationStack: NavigationStack + @State var sheetView: SheetView? = nil + let universalSize = UIScreen.main.bounds var body: some View { @@ -25,13 +33,21 @@ struct InnerCircleView: View { // header VStack(alignment: .leading) { - CircleNavigationView() + CircleNavigationView(sheetView: self.$sheetView) Spacer() } CircleFooterView() } + .sheet(item: self.$sheetView) {page in + switch page { + case SheetView.contacts: + ContactsPickerView() + case SheetView.inbox: + OnboardingTrioView()// test one for now + } + } .onAppear() { } }