Skip to content

Commit

Permalink
Merge branch 'dev' into Dana
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-b-m committed Dec 3, 2024
2 parents b938f90 + 8df45a4 commit 44fbe58
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Core_Data.xcdatamodeld/Core_Data.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23507" systemVersion="23G93" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="YES" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22758" systemVersion="23G93" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="YES" userDefinedModelVersionIdentifier="">
<entity name="ActiveProfile" representedClassName="ActiveProfile" syncable="YES" codeGenerationType="class">
<attribute name="active" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
Expand Down Expand Up @@ -116,7 +116,7 @@
</entity>
<entity name="Presets" representedClassName="Presets" syncable="YES" codeGenerationType="class">
<attribute name="carbs" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
<attribute name="dish" optional="YES" attributeType="String"/>
<attribute name="dish" optional="YES" attributeType="String" defaultValueString=" "/>
<attribute name="fat" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
<attribute name="protein" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
</entity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,9 @@ Enact a temp Basal or a temp target */
/* Search Saved Food presets */
"Search" = "Search";

/* Placeholder for name of dish when adding a new food preset */
"New" = "New";

/* */
"Empty" = "Empty";

Expand Down
124 changes: 61 additions & 63 deletions FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ extension AddCarbs {
@State private var presentPresets = false
@State private var string = ""
@State private var newPreset: (dish: String, carbs: Decimal, fat: Decimal, protein: Decimal) = ("", 0, 0, 0)

@FetchRequest(
entity: Presets.entity(),
sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)]
sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)], predicate: NSPredicate(
format: "dish != %@", " " as String
)
) var carbPresets: FetchedResults<Presets>

