reports icon indicating copy to clipboard operation
reports copied to clipboard

FB12196730: AppIntents should allow using if-statements in ParameterSummary

Open sindresorhus opened this issue 2 years ago • 3 comments

  • Date: 2023-05-21
  • Resolution: Open
  • Area: AppIntents
  • OS: macOS 13.4
  • Type: Suggestion

Description

When you need a parameter to only be visible if another parameter is true, you would currently have to do this:

static var parameterSummary: some ParameterSummary {
	When(\.$shouldLimit, .equalTo, true) {
		Summary("Shuffle \(\.$list)") {
			\.$shouldLimit
			\.$limit
		}
	} otherwise: {
		Summary("Shuffle \(\.$list)") {
			\.$shouldLimit
		}
	}
}

However, it becomes a boilerplate mess if you need more than one conditional parameter.

It would be much nicer if we could use normal if-statements like this:

static var parameterSummary: some ParameterSummary {
	Summary("Shuffle \(\.$list)") {
		\.$shouldLimit
		if shouldLimit {
			\.$limit
		}
	}
}

Here is a real-world example of how much duplication it causes: https://github.com/sindresorhus/Actions/blob/5323d86e210fd12d163ea18702eab513abc5daba/Shared/Actions/ChooseFromListExtended.swift#L57-L97 And with so much duplication, it's very easy to miss a parameter.

static var parameterSummary: some ParameterSummary {
	When(\.$selectMultiple, .equalTo, true) {
		When(\.$useTimeout, .equalTo, true) {
			Summary("Choose from \(\.$list)") {
				\.$prompt
				\.$selectMultiple
				\.$selectAllInitially
				\.$useTimeout
				\.$timeout
				\.$timeoutReturnValue
				\.$allowCustomItems
			}
		} otherwise: {
			Summary("Choose from \(\.$list)") {
				\.$prompt
				\.$selectMultiple
				\.$selectAllInitially
				\.$useTimeout
				\.$allowCustomItems
			}
		}
	} otherwise: {
		When(\.$useTimeout, .equalTo, true) {
			Summary("Choose from \(\.$list)") {
				\.$prompt
				\.$selectMultiple
				\.$useTimeout
				\.$timeout
				\.$timeoutReturnValue
				\.$allowCustomItems
			}
		} otherwise: {
			Summary("Choose from \(\.$list)") {
				\.$prompt
				\.$selectMultiple
				\.$useTimeout
				\.$allowCustomItems
			}
		}
	}
}

sindresorhus avatar May 21 '23 14:05 sindresorhus

There is a severely under-documented Switch/Case available which might solve this issue.

https://developer.apple.com/documentation/appintents/appintent/switch

gregggreg avatar Oct 06 '23 18:10 gregggreg

example use:

Switch(\.$match) {
	Case(.specific) {
		Summary {
			\.$match
			\.$specificMatch
			\.$theme
		}
	}
	Case(.latestOpponent) {
		Summary {
			\.$match
			\.$opponent
			\.$theme
		}
	}
	DefaultCase {
		Summary {
			\.$match
			\.$theme
		}
	}
}

gregggreg avatar Oct 06 '23 18:10 gregggreg

@gregggreg I'm well aware of Switch/Case. Like When, it doesn't solve the nesting problem described in the feedback report.

sindresorhus avatar Oct 07 '23 04:10 sindresorhus