Skip to content

Commit

Permalink
cleanup settings, add alarm volume control, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
creepymonster committed Jan 16, 2023
1 parent f27ef06 commit 6467214
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 101 deletions.
12 changes: 8 additions & 4 deletions App/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ struct AppState: DirectState {
UserDefaults.shared.transmitter = transmitter
}

self.alarmHigh = UserDefaults.standard.alarmHigh ?? 180
self.alarmLow = UserDefaults.standard.alarmLow ?? 80
self.alarmHigh = UserDefaults.standard.alarmHigh
self.alarmLow = UserDefaults.standard.alarmLow
self.alarmVolume = UserDefaults.standard.alarmVolume
self.appleCalendarExport = UserDefaults.standard.appleCalendarExport
self.appleHealthExport = UserDefaults.standard.appleHealthExport
self.bellmanAlarm = UserDefaults.standard.bellmanAlarm
Expand Down Expand Up @@ -75,7 +76,8 @@ struct AppState: DirectState {
self.sensorInterval = UserDefaults.standard.sensorInterval
self.showAnnotations = UserDefaults.standard.showAnnotations
self.transmitter = UserDefaults.shared.transmitter
self.smoothChartValues = UserDefaults.standard.smoothChartValues
self.showSmoothedGlucose = UserDefaults.standard.showSmoothedGlucose
self.showInsulinInput = UserDefaults.standard.showInsulinInput
}

