| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import AudioToolbox
- import SwiftUI
- import Swinject
- extension Snooze {
- struct RootView: BaseView {
- let resolver: Resolver
- @StateObject var state = StateModel()
- @State private var selectedInterval = 0
- @State private var snoozeDescription = "nothing to see here"
- private var pickerTimes: [TimeInterval] {
- var arr: [TimeInterval] = []
- let mins10 = 0.166_67
- let mins20 = mins10 * 2
- let mins30 = mins10 * 3
- // let mins40 = mins10 * 4
- for hr in 0 ..< 2 {
- for min in [0.0, mins20, mins20 * 2] {
- arr.append(TimeInterval(hours: Double(hr) + min))
- }
- }
- for hr in 2 ..< 4 {
- for min in [0.0, mins30] {
- arr.append(TimeInterval(hours: Double(hr) + min))
- }
- }
- for hr in 4 ... 8 {
- arr.append(TimeInterval(hours: Double(hr)))
- }
- return arr
- }
- private var formatter: DateComponentsFormatter {
- let formatter = DateComponentsFormatter()
- formatter.allowsFractionalUnits = false
- formatter.unitsStyle = .full
- return formatter
- }
- private var dateFormatter: DateFormatter {
- let formatter = DateFormatter()
- formatter.timeStyle = .short
- return formatter
- }
- private func formatInterval(_ interval: TimeInterval) -> String {
- formatter.string(from: interval)!
- }
- func getSnoozeDescription() -> String {
- var snoozeDescription = ""
- var celltext = ""
- switch state.alarm {
- case .high:
- celltext = NSLocalizedString("High Glucose Alarm active", comment: "High Glucose Alarm active")
- case .low:
- celltext = NSLocalizedString("Low Glucose Alarm active", comment: "Low Glucose Alarm active")
- case .none:
- celltext = NSLocalizedString("No Glucose Alarm active", comment: "No Glucose Alarm active")
- }
- if state.snoozeUntilDate > Date() {
- snoozeDescription = String(
- format: NSLocalizedString("snoozing until %@", comment: "snoozing until %@"),
- dateFormatter.string(from: state.snoozeUntilDate)
- )
- } else {
- snoozeDescription = NSLocalizedString("not snoozing", comment: "not snoozing")
- }
- return [celltext, snoozeDescription].joined(separator: ", ")
- }
- var snoozeButton: some View {
- VStack(alignment: .leading) {
- Button {
- let interval = pickerTimes[selectedInterval]
- let snoozeFor = formatter.string(from: interval)!
- let untilDate = Date() + interval
- state.snoozeUntilDate = untilDate < Date() ? .distantPast : untilDate
- debug(.default, "will snooze for \(snoozeFor) until \(dateFormatter.string(from: untilDate))")
- snoozeDescription = getSnoozeDescription()
- BaseUserNotificationsManager.stopSound()
- } label: {
- Text("Click to Snooze Alerts")
- .padding()
- }
- }
- }
- private var snoozePicker: some View {
- VStack {
- Picker(selection: $selectedInterval, label: Text("Strength")) {
- ForEach(0 ..< pickerTimes.count) {
- Text(formatInterval(self.pickerTimes[$0]))
- }
- }
- .pickerStyle(.wheel)
- }
- }
- var snoozeDesc: some View {
- VStack(alignment: .leading) {
- Text(snoozeDescription)
- }
- }
- var body: some View {
- Form {
- snoozeDesc
- snoozePicker
- snoozeButton
- }
- .navigationBarTitle("Snooze Alerts")
- .navigationBarTitleDisplayMode(.automatic)
- .navigationBarItems(leading: Button("Close", action: state.hideModal))
- .onAppear {
- configureView()
- snoozeDescription = getSnoozeDescription()
- }
- }
- }
- }
- extension TimeInterval {
- static func seconds(_ seconds: Double) -> TimeInterval {
- seconds
- }
- static func minutes(_ minutes: Double) -> TimeInterval {
- TimeInterval(minutes: minutes)
- }
- static func hours(_ hours: Double) -> TimeInterval {
- TimeInterval(hours: hours)
- }
- init(minutes: Double) {
- // self.init(minutes * 60)
- let m = minutes * 60
- self.init(m)
- }
- init(hours: Double) {
- self.init(minutes: hours * 60)
- }
- var minutes: Double {
- self / 60.0
- }
- var hours: Double {
- minutes / 60.0
- }
- }
|