DataTableRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 showNonPumpInsulin: Bool = false
  13. @State private var showFutureEntries: Bool = false // default to hide future entries
  14. @State private var showManualGlucose: Bool = false
  15. @State private var isAmountUnconfirmed: Bool = true
  16. @Environment(\.colorScheme) var colorScheme
  17. private var insulinFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 2
  21. return formatter
  22. }
  23. private var glucoseFormatter: NumberFormatter {
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. formatter.maximumFractionDigits = 0
  27. if state.units == .mmolL {
  28. formatter.maximumFractionDigits = 1
  29. formatter.roundingMode = .ceiling
  30. }
  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(.inline)
  57. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  58. .sheet(isPresented: $showManualGlucose) {
  59. addGlucoseView
  60. }
  61. .sheet(isPresented: $showNonPumpInsulin, onDismiss: { if isAmountUnconfirmed { state.nonPumpInsulinAmount = 0
  62. state.nonPumpInsulinDate = Date() } }) {
  63. addNonPumpInsulinView
  64. }
  65. }
  66. private var treatmentsList: some View {
  67. List {
  68. HStack {
  69. Button(action: { showFutureEntries.toggle() }, label: {
  70. HStack {
  71. Image(systemName: showFutureEntries ? "calendar.badge.minus" : "calendar.badge.plus")
  72. .foregroundColor(Color.accentColor)
  73. Text(showFutureEntries ? "Hide Future" : "Show Future")
  74. .foregroundColor(Color.secondary)
  75. .font(.caption)
  76. }.frame(maxWidth: .infinity, alignment: .leading)
  77. }).buttonStyle(.borderless)
  78. Spacer()
  79. Button(action: { showNonPumpInsulin = true
  80. state.nonPumpInsulinDate = Date() }, label: {
  81. HStack {
  82. Text("Add")
  83. .foregroundColor(Color.secondary)
  84. .font(.caption)
  85. Image(systemName: "syringe")
  86. .foregroundColor(Color.accentColor)
  87. }.frame(maxWidth: .infinity, alignment: .trailing)
  88. }).buttonStyle(.borderless)
  89. }
  90. if !state.treatments.isEmpty {
  91. if !showFutureEntries {
  92. ForEach(state.treatments.filter { item in
  93. item.date <= Date()
  94. }) { item in
  95. treatmentView(item)
  96. }
  97. } else {
  98. ForEach(state.treatments) { item in
  99. treatmentView(item)
  100. }
  101. }
  102. } else {
  103. HStack {
  104. Text("No data.")
  105. }
  106. }
  107. }
  108. }
  109. private var glucoseList: some View {
  110. List {
  111. HStack {
  112. Text("Time").foregroundStyle(.secondary)
  113. Spacer()
  114. Text(state.units.rawValue).foregroundStyle(.secondary)
  115. Button(
  116. action: { showManualGlucose = true
  117. state.manualGlucose = 0 },
  118. label: { Image(systemName: "plus.circle.fill").foregroundStyle(.secondary)
  119. }
  120. ).buttonStyle(.borderless)
  121. }
  122. if !state.glucose.isEmpty {
  123. ForEach(state.glucose) { item in
  124. glucoseView(item, isManual: item.glucose)
  125. }
  126. .onDelete(perform: deleteGlucose)
  127. } else {
  128. HStack {
  129. Text("No data.")
  130. }
  131. }
  132. }
  133. }
  134. var addGlucoseView: some View {
  135. NavigationView {
  136. VStack {
  137. Form {
  138. Section {
  139. HStack {
  140. Text("New Glucose")
  141. DecimalTextField(
  142. " ... ",
  143. value: $state.manualGlucose,
  144. formatter: glucoseFormatter,
  145. autofocus: true,
  146. cleanInput: true
  147. )
  148. Text(state.units.rawValue).foregroundStyle(.secondary)
  149. }
  150. }
  151. Section {
  152. HStack {
  153. let limitLow: Decimal = state.units == .mmolL ? 0.8 : 40
  154. let limitHigh: Decimal = state.units == .mgdL ? 14 : 720
  155. Button {
  156. state.addManualGlucose()
  157. isAmountUnconfirmed = false
  158. showManualGlucose = false
  159. }
  160. label: { Text("Save") }
  161. .frame(maxWidth: .infinity, alignment: .center)
  162. .disabled(state.manualGlucose < limitLow || state.manualGlucose > limitHigh)
  163. }
  164. }
  165. }
  166. }
  167. .onAppear(perform: configureView)
  168. .navigationTitle("Add Glucose")
  169. .navigationBarTitleDisplayMode(.automatic)
  170. .navigationBarItems(leading: Button("Close", action: { showManualGlucose = false }))
  171. }
  172. }
  173. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  174. HStack {
  175. Image(systemName: "circle.fill").foregroundColor(item.color)
  176. Text(dateFormatter.string(from: item.date))
  177. .moveDisabled(true)
  178. Text((item.isSMB ?? false) ? "SMB" : item.type.name)
  179. Text(item.amountText).foregroundColor(.secondary)
  180. if let duration = item.durationText {
  181. Text(duration).foregroundColor(.secondary)
  182. }
  183. if item.type == .carbs {
  184. if item.note != "" {
  185. Spacer()
  186. Text(item.note ?? "").foregroundColor(.brown)
  187. }
  188. Spacer()
  189. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  190. .contentShape(Rectangle())
  191. .padding(.vertical)
  192. .onTapGesture {
  193. removeCarbsAlert = Alert(
  194. title: Text("Delete carbs?"),
  195. message: Text(item.amountText),
  196. primaryButton: .destructive(
  197. Text("Delete"),
  198. action: {
  199. state.deleteCarbs(item) }
  200. ),
  201. secondaryButton: .cancel()
  202. )
  203. isRemoveCarbsAlertPresented = true
  204. }
  205. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  206. removeCarbsAlert!
  207. }
  208. }
  209. if item.type == .fpus {
  210. Spacer()
  211. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  212. .contentShape(Rectangle())
  213. .padding(.vertical)
  214. .onTapGesture {
  215. removeCarbsAlert = Alert(
  216. title: Text("Delete carb equivalents?"),
  217. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  218. primaryButton: .destructive(
  219. Text("Delete"),
  220. action: { state.deleteCarbs(item) }
  221. ),
  222. secondaryButton: .cancel()
  223. )
  224. isRemoveCarbsAlertPresented = true
  225. }
  226. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  227. removeCarbsAlert!
  228. }
  229. }
  230. if item.type == .bolus {
  231. Spacer()
  232. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  233. .contentShape(Rectangle())
  234. .padding(.vertical)
  235. .onTapGesture {
  236. removeInsulinAlert = Alert(
  237. title: Text("Delete insulin?"),
  238. message: Text(item.amountText),
  239. primaryButton: .destructive(
  240. Text("Delete"),
  241. action: { state.deleteInsulin(item) }
  242. ),
  243. secondaryButton: .cancel()
  244. )
  245. isRemoveInsulinAlertPresented = true
  246. }
  247. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  248. removeInsulinAlert!
  249. }
  250. }
  251. }
  252. }
  253. var addNonPumpInsulinView: some View {
  254. NavigationView {
  255. VStack {
  256. Form {
  257. Section {
  258. HStack {
  259. Text("Amount")
  260. Spacer()
  261. DecimalTextField(
  262. "0",
  263. value: $state.nonPumpInsulinAmount,
  264. formatter: insulinFormatter,
  265. autofocus: true,
  266. cleanInput: true
  267. )
  268. Text("U").foregroundColor(.secondary)
  269. }
  270. }
  271. Section {
  272. DatePicker("Date", selection: $state.nonPumpInsulinDate, in: ...Date())
  273. }
  274. let amountWarningCondition = (state.nonPumpInsulinAmount > state.maxBolus) &&
  275. (state.nonPumpInsulinAmount <= state.maxBolus * 3)
  276. Section {
  277. HStack {
  278. Button {
  279. state.addNonPumpInsulin()
  280. isAmountUnconfirmed = false
  281. showNonPumpInsulin = false
  282. }
  283. label: {
  284. Text("Log non-pump insulin")
  285. }
  286. .foregroundColor(amountWarningCondition ? Color.white : Color.accentColor)
  287. .frame(maxWidth: .infinity, alignment: .center)
  288. .disabled(
  289. state.nonPumpInsulinAmount <= 0 || state.nonPumpInsulinAmount > state
  290. .maxBolus * 3
  291. )
  292. }
  293. }
  294. header: {
  295. if amountWarningCondition
  296. {
  297. Text("⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!")
  298. }
  299. }
  300. .listRowBackground(
  301. amountWarningCondition ? Color
  302. .red : colorScheme == .dark ? Color(UIColor.secondarySystemBackground) : Color.white
  303. )
  304. }
  305. }
  306. .onAppear(perform: configureView)
  307. .navigationTitle("Non-Pump Insulin")
  308. .navigationBarTitleDisplayMode(.inline)
  309. .navigationBarItems(leading: Button("Close", action: { showNonPumpInsulin = false
  310. state.nonPumpInsulinAmount = 0 }))
  311. }
  312. }
  313. @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> some View {
  314. VStack(alignment: .leading, spacing: 4) {
  315. HStack {
  316. Text(dateFormatter.string(from: item.glucose.dateString))
  317. Spacer()
  318. Text(item.glucose.glucose.map {
  319. glucoseFormatter.string(from: Double(
  320. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  321. ) as NSNumber)!
  322. } ?? "--")
  323. if isManual.type == GlucoseType.manual.rawValue {
  324. Image(systemName: "drop.fill").symbolRenderingMode(.monochrome).foregroundStyle(.red)
  325. } else {
  326. Text(item.glucose.direction?.symbol ?? "--")
  327. }
  328. }
  329. }
  330. }
  331. private func deleteGlucose(at offsets: IndexSet) {
  332. state.deleteGlucose(at: offsets[offsets.startIndex])
  333. }
  334. }
  335. }