Glimpse: Cache cursor indexes

Change-Id: I5f7f4bbf5fc5ae987e01fb2bbac43bb9951210a4
This commit is contained in:
Sebastiano Barezzi 2023-08-06 01:22:08 +02:00
parent 4bbb71c891
commit f6b8998442
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
2 changed files with 31 additions and 12 deletions

View File

@ -5,6 +5,7 @@
package org.lineageos.glimpse.thumbnail
import android.database.Cursor
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
@ -25,6 +26,12 @@ class MediaViewerAdapter(
private val exoPlayer: ExoPlayer,
private val currentPositionLiveData: LiveData<Int>,
) : BaseCursorAdapter<MediaViewerAdapter.MediaViewHolder>() {
// Cursor indexes
private var idIndex = -1
private var isFavoriteIndex = -1
private var mediaTypeIndex = -1
private var dateAddedIndex = -1
init {
setHasStableIds(true)
}
@ -50,14 +57,20 @@ class MediaViewerAdapter(
holder.onViewDetachedFromWindow()
}
override fun onChangedCursor(cursor: Cursor?) {
super.onChangedCursor(cursor)
cursor?.let {
idIndex = it.getColumnIndex(MediaStore.Files.FileColumns._ID)
isFavoriteIndex = it.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
mediaTypeIndex = it.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
dateAddedIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
}
}
fun getMediaFromMediaStore(position: Int): Media? {
val cursor = cursor ?: return null
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
val isFavoriteIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
val mediaTypeIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
val dateAddedIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
cursor.moveToPosition(position)
val id = cursor.getLong(idIndex)
@ -75,7 +88,6 @@ class MediaViewerAdapter(
private fun getIdFromMediaStore(position: Int): Long {
val cursor = cursor ?: return 0
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
cursor.moveToPosition(position)
return cursor.getLong(idIndex)
}

View File

@ -30,6 +30,12 @@ class ThumbnailAdapter(
private var recyclerView: RecyclerView? = null
// Cursor indexes
private var idIndex = -1
private var isFavoriteIndex = -1
private var mediaTypeIndex = -1
private var dateAddedIndex = -1
init {
setHasStableIds(true)
}
@ -126,6 +132,13 @@ class ThumbnailAdapter(
super.onChangedCursor(cursor)
headersPositions.clear()
cursor?.let {
idIndex = it.getColumnIndex(MediaStore.Files.FileColumns._ID)
isFavoriteIndex = it.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
mediaTypeIndex = it.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
dateAddedIndex = it.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
}
}
private fun getTruePosition(position: Int) =
@ -139,7 +152,6 @@ class ThumbnailAdapter(
private fun getIdFromMediaStore(position: Int): Long {
val cursor = cursor ?: return 0
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
cursor.moveToPosition(getTruePosition(position))
return cursor.getLong(idIndex)
}
@ -147,11 +159,6 @@ class ThumbnailAdapter(
private fun getMediaFromMediaStore(position: Int): Media? {
val cursor = cursor ?: return null
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
val isFavoriteIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.IS_FAVORITE)
val mediaTypeIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)
val dateAddedIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATE_ADDED)
cursor.moveToPosition(position)
val id = cursor.getLong(idIndex)