ContactTrickRootView.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: Bool = false
  10. @Environment(\.colorScheme) var colorScheme
  11. @Environment(AppState.self) var appState
  12. var body: some View {
  13. Form {
  14. contactTrickList
  15. }
  16. .onAppear(perform: configureView)
  17. .navigationTitle("Contacts Configuration")
  18. .navigationBarTitleDisplayMode(.large)
  19. .scrollContentBackground(.hidden)
  20. .background(appState.trioBackgroundColor(for: colorScheme))
  21. .toolbar {
  22. ToolbarItem(placement: .topBarTrailing) {
  23. Button(action: {
  24. isAddSheetPresented.toggle()
  25. }) {
  26. HStack {
  27. Text("Add Contact")
  28. Image(systemName: "plus")
  29. }
  30. }
  31. }
  32. }
  33. .sheet(isPresented: $isAddSheetPresented) {
  34. AddContactTrickSheet(state: state)
  35. }
  36. }
  37. private var contactTrickList: some View {
  38. List {
  39. if state.contactTrickEntries.isEmpty {
  40. Section(
  41. header: Text(""),
  42. content: {
  43. Text("No Contact Trick Entries.")
  44. }
  45. ).listRowBackground(Color.chart)
  46. } else {
  47. ForEach(state.contactTrickEntries, id: \.id) { entry in
  48. NavigationLink(destination: ContactTrickDetailView(entry: entry, state: state)) {
  49. HStack {
  50. // TODO: - make this beautiful @Dan
  51. ZStack {
  52. Circle()
  53. .fill(entry.darkMode ? .black : .white)
  54. .foregroundColor(.white)
  55. .frame(width: 40, height: 40)
  56. Image(uiImage: ContactPicture.getImage(contact: entry, state: state.previewState))
  57. .resizable()
  58. .frame(width: 40, height: 40)
  59. .clipShape(Circle())
  60. Circle()
  61. .stroke(lineWidth: 2)
  62. .foregroundColor(.white)
  63. .frame(width: 40, height: 40)
  64. }
  65. // Entry name
  66. Text("\(entry.name)")
  67. }
  68. }
  69. }
  70. .onDelete(perform: onDelete)
  71. }
  72. }.listRowBackground(Color.chart)
  73. }
  74. private func onDelete(offsets: IndexSet) {
  75. Task {
  76. for offset in offsets {
  77. let entry = state.contactTrickEntries[offset]
  78. await state.deleteContact(entry: entry)
  79. }
  80. }
  81. }
  82. }
  83. }