DataTableRootView.swift 14 KB

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