ContactTrickRootView.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import Contacts
  2. import ContactsUI
  3. import SwiftUI
  4. import Swinject
  5. extension ContactTrick {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @State var state = StateModel()
  9. @State private var isAddSheetPresented = false
  10. var body: some View {
  11. NavigationView {
  12. contactTrickList
  13. .navigationTitle("Contact Tricks")
  14. .onAppear(perform: configureView)
  15. .navigationBarItems(
  16. trailing: Button(action: {
  17. isAddSheetPresented.toggle()
  18. }) {
  19. Image(systemName: "plus")
  20. }
  21. )
  22. .sheet(isPresented: $isAddSheetPresented) {
  23. AddContactTrickSheet(state: state)
  24. }
  25. }
  26. }
  27. private var contactTrickList: some View {
  28. List {
  29. ForEach(state.contactTrickEntries, id: \.id) { entry in
  30. NavigationLink(destination: ContactTrickDetailView(entry: entry, state: state)) {
  31. HStack {
  32. // TODO: - make this beautiful @Dan
  33. ZStack {
  34. Circle()
  35. .fill(entry.darkMode ? .black : .white)
  36. .foregroundColor(.white)
  37. .frame(width: 40, height: 40)
  38. Image(uiImage: ContactPicture.getImage(contact: entry, state: state.previewState))
  39. .resizable()
  40. .frame(width: 40, height: 40)
  41. .clipShape(Circle())
  42. Circle()
  43. .stroke(lineWidth: 2)
  44. .foregroundColor(.white)
  45. .frame(width: 40, height: 40)
  46. }
  47. // Entry name
  48. Text("\(entry.name)")
  49. }
  50. }
  51. }
  52. .onDelete(perform: onDelete)
  53. }
  54. }
  55. private func onDelete(offsets: IndexSet) {
  56. Task {
  57. for offset in offsets {
  58. let entry = state.contactTrickEntries[offset]
  59. await state.deleteContact(entry: entry)
  60. }
  61. }
  62. }
  63. }
  64. }