| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import CoreData
- import SwiftUI
- import Swinject
- extension NightscoutConfig {
- struct RootView: BaseView {
- let resolver: Resolver
- let displayClose: Bool
- @StateObject var state = StateModel()
- @State var importAlert: Alert?
- @State var isImportAlertPresented = false
- @State var importedHasRun = 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
- )
- }
- @FetchRequest(
- entity: ImportError.entity(),
- sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)], predicate: NSPredicate(
- format: "date > %@", Date().addingTimeInterval(-1.minutes.timeInterval) as NSDate
- )
- ) var fetchedErrors: FetchedResults<ImportError>
- private var portFormater: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.allowsFloats = false
- formatter.usesGroupingSeparator = false
- return formatter
- }
- var body: some View {
- Form {
- NavigationLink("Connect", destination: NightscoutConnectView(state: state))
- NavigationLink("Upload", destination: NightscoutUploadView(state: state))
- NavigationLink("Fetch and Remote Control", destination: NightscoutFetchView(state: state))
- Section(
- header: Text("Import Settings from Nightscout"),
- footer: VStack(alignment: .leading, spacing: 2) {
- Text(
- "Importing settings from Nightscout will overwrite these settings in Trio Settings -> Configuration:"
- )
- Text(" • ") + Text("DIA (Pump settings)")
- Text(" • ") + Text("Basal Profile")
- Text(" • ") + Text("Insulin Sensitivities")
- Text(" • ") + Text("Carb Ratios")
- Text(" • ") + Text("Target Glucose")
- }
- ) {
- Button("Import settings") {
- importAlert = Alert(
- title: Text("Import settings?"),
- message: Text(
- "\n" +
- NSLocalizedString(
- "This will replace some or all of your current pump settings. Are you sure you want to import profile settings from Nightscout?",
- comment: "Profile Import Alert"
- ) +
- "\n"
- ),
- primaryButton: .destructive(
- Text("Yes, Import"),
- action: {
- state.importSettings()
- importedHasRun = true
- }
- ),
- secondaryButton: .cancel()
- )
- isImportAlertPresented.toggle()
- }.disabled(state.url.isEmpty || state.connecting)
- .alert(isPresented: $importedHasRun) {
- Alert(
- title: Text((fetchedErrors.first?.error ?? "").count < 4 ? "Settings imported" : "Import Error"),
- message: Text(
- (fetchedErrors.first?.error ?? "").count < 4 ?
- NSLocalizedString(
- "\nNow please verify all of your new settings thoroughly: \n\n • DIA (Pump settings)\n • Basal Profile\n • Insulin Sensitivities\n • Carb Ratios\n • Target Glucose\n\n in Trio Settings -> Configuration.\n\nBad or invalid profile settings could have disastrous effects.",
- comment: "Imported Profiles Alert"
- ) :
- NSLocalizedString(fetchedErrors.first?.error ?? "", comment: "Import Error")
- ),
- primaryButton: .destructive(
- Text("OK")
- ),
- secondaryButton: .cancel()
- )
- }
- }
-
- Section {
- Button("Backfill glucose") {
- Task {
- state.backfillGlucose()
- }
- }
- .disabled(state.url.isEmpty || state.connecting || state.backfilling)
- } header: { Text("Backfill glucose from Nightscout")
- }
- }
- .navigationBarTitle("Nightscout Config")
- .navigationBarTitleDisplayMode(.automatic)
- .navigationBarItems(leading: displayClose ? Button("Close", action: state.hideModal) : nil)
- .alert(isPresented: $isImportAlertPresented) {
- importAlert!
- }
- .onAppear(perform: configureView)
- }
- }
- }
|