Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next/previous item for normal mode #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions AudioPlayer/AudioPlayer/item/AudioItemQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ class AudioItemQueue {
if mode.contains(.repeatAll) && nextPosition <= 0 {
nextPosition = queue.count
}

while nextPosition > 0 {
let previousPosition = nextPosition - 1
var previousPosition = nextPosition - 1
nextPosition = previousPosition
if previousPosition == queue.count - 1 && mode == .normal {
previousPosition -= 1
}
let item = queue[previousPosition]

if shouldConsiderItem(item: item) {
historic.append(item)
return item
Expand All @@ -199,7 +201,7 @@ class AudioItemQueue {
/// A boolean value indicating whether the queue has a previous item to play or not.
var hasPreviousItem: Bool {
if !queue.isEmpty &&
(nextPosition > 0 || mode.contains(.repeat) || mode.contains(.repeatAll)) {
(nextPosition > 1 || mode.contains(.repeat) || mode.contains(.repeatAll)) {
return true
}
return false
Expand Down
33 changes: 32 additions & 1 deletion AudioPlayer/AudioPlayerTests/AudioItemQueue_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,38 @@ class AudioItemQueue_Tests: XCTestCase {
XCTAssert(queue.hasNextItem)
XCTAssert(queue.previousItem() === item1)
}

func testHasPrevious() {
let queue = AudioItemQueue(items: [item1, item2], mode: .normal)
XCTAssertFalse(queue.hasPreviousItem)
XCTAssert(queue.nextItem() === item1)
XCTAssertFalse(queue.hasPreviousItem)
XCTAssert(queue.nextItem() === item2)
XCTAssertTrue(queue.hasPreviousItem)
XCTAssert(queue.previousItem() === item1)
XCTAssert(queue.nextItem() === item2)
}

func testPreviousWithTwoItems() {
let queue = AudioItemQueue(items: [item1, item2], mode: .normal)
XCTAssertFalse(queue.hasPreviousItem)
XCTAssert(queue.nextItem() === item1)
XCTAssertFalse(queue.hasPreviousItem)
XCTAssert(queue.nextItem() === item2)
XCTAssertTrue(queue.hasPreviousItem)
XCTAssert(queue.previousItem() === item1)
XCTAssert(queue.nextItem() === item2)
}

func testPreviousWithOneItem() {
let queue = AudioItemQueue(items: [item1], mode: .normal)
XCTAssertFalse(queue.hasPreviousItem)
XCTAssertTrue(queue.hasNextItem)

XCTAssert(queue.nextItem() === item1)
XCTAssertFalse(queue.hasPreviousItem)
XCTAssertFalse(queue.hasNextItem)
}

func testPreviousInRepeatMode() {
let queue = AudioItemQueue(items: [item1, item2, item3], mode: .repeat)
Expand Down Expand Up @@ -214,7 +246,6 @@ class AudioItemQueue_Tests: XCTestCase {
queue.nextPosition = queue.queue.count
queue.delegate = delegate

XCTAssert(queue.previousItem() === item3)
XCTAssert(queue.previousItem() === item1)
XCTAssertNil(queue.previousItem())
}
Expand Down