AddContactImageSheet.swift 9.8 KB

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