| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- //
- // MockPumpManagerSettingsView.swift
- // MockKitUI
- //
- // Created by Nathaniel Hamming on 2023-05-18.
- // Copyright © 2023 LoopKit Authors. All rights reserved.
- //
- import SwiftUI
- import LoopKit
- import LoopKitUI
- import MockKit
- struct MockPumpManagerSettingsView: View {
- fileprivate enum PresentedAlert {
- case resumeInsulinDeliveryError(Error)
- case suspendInsulinDeliveryError(Error)
- }
-
- @Environment(\.dismissAction) private var dismiss
- @Environment(\.guidanceColors) private var guidanceColors
- @Environment(\.insulinTintColor) private var insulinTintColor
- @ObservedObject var viewModel: MockPumpManagerSettingsViewModel
-
- @State private var showSuspendOptions = false
- @State private var presentedAlert: PresentedAlert?
- private var supportedInsulinTypes: [InsulinType]
- private var appName: String
- private let allowDebugFeatures : Bool
- private var title: String
-
- init(pumpManager: MockPumpManager, supportedInsulinTypes: [InsulinType], appName: String, allowDebugFeatures: Bool) {
- viewModel = MockPumpManagerSettingsViewModel(pumpManager: pumpManager)
- title = pumpManager.localizedTitle
- self.supportedInsulinTypes = supportedInsulinTypes
- self.appName = appName
- self.allowDebugFeatures = allowDebugFeatures
- }
-
- var body: some View {
- List {
- statusSection
-
- activitySection
-
- configurationSection
-
- supportSection
- }
- .insetGroupedListStyle()
- .navigationBarItems(trailing: doneButton)
- .navigationBarTitle(Text(title), displayMode: .large)
- .alert(item: $presentedAlert, content: alert(for:))
- }
-
- @ViewBuilder
- private var statusSection: some View {
- Section {
- VStack(spacing: 8) {
- pumpProgressView
- .openMockPumpSettingsOnLongPress(enabled: true, pumpManager: viewModel.pumpManager, supportedInsulinTypes: supportedInsulinTypes)
- Divider()
- insulinInfo
- }
- }
- }
-
- private var pumpProgressView: some View {
- HStack(alignment: .center, spacing: 16) {
- pumpImage
- expirationArea
- .offset(y: -3)
- }
- }
-
- private var pumpImage: some View {
- ZStack {
- RoundedRectangle(cornerRadius: 5)
- .fill(Color(frameworkColor: "LightGrey")!)
- .frame(width: 77, height: 76)
- Image(frameworkImage: "Pump Simulator")
- .resizable()
- .aspectRatio(contentMode: ContentMode.fit)
- .frame(maxHeight: 70)
- .frame(width: 70)
- }
- }
-
- private var expirationArea: some View {
- VStack(alignment: .leading) {
- expirationText
- .offset(y: 4)
- expirationTime
- .offset(y: 10)
- progressBar
- }
- }
-
- private var expirationText: some View {
- Text("Pump expires in ")
- .font(.subheadline)
- .foregroundColor(.secondary)
- }
-
- private var expirationTime: some View {
- HStack(alignment: .lastTextBaseline) {
- Text("2")
- .font(.system(size: 24, weight: .heavy, design: .default))
- Text("days")
- .font(.system(size: 15, weight: .regular, design: .default))
- .foregroundColor(.secondary)
- .offset(x: -3)
- }
- }
-
- private var progressBar: some View {
- ProgressView(progress: viewModel.pumpExpirationPercentComplete)
- .accentColor(insulinTintColor)
- }
-
- var insulinInfo: some View {
- InsulinStatusView(viewModel: viewModel)
- .environment(\.guidanceColors, guidanceColors)
- .environment(\.insulinTintColor, insulinTintColor)
- }
-
- @ViewBuilder
- private var activitySection: some View {
- if (allowDebugFeatures) {
- settingsSubSection
- }
- suspendResumeInsulinSubSection
- deviceDetailsSubSection
- replaceSystemComponentsSubSection
- }
-
- private var suspendResumeInsulinSubSection: some View {
- Section(header: SectionHeader(label: LocalizedString("Activity", comment: "Section header for the activity section"))) {
- Button(action: suspendResumeTapped) {
- HStack {
- Image(systemName: "pause.circle.fill")
- .foregroundColor(viewModel.isDeliverySuspended ? guidanceColors.warning : .accentColor)
- Text(viewModel.suspendResumeInsulinDeliveryLabel)
- Spacer()
- if viewModel.transitioningSuspendResumeInsulinDelivery {
- ActivityIndicator(isAnimating: .constant(true), style: .medium)
- }
- }
- }
- .disabled(viewModel.transitioningSuspendResumeInsulinDelivery)
- if viewModel.isDeliverySuspended {
- LabeledValueView(label: LocalizedString("Suspended At", comment: "Label for suspended at field"),
- value: viewModel.suspendedAtString)
- }
- }
- }
-
- private func suspendResumeTapped() {
- if viewModel.isDeliverySuspended {
- viewModel.resumeDelivery() { error in
- if let error = error {
- self.presentedAlert = .resumeInsulinDeliveryError(error)
- }
- }
- } else {
- viewModel.suspendDelivery() { error in
- if let error = error {
- self.presentedAlert = .suspendInsulinDeliveryError(error)
- }
- }
- }
- }
-
- private var deviceDetailsSubSection: some View {
- Section {
- LabeledValueView(label: "Pump Paired", value: viewModel.lastPumpPairedDateTimeString)
-
- LabeledValueView(label: "Pump Expires", value: viewModel.pumpExpirationDateTimeString)
-
- NavigationLink(destination: DemoPlaceHolderView(appName: appName)) {
- Text("Device Details")
- }
- }
- }
-
- private var replaceSystemComponentsSubSection: some View {
- Section {
- NavigationLink(destination: DemoPlaceHolderView(appName: appName)) {
- Text("Replace Pump")
- .foregroundColor(.accentColor)
- }
- }
- }
- private var settingsSubSection: some View {
- Section {
- NavigationLink(destination: MockPumpManagerControlsView(pumpManager: viewModel.pumpManager, supportedInsulinTypes: supportedInsulinTypes)) {
- Text("Simulator Settings")
- }
- }
- }
- @ViewBuilder
- private var configurationSection: some View {
- notificationSubSection
-
- pumpTimeSubSection
- }
-
- private var notificationSubSection: some View {
- Section(header: SectionHeader(label: "Configuration")) {
- NavigationLink(destination: DemoPlaceHolderView(appName: appName)) {
- Text("Notification Settings")
- }
- }
- }
-
- private var pumpTimeSubSection: some View {
- Section {
- TimeView(label: "Pump Time")
- }
- }
-
- private var supportSection: some View {
- Section(header: SectionHeader(label: "Support")) {
- NavigationLink(destination: DemoPlaceHolderView(appName: appName)) {
- Text("Get help with your pump")
- }
- }
- }
-
- private var doneButton: some View {
- Button(LocalizedString("Done", comment: "Settings done button label"), action: dismiss)
- }
-
- private func alert(for presentedAlert: PresentedAlert) -> SwiftUI.Alert {
- switch presentedAlert {
- case .suspendInsulinDeliveryError(let error):
- return Alert(
- title: Text("Failed to Suspend Insulin Delivery"),
- message: Text(error.localizedDescription)
- )
- case .resumeInsulinDeliveryError(let error):
- return Alert(
- title: Text("Failed to Resume Insulin Delivery"),
- message: Text(error.localizedDescription)
- )
- }
- }
- }
- extension MockPumpManagerSettingsView.PresentedAlert: Identifiable {
- var id: Int {
- switch self {
- case .resumeInsulinDeliveryError:
- return 0
- case .suspendInsulinDeliveryError:
- return 1
- }
- }
- }
- struct MockPumpManagerSettingsView_Previews: PreviewProvider {
- static var previews: some View {
- MockPumpManagerSettingsView(pumpManager: MockPumpManager(), supportedInsulinTypes: [], appName: "Loop", allowDebugFeatures: false)
- }
- }
|