I have an array of things and I'd like to say, get the item in the array I'm selecting as well as the next two. So three in total. However, if the item I'm select is the last thing, I want to say get the two preceding. And finally if it's the second to last thing, I want to say get the one before and the one after.
Right now I have code like so:
private func handleWhichSongsToPlay(_ track: Track, loadedTracks: MusicItemCollection<Track>) -> [Track] {
// loadedTracks is where track comes from so it has to exist
let trackIndex = loadedTracks.firstIndex(where: { $0.id == track.id })!
var rangeOfTracks: [Track] = []
if trackIndex == loadedTracks.endIndex - 1 {
// two songs before
rangeOfTracks.append(contentsOf: loadedTracks[(trackIndex - 2)...trackIndex])
} else if trackIndex == loadedTracks.endIndex - 2 {
// song before and after
rangeOfTracks.append(contentsOf: loadedTracks[(trackIndex - 1)...(trackIndex + 1)])
} else {
// two songs after
rangeOfTracks.append(contentsOf: loadedTracks[trackIndex...(trackIndex + 2)])
}
return rangeOfTracks
}
But I feel like there must be a better way.