Skip to content

Commit 67bba7e

Browse files
committed
fix: correct cursor postion
1 parent 9b7abcb commit 67bba7e

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

android/src/main/java/com/musiclibrary/tracks/GetTracksQuery.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

example/src/TrackList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export default function TrackList() {
104104

105105
Alert.alert('Success', `Picked ${results.length} tracks from:\n${uri}`);
106106
} catch (err) {
107-
Alert.alert('Error', err as string);
107+
console.log(err, 'error');
108+
Alert.alert('Error', 'Failed to get tracks from directory');
108109
} finally {
109110
setLoading(false);
110111
}

0 commit comments

Comments
 (0)