Skip to content

Commit 5ecb201

Browse files
committed
remove redundant PlayerState enum
Signed-off-by: Adam Ratzman <[email protected]>
1 parent b780494 commit 5ecb201

File tree

5 files changed

+134
-24
lines changed

5 files changed

+134
-24
lines changed

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/client/ClientPlayerApi.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.adamratzman.spotify.models.CursorBasedPagingObject
1313
import com.adamratzman.spotify.models.Device
1414
import com.adamratzman.spotify.models.PlayHistory
1515
import com.adamratzman.spotify.models.PlayableUri
16+
import com.adamratzman.spotify.models.ResultEnum
1617
import com.adamratzman.spotify.models.serialization.mapToJsonString
1718
import com.adamratzman.spotify.models.serialization.toCursorBasedPagingObject
1819
import com.adamratzman.spotify.models.serialization.toInnerObject
@@ -295,20 +296,22 @@ public class ClientPlayerApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
295296
*
296297
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/player/set-repeat-mode-on-users-playback/)**
297298
*/
298-
public enum class PlayerRepeatState {
299+
public enum class PlayerRepeatState(public val identifier: String): ResultEnum {
299300
/**
300301
* Repeat the current track
301302
*/
302-
TRACK,
303+
TRACK("track"),
303304

304305
/**
305306
* Repeat the current context
306307
*/
307-
CONTEXT,
308+
CONTEXT("context"),
308309

309310
/**
310311
* Will turn repeat off
311312
*/
312-
OFF;
313+
OFF("off");
314+
315+
override fun retrieveIdentifier(): String = identifier
313316
}
314317
}

src/commonMain/kotlin/com.adamratzman.spotify/models/PagingObjects.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public class NullablePagingObject<T : Any>(
7474
return pagingObject
7575
}
7676

77-
override fun get(index: Int): T? = items[index]
7877
override fun iterator(): Iterator<T?> = items.iterator()
7978
override fun listIterator(): ListIterator<T?> = items.listIterator()
8079
override fun listIterator(index: Int): ListIterator<T?> = items.listIterator(index)
@@ -96,7 +95,7 @@ public data class PagingObject<T : Any>(
9695
override val previous: String? = null,
9796
override val total: Int = 0
9897
) : AbstractPagingObject<T, PagingObject<T>>() {
99-
override fun get(index: Int): T = items[index]
98+
override fun get(index: Int): T = super.get(index)!!
10099

101100
override fun iterator(): Iterator<T> = items.iterator()
102101
override fun listIterator(): ListIterator<T> = items.listIterator()
@@ -332,7 +331,7 @@ public data class CursorBasedPagingObject<T : Any>(
332331
return pagingObjects.distinctBy { it.href }
333332
}
334333

335-
override fun get(index: Int): T = items[index]
334+
override fun get(index: Int): T = super.get(index)!!
336335
override fun iterator(): Iterator<T> = items.iterator()
337336
override fun listIterator(): ListIterator<T> = items.listIterator()
338337
override fun listIterator(index: Int): ListIterator<T> = items.listIterator(index)
@@ -505,6 +504,7 @@ public abstract class PagingObjectBase<T : Any, Z : PagingObjectBase<T, Z>> : Li
505504
override fun indexOf(element: T?): Int = items.indexOf(element)
506505
override fun isEmpty(): Boolean = items.isEmpty()
507506
override fun lastIndexOf(element: T?): Int = items.lastIndexOf(element)
507+
override fun get(index: Int): T? = items[index]
508508
}
509509

