@@ -57,24 +57,28 @@ object GetTracksQuery {
5757 val albumIdColumn = c.getColumnIndexOrThrow(MediaStore .Audio .Media .ALBUM_ID )
5858
5959 // Jump to the specified start position
60- if (options.after != null ) {
60+ val foundAfter = if (options.after == null ) {
61+ cursor.moveToFirst() // Move to the first record
62+ true
63+ } else {
6164 var found = false
62- while (c.moveToNext()) {
63- val id = c.getLong(idColumn).toString()
64- if (id == options.after) {
65- found = true
66- break
67- }
68- }
69- if (! found && c.count > 0 ) {
70- throw IllegalArgumentException (" Invalid cursor position: ${options.after} " )
65+ if (cursor.moveToFirst()) {
66+ do {
67+ val id = cursor.getLong(idColumn).toString()
68+ if (id == options.after) {
69+ found = true
70+ break
71+ }
72+ } while (cursor.moveToNext())
7173 }
74+ // Move to the next record after the specified after if found
75+ found && cursor.moveToNext()
7276 }
7377
7478 var count = 0
7579 val maxItems = options.first.coerceAtMost(1000 ) // Limit the maximum number of queries
7680
77- while (c.moveToNext() && count < maxItems) {
81+ while (foundAfter && count < maxItems) {
7882 try {
7983 val id = c.getLong(idColumn)
8084 val title = c.getString(titleColumn) ? : " "
@@ -114,10 +118,12 @@ object GetTracksQuery {
114118 // Continue processing other tracks if a single track fails to parse
115119 continue
116120 }
121+
122+ if (! cursor.moveToNext()) break
117123 }
118124
119125 // Check if there are more data
120- hasNextPage = c.moveToNext()
126+ hasNextPage = ! c.isAfterLast
121127 }
122128
123129 return PaginatedResult (
0 commit comments