Skip to content

Commit

Permalink
Fixed issue with changing display items on the phone app.
Browse files Browse the repository at this point in the history
  • Loading branch information
msimms committed Sep 3, 2024
1 parent 65b34ad commit 136330c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
33 changes: 22 additions & 11 deletions View Models/LiveActivityVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RenderedActivityAttribute {
var title: String = ""
var value: String = ""
var units: String = ""
var position: Int = 0;
}

func attributeNameCallback(name: Optional<UnsafePointer<Int8>>, context: Optional<UnsafeMutableRawPointer>) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -124,25 +126,25 @@ 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
}

// 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)
Expand All @@ -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

Expand Down Expand Up @@ -347,62 +349,71 @@ class LiveActivityVM : ObservableObject {
if self.needsFullScreenRefresh {
self.attr1.title = activityAttribute
self.attr1.units = measureStr
self.attr1.position = index
}
break
case 1:
self.attr2.value = valueStr
if self.needsFullScreenRefresh {
self.attr2.title = activityAttribute
self.attr2.units = measureStr
self.attr2.position = index
}
break
case 2:
self.attr3.value = valueStr
if self.needsFullScreenRefresh {
self.attr3.title = activityAttribute
self.attr3.units = measureStr
self.attr3.position = index
}
break
case 3:
self.attr4.value = valueStr
if self.needsFullScreenRefresh {
self.attr4.title = activityAttribute
self.attr4.units = measureStr
self.attr4.position = index
}
break
case 4:
self.attr5.value = valueStr
if self.needsFullScreenRefresh {
self.attr5.title = activityAttribute
self.attr5.units = measureStr
self.attr5.position = index
}
break
case 5:
self.attr6.value = valueStr
if self.needsFullScreenRefresh {
self.attr6.title = activityAttribute
self.attr6.units = measureStr
self.attr6.position = index
}
break
case 6:
self.attr7.value = valueStr
if self.needsFullScreenRefresh {
self.attr7.title = activityAttribute
self.attr7.units = measureStr
self.attr7.position = index
}
break
case 7:
self.attr8.value = valueStr
if self.needsFullScreenRefresh {
self.attr8.title = activityAttribute
self.attr8.units = measureStr
self.attr8.position = index
}
break
case 8:
self.attr9.value = valueStr
if self.needsFullScreenRefresh {
self.attr9.title = activityAttribute
self.attr9.units = measureStr
self.attr9.position = index
}
break
default:
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions Watch App/ActivityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
71 changes: 37 additions & 34 deletions iPhone App/ActivityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
Expand All @@ -60,35 +62,34 @@ 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()
let storedActivityVM = StoredActivityVM(activitySummary: summary)
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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -122,18 +124,19 @@ 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)
.padding(1)
Text(attr.units)
.font(.system(size: 16))
.foregroundColor(labelColor)
}
})
}

var body: some View {
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 136330c

Please sign in to comment.