DataTableRootView.swift 16 KB

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