Quellcode durchsuchen

Basal profile UI

Ivan Valkou vor 5 Jahren
Ursprung
Commit
23e409995c

+ 2 - 2
FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorViewModel.swift

@@ -26,7 +26,7 @@ extension BasalProfileEditor {
 
         func add() {
             var selected = 0
-            var rate = 1
+            var rate = 0
             if let last = items.last {
                 selected = last.timeIndex + 1
                 rate = last.rateIndex
@@ -39,7 +39,7 @@ extension BasalProfileEditor {
 
         func save() {}
 
-        func itemsDidChange() {
+        func validate() {
             DispatchQueue.main.async {
                 let uniq = Array(Set(self.items))
                 let sorted = uniq.sorted { $0.timeIndex < $1.timeIndex }

+ 31 - 19
FreeAPS/Sources/Modules/BasalProfileEditor/View/BasalProfileEditorRootView.swift

@@ -22,9 +22,19 @@ extension BasalProfileEditor {
             Form {
                 Section(header: Text("Schedule")) {
                     List {
-                        ForEach(viewModel.items.indexed(), id: \.1.id) { index, _ in
+                        ForEach(viewModel.items.indexed(), id: \.1.id) { index, item in
                             NavigationLink(destination: pickers(for: index)) {
-                                Text("text")
+                                HStack {
+                                    Text("Rate").foregroundColor(.secondary)
+                                    Text(
+                                        "\(rateFormatter.string(from: viewModel.rateValues[item.rateIndex] as NSNumber) ?? "0") U/h"
+                                    )
+                                    Spacer()
+                                    Text("starts at").foregroundColor(.secondary)
+                                    Text(
+                                        "\(dateFormatter.string(from: Date(timeIntervalSince1970: viewModel.timeValues[item.timeIndex])))"
+                                    )
+                                }
                             }
                             .moveDisabled(true)
                         }
@@ -37,7 +47,7 @@ extension BasalProfileEditor {
                     label: {
                         Text(viewModel.syncInProgress ? "Saving..." : "Save on Pump")
                     }
-                    .disabled(viewModel.syncInProgress)
+                    .disabled(viewModel.syncInProgress || viewModel.items.isEmpty)
                 }
             }
             .navigationTitle("Basal Profile")
@@ -47,38 +57,40 @@ extension BasalProfileEditor {
                 trailing: EditButton()
             )
             .environment(\.editMode, $editMode)
+            .onAppear {
+                viewModel.validate()
+            }
         }
 
         private func pickers(for index: Int) -> some View {
             GeometryReader { geometry in
                 VStack {
                     HStack {
-                        Text("Time").frame(width: geometry.size.width / 2)
                         Text("Rate").frame(width: geometry.size.width / 2)
+                        Text("Time").frame(width: geometry.size.width / 2)
                     }
                     HStack(spacing: 0) {
-                        Picker(selection: $viewModel.items[index].timeIndex, label: EmptyView()) {
-                            ForEach(0 ..< viewModel.timeValues.count, id: \.self) { i in
+                        Picker(selection: $viewModel.items[index].rateIndex, label: EmptyView()) {
+                            ForEach(0 ..< viewModel.rateValues.count, id: \.self) { i in
                                 Text(
-                                    self.dateFormatter
-                                        .string(from: Date(
-                                            timeIntervalSince1970: viewModel
-                                                .timeValues[i]
-                                        ))
+                                    (
+                                        self.rateFormatter
+                                            .string(from: viewModel.rateValues[i] as NSNumber) ?? ""
+                                    ) + " U/h"
                                 ).tag(i)
                             }
                         }
-                        .disabled(index == 0)
                         .frame(maxWidth: geometry.size.width / 2)
                         .clipped()
 
-                        Picker(selection: $viewModel.items[index].rateIndex, label: EmptyView()) {
-                            ForEach(0 ..< viewModel.rateValues.count, id: \.self) { i in
+                        Picker(selection: $viewModel.items[index].timeIndex, label: EmptyView()) {
+                            ForEach(0 ..< viewModel.timeValues.count, id: \.self) { i in
                                 Text(
-                                    (
-                                        self.rateFormatter
-                                            .string(from: viewModel.rateValues[i] as NSNumber) ?? ""
-                                    ) + " U/h"
+                                    self.dateFormatter
+                                        .string(from: Date(
+                                            timeIntervalSince1970: viewModel
+                                                .timeValues[i]
+                                        ))
                                 ).tag(i)
                             }
                         }
@@ -108,7 +120,7 @@ extension BasalProfileEditor {
 
         private func onDelete(offsets: IndexSet) {
             viewModel.items.remove(atOffsets: offsets)
-            viewModel.itemsDidChange()
+            viewModel.validate()
         }
     }
 }