Skip to content

Commit 988a854

Browse files
committed
Manual sorting
1 parent 6301b30 commit 988a854

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

2002/dev_2002.patch

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ index 0b4cb551..9d97f342 100644
5252
ForEach(pluginMenuItems.filter {$0.section == .configuration}) { item in
5353
item.view
5454
}
55-
Submodule LoopKit 8e58b7e..b13348b:
55+
Submodule LoopKit 8e58b7e..91a0054:
5656
diff --git a/LoopKit/LoopKit.xcodeproj/project.pbxproj b/LoopKit/LoopKit.xcodeproj/project.pbxproj
5757
index bfdc9db..7dded80 100644
5858
--- a/LoopKit/LoopKit.xcodeproj/project.pbxproj
@@ -154,10 +154,10 @@ index bfdc9db..7dded80 100644
154154
B41A60AF23D1DB5B00636320 /* TableViewTitleLabel.swift in Sources */,
155155
diff --git a/LoopKit/LoopKitUI/ViewModels/ProfileViewModel+FileManagement.swift b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel+FileManagement.swift
156156
new file mode 100644
157-
index 0000000..57f11a5
157+
index 0000000..dc23e92
158158
--- /dev/null
159159
+++ b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel+FileManagement.swift
160-
@@ -0,0 +1,308 @@
160+
@@ -0,0 +1,333 @@
161161
+//
162162
+// ProfileViewModel+FileManagement.swift
163163
+// LoopKitUI
@@ -260,7 +260,8 @@ index 0000000..57f11a5
260260
+ correctionRange: (therapySettings.glucoseTargetRangeSchedule?.schedule(for: .milligramsPerDeciliter)!)!,
261261
+ carbRatioSchedule: therapySettings.carbRatioSchedule!,
262262
+ basalRateSchedule: therapySettings.basalRateSchedule!,
263-
+ insulinSensitivitySchedule: (therapySettings.insulinSensitivitySchedule?.schedule(for: .milligramsPerDeciliter)!)!
263+
+ insulinSensitivitySchedule: (therapySettings.insulinSensitivitySchedule?.schedule(for: .milligramsPerDeciliter)!)!,
264+
+ sortOrder: -1
264265
+ )
265266
+
266267
+ let jsonData = try encodeProfile(profile)
@@ -297,7 +298,8 @@ index 0000000..57f11a5
297298
+ correctionRange: profile.correctionRange,
298299
+ carbRatioSchedule: profile.carbRatioSchedule,
299300
+ basalRateSchedule: profile.basalRateSchedule,
300-
+ insulinSensitivitySchedule: profile.insulinSensitivitySchedule
301+
+ insulinSensitivitySchedule: profile.insulinSensitivitySchedule,
302+
+ sortOrder: profile.sortOrder
301303
+ )
302304
+
303305
+ if let existingProfile = getProfileReference(withName: newName) {
@@ -314,6 +316,30 @@ index 0000000..57f11a5
314316
+ print("An error occurred while renaming profile: \(error)")
315317
+ }
316318
+ }
319+
+
320+
+ public func updateProfilesOrder() {
321+
+ for (index, profileReference) in profiles.enumerated() {
322+
+ do {
323+
+ var profile = try getProfile(from: profileReference)
324+
+ let newProfile = Profile(
325+
+ name: profile.name,
326+
+ correctionRange: profile.correctionRange,
327+
+ carbRatioSchedule: profile.carbRatioSchedule,
328+
+ basalRateSchedule: profile.basalRateSchedule,
329+
+ insulinSensitivitySchedule: profile.insulinSensitivitySchedule,
330+
+ sortOrder: index
331+
+ )
332+
+
333+
+ let jsonData = try encodeProfile(newProfile)
334+
+ let fileURL = profilesDirectory.appendingPathComponent(profileReference.fileName)
335+
+ try jsonData.write(to: fileURL)
336+
+
337+
+ } catch {
338+
+ print("An error occurred while updating profile order: \(error)")
339+
+ }
340+
+ }
341+
+ loadProfiles()
342+
+ }
317343
+
318344
+ public func loadProfiles() {
319345
+ do {
@@ -323,12 +349,11 @@ index 0000000..57f11a5
323349
+ for fileURL in profileFiles {
324350
+ let data = try Data(contentsOf: fileURL)
325351
+ let profile = try decodeProfile(from: data)
326-
+ let profileRef = ProfileReference(name: profile.name, fileName: fileURL.lastPathComponent)
352+
+ let profileRef = ProfileReference(name: profile.name, fileName: fileURL.lastPathComponent, sortOrder: profile.sortOrder)
327353
+ newProfiles.append(profileRef)
328354
+ }
329355
+
330-
+ // Sort the profiles alphabetically by their name
331-
+ newProfiles.sort { $0.name < $1.name }
356+
+ newProfiles.sort { $0.sortOrder ?? 0 < $1.sortOrder ?? 0 }
332357
+
333358
+ self.profiles = newProfiles
334359
+ } catch {
@@ -468,10 +493,10 @@ index 0000000..57f11a5
468493
+}
469494
diff --git a/LoopKit/LoopKitUI/ViewModels/ProfileViewModel.swift b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel.swift
470495
new file mode 100644
471-
index 0000000..fe9dd4e
496+
index 0000000..3fe0362
472497
--- /dev/null
473498
+++ b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel.swift
474-
@@ -0,0 +1,80 @@
499+
@@ -0,0 +1,82 @@
475500
+//
476501
+// ProfileViewModel.swift
477502
+// LoopKitUI
@@ -521,11 +546,13 @@ index 0000000..fe9dd4e
521546
+ let carbRatioSchedule: CarbRatioSchedule
522547
+ let basalRateSchedule: BasalRateSchedule
523548
+ let insulinSensitivitySchedule: InsulinSensitivitySchedule
549+
+ var sortOrder: Int?
524550
+}
525551
+
526552
+public struct ProfileReference: Codable, Equatable {
527553
+ var name: String
528554
+ var fileName: String
555+
+ var sortOrder: Int?
529556
+}
530557
+
531558
+
@@ -971,10 +998,10 @@ index 0000000..1192e88
971998
+}
972999
diff --git a/LoopKit/LoopKitUI/Views/Settings Editors/ProfileView.swift b/LoopKit/LoopKitUI/Views/Settings Editors/ProfileView.swift
9731000
new file mode 100644
974-
index 0000000..d8d16ff
1001+
index 0000000..e1acd9c
9751002
--- /dev/null
9761003
+++ b/LoopKit/LoopKitUI/Views/Settings Editors/ProfileView.swift
977-
@@ -0,0 +1,138 @@
1004+
@@ -0,0 +1,145 @@
9781005
+//
9791006
+// ProfileView.swift
9801007
+// LoopKitUI
@@ -996,7 +1023,7 @@ index 0000000..d8d16ff
9961023
+ @State private var isAddingNewProfile = false
9971024
+ @State private var selectedProfileIndex: Int? = nil
9981025
+ @State private var showUpdateAlert = false
999-
+
1026+
+ @State private var refreshID = UUID()
10001027
+ public init(viewModel: ProfileViewModel) {
10011028
+ self.viewModel = viewModel
10021029
+ }
@@ -1042,7 +1069,8 @@ index 0000000..d8d16ff
10421069
+ }
10431070
+ }
10441071
+ }
1045-
+ }.padding(.top, -15)
1072+
+ .onMove(perform: moveProfile)
1073+
+ }.padding(.top, -15).id(refreshID)
10461074
+
10471075
+ }
10481076
+ }
@@ -1085,6 +1113,12 @@ index 0000000..d8d16ff
10851113
+ }
10861114
+ }
10871115
+ }
1116+
+
1117+
+ func moveProfile(from source: IndexSet, to destination: Int) {
1118+
+ viewModel.profiles.move(fromOffsets: source, toOffset: destination)
1119+
+ refreshID = UUID()
1120+
+ viewModel.updateProfilesOrder()
1121+
+ }
10881122
+}
10891123
+
10901124
+struct ProfileView_Previews: PreviewProvider {

0 commit comments

Comments
 (0)