2
2
package com.adamratzman.spotify.models
3
3
4
4
import com.adamratzman.spotify.SpotifyApi
5
+ import com.adamratzman.spotify.SpotifyRestAction
5
6
import com.adamratzman.spotify.http.SpotifyEndpoint
6
7
import com.adamratzman.spotify.models.serialization.toCursorBasedPagingObject
7
8
import com.adamratzman.spotify.models.serialization.toPagingObject
8
9
import com.adamratzman.spotify.utils.runBlocking
9
- import kotlin.reflect.KClass
10
10
import kotlinx.coroutines.Dispatchers
11
11
import kotlinx.coroutines.ExperimentalCoroutinesApi
12
12
import kotlinx.coroutines.flow.Flow
@@ -18,6 +18,8 @@ import kotlinx.coroutines.flow.toList
18
18
import kotlinx.serialization.SerialName
19
19
import kotlinx.serialization.Serializable
20
20
import kotlinx.serialization.Transient
21
+ import kotlin.coroutines.CoroutineContext
22
+ import kotlin.reflect.KClass
21
23
22
24
/*
23
25
Types used in PagingObjects and CursorBasedPagingObjects:
@@ -102,6 +104,7 @@ class PagingObject<T : Any>(
102
104
pagingObjects.add(nxt)
103
105
nxt = nxt.next?.let { nxt?.getNext() }
104
106
}
107
+
105
108
// we don't need to reverse here, as it's in order
106
109
return pagingObjects.asSequence()
107
110
}
@@ -115,7 +118,8 @@ class PagingObject<T : Any>(
115
118
/* *
116
119
* Get all items of type [T] associated with the request
117
120
*/
118
- suspend fun getAllItems () = endpoint!! .toAction { getAll().complete().map { it.items }.flatten() }
121
+ override suspend fun getAllItems (context : CoroutineContext )
122
+ = endpoint!! .toAction { getAll().suspendComplete(context).map { it.items }.flatten().asSequence() }
119
123
}
120
124
121
125
/* *
@@ -150,8 +154,8 @@ class CursorBasedPagingObject<T : Any>(
150
154
/* *
151
155
* Get all items of type [T] associated with the request
152
156
*/
153
- fun getAllItems () = endpoint!! .toAction {
154
- getAll().complete( ).map { it.items }.flatten().toList ()
157
+ override suspend fun getAllItems (context : CoroutineContext ) = endpoint!! .toAction {
158
+ getAll().suspendComplete(context ).map { it.items }.flatten().asSequence ()
155
159
}
156
160
157
161
@Suppress(" UNCHECKED_CAST" )
@@ -219,6 +223,8 @@ abstract class AbstractPagingObject<T : Any>(
219
223
internal abstract suspend fun getImpl (type : PagingTraversalType ): AbstractPagingObject <T >?
220
224
internal abstract suspend fun getAllImpl (): Sequence <AbstractPagingObject <T >>
221
225
226
+ internal abstract suspend fun getAllItems (context : CoroutineContext = Dispatchers .Default ): SpotifyRestAction <Sequence <T >>
227
+
222
228
private suspend fun getNextImpl () = getImpl(PagingTraversalType .FORWARDS )
223
229
private suspend fun getPreviousImpl () = getImpl(PagingTraversalType .BACKWARDS )
224
230
@@ -253,7 +259,7 @@ abstract class AbstractPagingObject<T : Any>(
253
259
254
260
@ExperimentalCoroutinesApi
255
261
fun flowStartOrdered (): Flow <AbstractPagingObject <T >> =
256
- flow< AbstractPagingObject < T >> {
262
+ flow {
257
263
if (previous == null ) return @flow
258
264
flowBackward().toList().reversed().also {
259
265
emitAll(it.asFlow())
0 commit comments