Skip to content

Commit

Permalink
Player: context switch will play first song from new context
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed May 15, 2022
1 parent 462f75a commit d6ae955
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
21 changes: 8 additions & 13 deletions Amperfy/Player/MusicPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,17 @@ class MusicPlayer: NSObject, BackendAudioPlayerNotifiable {
if context.type == .music {
queueHandler.contextName = context.name
}
if !wasUserQueuePlaying {
if context.index == 0 {
insertIntoPlayer(playable: activePlayable)
} else {
play(playerIndex: PlayerIndex(queueType: .next, index: context.index-1))
}
if let topUserQueueItem = topUserQueueItem {

if queueHandler.isUserQueuePlaying {
play(playerIndex: PlayerIndex(queueType: .next, index: context.index))
if !wasUserQueuePlaying, let topUserQueueItem = topUserQueueItem {
queueHandler.insertUserQueue(playables: [topUserQueueItem])
play(playerIndex: PlayerIndex(queueType: .user, index: 0))
}
} else if context.index > 0, let currentlyPlayingElement = currentlyPlaying {
_ = queueHandler.markAndGetPlayableAsPlaying(at: PlayerIndex(queueType: .next, index: context.index-1))
queueHandler.insertUserQueue(playables: [currentlyPlayingElement])
_ = queueHandler.markAndGetPlayableAsPlaying(at: PlayerIndex(queueType: .user, index: 0))
} else if context.index == 0 {
insertIntoPlayer(playable: activePlayable)
} else {
play(playerIndex: PlayerIndex(queueType: .next, index: context.index-1))
}
play()
}

func play(playerIndex: PlayerIndex) {
Expand Down
48 changes: 24 additions & 24 deletions AmperfyTests/Cases/Player/MusicPlayerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 3)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.nextQueue.count, 2)
XCTAssertEqual(testPlayer.currentlyPlaying, songId1)
}

func testPlayMulitpleSongs_WaitingQueuePlaying8() {
Expand All @@ -569,9 +569,9 @@ class MusicPlayerTest: XCTestCase {
testPlayer.play(context: PlayContext(name: "", playables: [songId1, songId2, songId3]))
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 3)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.userQueue.count, 1)
XCTAssertEqual(testPlayer.nextQueue.count, 2)
XCTAssertEqual(testPlayer.currentlyPlaying, songId1)
}

func testPlaySong_WaitingQueuePlaying8() {
Expand All @@ -584,9 +584,9 @@ class MusicPlayerTest: XCTestCase {
testPlayer.play(context: PlayContext(name: "", playables: [songId1]))
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.userQueue.count, 1)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying, songId1)
}

func testPlaySong_WaitingQueuePlaying9() {
Expand All @@ -605,8 +605,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 2)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying, songId3)
}

func testPlayMulitpleSongs_WaitingQueuePlaying3() {
Expand All @@ -625,8 +625,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 2)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying, songId3)
}

func testPlayMulitpleSongs_WaitingQueuePlaying4() {
Expand All @@ -641,8 +641,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying, songId1)
}

func testPlayMulitpleSongs_WaitingQueuePlaying5() {
Expand All @@ -657,8 +657,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying, songId0)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying, songId1)
}

func testPlayMulitpleSongs_WaitingQueuePlaying6() {
Expand Down Expand Up @@ -686,8 +686,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId0.id)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId1.id)
}

func testPlayMulitpleSongs_userQueueHasElements_notUserQueuePlaying2() {
Expand All @@ -703,8 +703,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 0)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 3)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId0.id)
XCTAssertEqual(testPlayer.nextQueue.count, 2)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId1.id)
}

func testPlayMulitpleSongs_userQueueHasElements_notUserQueuePlaying3() {
Expand All @@ -720,8 +720,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 1)
XCTAssertEqual(testPlayer.userQueue.count, 0)
XCTAssertEqual(testPlayer.nextQueue.count, 2)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId0.id)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId2.id)
}

func testPlayMulitpleSongs_userQueueHasElements_notUserQueuePlaying4() {
Expand All @@ -739,8 +739,8 @@ class MusicPlayerTest: XCTestCase {
XCTAssertTrue(testPlayer.isPlaying)
XCTAssertEqual(testPlayer.prevQueue.count, 2)
XCTAssertEqual(testPlayer.userQueue.count, 2)
XCTAssertEqual(testPlayer.nextQueue.count, 1)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId0.id)
XCTAssertEqual(testPlayer.nextQueue.count, 0)
XCTAssertEqual(testPlayer.currentlyPlaying?.id, songId3.id)
XCTAssertEqual(testPlayer.userQueue[0].id, songId4.id)
XCTAssertEqual(testPlayer.userQueue[1].id, songId5.id)
}
Expand Down

0 comments on commit d6ae955

Please sign in to comment.