@@ -52,7 +52,7 @@ index 0b4cb551..9d97f342 100644
52
52
ForEach(pluginMenuItems.filter {$0.section == .configuration}) { item in
53
53
item.view
54
54
}
55
- Submodule LoopKit 8e58b7e..b13348b :
55
+ Submodule LoopKit 8e58b7e..91a0054 :
56
56
diff --git a/LoopKit/LoopKit.xcodeproj/project.pbxproj b/LoopKit/LoopKit.xcodeproj/project.pbxproj
57
57
index bfdc9db..7dded80 100644
58
58
--- a/LoopKit/LoopKit.xcodeproj/project.pbxproj
@@ -154,10 +154,10 @@ index bfdc9db..7dded80 100644
154
154
B41A60AF23D1DB5B00636320 /* TableViewTitleLabel.swift in Sources */,
155
155
diff --git a/LoopKit/LoopKitUI/ViewModels/ProfileViewModel+FileManagement.swift b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel+FileManagement.swift
156
156
new file mode 100644
157
- index 0000000..57f11a5
157
+ index 0000000..dc23e92
158
158
--- /dev/null
159
159
+++ b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel+FileManagement.swift
160
- @@ -0,0 +1,308 @@
160
+ @@ -0,0 +1,333 @@
161
161
+ //
162
162
+ // ProfileViewModel+FileManagement.swift
163
163
+ // LoopKitUI
@@ -260,7 +260,8 @@ index 0000000..57f11a5
260
260
+ correctionRange: (therapySettings.glucoseTargetRangeSchedule?.schedule(for: .milligramsPerDeciliter)!)!,
261
261
+ carbRatioSchedule: therapySettings.carbRatioSchedule!,
262
262
+ basalRateSchedule: therapySettings.basalRateSchedule!,
263
- + insulinSensitivitySchedule: (therapySettings.insulinSensitivitySchedule?.schedule(for: .milligramsPerDeciliter)!)!
263
+ + insulinSensitivitySchedule: (therapySettings.insulinSensitivitySchedule?.schedule(for: .milligramsPerDeciliter)!)!,
264
+ + sortOrder: -1
264
265
+ )
265
266
+
266
267
+ let jsonData = try encodeProfile(profile)
@@ -297,7 +298,8 @@ index 0000000..57f11a5
297
298
+ correctionRange: profile.correctionRange,
298
299
+ carbRatioSchedule: profile.carbRatioSchedule,
299
300
+ basalRateSchedule: profile.basalRateSchedule,
300
- + insulinSensitivitySchedule: profile.insulinSensitivitySchedule
301
+ + insulinSensitivitySchedule: profile.insulinSensitivitySchedule,
302
+ + sortOrder: profile.sortOrder
301
303
+ )
302
304
+
303
305
+ if let existingProfile = getProfileReference(withName: newName) {
@@ -314,6 +316,30 @@ index 0000000..57f11a5
314
316
+ print("An error occurred while renaming profile: \(error)")
315
317
+ }
316
318
+ }
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
+ + }
317
343
+
318
344
+ public func loadProfiles() {
319
345
+ do {
@@ -323,12 +349,11 @@ index 0000000..57f11a5
323
349
+ for fileURL in profileFiles {
324
350
+ let data = try Data(contentsOf: fileURL)
325
351
+ 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 )
327
353
+ newProfiles.append(profileRef)
328
354
+ }
329
355
+
330
- + // Sort the profiles alphabetically by their name
331
- + newProfiles.sort { $0.name < $1.name }
356
+ + newProfiles.sort { $0.sortOrder ?? 0 < $1.sortOrder ?? 0 }
332
357
+
333
358
+ self.profiles = newProfiles
334
359
+ } catch {
@@ -468,10 +493,10 @@ index 0000000..57f11a5
468
493
+ }
469
494
diff --git a/LoopKit/LoopKitUI/ViewModels/ProfileViewModel.swift b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel.swift
470
495
new file mode 100644
471
- index 0000000..fe9dd4e
496
+ index 0000000..3fe0362
472
497
--- /dev/null
473
498
+++ b/LoopKit/LoopKitUI/ViewModels/ProfileViewModel.swift
474
- @@ -0,0 +1,80 @@
499
+ @@ -0,0 +1,82 @@
475
500
+ //
476
501
+ // ProfileViewModel.swift
477
502
+ // LoopKitUI
@@ -521,11 +546,13 @@ index 0000000..fe9dd4e
521
546
+ let carbRatioSchedule: CarbRatioSchedule
522
547
+ let basalRateSchedule: BasalRateSchedule
523
548
+ let insulinSensitivitySchedule: InsulinSensitivitySchedule
549
+ + var sortOrder: Int?
524
550
+ }
525
551
+
526
552
+ public struct ProfileReference: Codable, Equatable {
527
553
+ var name: String
528
554
+ var fileName: String
555
+ + var sortOrder: Int?
529
556
+ }
530
557
+
531
558
+
@@ -971,10 +998,10 @@ index 0000000..1192e88
971
998
+ }
972
999
diff --git a/LoopKit/LoopKitUI/Views/Settings Editors/ProfileView.swift b/LoopKit/LoopKitUI/Views/Settings Editors/ProfileView.swift
973
1000
new file mode 100644
974
- index 0000000..d8d16ff
1001
+ index 0000000..e1acd9c
975
1002
--- /dev/null
976
1003
+++ b/LoopKit/LoopKitUI/Views/Settings Editors/ProfileView.swift
977
- @@ -0,0 +1,138 @@
1004
+ @@ -0,0 +1,145 @@
978
1005
+ //
979
1006
+ // ProfileView.swift
980
1007
+ // LoopKitUI
@@ -996,7 +1023,7 @@ index 0000000..d8d16ff
996
1023
+ @State private var isAddingNewProfile = false
997
1024
+ @State private var selectedProfileIndex: Int? = nil
998
1025
+ @State private var showUpdateAlert = false
999
- +
1026
+ + @State private var refreshID = UUID()
1000
1027
+ public init(viewModel: ProfileViewModel) {
1001
1028
+ self.viewModel = viewModel
1002
1029
+ }
@@ -1042,7 +1069,8 @@ index 0000000..d8d16ff
1042
1069
+ }
1043
1070
+ }
1044
1071
+ }
1045
- + }.padding(.top, -15)
1072
+ + .onMove(perform: moveProfile)
1073
+ + }.padding(.top, -15).id(refreshID)
1046
1074
+
1047
1075
+ }
1048
1076
+ }
@@ -1085,6 +1113,12 @@ index 0000000..d8d16ff
1085
1113
+ }
1086
1114
+ }
1087
1115
+ }
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
+ + }
1088
1122
+ }
1089
1123
+
1090
1124
+ struct ProfileView_Previews: PreviewProvider {
0 commit comments