HealthKitApi.swift 13.6 KB
// // Copyright 2013 The Flutter Authors. All rights reserved.
// Autogenerated from Pigeon (v25.5.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon

import Foundation

#if os(iOS)
  import Flutter
#elseif os(macOS)
  import FlutterMacOS
#else
  #error("Unsupported platform.")
#endif


private func wrapResult(_ result: Any?) -> [Any?] {
  return [result]
}

private func wrapError(_ error: Any) -> [Any?] {
  if let pigeonError = error as? PigeonError {
    return [
      pigeonError.code,
      pigeonError.message,
      pigeonError.details,
    ]
  }
  if let flutterError = error as? FlutterError {
    return [
      flutterError.code,
      flutterError.message,
      flutterError.details,
    ]
  }
  return [
    "\(error)",
    "\(type(of: error))",
    "Stacktrace: \(Thread.callStackSymbols)",
  ]
}

private func isNullish(_ value: Any?) -> Bool {
  return value is NSNull || value == nil
}

private func nilOrValue<T>(_ value: Any?) -> T? {
  if value is NSNull { return nil }
  return value as! T?
}

func deepEqualsHealthKitApi(_ lhs: Any?, _ rhs: Any?) -> Bool {
  let cleanLhs = nilOrValue(lhs) as Any?
  let cleanRhs = nilOrValue(rhs) as Any?
  switch (cleanLhs, cleanRhs) {
  case (nil, nil):
    return true

  case (nil, _), (_, nil):
    return false

  case is (Void, Void):
    return true

  case let (cleanLhsHashable, cleanRhsHashable) as (AnyHashable, AnyHashable):
    return cleanLhsHashable == cleanRhsHashable

  case let (cleanLhsArray, cleanRhsArray) as ([Any?], [Any?]):
    guard cleanLhsArray.count == cleanRhsArray.count else { return false }
    for (index, element) in cleanLhsArray.enumerated() {
      if !deepEqualsHealthKitApi(element, cleanRhsArray[index]) {
        return false
      }
    }
    return true

  case let (cleanLhsDictionary, cleanRhsDictionary) as ([AnyHashable: Any?], [AnyHashable: Any?]):
    guard cleanLhsDictionary.count == cleanRhsDictionary.count else { return false }
    for (key, cleanLhsValue) in cleanLhsDictionary {
      guard cleanRhsDictionary.index(forKey: key) != nil else { return false }
      if !deepEqualsHealthKitApi(cleanLhsValue, cleanRhsDictionary[key]!) {
        return false
      }
    }
    return true

  default:
    // Any other type shouldn't be able to be used with pigeon. File an issue if you find this to be untrue.
    return false
  }
}

func deepHashHealthKitApi(value: Any?, hasher: inout Hasher) {
  if let valueList = value as? [AnyHashable] {
     for item in valueList { deepHashHealthKitApi(value: item, hasher: &hasher) }
     return
  }

  if let valueDict = value as? [AnyHashable: AnyHashable] {
    for key in valueDict.keys { 
      hasher.combine(key)
      deepHashHealthKitApi(value: valueDict[key]!, hasher: &hasher)
    }
    return
  }

  if let hashableValue = value as? AnyHashable {
    hasher.combine(hashableValue.hashValue)
  }

  return hasher.combine(String(describing: value))
}

    

/// Generated class from Pigeon that represents data sent in messages.
struct HealthAuthorization: Hashable {
  /// -1: AppleHealth不可用, 0: 需要请求授权, 1: 已被授权且能获取到部分数据, 2: 已被拒绝或者没有数据
  var status: Int64
  var hasData: Bool


  // swift-format-ignore: AlwaysUseLowerCamelCase
  static func fromList(_ pigeonVar_list: [Any?]) -> HealthAuthorization? {
    let status = pigeonVar_list[0] as! Int64
    let hasData = pigeonVar_list[1] as! Bool

    return HealthAuthorization(
      status: status,
      hasData: hasData
    )
  }
  func toList() -> [Any?] {
    return [
      status,
      hasData,
    ]
  }
  static func == (lhs: HealthAuthorization, rhs: HealthAuthorization) -> Bool {
    return deepEqualsHealthKitApi(lhs.toList(), rhs.toList())  }
  func hash(into hasher: inout Hasher) {
    deepHashHealthKitApi(value: toList(), hasher: &hasher)
  }
}

