AlertActionsBuilder.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /// A result builder for `[AlertAction]`.
  2. @resultBuilder
  3. public struct AlertActionsBuilder {
  4. /// Called when no actions are provided.
  5. ///
  6. /// - Returns: A default "OK" action.
  7. public static func buildBlock() -> [AlertAction] {
  8. [.default]
  9. }
  10. @MainActor
  11. public static func buildPartialBlock(first: Button<TupleView1<Text>>) -> [AlertAction] {
  12. [
  13. AlertAction(
  14. label: first.body.view0.view0.string,
  15. action: first.action
  16. )
  17. ]
  18. }
  19. public static func buildPartialBlock(first: Block) -> [AlertAction] {
  20. first.actions
  21. }
  22. @MainActor
  23. public static func buildPartialBlock(
  24. accumulated: [AlertAction],
  25. next: Button<TupleView1<Text>>
  26. ) -> [AlertAction] {
  27. accumulated + [
  28. AlertAction(
  29. label: next.body.view0.view0.string,
  30. action: next.action
  31. )
  32. ]
  33. }
  34. public static func buildPartialBlock(
  35. accumulated: [AlertAction],
  36. next: Block
  37. ) -> [AlertAction] {
  38. accumulated + next.actions
  39. }
  40. public static func buildOptional(_ component: [AlertAction]?) -> Block {
  41. Block(actions: component ?? [])
  42. }
  43. public static func buildEither(first component: [AlertAction]) -> Block {
  44. Block(actions: component)
  45. }
  46. public static func buildEither(second component: [AlertAction]) -> Block {
  47. Block(actions: component)
  48. }
  49. public struct Block {
  50. var actions: [AlertAction]
  51. }
  52. }