DataTableRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 isAmountUnconfirmed: Bool = true
  14. @State private var showFutureEntries: Bool = true
  15. @State private var newGlucose = false
  16. @State private var isLayered = false
  17. @FocusState private var isFocused: Bool
  18. @Environment(\.colorScheme) var colorScheme
  19. private var insulinFormatter: NumberFormatter {
  20. let formatter = NumberFormatter()
  21. formatter.numberStyle = .decimal
  22. formatter.maximumFractionDigits = 2
  23. return formatter
  24. }
  25. private var glucoseFormatter: NumberFormatter {
  26. let formatter = NumberFormatter()
  27. formatter.numberStyle = .decimal
  28. formatter.maximumFractionDigits = 0
  29. if state.units == .mmolL {
  30. formatter.maximumFractionDigits = 1
  31. formatter.roundingMode = .ceiling
  32. }
  33. return formatter
  34. }
  35. private var dateFormatter: DateFormatter {
  36. let formatter = DateFormatter()
  37. formatter.timeStyle = .short
  38. return formatter
  39. }
  40. var body: some View {
  41. VStack {
  42. Picker("Mode", selection: $state.mode) {
  43. ForEach(Mode.allCases.indexed(), id: \.1) { index, item in
  44. Text(item.name).tag(index)
  45. }
  46. }
  47. .pickerStyle(SegmentedPickerStyle())
  48. .padding(.horizontal)
  49. Form {
  50. switch state.mode {
  51. case .treatments: treatmentsList
  52. case .glucose: glucoseList
  53. }
  54. }
  55. }
  56. .onAppear(perform: configureView)
  57. .navigationTitle(isLayered ? "" : "History")
  58. .blur(radius: isLayered ? 4.0 : 0)
  59. .navigationBarTitleDisplayMode(.automatic)
  60. .navigationBarItems(leading: Button(isLayered ? "" : "Close", action: state.hideModal))
  61. .popup(isPresented: newGlucose, alignment: .center, direction: .top) {
  62. addGlucose
  63. }
  64. .sheet(isPresented: $showNonPumpInsulin, onDismiss: { if isAmountUnconfirmed { state.nonPumpInsulinAmount = 0
  65. state.nonPumpInsulinDate = Date() } }) {
  66. addNonPumpInsulinView
  67. }
  68. }
  69. private var treatmentsList: some View {
  70. List {
  71. HStack {
  72. Button(action: { showFutureEntries.toggle() }, label: {
  73. HStack {
  74. Image(systemName: showFutureEntries ? "calendar.badge.minus" : "calendar.badge.plus")
  75. .foregroundColor(Color.accentColor)
  76. Text(showFutureEntries ? "Hide Future" : "Show Future")
  77. .foregroundColor(Color.accentColor)
  78. .font(.body)
  79. }.frame(maxWidth: .infinity, alignment: .leading)
  80. }).buttonStyle(.borderless)
  81. Spacer()
  82. Button(action: { showNonPumpInsulin = true
  83. state.nonPumpInsulinDate = Date() }, label: {
  84. HStack {
  85. Text(
  86. NSLocalizedString("External Insulin", comment: "External Insulin button text")
  87. )
  88. .foregroundColor(Color.accentColor)
  89. .font(.body)
  90. Image(systemName: "plus.circle.fill")
  91. .foregroundColor(Color.accentColor)
  92. }.frame(maxWidth: .infinity, alignment: .trailing)
  93. }).buttonStyle(.borderless)
  94. }
  95. if !state.treatments.isEmpty {
  96. if showFutureEntries {
  97. ForEach(state.treatments.filter { item in
  98. item.date <= Date()
  99. }) { item in
  100. treatmentView(item)
  101. }
  102. } else {
  103. ForEach(state.treatments) { item in
  104. treatmentView(item)
  105. }
  106. }
  107. } else {
  108. HStack {
  109. Text(NSLocalizedString("No data.", comment: "No data text when no entries in history list"))
  110. }
  111. }
  112. }
  113. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  114. removeInsulinAlert!
  115. }
  116. }
  117. private var glucoseList: some View {
  118. List {
  119. Button {
  120. newGlucose = true
  121. isFocused = true
  122. isLayered.toggle()
  123. }
  124. label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
  125. .padding(.trailing, 20)
  126. ForEach(state.glucose) { item in
  127. glucoseView(item, isManual: item.glucose)
  128. }.onDelete(perform: deleteGlucose)
  129. }
  130. }
  131. private var addGlucose: some View {
  132. VStack {
  133. Form {
  134. Section {
  135. HStack {
  136. Text("Glucose").font(.custom("popup", fixedSize: 18))
  137. DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
  138. .focused($isFocused).font(.custom("glucose", fixedSize: 22))
  139. Text(state.units.rawValue).foregroundStyle(.secondary)
  140. }
  141. }
  142. header: {
  143. Text("Blood Glucose Test").foregroundColor(.secondary).font(.custom("popupHeader", fixedSize: 12))
  144. .padding(.top)
  145. }
  146. HStack {
  147. Button {
  148. newGlucose = false
  149. isLayered = false
  150. }
  151. label: { Text("Cancel").foregroundColor(.red) }
  152. .frame(maxWidth: .infinity, alignment: .leading)
  153. Spacer()
  154. Button {
  155. state.addManualGlucose()
  156. newGlucose = false
  157. isLayered = false
  158. }
  159. label: { Text("Save") }
  160. .frame(maxWidth: .infinity, alignment: .trailing)
  161. .disabled(state.manualGlcuose <= 0)
  162. }
  163. .buttonStyle(BorderlessButtonStyle())
  164. .font(.custom("popupButtons", fixedSize: 16))
  165. }
  166. }
  167. .frame(minHeight: 220, maxHeight: 260).cornerRadius(20)
  168. .background(
  169. RoundedRectangle(cornerRadius: 20, style: .continuous)
  170. .fill(Color(.tertiarySystemBackground))
  171. ).shadow(radius: 40)
  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(NSLocalizedString("Amount", comment: ""))
  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(NSLocalizedString(
  285. "Log non-pump insulin",
  286. comment: "Log non-pump insulin button text"
  287. ))
  288. }
  289. .foregroundColor(amountWarningCondition ? Color.white : Color.accentColor)
  290. .frame(maxWidth: .infinity, alignment: .center)
  291. .disabled(
  292. state.nonPumpInsulinAmount <= 0 || state.nonPumpInsulinAmount > state
  293. .maxBolus * 3
  294. )
  295. }
  296. }
  297. header: {
  298. if amountWarningCondition
  299. {
  300. Text(NSLocalizedString(
  301. "⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!",
  302. comment: "Non-pump insulin maxBolus * 3 alert text"
  303. ))
  304. }
  305. }
  306. .listRowBackground(
  307. amountWarningCondition ? Color
  308. .red : colorScheme == .dark ? Color(UIColor.secondarySystemBackground) : Color.white
  309. )
  310. }
  311. }
  312. .onAppear(perform: configureView)
  313. .navigationTitle("Non-Pump Insulin")
  314. .navigationBarTitleDisplayMode(.inline)
  315. .navigationBarItems(leading: Button("Close", action: { showNonPumpInsulin = false
  316. state.nonPumpInsulinAmount = 0 }))
  317. }
  318. }
  319. @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> some View {
  320. VStack(alignment: .leading, spacing: 4) {
  321. HStack {
  322. Text(dateFormatter.string(from: item.glucose.dateString))
  323. Spacer()
  324. Text(item.glucose.glucose.map {
  325. glucoseFormatter.string(from: Double(
  326. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  327. ) as NSNumber)!
  328. } ?? "--")
  329. Text(state.units.rawValue)
  330. if isManual.type == GlucoseType.manual.rawValue {
  331. Image(systemName: "drop.fill").symbolRenderingMode(.monochrome).foregroundStyle(.red)
  332. } else {
  333. Text(item.glucose.direction?.symbol ?? "--")
  334. }
  335. }
  336. }
  337. }
  338. private func deleteGlucose(at offsets: IndexSet) {
  339. state.deleteGlucose(at: offsets[offsets.startIndex])
  340. }
  341. }
  342. }