Skip to content

Commit

Permalink
Rename restart API as replay (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
defagos authored Sep 4, 2023
1 parent 94b7771 commit 4b2f01e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
10 changes: 5 additions & 5 deletions Demo/Sources/Players/PlaybackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private struct MainView: View {
}

private var isUserInterfaceHidden: Bool {
visibilityTracker.isUserInterfaceHidden && !areControlsAlwaysVisible && !player.canRestart()
visibilityTracker.isUserInterfaceHidden && !areControlsAlwaysVisible && !player.canReplay()
}

private func magnificationGesture() -> some Gesture {
Expand Down Expand Up @@ -181,7 +181,7 @@ private struct PlaybackButton: View {
@ObservedObject var player: Player

private var imageName: String {
if player.canRestart() {
if player.canReplay() {
return "arrow.counterclockwise.circle.fill"
}
else {
Expand All @@ -203,12 +203,12 @@ private struct PlaybackButton: View {
.aspectRatio(contentMode: .fit)
.frame(minWidth: 120, maxHeight: 90)
.opacity(player.isBusy ? 0 : 1)
.animation(.defaultLinear, values: player.playbackState, player.canRestart())
.animation(.defaultLinear, values: player.playbackState, player.canReplay())
}

private func play() {
if player.canRestart() {
player.restart()
if player.canReplay() {
player.replay()
}
else {
player.togglePlayPause()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import Foundation

public extension Player {
/// Checks whether the player has finished playing its content and can be restarted.
/// Checks whether the player has finished playing and its content can be played again from the start.
///
/// - Returns: `true` if possible.
func canRestart() -> Bool {
func canReplay() -> Bool {
guard !storedItems.isEmpty else { return false }
return currentItem.smoothPlayerItem(in: storedItems) == nil
}

/// Restarts playback if possible.
func restart() {
guard canRestart() else { return }
/// Replays the content from the start, resuming playback automatically.
func replay() {
guard canReplay() else { return }
play()
try? setCurrentIndex(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import Foundation
import Nimble
import Streams

final class RestartChecksTests: TestCase {
final class ReplayChecksTests: TestCase {
func testEmptyPlayer() {
let player = Player()
expect(player.canRestart()).to(beFalse())
expect(player.canReplay()).to(beFalse())
}

func testWithOneGoodItem() {
let player = Player(item: .simple(url: Stream.shortOnDemand.url))
expect(player.canRestart()).to(beFalse())
expect(player.canReplay()).to(beFalse())
}

func testWithOneGoodItemPlayedEntirely() {
let player = Player(item: .simple(url: Stream.shortOnDemand.url))
player.play()
expect(player.canRestart()).toEventually(beTrue())
expect(player.canReplay()).toEventually(beTrue())
}

func testWithOneBadItem() {
let player = Player(item: .simple(url: Stream.unavailable.url))
expect(player.canRestart()).toEventually(beTrue())
expect(player.canReplay()).toEventually(beTrue())
}

func testWithManyGoodItems() {
Expand All @@ -38,7 +38,7 @@ final class RestartChecksTests: TestCase {
.simple(url: Stream.shortOnDemand.url)
])
player.play()
expect(player.canRestart()).toEventually(beTrue())
expect(player.canReplay()).toEventually(beTrue())
}

func testWithManyBadItems() {
Expand All @@ -47,7 +47,7 @@ final class RestartChecksTests: TestCase {
.simple(url: Stream.unavailable.url)
])
player.play()
expect(player.canRestart()).toEventually(beTrue())
expect(player.canReplay()).toEventually(beTrue())
}

func testWithOneGoodItemAndOneBadItem() {
Expand All @@ -56,6 +56,6 @@ final class RestartChecksTests: TestCase {
.simple(url: Stream.unavailable.url)
])
player.play()
expect(player.canRestart()).toEventually(beTrue())
expect(player.canReplay()).toEventually(beTrue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import Foundation
import Nimble
import Streams

final class RestartTests: TestCase {
final class ReplayTests: TestCase {
func testWithOneGoodItem() {
let player = Player(item: .simple(url: Stream.shortOnDemand.url))
player.restart()
player.replay()
expect(player.currentIndex).to(equal(0))
}

func testWithOneGoodItemPlayedEntirely() {
let player = Player(item: .simple(url: Stream.shortOnDemand.url))
player.play()
expect(player.currentIndex).toEventually(beNil())
player.restart()
player.replay()
expect(player.currentIndex).toEventually(equal(0))
}

func testWithOneBadItem() {
let player = Player(item: .simple(url: Stream.unavailable.url))
expect(player.currentIndex).toEventually(beNil())
player.restart()
player.replay()
expect(player.currentIndex).toEventually(equal(0))
}

Expand All @@ -39,7 +39,7 @@ final class RestartTests: TestCase {
])
player.play()
expect(player.currentIndex).toEventually(equal(1))
player.restart()
player.replay()
expect(player.currentIndex).to(equal(1))
}

Expand All @@ -50,7 +50,7 @@ final class RestartTests: TestCase {
])
player.play()
expect(player.currentIndex).toEventually(beNil())
player.restart()
player.replay()
expect(player.currentIndex).to(equal(0))
}

Expand All @@ -61,7 +61,17 @@ final class RestartTests: TestCase {
])
player.play()
expect(player.currentIndex).toEventually(beNil())
player.restart()
player.replay()
expect(player.currentIndex).to(equal(0))
}

func testResumePlaybackIfNeeded() {
let player = Player(item: .simple(url: Stream.shortOnDemand.url))
player.play()
expect(player.currentIndex).toEventually(beNil())
player.pause()
player.replay()
expect(player.currentIndex).toEventually(equal(0))
expect(player.playbackState).toEventually(equal(.playing))
}
}

0 comments on commit 4b2f01e

Please sign in to comment.