From 136330ccb3eb9e5d03c4346b7c37dcef87407e65 Mon Sep 17 00:00:00 2001 From: Mike Simms Date: Tue, 3 Sep 2024 08:43:04 -0400 Subject: [PATCH] Fixed issue with changing display items on the phone app. --- View Models/LiveActivityVM.swift | 33 ++++++++++----- Watch App/ActivityView.swift | 14 +++---- iPhone App/ActivityView.swift | 71 +++++++++++++++++--------------- 3 files changed, 66 insertions(+), 52 deletions(-) diff --git a/View Models/LiveActivityVM.swift b/View Models/LiveActivityVM.swift index c0267c2..9a216e5 100644 --- a/View Models/LiveActivityVM.swift +++ b/View Models/LiveActivityVM.swift @@ -24,6 +24,7 @@ class RenderedActivityAttribute { var title: String = "" var value: String = "" var units: String = "" + var position: Int = 0; } func attributeNameCallback(name: Optional>, context: Optional) { @@ -54,6 +55,7 @@ class LiveActivityVM : ObservableObject { @Published var viewType: ActivityViewType = ACTIVITY_VIEW_COMPLEX + var activityTypeToUse: String = "" var isInProgress: Bool = false var isPaused: Bool = false var isStopped: Bool = false // Has been stopped (after being started) @@ -86,8 +88,8 @@ class LiveActivityVM : ObservableObject { NotificationCenter.default.addObserver(self, selector: #selector(self.messageReceived), name: Notification.Name(rawValue: NOTIFICATION_NAME_PRINT_MESSAGE), object: nil) self.activityType = activityType + self.activityTypeToUse = activityType - var activityTypeToUse = activityType var orphanedActivityIndex: size_t = 0 var isNewActivity: Bool = true @@ -105,7 +107,7 @@ class LiveActivityVM : ObservableObject { let orphanedActivityType = String(cString: orphanedActivityTypePtr!.assumingMemoryBound(to: CChar.self)) if orphanedActivityType.count > 0 { - activityTypeToUse = orphanedActivityType + self.activityTypeToUse = orphanedActivityType if recreateOrphanedActivities { ReCreateOrphanedActivity(orphanedActivityIndex) @@ -124,7 +126,7 @@ class LiveActivityVM : ObservableObject { // Create the backend structures needed to do the activity. if isNewActivity == true { - CreateActivityObject(activityTypeToUse) + CreateActivityObject(self.activityTypeToUse) // Generate a unique identifier for this activity. self.activityId = NSUUID().uuidString @@ -132,17 +134,17 @@ class LiveActivityVM : ObservableObject { // Which attributes does the user wish to display when doing this activity? let activityPrefs = ActivityPreferences() - self.activityAttributePrefs = activityPrefs.getActivityLayout(activityType: activityTypeToUse) + self.activityAttributePrefs = activityPrefs.getActivityLayout(activityType: self.activityTypeToUse) // Preferred view layout. - self.viewType = ActivityPreferences.getDefaultViewForActivityType(activityType: activityTypeToUse) + self.viewType = ActivityPreferences.getDefaultViewForActivityType(activityType: self.activityTypeToUse) // Which sensors are useful? let sensorTypes = getUsableSensorTypes() // Configure the location accuracy parameters. - SensorMgr.shared.location.minAllowedHorizontalAccuracy = Double(ActivityPreferences.getMinLocationHorizontalAccuracy(activityType: activityTypeToUse)) - SensorMgr.shared.location.minAllowedVerticalAccuracy = Double(ActivityPreferences.getMinLocationVerticalAccuracy(activityType: activityTypeToUse)) + SensorMgr.shared.location.minAllowedHorizontalAccuracy = Double(ActivityPreferences.getMinLocationHorizontalAccuracy(activityType: self.activityTypeToUse)) + SensorMgr.shared.location.minAllowedVerticalAccuracy = Double(ActivityPreferences.getMinLocationVerticalAccuracy(activityType: self.activityTypeToUse)) // Start the sensors. SensorMgr.shared.startSensors(usableSensors: sensorTypes) @@ -169,8 +171,8 @@ class LiveActivityVM : ObservableObject { // Timer to periodically refresh the view. self.needsFullScreenRefresh = true - let startStopBeepEnabled = ActivityPreferences.getStartStopBeepEnabled(activityType: activityTypeToUse) - let splitBeepEnabled = ActivityPreferences.getSplitBeepEnabled(activityType: activityTypeToUse) + let startStopBeepEnabled = ActivityPreferences.getStartStopBeepEnabled(activityType: self.activityTypeToUse) + let splitBeepEnabled = ActivityPreferences.getSplitBeepEnabled(activityType: self.activityTypeToUse) let preferredUnits = Preferences.preferredUnitSystem() self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { tempTimer in @@ -347,6 +349,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr1.title = activityAttribute self.attr1.units = measureStr + self.attr1.position = index } break case 1: @@ -354,6 +357,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr2.title = activityAttribute self.attr2.units = measureStr + self.attr2.position = index } break case 2: @@ -361,6 +365,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr3.title = activityAttribute self.attr3.units = measureStr + self.attr3.position = index } break case 3: @@ -368,6 +373,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr4.title = activityAttribute self.attr4.units = measureStr + self.attr4.position = index } break case 4: @@ -375,6 +381,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr5.title = activityAttribute self.attr5.units = measureStr + self.attr5.position = index } break case 5: @@ -382,6 +389,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr6.title = activityAttribute self.attr6.units = measureStr + self.attr6.position = index } break case 6: @@ -389,6 +397,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr7.title = activityAttribute self.attr7.units = measureStr + self.attr7.position = index } break case 7: @@ -396,6 +405,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr8.title = activityAttribute self.attr8.units = measureStr + self.attr8.position = index } break case 8: @@ -403,6 +413,7 @@ class LiveActivityVM : ObservableObject { if self.needsFullScreenRefresh { self.attr9.title = activityAttribute self.attr9.units = measureStr + self.attr9.position = index } break default: @@ -623,8 +634,8 @@ class LiveActivityVM : ObservableObject { self.activityAttributePrefs.remove(at: position) self.activityAttributePrefs.insert(attributeName, at: position) self.needsFullScreenRefresh = true - - ActivityPreferences.setActivityLayout(activityType: self.activityType, layout: self.activityAttributePrefs) + + ActivityPreferences.setActivityLayout(activityType: self.activityTypeToUse, layout: self.activityAttributePrefs) } func setWatchActivityAttributeColor(attributeName: String, colorName: String) { diff --git a/Watch App/ActivityView.swift b/Watch App/ActivityView.swift index 6cd193f..0c5719c 100644 --- a/Watch App/ActivityView.swift +++ b/Watch App/ActivityView.swift @@ -125,7 +125,7 @@ struct ActivityView: View { self.showingActivityColorSelection1 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection1, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 0) + self.selectAttributeToDisplay(position: self.activityVM.attr1.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection1, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr1.title) @@ -161,7 +161,7 @@ struct ActivityView: View { self.showingActivityColorSelection2 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection2, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 1) + self.selectAttributeToDisplay(position: self.activityVM.attr2.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection2, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr2.title) @@ -182,7 +182,7 @@ struct ActivityView: View { self.showingActivityColorSelection3 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection3, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 2) + self.selectAttributeToDisplay(position: self.activityVM.attr3.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection3, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr3.title) @@ -207,7 +207,7 @@ struct ActivityView: View { self.showingActivityColorSelection4 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection4, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 3) + self.selectAttributeToDisplay(position: self.activityVM.attr4.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection4, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr4.title) @@ -228,7 +228,7 @@ struct ActivityView: View { self.showingActivityColorSelection5 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection5, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 4) + self.selectAttributeToDisplay(position: self.activityVM.attr5.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection5, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr5.title) @@ -253,7 +253,7 @@ struct ActivityView: View { self.showingActivityColorSelection6 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection6, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 5) + self.selectAttributeToDisplay(position: self.activityVM.attr6.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection6, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr6.title) @@ -274,7 +274,7 @@ struct ActivityView: View { self.showingActivityColorSelection7 = self.canShowAttributeMenu() } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection7, titleVisibility: .visible) { - self.selectAttributeToDisplay(position: 6) + self.selectAttributeToDisplay(position: self.activityVM.attr7.position) } .confirmationDialog("Select the color to use", isPresented: self.$showingActivityColorSelection7, titleVisibility: .visible) { self.selectColorToUse(attributeName: self.activityVM.attr7.title) diff --git a/iPhone App/ActivityView.swift b/iPhone App/ActivityView.swift index 21183ec..01983a5 100644 --- a/iPhone App/ActivityView.swift +++ b/iPhone App/ActivityView.swift @@ -7,6 +7,7 @@ import SwiftUI import MapKit let MAX_THREAT_DISTANCE_METERS = 160.0 +var selectedItem: Int = 0 struct ActivityIndicator: UIViewRepresentable { typealias UIView = UIActivityIndicatorView @@ -51,6 +52,7 @@ struct ActivityView: View { @State private var showingStartError: Bool = false @State private var showingExtraWeightAlert: Bool = false @State private var additionalWeight: NumbersOnly = NumbersOnly(initialDoubleValue: 0.0) + @State private var tappedAttr: RenderedActivityAttribute = RenderedActivityAttribute() var sensorMgr = SensorMgr.shared var broadcastMgr = BroadcastManager.shared var activityType: String = "" @@ -60,26 +62,25 @@ struct ActivityView: View { Array(repeating: .init(.adaptive(minimum: 120)), count: 2) } - func selectAttributeToDisplay(position: Int) -> some View { - return VStack() { - Button("Cancel") {} + func selectAttributeToDisplay() -> some View { + return VStack(content: { ForEach(self.activityVM.getActivityAttributeNames(), id: \.self) { item in - Button { - self.activityVM.setDisplayedActivityAttributeName(position: position, attributeName: item) - } label: { + Button(action: { + self.activityVM.setDisplayedActivityAttributeName(position: self.tappedAttr.position, attributeName: item) + }) { Text(item) } } - } + }) } - + func canShowAttributeMenu() -> Bool { if !IsActivityInProgress() { return true } return ActivityPreferences.getAllowScreenPressesDuringActivity(activityType: self.activityType) } - + func stop() -> StoredActivityVM { self.stopping = true let summary = self.activityVM.stop() @@ -87,8 +88,8 @@ struct ActivityView: View { storedActivityVM.load() return storedActivityVM } - - func renderItem(position: Int, attr: RenderedActivityAttribute, labelColor: Color, textColor: Color) -> some View { + + func renderItem(attr: RenderedActivityAttribute, labelColor: Color, textColor: Color) -> some View { VStack(alignment: .center) { Text(attr.title) .font(.system(size: 16)) @@ -98,9 +99,10 @@ struct ActivityView: View { .foregroundColor(self.colorScheme == .dark ? .white : textColor) .onTapGesture { self.showingActivityAttributeSelection = self.canShowAttributeMenu() + self.tappedAttr = attr } .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection, titleVisibility: .visible) { - selectAttributeToDisplay(position: position) + self.selectAttributeToDisplay() } .allowsTightening(true) .lineLimit(1) @@ -112,8 +114,8 @@ struct ActivityView: View { } } - func renderLargeItem(position: Int, attr: RenderedActivityAttribute, labelColor: Color, textColor: Color) -> some View { - VStack(alignment: .center) { + func renderLargeItem(attr: RenderedActivityAttribute, labelColor: Color, textColor: Color) -> some View { + VStack(alignment: .center, content: { Text(attr.title) .font(.system(size: 16)) .foregroundColor(labelColor) @@ -122,10 +124,11 @@ struct ActivityView: View { .foregroundColor(self.colorScheme == .dark ? .white : textColor) .onTapGesture { self.showingActivityAttributeSelection = self.canShowAttributeMenu() + self.tappedAttr = attr } - .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection, titleVisibility: .visible) { - selectAttributeToDisplay(position: position) - } + .confirmationDialog("Select the attribute to display", isPresented: self.$showingActivityAttributeSelection, titleVisibility: .visible, actions: { + self.selectAttributeToDisplay() + }) .allowsTightening(true) .lineLimit(1) .minimumScaleFactor(0.75) @@ -133,7 +136,7 @@ struct ActivityView: View { Text(attr.units) .font(.system(size: 16)) .foregroundColor(labelColor) - } + }) } var body: some View { @@ -175,7 +178,7 @@ struct ActivityView: View { // Main value VStack(alignment: .center) { - self.renderLargeItem(position: 0, attr: self.activityVM.attr1, labelColor: labelColor, textColor: textColor) + self.renderLargeItem(attr: self.activityVM.attr1, labelColor: labelColor, textColor: textColor) } .padding(20) @@ -190,14 +193,14 @@ struct ActivityView: View { else if self.activityVM.viewType == ACTIVITY_VIEW_COMPLEX { ScrollView(.vertical, showsIndicators: false) { LazyVGrid(columns: self.items, spacing: 20) { - self.renderItem(position: 1, attr: self.activityVM.attr2, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 2, attr: self.activityVM.attr3, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 3, attr: self.activityVM.attr4, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 4, attr: self.activityVM.attr5, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 5, attr: self.activityVM.attr6, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 6, attr: self.activityVM.attr7, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 7, attr: self.activityVM.attr8, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 8, attr: self.activityVM.attr9, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr2, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr3, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr4, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr5, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr6, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr7, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr8, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr9, labelColor: labelColor, textColor: textColor) } .padding(.horizontal) } @@ -207,9 +210,9 @@ struct ActivityView: View { else if self.activityVM.viewType == ACTIVITY_VIEW_SIMPLE { ScrollView(.vertical, showsIndicators: false) { VStack(alignment: .center) { - self.renderLargeItem(position: 1, attr: self.activityVM.attr2, labelColor: labelColor, textColor: textColor) - self.renderLargeItem(position: 2, attr: self.activityVM.attr3, labelColor: labelColor, textColor: textColor) - self.renderLargeItem(position: 3, attr: self.activityVM.attr4, labelColor: labelColor, textColor: textColor) + self.renderLargeItem(attr: self.activityVM.attr2, labelColor: labelColor, textColor: textColor) + self.renderLargeItem(attr: self.activityVM.attr3, labelColor: labelColor, textColor: textColor) + self.renderLargeItem(attr: self.activityVM.attr4, labelColor: labelColor, textColor: textColor) } .padding(.horizontal) Spacer() @@ -220,10 +223,10 @@ struct ActivityView: View { else if self.activityVM.viewType == ACTIVITY_VIEW_MAPPED { ScrollView(.vertical, showsIndicators: false) { LazyVGrid(columns: self.items, spacing: 20) { - self.renderItem(position: 1, attr: self.activityVM.attr2, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 2, attr: self.activityVM.attr3, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 3, attr: self.activityVM.attr4, labelColor: labelColor, textColor: textColor) - self.renderItem(position: 4, attr: self.activityVM.attr5, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr2, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr3, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr4, labelColor: labelColor, textColor: textColor) + self.renderItem(attr: self.activityVM.attr5, labelColor: labelColor, textColor: textColor) } .padding(.horizontal) Spacer()