Skip to content

Commit

Permalink
add edit profiles
Browse files Browse the repository at this point in the history
Add edit based on PR nightscout#235
  • Loading branch information
avouspierre committed May 25, 2024
1 parent a4cd039 commit c0bb426
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ extension OverrideProfilesConfig {
@Published var profileName: String = ""
@Published var isPreset: Bool = false
@Published var presets: [OverrideProfil] = []
// @Published var selection: OverrideProfil?
@Published var advancedSettings: Bool = false
@Published var isfAndCr: Bool = true
@Published var isf: Bool = true
Expand Down Expand Up @@ -85,48 +84,96 @@ extension OverrideProfilesConfig {
presets = overrideStorage.presets()
}

func updatePreset(_ presetId: String) {
let overridePresetToSave = OverrideProfil(
id: presetId,
name: profileName,
duration: _indefinite ? nil : duration,
indefinite: _indefinite,
percentage: percentage,
target: override_target ? (units == .mmolL ? target.asMgdL : target) : 0,
advancedSettings: advancedSettings,
smbIsOff: smbIsOff,
isfAndCr: isfAndCr,
isf: isfAndCr ? false : isf,
cr: isfAndCr ? false : cr,
smbIsScheduledOff: smbIsScheduledOff,
start: smbIsScheduledOff ? start : nil,
end: smbIsScheduledOff ? end : nil,
smbMinutes: smbMinutes,
uamMinutes: uamMinutes
)

overrideStorage.storeOverridePresets([overridePresetToSave])
presets = overrideStorage.presets()
}

func selectProfile(id_: String) {
guard id_ != "" else { return }
_ = overrideStorage.applyOverridePreset(id_)
}

func savedSettings() {
func reset() {
percentage = 100
isEnabled = false
_indefinite = true
duration = 0
target = 0
override_target = false
smbIsOff = false
id = ""
profileName = ""
advancedSettings = false
isfAndCr = true
isf = true
cr = true
smbIsScheduledOff = false
start = 0
end = 23
smbMinutes = defaultSmbMinutes
uamMinutes = defaultUamMinutes
}

func displayCurrentOverride() {
guard let currentOverride = overrideStorage.current() else {
isEnabled = false
return
}

isEnabled = true
percentage = currentOverride.percentage ?? 100
_indefinite = currentOverride.indefinite ?? true
duration = currentOverride.duration ?? 0
smbIsOff = currentOverride.smbIsOff ?? false
advancedSettings = currentOverride.advancedSettings ?? false
isfAndCr = currentOverride.isfAndCr ?? true
smbIsScheduledOff = currentOverride.smbIsScheduledOff ?? false
displayOverrideProfil(profil: currentOverride)
}

func displayOverrideProfil(profil: OverrideProfil) {
percentage = profil.percentage ?? 100
_indefinite = profil.indefinite ?? true
duration = profil.duration ?? 0
smbIsOff = profil.smbIsOff ?? false
advancedSettings = profil.advancedSettings ?? false
isfAndCr = profil.isfAndCr ?? true
smbIsScheduledOff = profil.smbIsScheduledOff ?? false

if advancedSettings {
if !isfAndCr {
isf = currentOverride.isf ?? false
cr = currentOverride.cr ?? false
isf = profil.isf ?? false
cr = profil.cr ?? false
}
if smbIsScheduledOff {
start = currentOverride.start ?? 0
end = currentOverride.end ?? 0
start = profil.start ?? 0
end = profil.end ?? 0
}

smbMinutes = currentOverride.smbMinutes ?? defaultSmbMinutes
uamMinutes = currentOverride.uamMinutes ?? defaultUamMinutes
smbMinutes = profil.smbMinutes ?? defaultSmbMinutes
uamMinutes = profil.uamMinutes ?? defaultUamMinutes
}

let overrideTarget = currentOverride.target ?? 0
let overrideTarget = profil.target ?? 0
if overrideTarget != 0 {
override_target = true
target = units == .mmolL ? overrideTarget.asMmolL : overrideTarget
}
if !_indefinite {
let durationOverride = currentOverride.duration ?? 0
let date = currentOverride.createdAt ?? Date()
let durationOverride = profil.duration ?? 0
let date = profil.createdAt ?? Date()
duration = max(0, durationOverride + Decimal(Date().distance(to: date).minutes))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extension OverrideProfilesConfig {
@State private var showingDetail = false
@State private var alertSring = ""
@State var isSheetPresented: Bool = false
@State private var selectedPreset: OverrideProfil?
@State private var isEditSheetPresented: Bool = false

@Environment(\.dismiss) var dismiss

Expand Down Expand Up @@ -56,12 +58,40 @@ extension OverrideProfilesConfig {
Form {
if state.presets.isNotEmpty {
Section {
ForEach(state.presets) { preset in
ForEach(state.presets.indices, id: \.self) { index in
let preset = state.presets[index]
profilesView(for: preset)
}.onDelete(perform: removeProfile)
.listRowBackground(
isEditSheetPresented && preset == selectedPreset ?
Color.blue.opacity(0.1)
: .none
)
.swipeActions {
Button(role: .destructive) {
removeProfile(at: IndexSet(integer: index))
} label: {
Label("Delete", systemImage: "trash")
}

Button {
selectedPreset = preset
state.profileName = preset.name ?? ""
state.displayOverrideProfil(profil: selectedPreset!)
isEditSheetPresented = true
} label: {
Label("Edit", systemImage: "square.and.pencil")
}.tint(.blue)
}
}
}
}
Section {
if isEditSheetPresented {
HStack {
Text("Name of the profile").foregroundColor(.secondary)
TextField("", text: $state.profileName).multilineTextAlignment(.trailing)
}
}
VStack {
Slider(
value: $state.percentage,
Expand Down Expand Up @@ -228,9 +258,16 @@ extension OverrideProfilesConfig {
}
)
Button {
isSheetPresented = true
if isEditSheetPresented {
guard let selectedPreset = selectedPreset else { return }
state.updatePreset(selectedPreset.id)
isEditSheetPresented = false

} else {
isSheetPresented = true
}
}
label: { Text("Save as Profile") }
label: { isEditSheetPresented ? Text("Update the profile") : Text("Save as Profile") }
.tint(.orange)
.frame(maxWidth: .infinity, alignment: .trailing)
.buttonStyle(BorderlessButtonStyle())
Expand Down Expand Up @@ -259,10 +296,16 @@ extension OverrideProfilesConfig {
.tint(.red)
}
.onAppear(perform: configureView)
.onAppear { state.savedSettings() }
.onAppear { state.displayCurrentOverride() }
.navigationBarTitle("Profiles")
.navigationBarTitleDisplayMode(.automatic)
.navigationBarItems(leading: Button("Close", action: state.hideModal))
.navigationBarItems(
leading: Button("Close", action: state.hideModal),
trailing: isEditSheetPresented ? Button("Cancel", action: {
isEditSheetPresented = false
state.reset()
}) : nil
)
}

@ViewBuilder private func profilesView(for preset: OverrideProfil) -> some View {
Expand Down

0 comments on commit c0bb426

Please sign in to comment.