@Environment(\.managedObjectContext) var moc
Expand All @@ -47,7 +49,7 @@ extension AddCarbs {

Section {
// Saved Food presets
mealPresets
mealPresets.padding(.vertical, 9)

HStack {
Text("Carbs").fontWeight(.semibold)
Expand Down Expand Up @@ -105,9 +107,6 @@ extension AddCarbs {
label: { Image(systemName: "plus.circle") }.tint(.blue).buttonStyle(.borderless)
}
}
.popover(isPresented: $isPromptPresented) {
presetPopover
}
}
// Optional Hypo Treatment
if state.carbs > 0, let profile = state.id, profile != "None", state.carbsRequired != nil {
Expand Down Expand Up @@ -152,39 +151,6 @@ extension AddCarbs {
.sheet(isPresented: $presentPresets, content: { presetView })
}

private var presetPopover: some View {
Form {
Section {
TextField("Name Of Dish", text: $dish)
Button {
saved = true
if dish != "", saved {
let preset = Presets(context: moc)
preset.dish = dish
preset.fat = state.fat as NSDecimalNumber
preset.protein = state.protein as NSDecimalNumber
preset.carbs = state.carbs as NSDecimalNumber
try? moc.save()

state.selection = preset
if state.combinedPresets.isEmpty {
state.addPresetToNewMeal()
}

saved = false
isPromptPresented = false
}
}
label: { Text("Save") }
Button {
dish = ""
saved = false
isPromptPresented = false }
label: { Text("Cancel") }
} header: { Text("Enter Meal Preset Name") }
}.dynamicTypeSize(...DynamicTypeSize.xxLarge)
}

private var empty: Bool {
state.carbs <= 0 && state.fat <= 0 && state.protein <= 0
}
Expand Down Expand Up @@ -228,7 +194,7 @@ extension AddCarbs {
if !empty {
Section {
Button {
isPromptPresented = true
addfromCarbsView()
}
label: {
HStack {
Expand All @@ -242,20 +208,30 @@ extension AddCarbs {
header: { Text("Save") }
}

if carbPresets.count > 4 {
let filtered = carbPresets.filter { ($0.dish ?? "").count > 1 }.removeDublicates()
if filtered.count > 4 {
Section {
TextField("Search", text: $string)
} header: { Text("Search") }
}
let data = string.isEmpty ? carbPresets
.filter { _ in true } : carbPresets
let data = string.isEmpty ? filtered : carbPresets
.filter { ($0.dish ?? "").localizedCaseInsensitiveContains(string) }

Section {
ForEach(data, id: \.self) { preset in
presetsList(for: preset)
}.onDelete(perform: delete)
} header: { Text("Saved Food") }
} header: {
HStack {
Text("Saved Food")
Button {
state.presetToEdit = Presets(context: moc)
newPreset = (NSLocalizedString("New", comment: ""), 0, 0, 0)
state.edit = true
} label: { Image(systemName: "plus").font(.system(size: 22)) }
.buttonStyle(.borderless).frame(maxWidth: .infinity, alignment: .trailing)
}
}
}
.sheet(isPresented: $state.edit, content: { editView })
}
Expand All @@ -264,31 +240,32 @@ extension AddCarbs {
Form {
Section {
HStack {
TextField("\(state.presetToEdit?.dish ?? "")", text: $newPreset.dish)
TextField("", text: $newPreset.dish)
}
HStack {
Text("Carbs").foregroundStyle(.secondary)
Spacer()
DecimalTextField("\(state.presetToEdit?.carbs ?? 0)", value: $newPreset.carbs, formatter: formatter)
DecimalTextField("0", value: $newPreset.carbs, formatter: formatter)
}
HStack {
Text("Fat").foregroundStyle(.secondary)
Spacer()
DecimalTextField("\(state.presetToEdit?.fat ?? 0)", value: $newPreset.fat, formatter: formatter)
DecimalTextField("0", value: $newPreset.fat, formatter: formatter)
}
HStack {
Text("Protein").foregroundStyle(.secondary)
Spacer()
DecimalTextField("\(state.presetToEdit?.protein ?? 0)", value: $newPreset.protein, formatter: formatter)
DecimalTextField("0", value: $newPreset.protein, formatter: formatter)
}
} header: { Text("Edit") }
} header: { Text(!addingNew ? NSLocalizedString("Edit", comment: "") : NSLocalizedString("Add", comment: "")) }

Section {
Button { save() }
label: { Text("Save") }
.frame(maxWidth: .infinity, alignment: .center)
.listRowBackground(Color(.systemBlue))
.listRowBackground(!disabled ? Color(.systemBlue) : Color(.systemGray4))
.tint(.white)
.disabled(disabled)
}
}
}
Expand Down Expand Up @@ -324,7 +301,8 @@ extension AddCarbs {
@ViewBuilder private func presetsList(for preset: Presets) -> some View {
let dish = preset.dish ?? ""

if dish != "" {
// Only list saved entries
if !preset.hasChanges {
HStack {
VStack(alignment: .leading) {
Text(dish)
Expand Down Expand Up @@ -361,26 +339,25 @@ extension AddCarbs {
private var minusButton: some View {
Button {
state.subtract()

if empty {
state.selection = nil
state.combinedPresets = []
}
}
label: { Image(systemName: "minus.circle.fill")
}.frame(maxWidth: .infinity, alignment: .leading)
.buttonStyle(.borderless)
.disabled(state.selection == nil)
}
.buttonStyle(.borderless)
.disabled(state.selection == nil)
}

private var plusButton: some View {
Button {
state.plus()
}
label: { Image(systemName: "plus.circle.fill")
}.frame(maxWidth: .infinity, alignment: .trailing)
.buttonStyle(.borderless)
.disabled(state.selection == nil)
}
.buttonStyle(.borderless)
.disabled(state.selection == nil)
}

private func delete(at offsets: IndexSet) {
Expand All @@ -401,13 +378,20 @@ extension AddCarbs {
preset.carbs = newPreset.carbs as NSDecimalNumber
preset.fat = newPreset.fat as NSDecimalNumber
preset.protein = newPreset.protein as NSDecimalNumber
} else if !disabled {
let preset = Presets(context: moc)
preset.carbs = newPreset.carbs as NSDecimalNumber
preset.fat = newPreset.fat as NSDecimalNumber
preset.protein = newPreset.protein as NSDecimalNumber
preset.dish = newPreset.dish
}

if moc.hasChanges {
do {
try moc.save()
} catch { /* To do: add error */ }
}
if moc.hasChanges {
do {
try moc.save()
} catch { /* To do: add error */ }
}

state.edit = false
}

Expand All @@ -418,12 +402,26 @@ extension AddCarbs {
newPreset.protein = (state.presetToEdit?.protein ?? 0) as Decimal
}

private func addfromCarbsView() {
newPreset = (NSLocalizedString("New", comment: ""), state.carbs, state.fat, state.protein)
state.edit = true
}

private func reset() {
presentPresets = false
string = ""
state.presetToEdit = nil // Probably not needed
state.edit = false // Probably not needed
}

private var disabled: Bool {
(newPreset == (NSLocalizedString("New", comment: ""), 0, 0, 0)) || (newPreset.dish == "") ||
(newPreset.carbs + newPreset.fat + newPreset.protein <= 0)
}

private var addingNew: Bool {
((state.presetToEdit?.dish) == nil)
}
}
}

Expand Down

0 comments on commit 44fbe58

Please sign in to comment.