DataTableRootView.swift 16 KB

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