AddContactImageSheet.swift 10 KB

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