/// The user's current Huawei Health activity-ring goals.
///
/// Units: move is kcal, exercise is seconds, and stand is hours.
///
/// Generated class from Pigeon that represents data sent in messages.
struct HealthActivityGoal: Hashable {
  var move: Int64? = nil
  var step: Int64? = nil
  var stand: Int64? = nil
  var exercise: Int64? = nil


  // swift-format-ignore: AlwaysUseLowerCamelCase
  static func fromList(_ pigeonVar_list: [Any?]) -> HealthActivityGoal? {
    let move: Int64? = nilOrValue(pigeonVar_list[0])
    let step: Int64? = nilOrValue(pigeonVar_list[1])
    let stand: Int64? = nilOrValue(pigeonVar_list[2])
    let exercise: Int64? = nilOrValue(pigeonVar_list[3])

    return HealthActivityGoal(
      move: move,
      step: step,
      stand: stand,
      exercise: exercise
    )
  }
  func toList() -> [Any?] {
    return [
      move,
      step,
      stand,
      exercise,
    ]
  }
  static func == (lhs: HealthActivityGoal, rhs: HealthActivityGoal) -> Bool {
    return deepEqualsHealthKitApi(lhs.toList(), rhs.toList())  }
  func hash(into hasher: inout Hasher) {
    deepHashHealthKitApi(value: toList(), hasher: &hasher)
  }
}

/// One workout record read from Health Service Kit.
///
/// Generated class from Pigeon that represents data sent in messages.
struct HealthWorkoutDataPoint: Hashable {
  /// Unix timestamp in seconds.
  var startTime: Int64
  /// Unix timestamp in seconds.
  var endTime: Int64
  /// The native Health Service Kit exercise-type identifier.
  var activityType: Int64


  // swift-format-ignore: AlwaysUseLowerCamelCase
  static func fromList(_ pigeonVar_list: [Any?]) -> HealthWorkoutDataPoint? {
    let startTime = pigeonVar_list[0] as! Int64
    let endTime = pigeonVar_list[1] as! Int64
    let activityType = pigeonVar_list[2] as! Int64

    return HealthWorkoutDataPoint(
      startTime: startTime,
      endTime: endTime,
      activityType: activityType
    )
  }
  func toList() -> [Any?] {
    return [
      startTime,
      endTime,
      activityType,
    ]
  }
  static func == (lhs: HealthWorkoutDataPoint, rhs: HealthWorkoutDataPoint) -> Bool {
    return deepEqualsHealthKitApi(lhs.toList(), rhs.toList())  }
  func hash(into hasher: inout Hasher) {
    deepHashHealthKitApi(value: toList(), hasher: &hasher)
  }
}

private class HealthKitApiPigeonCodecReader: FlutterStandardReader {
  override func readValue(ofType type: UInt8) -> Any? {
    switch type {
    case 129:
      return HealthAuthorization.fromList(self.readValue() as! [Any?])
    case 130:
      return HealthActivityGoal.fromList(self.readValue() as! [Any?])
    case 131:
      return HealthWorkoutDataPoint.fromList(self.readValue() as! [Any?])
    default:
      return super.readValue(ofType: type)
    }
  }
}

private class HealthKitApiPigeonCodecWriter: FlutterStandardWriter {
  override func writeValue(_ value: Any) {
    if let value = value as? HealthAuthorization {
      super.writeByte(129)
      super.writeValue(value.toList())
    } else if let value = value as? HealthActivityGoal {
      super.writeByte(130)
      super.writeValue(value.toList())
    } else if let value = value as? HealthWorkoutDataPoint {
      super.writeByte(131)
      super.writeValue(value.toList())
    } else {
      super.writeValue(value)
    }
  }
}

private class HealthKitApiPigeonCodecReaderWriter: FlutterStandardReaderWriter {
  override func reader(with data: Data) -> FlutterStandardReader {
    return HealthKitApiPigeonCodecReader(data: data)
  }

  override func writer(with data: NSMutableData) -> FlutterStandardWriter {
    return HealthKitApiPigeonCodecWriter(data: data)
  }
}

