Skip to content

Commit 74b7645

Browse files
committed
directly implement list methods in AbstractPagingObject
1 parent 213e45a commit 74b7645

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.adamratzman.spotify.http.SpotifyEndpoint
66
import com.adamratzman.spotify.models.serialization.toCursorBasedPagingObject
77
import com.adamratzman.spotify.models.serialization.toPagingObject
88
import com.adamratzman.spotify.utils.runBlocking
9-
import kotlin.reflect.KClass
109
import kotlinx.coroutines.Dispatchers
1110
import kotlinx.coroutines.ExperimentalCoroutinesApi
1211
import kotlinx.coroutines.flow.Flow
@@ -18,6 +17,7 @@ import kotlinx.coroutines.flow.toList
1817
import kotlinx.serialization.SerialName
1918
import kotlinx.serialization.Serializable
2019
import kotlinx.serialization.Transient
20+
import kotlin.reflect.KClass
2121

2222
/*
2323
Types used in PagingObjects and CursorBasedPagingObjects:
@@ -209,7 +209,7 @@ abstract class AbstractPagingObject<T : Any>(
209209
@Transient open val offset: Int = 0,
210210
@Transient open val previous: String? = null,
211211
@Transient open val total: Int = -1
212-
) : List<T> by items {
212+
) : List<T> {
213213
@Transient
214214
internal var endpoint: SpotifyEndpoint? = null
215215

@@ -262,6 +262,22 @@ abstract class AbstractPagingObject<T : Any>(
262262

263263
@ExperimentalCoroutinesApi
264264
fun flowEndOrdered(): Flow<AbstractPagingObject<T>> = flowForward()
265+
266+
267+
// A paging object is also a list, and instantiation by deserialization doesn't properly store the items list internally
268+
// so we implement list methods
269+
270+
override val size: Int get() = items.size
271+
override fun contains(element: T) = items.contains(element)
272+
override fun containsAll(elements: Collection<T>) = items.containsAll(elements)
273+
override fun get(index: Int) = items[index]
274+
override fun indexOf(element: T) = items.indexOf(element)
275+
override fun isEmpty() = items.isEmpty()
276+
override fun iterator() = items.iterator()
277+
override fun lastIndexOf(element: T) = items.lastIndexOf(element)
278+
override fun listIterator() = items.listIterator()
279+
override fun listIterator(index: Int) =items.listIterator(index)
280+
override fun subList(fromIndex: Int, toIndex: Int) = items.subList(fromIndex, toIndex)
265281
}
266282

267283
internal fun Any.instantiatePagingObjects(spotifyApi: SpotifyApi<*, *>) = when (this) {

0 commit comments

Comments
 (0)