| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import SwiftUI
- import Swinject
- extension AutotuneConfig {
- struct RootView: BaseView {
- let resolver: Resolver
- @StateObject var state = StateModel()
- @State var replaceAlert = false
- @Environment(\.colorScheme) var colorScheme
- var color: LinearGradient {
- colorScheme == .dark ? LinearGradient(
- gradient: Gradient(colors: [
- Color.bgDarkBlue,
- Color.bgDarkerDarkBlue
- ]),
- startPoint: .top,
- endPoint: .bottom
- )
- :
- LinearGradient(
- gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
- startPoint: .top,
- endPoint: .bottom
- )
- }
- private var isfFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- private var rateFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- private var dateFormatter: DateFormatter {
- let formatter = DateFormatter()
- formatter.dateStyle = .medium
- formatter.timeStyle = .short
- return formatter
- }
- var body: some View {
- Form {
- Section {
- Toggle("Use Autotune", isOn: $state.useAutotune)
- if state.useAutotune {
- Toggle("Only Autotune Basal Insulin", isOn: $state.onlyAutotuneBasals)
- }
- }
- Section {
- HStack {
- Text("Last run")
- Spacer()
- Text(dateFormatter.string(from: state.publishedDate))
- }
- Button { state.run() }
- label: { Text("Run now") }
- }
- if let autotune = state.autotune {
- if !state.onlyAutotuneBasals {
- Section {
- HStack {
- Text("Carb ratio")
- Spacer()
- Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
- Text("g/U").foregroundColor(.secondary)
- }
- HStack {
- Text("Sensitivity")
- Spacer()
- if state.units == .mmolL {
- Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
- } else {
- Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
- }
- Text(state.units.rawValue + "/U").foregroundColor(.secondary)
- }
- }
- }
- Section(header: Text("Basal profile")) {
- ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
- HStack {
- Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
- Spacer()
- Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
- Text("U/hr").foregroundColor(.secondary)
- }
- }
- HStack {
- Text("Total")
- .bold()
- .foregroundColor(.primary)
- Spacer()
- Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
- .foregroundColor(.primary) +
- Text(" U/day")
- .foregroundColor(.secondary)
- }
- }
- Section {
- Button {
- Task {
- await state.delete()
- }
- }
- label: { Text("Delete autotune data") }
- .foregroundColor(.red)
- }
- Section {
- Button {
- replaceAlert = true
- }
- label: { Text("Save as your Normal Basal Rates") }
- } header: {
- Text("Replace Normal Basal")
- }
- }
- }
- .scrollContentBackground(.hidden).background(color)
- .onAppear(perform: configureView)
- .navigationTitle("Autotune")
- .navigationBarTitleDisplayMode(.automatic)
- .alert(Text("Are you sure?"), isPresented: $replaceAlert) {
- Button("Yes", action: {
- state.replace()
- replaceAlert.toggle()
- })
- Button("No", action: { replaceAlert.toggle() })
- }
- }
- }
- }
|