AddContactTrickSheet.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 isDarkMode: Bool = false
  8. @State private var ringWidth: ContactTrickEntry.RingWidth = .regular
  9. @State private var ringGap: ContactTrickEntry.RingGap = .small
  10. @State private var layout: ContactTrickLayout = .single
  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. darkMode: isDarkMode,
  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.darkMode ? .black : .white)
  47. .foregroundColor(.white)
  48. .frame(width: 100, height: 100)
  49. Image(uiImage: ContactPicture.getImage(contact: previewEntry, state: state.previewState))
  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. }
  70. Toggle("Dark Mode", isOn: $isDarkMode)
  71. }.listRowBackground(Color.chart)
  72. // Primary Value Section
  73. Section(header: Text("Display Values")) {
  74. if layout == .single {
  75. Picker("Primary", selection: $primary) {
  76. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  77. Text(value.displayName).tag(value)
  78. }
  79. }
  80. }
  81. Picker("Top Value", selection: $top) {
  82. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  83. Text(value.displayName).tag(value)
  84. }
  85. }
  86. Picker("Bottom Value", selection: $bottom) {
  87. ForEach(ContactTrickValue.allCases, id: \.id) { value in
  88. Text(value.displayName).tag(value)
  89. }
  90. }
  91. }.listRowBackground(Color.chart)
  92. // Ring Settings Section
  93. Section(header: Text("Ring Settings")) {
  94. Picker("Ring Type", selection: $ring) {
  95. ForEach(ContactTrickLargeRing.allCases, id: \.self) { ring in
  96. Text(ring.displayName).tag(ring)
  97. }
  98. }
  99. if ring != .none {
  100. Picker("Ring Width", selection: $ringWidth) {
  101. ForEach(ContactTrickEntry.RingWidth.allCases, id: \.self) { width in
  102. Text(width.displayName).tag(width)
  103. }
  104. }
  105. Picker("Ring Gap", selection: $ringGap) {
  106. ForEach(ContactTrickEntry.RingGap.allCases, id: \.self) { gap in
  107. Text(gap.displayName).tag(gap)
  108. }
  109. }
  110. }
  111. }.listRowBackground(Color.chart)
  112. // Font Settings Section
  113. Section(header: Text("Font Settings")) {
  114. fontSizePicker
  115. secondaryFontSizePicker
  116. fontWeightPicker
  117. fontWidthPicker
  118. }.listRowBackground(Color.chart)
  119. }
  120. stickySaveButton
  121. }
  122. .navigationTitle("Add Contact Items")
  123. .navigationBarTitleDisplayMode(.inline)
  124. .listSectionSpacing(10)
  125. .padding(.top, 30)
  126. .ignoresSafeArea(edges: .top)
  127. .scrollContentBackground(.hidden)
  128. .background(appState.trioBackgroundColor(for: colorScheme))
  129. .toolbar {
  130. ToolbarItem(placement: .topBarLeading) {
  131. Button("Cancel") {
  132. dismiss()
  133. }
  134. }
  135. ToolbarItem(placement: .topBarTrailing) {
  136. Button(
  137. action: {
  138. state.isHelpSheetPresented.toggle()
  139. },
  140. label: {
  141. Image(systemName: "questionmark.circle")
  142. }
  143. )
  144. }
  145. }
  146. .sheet(isPresented: $state.isHelpSheetPresented) {
  147. NavigationStack {
  148. List {
  149. Text("Lorem Ipsum Dolor Sit Amet")
  150. }
  151. .padding(.trailing, 10)
  152. .navigationBarTitle("Help", displayMode: .inline)
  153. Button { state.isHelpSheetPresented.toggle() }
  154. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  155. .buttonStyle(.bordered)
  156. .padding(.top)
  157. }
  158. .padding()
  159. .presentationDetents(
  160. [.fraction(0.9), .large],
  161. selection: $state.helpSheetDetent
  162. )
  163. }
  164. }
  165. }
  166. var stickySaveButton: some View {
  167. ZStack {
  168. Rectangle()
  169. .frame(width: UIScreen.main.bounds.width, height: 65)
  170. .foregroundStyle(colorScheme == .dark ? Color.bgDarkerDarkBlue : Color.white)
  171. .background(.thinMaterial)
  172. .opacity(0.8)
  173. .clipShape(Rectangle())
  174. Button(action: {
  175. saveNewEntry()
  176. }, label: {
  177. Text("Save").padding(10)
  178. })
  179. .frame(width: UIScreen.main.bounds.width * 0.9, alignment: .center)
  180. .background(Color(.systemBlue))
  181. .tint(.white)
  182. .clipShape(RoundedRectangle(cornerRadius: 8))
  183. .padding(5)
  184. }
  185. }
  186. private var fontSizePicker: some View {
  187. Picker("Font Size", selection: $fontSize) {
  188. ForEach(ContactTrickEntry.FontSize.allCases, id: \.self) { size in
  189. Text(size.displayName).tag(size)
  190. }
  191. }
  192. }
  193. private var secondaryFontSizePicker: some View {
  194. Picker("Secondary Font Size", selection: $secondaryFontSize) {
  195. ForEach(ContactTrickEntry.FontSize.allCases, id: \.self) { size in
  196. Text(size.displayName).tag(size)
  197. }
  198. }
  199. }
  200. private var fontWeightPicker: some View {
  201. Picker("Font Weight", selection: $fontWeight) {
  202. ForEach(
  203. [Font.Weight.light, Font.Weight.regular, Font.Weight.medium, Font.Weight.bold, Font.Weight.black],
  204. id: \.self
  205. ) { weight in
  206. Text("\(weight.displayName)".capitalized).tag(weight)
  207. }
  208. }
  209. }
  210. private var fontWidthPicker: some View {
  211. Picker("Font Width", selection: $fontWidth) {
  212. ForEach(
  213. [Font.Width.standard, Font.Width.condensed, Font.Width.expanded],
  214. id: \.self
  215. ) { width in
  216. Text("\(width.displayName)".capitalized).tag(width)
  217. }
  218. }
  219. }
  220. private func saveNewEntry() {
  221. // Save the currently previewed entry
  222. Task {
  223. await state.createAndSaveContactTrick(entry: previewEntry, name: "Trio \(state.contactTrickEntries.count + 1)")
  224. dismiss()
  225. }
  226. }
  227. }