FocusFilterIntent react to FocusMode deactivating

Hey, I was wondering on how to react to the focus mode being turned off inside the FocusFilterIntent. I've successfully managed to call a specific action when the focus is being set, but I now want to deactivate/ react when the focus is being deactivated. How can I achieve something like this?

Answered by Maximillian-Dev in 826058022

I've found the issue. The FocusFilterIntent was only called once, since I've had Parameters inside that were not optional. It seems like it isn't able to infer the parameters that are non optional when deactivating the Focus Mode. So simply marking the Parameters as optional solves this problem.

struct FocusFilterIntent: SetFocusFilterIntent {
  static let title: LocalizedStringResource = "Some Title"
  static let description: LocalizedStringResource = """
  Some Description
  """

  var displayRepresentation: DisplayRepresentation {
    DisplayRepresentation(title: "App", subtitle: "Subtitle")
  }

  @Parameter(title: "Param1")
  var param1: Param1? // turned to optional 

func perform() async throws -> some IntentResult {
    Logger.focus.info("FocusFilterIntent called")

    if let param1 = param1 {
      // do something, filter is activated
    } else {
      // filter is deactivated
    }

    return .result()
  }

What I've realized is that the FocusFilterIntent usually should be called twice (on activating Focus once and on deactivating once). However, my Extension is only called when activating. Did somebody else experience similar issues?

Accepted Answer

I've found the issue. The FocusFilterIntent was only called once, since I've had Parameters inside that were not optional. It seems like it isn't able to infer the parameters that are non optional when deactivating the Focus Mode. So simply marking the Parameters as optional solves this problem.

struct FocusFilterIntent: SetFocusFilterIntent {
  static let title: LocalizedStringResource = "Some Title"
  static let description: LocalizedStringResource = """
  Some Description
  """

  var displayRepresentation: DisplayRepresentation {
    DisplayRepresentation(title: "App", subtitle: "Subtitle")
  }

  @Parameter(title: "Param1")
  var param1: Param1? // turned to optional 

func perform() async throws -> some IntentResult {
    Logger.focus.info("FocusFilterIntent called")

    if let param1 = param1 {
      // do something, filter is activated
    } else {
      // filter is deactivated
    }

    return .result()
  }
FocusFilterIntent react to FocusMode deactivating
 
 
Q