| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import ActivityKit
- import Combine
- import SwiftUI
- import Swinject
- extension NotificationsConfig {
- struct RootView: BaseView {
- let resolver: Resolver
- @StateObject var state = StateModel()
- @State private var systemLiveActivitySetting: Bool = {
- if #available(iOS 16.1, *) {
- ActivityAuthorizationInfo().areActivitiesEnabled
- } else {
- false
- }
- }()
- private var glucoseFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 0
- if state.units == .mmolL {
- formatter.maximumFractionDigits = 1
- }
- formatter.roundingMode = .halfUp
- return formatter
- }
- private var carbsFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 0
- return formatter
- }
- @ViewBuilder private func liveActivitySection() -> some View {
- if #available(iOS 16.2, *) {
- Section(
- header: Text("Live Activity"),
- footer: Text(
- liveActivityFooterText()
- ),
- content: {
- if !systemLiveActivitySetting {
- Button("Open Settings App") {
- UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
- }
- } else {
- Toggle("Show Live Activity", isOn: $state.useLiveActivity)
- }
- Picker(
- selection: $state.lockScreenView,
- label: Text("Lock screen widget")
- ) {
- ForEach(LockScreenView.allCases) { selection in
- Text(selection.displayName).tag(selection)
- }
- }
- }
- )
- .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
- self.systemLiveActivitySetting = $0
- })
- }
- }
- private func liveActivityFooterText() -> String {
- var footer =
- "Live activity displays blood glucose live on the lock screen and on the dynamic island (if available)"
- if !systemLiveActivitySetting {
- footer =
- "Live activities are turned OFF in system settings. To enable live activities, go to Settings app -> Trio -> Turn live Activities ON.\n\n" +
- footer
- }
- return footer
- }
- var body: some View {
- Form {
- Section(header: Text("Glucose")) {
- Toggle("Show glucose on the app badge", isOn: $state.glucoseBadge)
- Toggle("Always Notify Glucose", isOn: $state.glucoseNotificationsAlways)
- Toggle("Also play alert sound", isOn: $state.useAlarmSound)
- Toggle("Also add source info", isOn: $state.addSourceInfoToGlucoseNotifications)
- HStack {
- Text("Low")
- Spacer()
- TextFieldWithToolBar(text: $state.lowGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
- Text(state.units.rawValue).foregroundColor(.secondary)
- }
- HStack {
- Text("High")
- Spacer()
- TextFieldWithToolBar(text: $state.highGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
- Text(state.units.rawValue).foregroundColor(.secondary)
- }
- }
- Section(header: Text("Other")) {
- HStack {
- Text("Carbs Required Threshold")
- Spacer()
- TextFieldWithToolBar(text: $state.carbsRequiredThreshold, placeholder: "0", numberFormatter: carbsFormatter)
- Text("g").foregroundColor(.secondary)
- }
- }
- liveActivitySection()
- }.scrollContentBackground(.hidden)
- .onAppear(perform: configureView)
- .navigationBarTitle("Notifications")
- .navigationBarTitleDisplayMode(.automatic)
- }
- }
- }
|