AddContactTrickSheet.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import SwiftUI
  2. struct AddContactTrickSheet: View {
  3. @Environment(\.dismiss) var dismiss
  4. @Environment(\.colorScheme) var colorScheme
  5. @Environment(AppState.self) var appState
  6. @ObservedObject var state: ContactTrick.StateModel
  7. @State private var hasHighContrast: Bool = true
  8. @State private var ringWidth: ContactTrickEntry.RingWidth = .regular
  9. @State private var ringGap: ContactTrickEntry.RingGap = .small
  10. @State private var layout: ContactTrickLayout = .default
  11. @State private var primary: ContactTrickValue = .glucose
  12. @State private var top: ContactTrickValue = .none
  13. @State private var bottom: ContactTrickValue = .trend
  14. @State private var ring: ContactTrickLargeRing = .none
  15. @State private var fontSize: ContactTrickEntry.FontSize = .regular
  16. @State private var secondaryFontSize: ContactTrickEntry.FontSize = .small
  17. @State private var fontWeight: Font.Weight = .medium
  18. @State private var fontWidth: Font.Width = .standard
  19. private var previewEntry: ContactTrickEntry {
  20. ContactTrickEntry(
  21. id: UUID(),
  22. name: "", // automatically set and populated
  23. layout: layout,
  24. ring: ring,
  25. primary: primary,
  26. top: top,
  27. bottom: bottom,
  28. contactId: nil, // not needed for preview, gets set later in ContactTrickStateModel via ContactTrickManager
  29. hasHighContrast: hasHighContrast,
  30. ringWidth: ringWidth,
  31. ringGap: ringGap,
  32. fontSize: fontSize,
  33. secondaryFontSize: secondaryFontSize,
  34. fontWeight: fontWeight,
  35. fontWidth: fontWidth
  36. )
  37. }
  38. var body: some View {
  39. NavigationView {
  40. VStack {
  41. // Preview Section
  42. HStack {
  43. Spacer()
  44. ZStack {
  45. Circle()
  46. .fill(previewEntry.hasHighContrast ? .black : .white)
  47. .foregroundColor(.white)
  48. .frame(width: 100, height: 100)
  49. Image(uiImage: ContactPicture.getImage(contact: previewEntry, state: state.state))
  50. .resizable()
  51. .frame(width: 100, height: 100)
  52. .clipShape(Circle())
  53. Circle()
  54. .stroke(lineWidth: 2)
  55. .foregroundColor(.white)
  56. .frame(width: 100, height: 100)
  57. }
  58. Spacer()
  59. }
  60. .padding(.top, 40)
  61. .padding(.bottom)
  62. Form {
  63. // Layout Section
  64. Section(header: Text("Style")) {
  65. Picker("Layout", selection: $layout) {
  66. ForEach(ContactTrickLayout.allCases, id: \.id) { layout in
  67. Text(layout.displayName).tag(layout)
  68. }
  69. }.onChange(of: layout, { oldLayout, newLayout in
  70. if oldLayout != newLayout, newLayout == .split {
  71. top = .glucose
  72. } else {
  73. top = .none
  74. }
  75. })
  76. Toggle("High Contrast Mode", isOn: $hasHighContrast)
  77. }.listRowBackground(Color.chart)
  78. // Primary Value Section
  79. Section(header: Text("Display Values")) {
  80. Picker("Top Value", selection: $top) {
  81. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  82. Text(value.displayName).tag(value)
  83. }
  84. }
  85. if layout == .default {
  86. Picker("Primary", selection: $primary) {
  87. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  88. Text(value.displayName).tag(value)
  89. }
  90. }
  91. }
  92. Picker("Bottom Value", selection: $bottom) {
  93. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  94. Text(value.displayName).tag(value)
  95. }
  96. }
  97. }.listRowBackground(Color.chart)
  98. // Ring Settings Section
  99. Section(header: Text("Ring Settings")) {
  100. Picker("Ring Type", selection: $ring) {
  101. ForEach(ContactTrickLargeRing.allCases, id: \.self) { ring in
  102. Text(ring.displayName).tag(ring)
  103. }
  104. }
  105. if ring != .none {
  106. Picker("Ring Width", selection: $ringWidth) {
  107. ForEach(ContactTrickEntry.RingWidth.allCases, id: \.self) { width in
  108. Text(width.displayName).tag(width)
  109. }
  110. }
  111. Picker("Ring Gap", selection: $ringGap) {
  112. ForEach(ContactTrickEntry.RingGap.allCases, id: \.self) { gap in
  113. Text(gap.displayName).tag(gap)
  114. }
  115. }
  116. }
  117. }.listRowBackground(Color.chart)
  118. // Font Settings Section
  119. Section(header: Text("Font Settings")) {
  120. fontSizePicker
  121. if layout == .split {
  122. secondaryFontSizePicker
  123. }
  124. fontWeightPicker
  125. fontWidthPicker
  126. }.listRowBackground(Color.chart)
  127. }
  128. stickySaveButton
  129. }
  130. .navigationTitle("Add Contact Items")
  131. .navigationBarTitleDisplayMode(.inline)
  132. .listSectionSpacing(10)
  133. .padding(.top, 30)
  134. .ignoresSafeArea(edges: .top)
  135. .scrollContentBackground(.hidden)
  136. .background(appState.trioBackgroundColor(for: colorScheme))
  137. .toolbar {
  138. ToolbarItem(placement: .topBarLeading) {
  139. Button("Cancel") {
  140. dismiss()
  141. }
  142. }
  143. ToolbarItem(placement: .topBarTrailing) {
  144. Button(
  145. action: {
  146. state.isHelpSheetPresented.toggle()
  147. },
  148. label: {
  149. Image(systemName: "questionmark.circle")
  150. }
  151. )
  152. }
  153. }
  154. .sheet(isPresented: $state.isHelpSheetPresented) {
  155. NavigationStack {
  156. List {
  157. Text("Lorem Ipsum Dolor Sit Amet")
  158. }
  159. .padding(.trailing, 10)
  160. .navigationBarTitle("Help", displayMode: .inline)
  161. Button { state.isHelpSheetPresented.toggle() }
  162. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  163. .buttonStyle(.bordered)
  164. .padding(.top)
  165. }
  166. .padding()
  167. .presentationDetents(
  168. [.fraction(0.9), .large],
  169. selection: $state.helpSheetDetent
  170. )
  171. }
  172. }
  173. }
  174. var stickySaveButton: some View {
  175. ZStack {
  176. Rectangle()
  177. .frame(width: UIScreen.main.bounds.width, height: 65)
  178. .foregroundStyle(colorScheme == .dark ? Color.bgDarkerDarkBlue : Color.white)
  179. .background(.thinMaterial)
  180. .opacity(0.8)
  181. .clipShape(Rectangle())
  182. Button(action: {
  183. saveNewEntry()
  184. }, label: {
  185. Text("Save").padding(10)
  186. })
  187. .frame(width: UIScreen.main.bounds.width * 0.9, alignment: .center)
  188. .background(Color(.systemBlue))
  189. .tint(.white)
  190. .clipShape(RoundedRectangle(cornerRadius: 8))
  191. .padding(5)
  192. }
  193. }
  194. private var fontSizePicker: some View {
  195. Picker("Font Size", selection: $fontSize) {
  196. ForEach(ContactTrickEntry.FontSize.allCases, id: \.self) { size in
  197. Text(size.displayName).tag(size)
  198. }
  199. }
  200. }
  201. private var secondaryFontSizePicker: some View {
  202. Picker("Secondary Font Size", selection: $secondaryFontSize) {
  203. ForEach(ContactTrickEntry.FontSize.allCases, id: \.self) { size in
  204. Text(size.displayName).tag(size)
  205. }
  206. }
  207. }
  208. private var fontWeightPicker: some View {
  209. Picker("Font Weight", selection: $fontWeight) {
  210. ForEach(
  211. [Font.Weight.light, Font.Weight.regular, Font.Weight.medium, Font.Weight.bold, Font.Weight.black],
  212. id: \.self
  213. ) { weight in
  214. Text("\(weight.displayName)".capitalized).tag(weight)
  215. }
  216. }
  217. }
  218. private var fontWidthPicker: some View {
  219. Picker("Font Width", selection: $fontWidth) {
  220. ForEach(
  221. [Font.Width.standard, Font.Width.expanded],
  222. id: \.self
  223. ) { width in
  224. Text("\(width.displayName)".capitalized).tag(width)
  225. }
  226. }
  227. }
  228. private func saveNewEntry() {
  229. // Save the currently previewed entry
  230. Task {
  231. await state.createAndSaveContactTrick(entry: previewEntry, name: "Trio \(state.contactTrickEntries.count + 1)")
  232. dismiss()
  233. }
  234. }
  235. }