Skip to content

Commit fdaca4d

Browse files
committed
ref: optional target for Delegate
1 parent 2017d37 commit fdaca4d

9 files changed

Lines changed: 55 additions & 19 deletions

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,24 @@ ZonPlayer is a player library base on AVPlayer with cache and remote control sup
2525

2626
```swift
2727

28-
let player: ZonPlayable = ZonPlayer.player(URLConvertible)
29-
.session(ZonPlayer.Sessionable)
30-
.cache(ZonPlayer.Cacheable) // Conform ZonPlayer.Cacheable to customize cache category.
28+
let player: ZonPlayable = ZonPlayer.player(any URLConvertible)
29+
.session(any ZonPlayer.Sessionable)
30+
.cache(any ZonPlayer.Cacheable) // Conform ZonPlayer.Cacheable to customize cache category.
3131
.remoteControl(self) { wlf, payload in // Conform ZonPlayer.RemoteControllable to customize background playback controller.
3232
payload.title(String).artist(String)....
3333
}
3434
.onPaused(self) { wlf, payload in // Conform ZonPlayer.Observable to listen player.
3535
}
3636
.activate(in: ZonPlayerView)
3737

38+
// or
39+
let player: ZonPlayable = ZonPlayer.player(any URLConvertible)
40+
.on(\.session, any ZonPlayer.Sessionable)
41+
.on(\.cache, any ZonPlayer.Cacheable)
42+
.on(\.finish, .init { in })
43+
.on(\.pause, .init(on: self, block: { wlf, player in }))
44+
.activate()
45+
3846
// Conform ZonPlayer.Controllable to control player instance.
3947
player.pause()
4048
player.play()

Sources/Core/Private/Builder.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
//
77

88
final class Builder: ZonPlayer.Settable, @unchecked Sendable {
9-
109
let url: URLConvertible & Sendable
1110
init(url: URLConvertible & Sendable) {
1211
self.url = url
1312
}
1413

1514
var progressInterval: TimeInterval = 1
16-
var maxRetryCount: Int = 1
1715

1816
// MARK: - ZPObservable
1917
var callbackQueue: DispatchQueue = .main
@@ -22,7 +20,7 @@ final class Builder: ZonPlayer.Settable, @unchecked Sendable {
2220
var play: ZonPlayer.Delegate<(ZonPlayable, Float), Void>?
2321
var pause: ZonPlayer.Delegate<ZonPlayable, Void>?
2422
var finish: ZonPlayer.Delegate<(ZonPlayable, URL), Void>?
25-
var error: ZonPlayer.Delegate<(ZonPlayable, ZonPlayer.Error), Void>?
23+
var error: ZonPlayer.Delegate<(ZonPlayable?, ZonPlayer.Error), Void>?
2624
var progress: ZonPlayer.Delegate<(ZonPlayable, TimeInterval, TimeInterval), Void>?
2725
var duration: ZonPlayer.Delegate<(ZonPlayable, TimeInterval), Void>?
2826
var background: ZonPlayer.Delegate<(ZonPlayable, Bool), Void>?

Sources/Core/Private/CallbackCompositer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class CallbackCompositer: ZonPlayer.Observable, @unchecked Sendable {
5252
}
5353
}()
5454

55-
lazy var error: ZonPlayer.Delegate<(ZonPlayable, ZonPlayer.Error), Void>? = {
55+
lazy var error: ZonPlayer.Delegate<(ZonPlayable?, ZonPlayer.Error), Void>? = {
5656
.init().delegate(on: self) { wlf, input in
5757
wlf._callback { $0.error?.call(input) }
5858
wlf._monitor { $0.player(input.0, playFailed: input.1) }

Sources/Core/ZonPlayer+Monitorable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension ZonPlayer {
1515

1616
func playerPlayDidFinish(_ player: ZonPlayable, url: URL)
1717

18-
func player(_ player: ZonPlayable, playFailed error: ZonPlayer.Error)
18+
func player(_ player: ZonPlayable?, playFailed error: ZonPlayer.Error)
1919

2020
func player(_ player: ZonPlayable, playProgressDidChange currentTime: TimeInterval, totalTime: TimeInterval)
2121

@@ -35,7 +35,7 @@ extension ZonPlayer.Monitorable {
3535
public func player(_ player: ZonPlayable, didPlay rate: Float) {}
3636
public func playerDidPause(_ player: ZonPlayable) {}
3737
public func playerPlayDidFinish(_ player: ZonPlayable, url: URL) {}
38-
public func player(_ player: ZonPlayable, playFailed error: ZonPlayer.Error) {}
38+
public func player(_ player: ZonPlayable?, playFailed error: ZonPlayer.Error) {}
3939
public func player(
4040
_ player: ZonPlayable,
4141
playProgressDidChange currentTime: TimeInterval,

Sources/Usage/Player/ZonPlayer+Observable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension ZonPlayer {
1414
var play: Delegate<(ZonPlayable, Float), Void>? { get nonmutating set }
1515
var pause: Delegate<ZonPlayable, Void>? { get nonmutating set }
1616
var finish: Delegate<(ZonPlayable, URL), Void>? { get nonmutating set }
17-
var error: Delegate<(ZonPlayable, ZonPlayer.Error), Void>? { get nonmutating set }
17+
var error: Delegate<(ZonPlayable?, ZonPlayer.Error), Void>? { get nonmutating set }
1818
var progress: Delegate<(ZonPlayable, TimeInterval, TimeInterval), Void>? { get nonmutating set }
1919
var duration: Delegate<(ZonPlayable, TimeInterval), Void>? { get nonmutating set }
2020
var background: Delegate<(ZonPlayable, Bool), Void>? { get nonmutating set }
@@ -63,7 +63,7 @@ extension ZonPlayer.Observable {
6363
}
6464

6565
/// Listen to player failed because of an error.
66-
public func onError<T: AnyObject>(_ target: T, block: ((T, (ZonPlayable, ZonPlayer.Error)) -> Void)?) -> Self {
66+
public func onError<T: AnyObject>(_ target: T, block: ((T, (ZonPlayable?, ZonPlayer.Error)) -> Void)?) -> Self {
6767
error = (error ?? .init()).delegate(on: target, block: block)
6868
return self
6969
}

Sources/Usage/Player/ZonPlayer+Settable.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ extension ZonPlayer.Settable {
2525
ZonPlayer.Manager.shared.start(setter: self, in: view)
2626
}
2727
}
28+
29+
extension ZonPlayer.Settable {
30+
@discardableResult
31+
public func on<Value>(_ keyPath: ReferenceWritableKeyPath<Self, Value>, _ value: Value) -> Self {
32+
self[keyPath: keyPath] = value
33+
return self
34+
}
35+
}

Sources/Usage/ZonPlayer+Delegate.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,22 @@
88
extension ZonPlayer {
99
// From https://github.com/onevcat/Kingfisher/blob/277f1ab2c6664b19b4a412e32b094b201e2d5757/Sources/Utility/Delegate.swift#L71
1010
public final class Delegate<Input, Output>: @unchecked Sendable {
11+
public typealias Block = (Input) -> Output?
12+
13+
private var block: Block?
1114
public init() {}
1215

13-
private var block: ((Input) -> Output?)?
16+
public init(block: @escaping Block) {
17+
self.block = block
18+
}
19+
20+
public init<T: AnyObject>(on target: T, block: @escaping (T, Input) -> Output) {
21+
self.block = { [weak target] input in
22+
guard let target = target else { return nil }
23+
return block(target, input)
24+
}
25+
}
26+
1427
@discardableResult
1528
public func delegate<T: AnyObject>(on target: T, block: ((T, Input) -> Output)?) -> Self {
1629
self.block = { [weak target] input in
@@ -20,6 +33,12 @@ extension ZonPlayer {
2033
return self
2134
}
2235

36+
@discardableResult
37+
public func delegate(block: ((Input) -> Output)?) -> Self {
38+
self.block = block
39+
return self
40+
}
41+
2342
public func call(_ input: Input) -> Output? { block?(input) }
2443
public func callAsFunction(_ input: Input) -> Output? { call(input) }
2544
}

Tests/Playback/ControllerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class ControllerTests: QuickSpec {
1010
override static func spec() {
1111
describe("Test player controller") {
12-
it("play") {
12+
it("Play") {
1313
waitUntil(timeout: .seconds(5)) { done in
1414
let player = ZonPlayer
1515
.player(self._url)
@@ -26,7 +26,7 @@ final class ControllerTests: QuickSpec {
2626
}
2727
}
2828

29-
it("pause") {
29+
it("Pause") {
3030
waitUntil(timeout: .seconds(5)) { done in
3131
let player = ZonPlayer
3232
.player(self._url)

Tests/Playback/InvalidInitializationTests.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ final class InvalidInitializationTests: QuickSpec {
1010
override static func spec() {
1111
describe("Test player initialization") {
1212
it("Invalid url") {
13-
waitUntil { done in
13+
waitUntil(timeout: .seconds(5)) { done in
14+
var errorsCount = 2
1415
let invalidURL = ""
1516
let player = ZonPlayer
1617
.player(invalidURL)
18+
.cache(ZPC.Streaming())
1719
.onError(_delegate) { _, payload in
18-
guard case .invalidURL = payload.1 else {
19-
self.__zon_triggerUnexpectedError()
20-
return
20+
switch payload.1 {
21+
case .invalidURL, .playerTerminated:
22+
errorsCount -= 1
23+
default: return
2124
}
22-
done()
25+
if errorsCount == 0 { done() }
2326
}
2427
.activate()
2528
self._players.append(player)

0 commit comments

Comments
 (0)