class HealthKitApiPigeonCodec: FlutterStandardMessageCodec, @unchecked Sendable {
  static let shared = HealthKitApiPigeonCodec(readerWriter: HealthKitApiPigeonCodecReaderWriter())
}


/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
protocol HealthKitHostApi {
  /// Opens Huawei Health client authorization UI. Returns whether user granted.
  func checkHealthAppAuthorization(completion: @escaping (Result<HealthAuthorization, Error>) -> Void)
  func requestHealthClientAuthorization(completion: @escaping (Result<Bool, Error>) -> Void)
  /// Cancels all Huawei Health authorizations on OHOS.
  func cancelAuthorizations(completion: @escaping (Result<Bool, Error>) -> Void)
  /// Requests Huawei Health to upload the user's latest data to its cloud.
  ///
  /// This only triggers the Health app's configured device-to-cloud sync; it
  /// does not wait for, or return, the subsequently available cloud data.
  func syncHealthDataToCloud(completion: @escaping (Result<Bool, Error>) -> Void)
  /// Reads the current activity-ring goals from Huawei Health on OHOS.
  func readActivityGoal(completion: @escaping (Result<HealthActivityGoal?, Error>) -> Void)
  /// Reads workout records in the supplied Unix-seconds time range on OHOS.
  func readWorkoutData(startTime: Int64, endTime: Int64, completion: @escaping (Result<[HealthWorkoutDataPoint], Error>) -> Void)
}

/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
class HealthKitHostApiSetup {
  static var codec: FlutterStandardMessageCodec { HealthKitApiPigeonCodec.shared }
  /// Sets up an instance of `HealthKitHostApi` to handle messages through the `binaryMessenger`.
  static func setUp(binaryMessenger: FlutterBinaryMessenger, api: HealthKitHostApi?, messageChannelSuffix: String = "") {
    let channelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""
    /// Opens Huawei Health client authorization UI. Returns whether user granted.
    let checkHealthAppAuthorizationChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.checkHealthAppAuthorization\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      checkHealthAppAuthorizationChannel.setMessageHandler { _, reply in
        api.checkHealthAppAuthorization { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      checkHealthAppAuthorizationChannel.setMessageHandler(nil)
    }
    let requestHealthClientAuthorizationChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.requestHealthClientAuthorization\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      requestHealthClientAuthorizationChannel.setMessageHandler { _, reply in
        api.requestHealthClientAuthorization { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      requestHealthClientAuthorizationChannel.setMessageHandler(nil)
    }
    /// Cancels all Huawei Health authorizations on OHOS.
    let cancelAuthorizationsChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.cancelAuthorizations\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      cancelAuthorizationsChannel.setMessageHandler { _, reply in
        api.cancelAuthorizations { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      cancelAuthorizationsChannel.setMessageHandler(nil)
    }
    /// Requests Huawei Health to upload the user's latest data to its cloud.
    ///
    /// This only triggers the Health app's configured device-to-cloud sync; it
    /// does not wait for, or return, the subsequently available cloud data.
    let syncHealthDataToCloudChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.syncHealthDataToCloud\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      syncHealthDataToCloudChannel.setMessageHandler { _, reply in
        api.syncHealthDataToCloud { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      syncHealthDataToCloudChannel.setMessageHandler(nil)
    }
    /// Reads the current activity-ring goals from Huawei Health on OHOS.
    let readActivityGoalChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.readActivityGoal\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      readActivityGoalChannel.setMessageHandler { _, reply in
        api.readActivityGoal { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      readActivityGoalChannel.setMessageHandler(nil)
    }
    /// Reads workout records in the supplied Unix-seconds time range on OHOS.
    let readWorkoutDataChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.doublefeel_flutter.HealthKitHostApi.readWorkoutData\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      readWorkoutDataChannel.setMessageHandler { message, reply in
        let args = message as! [Any?]
        let startTimeArg = args[0] as! Int64
        let endTimeArg = args[1] as! Int64
        api.readWorkoutData(startTime: startTimeArg, endTime: endTimeArg) { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      readWorkoutDataChannel.setMessageHandler(nil)
    }
  }
}