@@ -11,43 +11,19 @@ import java.net.URLEncoder
1111import java.util.*
1212import java.util.function.Supplier
1313
14- data class CursorBasedPagingObject <out T >(val href : String , val items : List <T >, val limit : Int , val next : String? , val cursors : Cursor ,
15- val total : Int , val endpoint : SpotifyEndpoint ) {
16- inline fun <reified T > getNext (): SpotifyRestAction <CursorBasedPagingObject <T >? > = endpoint.toAction(
17- Supplier {
18- catch {
19- next?.let { endpoint.get(it).toCursorBasedPagingObject<T >(endpoint = endpoint) }
20- }
21- })
22-
23- inline fun <reified T > getAll (): SpotifyRestAction <List <CursorBasedPagingObject <T >>> {
24- return endpoint.toAction(Supplier {
25- this as CursorBasedPagingObject <T >
26- val pagingObjects = mutableListOf<CursorBasedPagingObject <T >>(this )
27- var next = getNext<T >().complete()
28- while (next != null ) {
29- pagingObjects.add(next)
30- next = getNext<T >().complete()
31- }
32- pagingObjects.toList()
33- })
34- }
35-
36- inline fun <reified T > getAllItems (): SpotifyRestAction <List <T >> {
37- return endpoint.toAction(Supplier {
38- getAll<T >().complete().map { it.items }.flatten()
39- })
40- }
41- }
42-
4314data class Cursor (val after : String )
4415
45- data class PagingObject <out T >(val href : String , val items : List <T >, val limit : Int , val next : String? = null , val offset : Int = 0 ,
16+ class CursorBasedPagingObject <out T >(href : String , items : List <T >, limit : Int , next : String? ,
17+ val cursors : Cursor , total : Int , endpoint : SpotifyEndpoint )
18+ : PagingObject <T >(href, items, limit, next, 0 , null , total, endpoint)
19+
20+ open class PagingObject <out T >(val href : String , val items : List <T >, val limit : Int , val next : String? = null , val offset : Int = 0 ,
4621 val previous : String? = null , val total : Int , var endpoint : SpotifyEndpoint ) {
4722 inline fun <reified T > getNext (): SpotifyRestAction <PagingObject <T >? > = endpoint.toAction(
4823 Supplier {
4924 catch {
50- next?.let { endpoint.get(it).toPagingObject<T >(endpoint = endpoint) }
25+ if (this is CursorBasedPagingObject ) next?.let { endpoint.get(it).toCursorBasedPagingObject<T >(endpoint = endpoint) }
26+ else next?.let { endpoint.get(it).toPagingObject<T >(endpoint = endpoint) }
5127 }
5228 })
5329
@@ -62,29 +38,40 @@ data class PagingObject<out T>(val href: String, val items: List<T>, val limit:
6238 this as PagingObject <T >
6339 return endpoint.toAction(
6440 Supplier {
65- val pagingObjects = mutableListOf<PagingObject <T >>()
66- var prev = previous?.let { getPrevious<T >().complete() }
67- while (prev != null ) {
68- pagingObjects.add(prev)
69- prev = prev.previous?.let { prev?.getPrevious<T >()?.complete() }
70- }
71- pagingObjects.reverse() // closer we are to current, the further we are from the start
72-
73- pagingObjects.add(this )
74-
75- var nxt = next?.let { getNext<T >().complete() }
76- while (nxt != null ) {
77- pagingObjects.add(nxt)
78- nxt = nxt.next?.let { nxt?.getNext<T >()?.complete() }
41+ if (this is CursorBasedPagingObject ) {
42+ this as CursorBasedPagingObject <T >
43+ val pagingObjects = mutableListOf<CursorBasedPagingObject <T >>(this )
44+ var next = getNext<T >().complete()
45+ while (next != null ) {
46+ pagingObjects.add(next as CursorBasedPagingObject <T >)
47+ next = getNext<T >().complete()
48+ }
49+ pagingObjects.toList()
50+ } else {
51+ val pagingObjects = mutableListOf<PagingObject <T >>()
52+ var prev = previous?.let { getPrevious<T >().complete() }
53+ while (prev != null ) {
54+ pagingObjects.add(prev)
55+ prev = prev.previous?.let { prev?.getPrevious<T >()?.complete() }
56+ }
57+ pagingObjects.reverse() // closer we are to current, the further we are from the start
58+
59+ pagingObjects.add(this )
60+
61+ var nxt = next?.let { getNext<T >().complete() }
62+ while (nxt != null ) {
63+ pagingObjects.add(nxt)
64+ nxt = nxt.next?.let { nxt?.getNext<T >()?.complete() }
65+ }
66+ // we don't need to reverse here, as it's in order
67+ pagingObjects.toList()
7968 }
80- // we don't need to reverse here, as it's in order
81- pagingObjects.toList()
8269 })
8370 }
8471
8572 inline fun <reified T > getAllItems (): SpotifyRestAction <List <T >> {
8673 return endpoint.toAction(Supplier {
87- getAll<T >().complete().asSequence().map { it as PagingObject < T > }.map { it .items }.toList().flatten()
74+ getAll<T >().complete().asSequence().map { it.items }.toList().flatten()
8875 })
8976 }
9077}
0 commit comments