我正在构建一个应用程序,它使用 Intents Extension 来跟踪各种指标,如体重、步数、炉膛速率等。我想根据他想要跟踪的指标向用户提供可供选择的测量单位。例如,如果用户跟踪饮用水,她可以在升、液量盎司、毫升或杯子之间进行选择,我将向她展示这些单位。如果用户跟踪步数,则唯一使用的单位是“步数”,为其提供消歧或动态选项是没有意义的。我注意到,当使用动态选项时,每次都会调用为其生成的provideUnitOptions函数,即使在解析函数中我发送了一个notRequired解析结果。
以下是我的意图处理程序中的函数:
func resolveMetric(for intent: MeasurementIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
let requestedMetric = intent.metric
if requestedMetric == nil {
completion(INStringResolutionResult.needsValue())
} else {
metricService.retrieveUserMetrics { (success) in
let metricNames = self.metricService.metricNamesMap.keys
if metricNames.contains(requestedMetric!){
completion(.success(with: requestedMetric!))
} else {
completion(.disambiguation(with: Array(metricNames)))
}
}
}
}
func provideMetricOptions(for intent: MeasurementIntent, with completion: @escaping ([String]?, Error?) -> Void) {
metricService.retrieveUserMetrics { (success) in
let metricNames = self.metricService.metricNamesMap.keys
completion(Array(metricNames), nil)
}
}
func resolveUnit(for intent: MeasurementIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
let metric = self.metricService.metricNamesMap[intent.metric!]!
let units = metric.units.getSystemUnits(defaultUnit: metric.defaultUnit).map { $0.name }
if units.count == 1 {
completion(.notRequired())
} else if intent.unit == nil {
completion(.disambiguation(with: units))
} else {
completion(.success(with: intent.unit!))
}
}
//This is called even if in resolveUnit I sent .notRequired resolution. Same for .success
func provideUnitOptions(for intent: MeasurementIntent, with completion: @escaping ([String]?, Error?) -> Void) {
let metric = self.metricService.metricNamesMap[intent.metric!]!
let units = metric.units.getSystemUnits(defaultUnit: metric.defaultUnit).map { $0.name }
completion(units, nil)
}
在provideUnitOptions之前和之后调用 resolveUnit 函数我可以 禁用动态选项,但在这种情况下,用户将无法使用快捷方式中的预定义单位。你觉得我应该怎么做?我错过了什么吗?谢谢!