| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import SwiftUI
- import Swinject
- extension Bolus {
- struct RootView: BaseView {
- let resolver: Resolver
- let waitForSuggestion: Bool
- @StateObject var state = StateModel()
- @State private var isAddInsulinAlertPresented = false
- private var formatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- var body: some View {
- Form {
- Section(header: Text("Recommendation")) {
- if state.waitForSuggestion {
- HStack {
- Text("Wait please").foregroundColor(.secondary)
- Spacer()
- ActivityIndicator(isAnimating: .constant(true), style: .medium) // fix iOS 15 bug
- }
- } else {
- HStack {
- Text("Insulin required").foregroundColor(.secondary)
- Spacer()
- Text(
- formatter
- .string(from: state.insulinRequired as NSNumber)! +
- NSLocalizedString(" U", comment: "Insulin unit")
- ).foregroundColor(.secondary)
- }.contentShape(Rectangle())
- .onTapGesture {
- state.amount = max(Decimal(round(Double(state.insulinRequired) * 20) / 20.0), 0) // round to x.x5
- // state.amount = max(0, self.apsManager.roundBolus(amount: state.insulinRequired)) //throws arror about optional parameter being nil
- }
- HStack {
- Text("Insulin recommended")
- Spacer()
- Text(
- formatter
- .string(from: state.insulinRecommended as NSNumber)! +
- NSLocalizedString(" U", comment: "Insulin unit")
- ).foregroundColor(.secondary)
- }.contentShape(Rectangle())
- .onTapGesture {
- state.amount = state.insulinRecommended
- }
- }
- }
- if !state.waitForSuggestion {
- Section(header: Text("Bolus")) {
- HStack {
- Text("Amount")
- Spacer()
- DecimalTextField(
- "0",
- value: $state.amount,
- formatter: formatter,
- autofocus: true,
- cleanInput: true
- )
- Text("U").foregroundColor(.secondary)
- }
- }
- Section {
- Button { state.add() }
- label: { Text("Enact bolus") }
- .disabled(state.amount <= 0)
- }
- Section {
- if waitForSuggestion {
- Button { state.showModal(for: nil) }
- label: { Text("Continue without bolus") }
- } else {
- Button { isAddInsulinAlertPresented = true }
- label: { Text("Add insulin without actually bolusing") }
- .disabled(state.amount <= 0)
- }
- }
- }
- }
- .alert(isPresented: $isAddInsulinAlertPresented) {
- let amount = formatter
- .string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
- return Alert(
- title: Text("Are you sure?"),
- message: Text("Add \(amount) without bolusing"),
- primaryButton: .destructive(
- Text("Add"),
- action: { state.addWithoutBolus() }
- ),
- secondaryButton: .cancel()
- )
- }
- .onAppear {
- configureView {
- state.waitForSuggestionInitial = waitForSuggestion
- state.waitForSuggestion = waitForSuggestion
- }
- }
- .navigationTitle("Enact Bolus")
- .navigationBarTitleDisplayMode(.automatic)
- .navigationBarItems(leading: Button("Close", action: state.hideModal))
- }
- }
- }
- // fix iOS 15 bug
- struct ActivityIndicator: UIViewRepresentable {
- @Binding var isAnimating: Bool
- let style: UIActivityIndicatorView.Style
- func makeUIView(context _: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
- UIActivityIndicatorView(style: style)
- }
- func updateUIView(_ uiView: UIActivityIndicatorView, context _: UIViewRepresentableContext<ActivityIndicator>) {
- isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
- }
- }
|