DataTableRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension DataTable {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State private var isRemoveCarbsAlertPresented = false
  9. @State private var removeCarbsAlert: Alert?
  10. @State private var isRemoveInsulinAlertPresented = false
  11. @State private var removeInsulinAlert: Alert?
  12. @State private var newGlucose = false
  13. @State private var showExternalInsulin = false
  14. @State private var isAmountUnconfirmed = true
  15. @Environment(\.colorScheme) var colorScheme
  16. private var insulinFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 2
  20. return formatter
  21. }
  22. private var glucoseFormatter: NumberFormatter {
  23. let formatter = NumberFormatter()
  24. formatter.numberStyle = .decimal
  25. formatter.maximumFractionDigits = 0
  26. if state.units == .mmolL {
  27. formatter.minimumFractionDigits = 1
  28. formatter.maximumFractionDigits = 1
  29. }
  30. formatter.roundingMode = .halfUp
  31. return formatter
  32. }
  33. private var dateFormatter: DateFormatter {
  34. let formatter = DateFormatter()
  35. formatter.timeStyle = .short
  36. return formatter
  37. }
  38. var body: some View {
  39. VStack {
  40. Picker("Mode", selection: $state.mode) {
  41. ForEach(Mode.allCases.indexed(), id: \.1) { index, item in
  42. Text(item.name).tag(index)
  43. }
  44. }
  45. .pickerStyle(SegmentedPickerStyle())
  46. .padding(.horizontal)
  47. Form {
  48. switch state.mode {
  49. case .treatments: treatmentsList
  50. case .glucose: glucoseList
  51. }
  52. }
  53. }
  54. .onAppear(perform: configureView)
  55. .navigationTitle("History")
  56. .navigationBarTitleDisplayMode(.automatic)
  57. .navigationBarItems(
  58. leading: Button("Close", action: state.hideModal),
  59. trailing: state.mode == .glucose ? EditButton().asAny() : EmptyView().asAny()
  60. )
  61. .sheet(isPresented: $showExternalInsulin, onDismiss: {
  62. if isAmountUnconfirmed {
  63. state.externalInsulinAmount = 0
  64. state.externalInsulinDate = Date()
  65. }
  66. }) {
  67. addExternalInsulinView
  68. }
  69. .popup(isPresented: newGlucose, alignment: .top, direction: .bottom) {
  70. VStack(spacing: 20) {
  71. HStack {
  72. Text("New Glucose")
  73. DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
  74. Text(state.units.rawValue)
  75. }.padding(.horizontal, 20)
  76. HStack {
  77. let limitLow: Decimal = state.units == .mmolL ? 2.2 : 40
  78. let limitHigh: Decimal = state.units == .mmolL ? 21 : 380
  79. Button { newGlucose = false }
  80. label: { Text("Cancel") }.frame(maxWidth: .infinity, alignment: .leading)
  81. Button {
  82. state.addManualGlucose()
  83. newGlucose = false
  84. }
  85. label: { Text("Save") }
  86. .frame(maxWidth: .infinity, alignment: .trailing)
  87. // .disabled(state.manualGlcuose < limitLow || state.manualGlcuose > limitHigh)
  88. }.padding(20)
  89. }
  90. .frame(maxHeight: 140)
  91. .background(
  92. RoundedRectangle(cornerRadius: 8, style: .continuous)
  93. .fill(Color(colorScheme == .dark ? UIColor.systemGray2 : UIColor.systemGray6))
  94. )
  95. }
  96. }
  97. private var treatmentsList: some View {
  98. List {
  99. HStack {
  100. Spacer()
  101. Button(action: { showExternalInsulin = true
  102. state.externalInsulinDate = Date() }, label: {
  103. HStack {
  104. Text("Add")
  105. .foregroundColor(Color.secondary)
  106. .font(.caption)
  107. Image(systemName: "syringe")
  108. .foregroundColor(Color.accentColor)
  109. }.frame(maxWidth: .infinity, alignment: .trailing)
  110. }).buttonStyle(.borderless)
  111. }
  112. if !state.treatments.isEmpty {
  113. ForEach(state.treatments) { item in
  114. treatmentView(item)
  115. }
  116. } else {
  117. HStack {
  118. Text("No data.")
  119. }
  120. }
  121. }
  122. }
  123. private var glucoseList: some View {
  124. List {
  125. Button { newGlucose = true }
  126. label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
  127. .padding(.trailing, 20)
  128. ForEach(state.glucose) { item in
  129. glucoseView(item)
  130. }.onDelete(perform: deleteGlucose)
  131. }
  132. }
  133. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  134. HStack {
  135. Image(systemName: "circle.fill").foregroundColor(item.color)
  136. Text(dateFormatter.string(from: item.date))
  137. .moveDisabled(true)
  138. Text(item.type.name)
  139. Text(item.amountText).foregroundColor(.secondary)
  140. if let duration = item.durationText {
  141. Text(duration).foregroundColor(.secondary)
  142. }
  143. if item.type == .carbs {
  144. if item.note != "" {
  145. Spacer()
  146. Text(item.note ?? "").foregroundColor(.brown)
  147. }
  148. Spacer()
  149. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  150. .contentShape(Rectangle())
  151. .padding(.vertical)
  152. .onTapGesture {
  153. removeCarbsAlert = Alert(
  154. title: Text("Delete carbs?"),
  155. message: Text(item.amountText),
  156. primaryButton: .destructive(
  157. Text("Delete"),
  158. action: { state.deleteCarbs(item) }
  159. ),
  160. secondaryButton: .cancel()
  161. )
  162. isRemoveCarbsAlertPresented = true
  163. }
  164. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  165. removeCarbsAlert!
  166. }
  167. }
  168. if item.type == .fpus {
  169. Spacer()
  170. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  171. .contentShape(Rectangle())
  172. .padding(.vertical)
  173. .onTapGesture {
  174. removeCarbsAlert = Alert(
  175. title: Text("Delete carb equivalents?"),
  176. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  177. primaryButton: .destructive(
  178. Text("Delete"),
  179. action: { state.deleteCarbs(item) }
  180. ),
  181. secondaryButton: .cancel()
  182. )
  183. isRemoveCarbsAlertPresented = true
  184. }
  185. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  186. removeCarbsAlert!
  187. }
  188. }
  189. if item.type == .bolus {
  190. Spacer()
  191. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  192. .contentShape(Rectangle())
  193. .padding(.vertical)
  194. .onTapGesture {
  195. removeInsulinAlert = Alert(
  196. title: Text("Delete insulin?"),
  197. message: Text(item.amountText),
  198. primaryButton: .destructive(
  199. Text("Delete"),
  200. action: { state.deleteInsulin(item) }
  201. ),
  202. secondaryButton: .cancel()
  203. )
  204. isRemoveInsulinAlertPresented = true
  205. }
  206. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  207. removeInsulinAlert!
  208. }
  209. }
  210. }
  211. }
  212. var addExternalInsulinView: some View {
  213. NavigationView {
  214. VStack {
  215. Form {
  216. Section {
  217. HStack {
  218. Text("Amount")
  219. Spacer()
  220. DecimalTextField(
  221. "0",
  222. value: $state.externalInsulinAmount,
  223. formatter: insulinFormatter,
  224. autofocus: true,
  225. cleanInput: true
  226. )
  227. Text("U").foregroundColor(.secondary)
  228. }
  229. }
  230. Section {
  231. DatePicker("Date", selection: $state.externalInsulinDate, in: ...Date())
  232. }
  233. let amountWarningCondition = (state.externalInsulinAmount > state.maxBolus) &&
  234. (state.externalInsulinAmount <= state.maxBolus * 3)
  235. Section {
  236. HStack {
  237. Button {
  238. state.addExternalInsulin()
  239. isAmountUnconfirmed = false
  240. showExternalInsulin = false
  241. }
  242. label: {
  243. Text("Log external insulin")
  244. }
  245. .foregroundColor(amountWarningCondition ? Color.white : Color.accentColor)
  246. .frame(maxWidth: .infinity, alignment: .center)
  247. .disabled(
  248. state.externalInsulinAmount <= 0 || state.externalInsulinAmount > state
  249. .maxBolus * 3
  250. )
  251. }
  252. }
  253. header: {
  254. if amountWarningCondition
  255. {
  256. Text("⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!")
  257. }
  258. }
  259. .listRowBackground(
  260. amountWarningCondition ? Color
  261. .red : colorScheme == .dark ? Color(UIColor.secondarySystemBackground) : Color.white
  262. )
  263. }
  264. }
  265. .onAppear(perform: configureView)
  266. .navigationTitle("External Insulin")
  267. .navigationBarTitleDisplayMode(.inline)
  268. .navigationBarItems(leading: Button("Close", action: { showExternalInsulin = false
  269. state.externalInsulinAmount = 0 }))
  270. }
  271. }
  272. @ViewBuilder private func glucoseView(_ item: Glucose) -> some View {
  273. VStack(alignment: .leading, spacing: 4) {
  274. HStack {
  275. Text(dateFormatter.string(from: item.glucose.dateString))
  276. Spacer()
  277. Text(item.glucose.glucose.map {
  278. glucoseFormatter.string(from: Double(
  279. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  280. ) as NSNumber)!
  281. } ?? "--")
  282. Text(state.units.rawValue)
  283. Text(item.glucose.direction?.symbol ?? "--")
  284. }
  285. Text("ID: " + item.glucose.id).font(.caption2).foregroundColor(.secondary)
  286. }
  287. }
  288. private func deleteGlucose(at offsets: IndexSet) {
  289. state.deleteGlucose(at: offsets[offsets.startIndex])
  290. }
  291. }
  292. }