|
| 1 | +// |
| 2 | +// PlayerEventFilter.swift |
| 3 | +// swiftui-loop-videoplayer |
| 4 | +// |
| 5 | +// Created by Igor on 12.02.25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// A "parallel" structure for filtering PlayerEvent. |
| 11 | +/// Each case here: |
| 12 | +/// 1) Either ignores associated values (xxxAny) |
| 13 | +/// 2) Or matches cases that have no associated values in PlayerEvent. |
| 14 | +@available(iOS 14.0, macOS 11.0, tvOS 14.0, *) |
| 15 | +public enum PlayerEventFilter { |
| 16 | + /// Matches any `.seek(...)` case, regardless of Bool or currentTime |
| 17 | + case seekAny |
| 18 | + |
| 19 | + /// Matches `.paused` exactly (no associated values) |
| 20 | + case paused |
| 21 | + |
| 22 | + /// Matches `.waitingToPlayAtSpecifiedRate` (no associated values) |
| 23 | + case waitingToPlayAtSpecifiedRate |
| 24 | + |
| 25 | + /// Matches `.playing` (no associated values) |
| 26 | + case playing |
| 27 | + |
| 28 | + /// Matches any `.currentItemChanged(...)` case |
| 29 | + case currentItemChangedAny |
| 30 | + |
| 31 | + /// Matches `.currentItemRemoved` exactly (no associated values) |
| 32 | + case currentItemRemoved |
| 33 | + |
| 34 | + /// Matches any `.volumeChanged(...)` case |
| 35 | + case volumeChangedAny |
| 36 | + |
| 37 | + /// Matches any `.error(...)` case |
| 38 | + case errorAny |
| 39 | + |
| 40 | + /// Matches any `.boundsChanged(...)` case |
| 41 | + case boundsChangedAny |
| 42 | + |
| 43 | + /// Matches `.startedPiP` (no associated values) |
| 44 | + case startedPiP |
| 45 | + |
| 46 | + /// Matches `.stoppedPiP` (no associated values) |
| 47 | + case stoppedPiP |
| 48 | + |
| 49 | + /// Matches any `.itemStatusChanged(...)` case |
| 50 | + case itemStatusChangedAny |
| 51 | + |
| 52 | + /// Matches any `.duration(...)` case |
| 53 | + case durationAny |
| 54 | + |
| 55 | + /// Matches every possible event |
| 56 | + case all |
| 57 | +} |
| 58 | + |
| 59 | +extension PlayerEventFilter { |
| 60 | + /// Checks whether a given `PlayerEvent` matches this filter. |
| 61 | + /// |
| 62 | + /// - Parameter event: The `PlayerEvent` to inspect. |
| 63 | + /// - Returns: `true` if the event belongs to this case (ignoring parameters), `false` otherwise. |
| 64 | + func matches(_ event: PlayerEvent) -> Bool { |
| 65 | + switch (self, event) { |
| 66 | + /// Universal case |
| 67 | + case (.all, _): |
| 68 | + return true |
| 69 | + |
| 70 | + // Compare by case name only, ignoring associated values |
| 71 | + case (.seekAny, .seek): |
| 72 | + return true |
| 73 | + case (.paused, .paused): |
| 74 | + return true |
| 75 | + case (.waitingToPlayAtSpecifiedRate, .waitingToPlayAtSpecifiedRate): |
| 76 | + return true |
| 77 | + case (.playing, .playing): |
| 78 | + return true |
| 79 | + case (.currentItemChangedAny, .currentItemChanged): |
| 80 | + return true |
| 81 | + case (.currentItemRemoved, .currentItemRemoved): |
| 82 | + return true |
| 83 | + case (.volumeChangedAny, .volumeChanged): |
| 84 | + return true |
| 85 | + case (.errorAny, .error): |
| 86 | + return true |
| 87 | + case (.boundsChangedAny, .boundsChanged): |
| 88 | + return true |
| 89 | + case (.startedPiP, .startedPiP): |
| 90 | + return true |
| 91 | + case (.stoppedPiP, .stoppedPiP): |
| 92 | + return true |
| 93 | + case (.itemStatusChangedAny, .itemStatusChanged): |
| 94 | + return true |
| 95 | + case (.durationAny, .duration): |
| 96 | + return true |
| 97 | + |
| 98 | + // Default fallback: no match |
| 99 | + default: |
| 100 | + return false |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +extension Collection where Element == PlayerEventFilter { |
| 106 | + /// Checks whether any filter in this collection matches the given `PlayerEvent`. |
| 107 | + /// |
| 108 | + /// - Parameter event: The `PlayerEvent` to test. |
| 109 | + /// - Returns: `true` if at least one `PlayerEventFilter` in this collection matches the `event`; otherwise, `false`. |
| 110 | + func contains(_ event: PlayerEvent) -> Bool { |
| 111 | + return self.contains { filter in |
| 112 | + filter.matches(event) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments