AddContactTrickSheet.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import SwiftUI
  2. struct AddContactTrickSheet: View {
  3. @Environment(\.dismiss) var dismiss
  4. var state: ContactTrick.StateModel
  5. @State private var name: String = ""
  6. @State private var isDarkMode: Bool = false
  7. @State private var ringWidth: ContactTrickEntry.RingWidth = .regular
  8. @State private var ringGap: ContactTrickEntry.RingGap = .small
  9. @State private var layout: ContactTrickLayout = .single
  10. @State private var primary: ContactTrickValue = .glucose
  11. @State private var top: ContactTrickValue = .none
  12. @State private var bottom: ContactTrickValue = .none
  13. @State private var ring: ContactTrickLargeRing = .none
  14. @State private var fontSize: ContactTrickEntry.FontSize = .regular
  15. @State private var secondaryFontSize: ContactTrickEntry.FontSize = .small
  16. @State private var fontWeight: Font.Weight = .medium
  17. @State private var fontWidth: Font.Width = .standard
  18. private var previewEntry: ContactTrickEntry {
  19. ContactTrickEntry(
  20. id: UUID(),
  21. name: name,
  22. layout: layout,
  23. ring: ring,
  24. primary: primary,
  25. top: top,
  26. bottom: bottom,
  27. contactId: nil, // not needed for preview, gets set later in ContactTrickStateModel via ContactTrickManager
  28. darkMode: isDarkMode,
  29. ringWidth: ringWidth,
  30. ringGap: ringGap,
  31. fontSize: fontSize,
  32. secondaryFontSize: secondaryFontSize,
  33. fontWeight: fontWeight,
  34. fontWidth: fontWidth
  35. )
  36. }
  37. var body: some View {
  38. NavigationView {
  39. Form {
  40. // TODO: - make this beautiful @Dan
  41. // Preview Section
  42. Section {
  43. HStack {
  44. Spacer()
  45. Image(uiImage: ContactPicture.getImage(contact: previewEntry, state: state.previewState))
  46. .resizable()
  47. .aspectRatio(1, contentMode: .fit)
  48. .frame(width: 100, height: 100)
  49. .clipShape(Circle())
  50. .overlay(Circle().stroke(Color.gray, lineWidth: 1))
  51. Spacer()
  52. }
  53. }
  54. // Name Section
  55. TextField("Name", text: $name)
  56. // Layout Section
  57. Section(header: Text("Layout")) {
  58. Toggle("Dark Mode", isOn: $isDarkMode)
  59. Picker("Layout", selection: $layout) {
  60. ForEach(ContactTrickLayout.allCases, id: \.id) { layout in
  61. Text(layout.displayName).tag(layout)
  62. }
  63. }
  64. .pickerStyle(SegmentedPickerStyle())
  65. }
  66. // Primary Value Section
  67. Section(header: Text("Primary Value")) {
  68. Picker("Primary", selection: $primary) {
  69. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  70. Text(value.displayName).tag(value)
  71. }
  72. }
  73. }
  74. // Additional Values Section
  75. Section(header: Text("Additional Values")) {
  76. Picker("Top Value", selection: $top) {
  77. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  78. Text(value.displayName).tag(value)
  79. }
  80. }
  81. Picker("Bottom Value", selection: $bottom) {
  82. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  83. Text(value.displayName).tag(value)
  84. }
  85. }
  86. }
  87. // Ring Settings Section
  88. Section(header: Text("Ring Settings")) {
  89. Picker("Ring Type", selection: $ring) {
  90. ForEach(ContactTrickLargeRing.allCases, id: \.self) { ring in
  91. Text(ring.displayName).tag(ring)
  92. }
  93. }
  94. Picker("Ring Width", selection: $ringWidth) {
  95. ForEach(ContactTrickEntry.RingWidth.allCases, id: \.self) { width in
  96. Text(width.displayName).tag(width)
  97. }
  98. }
  99. Picker("Ring Gap", selection: $ringGap) {
  100. ForEach(ContactTrickEntry.RingGap.allCases, id: \.self) { gap in
  101. Text(gap.displayName).tag(gap)
  102. }
  103. }
  104. }
  105. // Font Settings Section
  106. Section(header: Text("Font Settings")) {
  107. fontSizePicker
  108. secondaryFontSizePicker
  109. fontWeightPicker
  110. fontWidthPicker
  111. }
  112. }
  113. .navigationBarTitle("Add Contact Trick", displayMode: .inline)
  114. .navigationBarItems(
  115. leading: Button("Cancel") {
  116. dismiss()
  117. },
  118. trailing: Button("Save") {
  119. saveNewEntry()
  120. }
  121. )
  122. }
  123. }
  124. private var fontSizePicker: some View {
  125. Picker("Font Size", selection: $fontSize) {
  126. ForEach(ContactTrickEntry.FontSize.allCases, id: \.self) { size in
  127. Text(size.displayName).tag(size)
  128. }
  129. }
  130. }
  131. private var secondaryFontSizePicker: some View {
  132. Picker("Secondary Font Size", selection: $secondaryFontSize) {
  133. ForEach(ContactTrickEntry.FontSize.allCases, id: \.self) { size in
  134. Text(size.displayName).tag(size)
  135. }
  136. }
  137. }
  138. private var fontWeightPicker: some View {
  139. Picker("Font Weight", selection: $fontWeight) {
  140. ForEach(
  141. [Font.Weight.light, Font.Weight.regular, Font.Weight.medium, Font.Weight.bold, Font.Weight.black],
  142. id: \.self
  143. ) { weight in
  144. Text("\(weight)".capitalized).tag(weight)
  145. }
  146. }
  147. }
  148. private var fontWidthPicker: some View {
  149. Picker("Font Width", selection: $fontWidth) {
  150. ForEach(
  151. [Font.Width.standard, Font.Width.condensed, Font.Width.expanded],
  152. id: \.self
  153. ) { width in
  154. Text("\(width)".capitalized).tag(width)
  155. }
  156. }
  157. }
  158. private func saveNewEntry() {
  159. // Save the currently previewed entry
  160. Task {
  161. await state.createAndSaveContactTrick(entry: previewEntry, name: name)
  162. dismiss()
  163. }
  164. }
  165. }