DataTableRootView.swift 15 KB

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