// MARK: Internal
Expand Down Expand Up @@ -111,6 +113,7 @@ struct AppState: DirectState {

var alarmHigh: Int { didSet { UserDefaults.standard.alarmHigh = alarmHigh } }
var alarmLow: Int { didSet { UserDefaults.standard.alarmLow = alarmLow } }
var alarmVolume: Float { didSet { UserDefaults.standard.alarmVolume = alarmVolume } }
var appleCalendarExport: Bool { didSet { UserDefaults.standard.appleCalendarExport = appleCalendarExport } }
var appleHealthExport: Bool { didSet { UserDefaults.standard.appleHealthExport = appleHealthExport } }
var bellmanAlarm: Bool { didSet { UserDefaults.standard.bellmanAlarm = bellmanAlarm } }
Expand Down Expand Up @@ -142,5 +145,6 @@ struct AppState: DirectState {
var sensorInterval: Int { didSet { UserDefaults.standard.sensorInterval = sensorInterval } }
var showAnnotations: Bool { didSet { UserDefaults.standard.showAnnotations = showAnnotations } }
var transmitter: Transmitter? { didSet { UserDefaults.shared.transmitter = transmitter } }
var smoothChartValues: Bool { didSet { UserDefaults.standard.smoothChartValues = smoothChartValues } }
var showSmoothedGlucose: Bool { didSet { UserDefaults.standard.showSmoothedGlucose = showSmoothedGlucose } }
var showInsulinInput: Bool { didSet { UserDefaults.standard.showInsulinInput = showInsulinInput } }
}
6 changes: 3 additions & 3 deletions App/Modules/Debug/Debug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private func debugMiddleware(service: LazyService<DebugService>) -> Middleware<D
return { state, action, _ in
switch action {
case .debugAlarm:
service.value.debugAlarm(sound: state.expiringAlarmSound, ignoreMute: state.ignoreMute)
service.value.debugAlarm(sound: state.expiringAlarmSound, volume: state.alarmVolume, ignoreMute: state.ignoreMute)

case .debugNotification:
service.value.debugNotification(sound: state.expiringAlarmSound, ignoreMute: state.ignoreMute)
Expand Down Expand Up @@ -49,13 +49,13 @@ private class DebugService {
DirectNotifications.shared.removeNotification(identifier: Identifier.debugAlarm.rawValue)
}

func debugAlarm(sound: NotificationSound, ignoreMute: Bool) {
func debugAlarm(sound: NotificationSound, volume: Float, ignoreMute: Bool) {
DirectNotifications.shared.ensureCanSendNotification { state in
guard state == .sound else {
return
}

DirectNotifications.shared.playSound(sound: sound, ignoreMute: ignoreMute)
DirectNotifications.shared.playSound(sound: sound, volume: volume, ignoreMute: ignoreMute)
}
}

Expand Down
12 changes: 6 additions & 6 deletions App/Modules/ExpiringNotification/ExpiringNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ private func expiringNotificationMiddelware(service: LazyService<ExpiringNotific
if sensor.state == .expired { // expired
DirectLog.info("Sensor is expired")

service.value.setSensorExpiredAlarm(sound: state.expiringAlarmSound, ignoreMute: state.ignoreMute)
service.value.setSensorExpiredAlarm(sound: state.expiringAlarmSound, volume: state.alarmVolume, ignoreMute: state.ignoreMute)

} else if sensor.remainingLifetime <= (24 * 60) { // less than 24 hours
DirectLog.info("Sensor is expiring in less than 24 hours")

service.value.setSensorExpiringAlarm(body: String(format: LocalizedString("Your sensor is about to expire. Replace sensor in %1$@."), sensor.remainingLifetime.inTimeSummary), sound: .none, ignoreMute: state.ignoreMute)
service.value.setSensorExpiringAlarm(body: String(format: LocalizedString("Your sensor is about to expire. Replace sensor in %1$@."), sensor.remainingLifetime.inTimeSummary), sound: .none, volume: state.alarmVolume, ignoreMute: state.ignoreMute)
}

default:
Expand Down Expand Up @@ -75,7 +75,7 @@ private class ExpiringNotificationService {
DirectNotifications.shared.removeNotification(identifier: Identifier.sensorExpiringAlarm.rawValue)
}

func setSensorExpiredAlarm(sound: NotificationSound, ignoreMute: Bool) {
func setSensorExpiredAlarm(sound: NotificationSound, volume: Float, ignoreMute: Bool) {
guard nextExpiredAlert == nil || Date() >= nextExpiredAlert! else {
return
}
Expand All @@ -96,14 +96,14 @@ private class ExpiringNotificationService {
notification.body = LocalizedString("Your sensor has expired and needs to be replaced as soon as possible")

if state == .sound {
DirectNotifications.shared.playSound(sound: sound, ignoreMute: ignoreMute)
DirectNotifications.shared.playSound(sound: sound, volume: volume, ignoreMute: ignoreMute)
}

DirectNotifications.shared.addNotification(identifier: Identifier.sensorExpiringAlarm.rawValue, content: notification)
}
}

func setSensorExpiringAlarm(body: String, sound: NotificationSound, ignoreMute: Bool) {
func setSensorExpiringAlarm(body: String, sound: NotificationSound, volume: Float, ignoreMute: Bool) {
guard lastExpiringAlert != body else {
return
}
Expand All @@ -128,7 +128,7 @@ private class ExpiringNotificationService {
notification.body = body

if state == .sound {
DirectNotifications.shared.playSound(sound: sound, ignoreMute: ignoreMute)
DirectNotifications.shared.playSound(sound: sound, volume: volume, ignoreMute: ignoreMute)
}

DirectNotifications.shared.addNotification(identifier: Identifier.sensorExpiringAlarm.rawValue, content: notification)
Expand Down
12 changes: 6 additions & 6 deletions App/Modules/GlucoseNotification/GlucoseNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private func glucoseNotificationMiddelware(service: LazyService<GlucoseNotificat

if !isSnoozed {
if state.hasLowGlucoseAlarm {
service.value.setLowGlucoseAlarm(sound: state.lowGlucoseAlarmSound, ignoreMute: state.ignoreMute)
service.value.setLowGlucoseAlarm(sound: state.lowGlucoseAlarmSound, volume: state.alarmVolume, ignoreMute: state.ignoreMute)
}

return Just(.setAlarmSnoozeUntil(untilDate: Date().addingTimeInterval(5 * 60).toRounded(on: 1, .minute), autosnooze: true))
Expand All @@ -61,7 +61,7 @@ private func glucoseNotificationMiddelware(service: LazyService<GlucoseNotificat

if !isSnoozed {
if state.hasHighGlucoseAlarm {
service.value.setHighGlucoseAlarm(sound: state.highGlucoseAlarmSound, ignoreMute: state.ignoreMute)
service.value.setHighGlucoseAlarm(sound: state.highGlucoseAlarmSound, volume: state.alarmVolume, ignoreMute: state.ignoreMute)
}

return Just(.setAlarmSnoozeUntil(untilDate: Date().addingTimeInterval(5 * 60).toRounded(on: 1, .minute), autosnooze: true))
Expand Down Expand Up @@ -131,8 +131,8 @@ private class GlucoseNotificationService {
}
}

func setLowGlucoseAlarm(sound: NotificationSound, ignoreMute: Bool) {
DirectNotifications.shared.playSound(sound: sound, ignoreMute: ignoreMute)
func setLowGlucoseAlarm(sound: NotificationSound, volume: Float, ignoreMute: Bool) {
DirectNotifications.shared.playSound(sound: sound, volume: volume, ignoreMute: ignoreMute)
}

func setLowGlucoseNotification(glucose: SensorGlucose, glucoseUnit: GlucoseUnit, isSnoozed: Bool) {
Expand Down Expand Up @@ -164,8 +164,8 @@ private class GlucoseNotificationService {
}
}

func setHighGlucoseAlarm(sound: NotificationSound, ignoreMute: Bool) {
DirectNotifications.shared.playSound(sound: sound, ignoreMute: ignoreMute)
func setHighGlucoseAlarm(sound: NotificationSound, volume: Float, ignoreMute: Bool) {
DirectNotifications.shared.playSound(sound: sound, volume: volume, ignoreMute: ignoreMute)
}

func setHighGlucoseNotification(glucose: SensorGlucose, glucoseUnit: GlucoseUnit, isSnoozed: Bool) {
Expand Down
2 changes: 1 addition & 1 deletion App/Views/Lists/SensorGlucoseListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct SensorGlucoseListView: View {

Spacer()

if let glucoseValue = sensorGlucose.smoothGlucoseValue?.toInteger(), sensorGlucose.timestamp < store.state.smoothThreshold, DirectConfig.smoothSensorGlucoseValues {
if let glucoseValue = sensorGlucose.smoothGlucoseValue?.toInteger(), sensorGlucose.timestamp < store.state.smoothThreshold, DirectConfig.showSmoothedGlucose {
Text(verbatim: glucoseValue.asGlucose(glucoseUnit: store.state.glucoseUnit, withUnit: true))
.monospacedDigit()
.if(store.state.isAlarm(glucoseValue: glucoseValue) != .none) { text in
Expand Down
20 changes: 11 additions & 9 deletions App/Views/ListsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ struct ListsView: View {

var body: some View {
List {
Button("Add insulin", action: {
showingAddInsulinView = true
}).sheet(isPresented: $showingAddInsulinView, onDismiss: {
showingAddInsulinView = false
}) {
AddInsulinView { start, end, units, insulinType in
let insulinDelivery = InsulinDelivery(id: UUID(), starts: start, ends: end, units: units, type: insulinType)
store.dispatch(.addInsulinDelivery(insulinDeliveryValues: [insulinDelivery]))
if DirectConfig.showInsulinInput, store.state.showInsulinInput {
Button("Add insulin", action: {
showingAddInsulinView = true
}).sheet(isPresented: $showingAddInsulinView, onDismiss: {
showingAddInsulinView = false
}) {
AddInsulinView { start, end, units, insulinType in
let insulinDelivery = InsulinDelivery(id: UUID(), starts: start, ends: end, units: units, type: insulinType)
store.dispatch(.addInsulinDelivery(insulinDeliveryValues: [insulinDelivery]))
}
}
}

Expand All @@ -42,7 +44,7 @@ struct ListsView: View {
BloodGlucoseListView()
}

if DirectConfig.insulinDeliveryInput {
if DirectConfig.showInsulinInput, store.state.showInsulinInput {
InsulinDeliveryListView()
}

Expand Down
20 changes: 14 additions & 6 deletions App/Views/Overview/ChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct ChartView: View {
.cornerRadius(5)
}

if let selectedRawPoint = selectedRawSensorPoint, showUnsmoothedValues, store.state.smoothChartValues {
if let selectedRawPoint = selectedRawSensorPoint, showUnsmoothedValues, store.state.showSmoothedGlucose {
VStack(alignment: .leading) {
Text(selectedRawPoint.time.toLocalDateTime())
Text(selectedRawPoint.info).bold()
Expand Down Expand Up @@ -257,7 +257,7 @@ struct ChartView: View {
}
}

if showUnsmoothedValues, store.state.smoothChartValues {
if showUnsmoothedValues, store.state.showSmoothedGlucose {
if !rawSensorGlucoseSeries.isEmpty {
ForEach(rawSensorGlucoseSeries) { value in
LineMark(
Expand Down Expand Up @@ -335,7 +335,15 @@ struct ChartView: View {
}
}
.id(Config.chartID)
.onChange(of: store.state.sensorGlucoseValues) { _ in
.onChange(of: store.state.showSmoothedGlucose) { _ in
if shouldRefresh {
DirectLog.info("onChange: sensorGlucoseValues")

updateSeriesMetadata()
updateSensorSeries()
}

}.onChange(of: store.state.sensorGlucoseValues) { _ in
if shouldRefresh {
DirectLog.info("onChange: sensorGlucoseValues")

Expand Down Expand Up @@ -366,7 +374,7 @@ struct ChartView: View {
updateSeriesMetadata()
}

}.onChange(of: store.state.smoothChartValues) { _ in
}.onChange(of: store.state.showSmoothedGlucose) { _ in
showUnsmoothedValues = false

}.onChange(of: store.state.selectedDate) { _ in
Expand Down Expand Up @@ -617,7 +625,7 @@ struct ChartView: View {
var smoothSensorPointInfos: [Date: GlucoseDatapoint] = [:]
var rawSensorPointInfos: [Date: GlucoseDatapoint] = [:]

let smoothSensorGlucoseSeries = DirectConfig.smoothSensorGlucoseValues && store.state.smoothChartValues
let smoothSensorGlucoseSeries = DirectConfig.showSmoothedGlucose && store.state.showSmoothedGlucose
? populateSmoothValues(glucoseValues: store.state.sensorGlucoseValues)
: populateValues(glucoseValues: store.state.sensorGlucoseValues)
smoothSensorGlucoseSeries.forEach { value in
Expand All @@ -626,7 +634,7 @@ struct ChartView: View {
}
}

let rawSensorGlucoseSeries = store.state.smoothChartValues
let rawSensorGlucoseSeries = store.state.showSmoothedGlucose
? populateValues(glucoseValues: store.state.sensorGlucoseValues.filter {
$0.timestamp < store.state.smoothThreshold
})
Expand Down
43 changes: 43 additions & 0 deletions App/Views/Settings/AdditionalSettingsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// AdditionalSettings.swift
// GlucoseDirectApp
//
// Created by Reimar Metzen on 16.01.23.
//

import SwiftUI

struct AdditionalSettingsView: View {
@EnvironmentObject var store: DirectStore

var body: some View {
Section(
content: {
if DirectConfig.showSmoothedGlucose {
Toggle("Show smoothed glucose", isOn: showSmoothedGlucose).toggleStyle(SwitchToggleStyle(tint: Color.ui.accent))
}

if DirectConfig.showInsulinInput {
Toggle("Show insulin input", isOn: showInsulinInput).toggleStyle(SwitchToggleStyle(tint: Color.ui.accent))
}
},
header: {
Label("Additional settings", systemImage: "gearshape")
}
)
}

private var showSmoothedGlucose: Binding<Bool> {
Binding(
get: { store.state.showSmoothedGlucose },
set: { store.dispatch(.setShowSmoothedGlucose(enabled: $0)) }
)
}

private var showInsulinInput: Binding<Bool> {
Binding(
get: { store.state.showInsulinInput },
set: { store.dispatch(.setShowInsulinInput(enabled: $0)) }
)
}
}
33 changes: 29 additions & 4 deletions App/Views/Settings/AlarmSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ struct AlarmSettingsView: View {
Text(info.localizedDescription)
}
}.pickerStyle(.menu)

VStack(alignment: .leading) {
HStack {
Text("Alarm volume")
Spacer()
Text((store.state.alarmVolume * 100).asPercent())
}

Slider(value: alarmVolume, in: 0...1, step: 0.05)
}

Toggle("Ignore mute", isOn: ignoreMute).toggleStyle(SwitchToggleStyle(tint: Color.ui.accent))
},
Expand All @@ -55,6 +65,21 @@ struct AlarmSettingsView: View {
set: { store.dispatch(.setIgnoreMute(enabled: $0)) }
)
}

private var alarmVolume: Binding<Float> {
Binding(
get: { store.state.alarmVolume },
set: {
store.dispatch(.setAlarmVolume(volume: $0))

if DirectNotifications.shared.isPlaying() {
DirectNotifications.shared.setVolume(volume: $0)
} else {
DirectNotifications.shared.testSound(sound: .alarm, volume: $0)
}
}
)
}

private var selectedLowGlucoseAlarmSound: Binding<String> {
Binding(
Expand All @@ -63,7 +88,7 @@ struct AlarmSettingsView: View {
let sound = NotificationSound(rawValue: $0)!

store.dispatch(.setLowGlucoseAlarmSound(sound: sound))
DirectNotifications.shared.testSound(sound: sound)
DirectNotifications.shared.testSound(sound: sound, volume: store.state.alarmVolume)
}
)
}
Expand All @@ -75,7 +100,7 @@ struct AlarmSettingsView: View {
let sound = NotificationSound(rawValue: $0)!

store.dispatch(.setHighGlucoseAlarmSound(sound: sound))
DirectNotifications.shared.testSound(sound: sound)
DirectNotifications.shared.testSound(sound: sound, volume: store.state.alarmVolume)
}
)
}
Expand All @@ -87,7 +112,7 @@ struct AlarmSettingsView: View {
let sound = NotificationSound(rawValue: $0)!

store.dispatch(.setConnectionAlarmSound(sound: sound))
DirectNotifications.shared.testSound(sound: sound)
DirectNotifications.shared.testSound(sound: sound, volume: store.state.alarmVolume)
}
)
}
Expand All @@ -99,7 +124,7 @@ struct AlarmSettingsView: View {
let sound = NotificationSound(rawValue: $0)!

store.dispatch(.setExpiringAlarmSound(sound: sound))
DirectNotifications.shared.testSound(sound: sound)
DirectNotifications.shared.testSound(sound: sound, volume: store.state.alarmVolume)
}
)
}
Expand Down
Loading

0 comments on commit 6467214

Please sign in to comment.