510510
internal fun Any.instantiateLateinitsIfPagingObjects(api: GenericSpotifyApi) = when (this) {

src/commonMain/kotlin/com.adamratzman.spotify/models/Player.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
22
package com.adamratzman.spotify.models
33

4+
import com.adamratzman.spotify.endpoints.client.ClientPlayerApi
45
import com.adamratzman.spotify.utils.getExternalUrls
56
import com.adamratzman.spotify.utils.match
67
import kotlinx.serialization.SerialName
@@ -20,9 +21,17 @@ public data class PlayHistoryContext(
2021
@SerialName("external_urls") private val externalUrlsString: Map<String, String>,
2122
val href: String,
2223
val uri: SpotifyUri,
23-
val type: String
24+
@SerialName("type") val typeString: String
2425
) {
26+
val type: ContextType? get() = ContextType.values().find { it.spotifyType == typeString }
2527
val externalUrls: List<ExternalUrl> get() = getExternalUrls(externalUrlsString)
28+
29+
public enum class ContextType(public val spotifyType: String) {
30+
ARTIST("artist"),
31+
PLAYLIST("playlist"),
32+
ALBUM("album"),
33+
SHOW("show")
34+
}
2635
}
2736

2837
/**
@@ -111,18 +120,7 @@ public data class CurrentlyPlayingContext(
111120
@SerialName("repeat_state") val repeatStateString: String,
112121
val context: Context? = null
113122
) {
114-
val repeatState: RepeatState get() = RepeatState.values().match(repeatStateString)!!
115-
}
116-
117-
/**
118-
* How and if playback is repeating
119-
*/
120-
public enum class RepeatState(public val identifier: String) : ResultEnum {
121-
OFF("off"),
122-
TRACK("track"),
123-
CONTEXT("context");
124-
125-
override fun retrieveIdentifier(): String = identifier
123+
val repeatState: ClientPlayerApi.PlayerRepeatState get() = ClientPlayerApi.PlayerRepeatState.values().match(repeatStateString)!!
126124
}
127125

128126
/**

src/commonMain/kotlin/com.adamratzman.spotify/models/Track.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.serialization.Serializable
1313
* identified by their ISO 3166-1 alpha-2 code.
1414
* @param discNumber The disc number (usually 1 unless the album consists of more than one disc).
1515
* @param durationMs The track length in milliseconds.
16+
* @property length The track length in milliseconds. Alias of [durationMs]
1617
* @param explicit Whether or not the track has explicit lyrics ( true = yes it does; false = no it does not OR unknown).
1718
* @param href A link to the Web API endpoint providing full details of the track.
1819
* @param id The Spotify ID for the track.
@@ -60,6 +61,8 @@ public data class SimpleTrack(
6061

6162
val externalIds: List<ExternalId> get() = externalIdsString.map { ExternalId(it.key, it.value) }
6263

64+
val length: Int get() = durationMs
65+
6366
/**
6467
* Converts this [SimpleTrack] into a full [Track] object with the given
6568
* market
@@ -82,6 +85,7 @@ public data class SimpleTrack(
8285
* given market. Otherwise false.
8386
* @param discNumber The disc number (usually 1 unless the album consists of more than one disc).
8487
* @param durationMs The track length in milliseconds.
88+
* @property length The track length in milliseconds. Alias of [durationMs]
8589
* @param explicit Whether or not the track has explicit lyrics ( true = yes it does; false = no it does not OR unknown).
8690
* @param href A link to the Web API endpoint providing full details of the track.
8791
* @param id The Spotify ID for the track.
@@ -139,6 +143,8 @@ public data class Track(
139143

140144
val externalIds: List<ExternalId> get() = externalIdsString.map { ExternalId(it.key, it.value) }
141145

146+
val length: Int get() = durationMs
147+
142148
override fun getMembersThatNeedApiInstantiation(): List<NeedsApi?> = artists + album + linkedTrack + this
143149
}
144150

src/commonTest/kotlin/com.adamratzman/spotify/priv/ClientPlayerApiTest.kt

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package com.adamratzman.spotify.priv
22

33
import com.adamratzman.spotify.SpotifyClientApi
44
import com.adamratzman.spotify.buildSpotifyApi
5+
import com.adamratzman.spotify.endpoints.client.ClientPlayerApi
6+
import com.adamratzman.spotify.models.CollectionUri
7+
import com.adamratzman.spotify.models.PlayHistoryContext
8+
import com.adamratzman.spotify.models.PlayableUri
59
import com.adamratzman.spotify.models.SpotifyTrackUri
6-
import com.adamratzman.spotify.models.Track
710
import com.adamratzman.spotify.runBlockingTest
11+
import kotlinx.coroutines.delay
812
import kotlin.test.Test
13+
import kotlin.test.assertEquals
14+
import kotlin.test.assertNotNull
915
import kotlin.test.assertTrue
16+
import kotlin.time.ExperimentalTime
17+
import kotlin.time.measureTime
1018

19+
@ExperimentalTime
1120
class ClientPlayerApiTest {
1221
lateinit var api: SpotifyClientApi
1322

@@ -37,32 +46,126 @@ class ClientPlayerApiTest {
3746
playableUrisToPlay = listOf(SpotifyTrackUri("spotify:track:6WcinC5nKan2DMFUfjVerX")),
3847
deviceId = device.id
3948
)
49+
delay(1000)
4050
val getCurrentContext = suspend { api.player.getCurrentContext() }
4151
var context = getCurrentContext()
4252
assertTrue(context != null && context.isPlaying && context.track?.id == "6WcinC5nKan2DMFUfjVerX")
4353
api.player.pause()
4454
context = getCurrentContext()!!
4555

46-
assertTrue(!context.isPlaying && context.track?.id != null)
56+
assertTrue(!context.isPlaying)
57+
assertNotNull(context.track?.id)
4758

4859
val playlist = api.playlists.getPlaylist("37i9dQZF1DXcBWIGoYBM5M")!!
4960
api.player.startPlayback(
5061
collectionUri = playlist.uri
5162
)
63+
delay(1000)
5264
context = getCurrentContext()
5365
assertTrue(context != null && context.isPlaying && context.track?.id == playlist.tracks.items.first().track!!.id)
5466
api.player.pause()
67+
}
68+
}
69+
70+
@Test
71+
fun testGetRecentlyPlayed() {
72+
runBlockingTest {
73+
if (!testPrereq()) return@runBlockingTest
74+
api.player.getRecentlyPlayed()
75+
}
76+
}
5577

78+
@Test
79+
fun testGetCurrentlyPlaying() {
80+
runBlockingTest {
81+
if (!testPrereq()) return@runBlockingTest
82+
val device = api.player.getDevices().first()
5683

84+
val trackId = "7lPN2DXiMsVn7XUKtOW1CS"
85+
api.player.startPlayback(
86+
playableUrisToPlay = listOf(PlayableUri("spotify:track:$trackId")),
87+
deviceId = device.id
88+
)
89+
delay(1000)
90+
val currentlyPlayingObjectTrack = api.player.getCurrentlyPlaying()
91+
assertNotNull(currentlyPlayingObjectTrack)
92+
assertTrue(currentlyPlayingObjectTrack.isPlaying && currentlyPlayingObjectTrack.context == null)
93+
94+
val playlistId = "3DhwYIoAZ8mXlxiBkCuOx7"
95+
api.player.startPlayback(collectionUri = CollectionUri("spotify:playlist:3DhwYIoAZ8mXlxiBkCuOx7"))
96+
delay(1000)
97+
val currentlyPlayingObjectPlaylist = api.player.getCurrentlyPlaying()
98+
assertNotNull(currentlyPlayingObjectPlaylist)
99+
assertTrue(currentlyPlayingObjectPlaylist.isPlaying)
100+
assertEquals(playlistId, currentlyPlayingObjectPlaylist.context?.uri?.id)
101+
assertEquals(PlayHistoryContext.ContextType.PLAYLIST, currentlyPlayingObjectPlaylist.context?.type)
102+
103+
api.player.pause()
57104
}
58105
}
59106

60107
@Test
61-
fun testGetRecentlyPlayed() {
108+
fun testSeek() {
62109
runBlockingTest {
63110
if (!testPrereq()) return@runBlockingTest
64111
val device = api.player.getDevices().first()
65-
api.player.getRecentlyPlayed()
112+
113+
val trackId = "7lPN2DXiMsVn7XUKtOW1CS"
114+
val track = api.tracks.getTrack(trackId)!!
115+
api.player.startPlayback(
116+
playableUrisToPlay = listOf(PlayableUri("spotify:track:$trackId")),
117+
deviceId = device.id
118+
)
119+
api.player.pause()
120+
121+
val skipTo = track.length / 2
122+
val delay = measureTime {
123+
api.player.seek(skipTo.toLong())
124+
api.player.resume()
125+
}.inMilliseconds
126+
127+
val waitTime = 3000
128+
delay(waitTime.toLong())
129+
assertTrue(api.player.getCurrentlyPlaying()!!.progressMs!! >= waitTime - delay)
130+
api.player.skipForward()
131+
}
132+
}
133+
134+
@Test
135+
fun testSetPlaybackOptions() {
136+
runBlockingTest {
137+
if (!testPrereq()) return@runBlockingTest
138+
val device = api.player.getDevices().first()
139+
api.player.setRepeatMode(ClientPlayerApi.PlayerRepeatState.OFF, device.id)
140+
api.player.setVolume(50, device.id)
141+
val context = api.player.getCurrentContext()!!
142+
assertEquals(ClientPlayerApi.PlayerRepeatState.OFF, context.repeatState)
143+
144+
}
145+
}
146+
147+
@Test
148+
fun testSkipForwardBackward() {
149+
runBlockingTest {
150+
if (!testPrereq()) return@runBlockingTest
151+
val device = api.player.getDevices().first()
152+
153+
val playlist = api.playlists.getPlaylist("37i9dQZF1DXcBWIGoYBM5M")!!
154+
api.player.startPlayback(
155+
collectionUri = playlist.uri,
156+
deviceId = device.id
157+
)
158+
delay(1000)
159+
160+
api.player.skipForward()
161+
delay(500)
162+
assertEquals(playlist.tracks[1].track!!.id, api.player.getCurrentlyPlaying()!!.track.id)
163+
164+
api.player.skipBehind()
165+
delay(500)
166+
assertEquals(playlist.tracks[0].track!!.id, api.player.getCurrentlyPlaying()!!.track.id)
167+
168+
api.player.pause()
66169
}
67170
}
68171

0 commit comments

Comments
 (0)