Repository: expo/expo
File: packages/expo-widgets/ios/WidgetsModule.swift (Swift)

Code before:
      }

      WidgetsStorage.set(compressedData, forKey: "__expo_widgets_live_activity_\(name)")

      Task {
        let newState = LiveActivityAttributes.ContentState(name: name)
        await activity.update(ActivityContent(state: newState, staleDate: nil))
      }
    }
  }

  @objc func handleUserInteractionNotification(_ notification: Notification) {
    guard let userInfo = notification.userInfo as? [String: Any],
          let eventData = userInfo["eventData"] as? [String: Any]
    else { return }
    self.sendEvent(onUserInteraction, eventData)
  }
  
  private func sendPushToStartToken(activityPushToStartToken: String) {
    sendEvent(
      onPushToStartTokenReceived,
      [
        "activityPushToStartToken": activityPushToStartToken,
      ]
    )
  }

  private func observePushToStartToken() {
    guard #available(iOS 17.2, *), ActivityAuthorizationInfo().areActivitiesEnabled else { return }
    
    if let initialToken = (Activity<LiveActivityAttributes>.pushToStartToken?.reduce("") { $0 + String(format: "%02x", $1) }) {
      sendPushToStartToken(activityPushToStartToken: initialToken)
    }
    
    pushToStartTokenObserverTask = Task {
      for await data in Activity<LiveActivityAttributes>.pushToStartTokenUpdates {
        let token = data.reduce("") { $0 + String(format: "%02x", $1) }
        sendPushToStartToken(activityPushToStartToken: token)
      }
    }
  }

  private var pushNotificationsEnabled: Bool {
    Bundle.main.object(forInfoDictionaryKey: "ExpoWidgets_EnablePushNotifications") as? Bool ?? false
  }
}


Review comment (suggestion):
If possible, I'd turn this into a Record (no magic keys). I'm not 100% sure if records work with events though, you may have to do `record.toDictionary()` or something like that.

Code after:
      guard let compressedData = try nodesData?.brotliCompressed() else {
        throw LiveActivitiesNotSupportedException()
      }

      WidgetsStorage.set(compressedData, forKey: "__expo_widgets_live_activity_\(name)")

      Task {
        let newState = LiveActivityAttributes.ContentState(name: name)
        await activity.update(ActivityContent(state: newState, staleDate: nil))
      }
    }
  }

  @objc func handleUserInteractionNotification(_ notification: Notification) {
    guard let userInfo = notification.userInfo as? [String: Any],
          let eventData = userInfo["eventData"] as? [String: Any]
    else { return }
    self.sendEvent(onUserInteraction, eventData)
  }
  
  private func sendPushToStartToken(activityPushToStartToken: String) {
    sendEvent(
      onPushToStartTokenReceived,
      [
        "activityPushToStartToken": activityPushToStartToken,
      ]
    )
  }

  private func observePushToStartToken() {
    guard #available(iOS 17.2, *), ActivityAuthorizationInfo().areActivitiesEnabled else { return }
    
    if let initialToken = (Activity<LiveActivityAttributes>.pushToStartToken?.reduce("") { $0 + String(format: "%02x", $1) }) {
      sendPushToStartToken(activityPushToStartToken: initialToken)
    }
    
    pushToStartTokenObserverTask = Task {
      for await data in Activity<LiveActivityAttributes>.pushToStartTokenUpdates {
        let token = data.reduce("") { $0 + String(format: "%02x", $1) }
        sendPushToStartToken(activityPushToStartToken: token)
      }
    }
  }

  private var pushNotificationsEnabled: Bool {
    Bundle.main.object(forInfoDictionaryKey: pushNotificationsEnabledKey) as? Bool ?? false
  }
}