Fix iteration

pull/653/merge
Varun Patil 2023-10-01 19:11:44 -07:00
parent 19e5fa29dc
commit 73ff1b883f
2 changed files with 22 additions and 16 deletions

View File

@ -39,15 +39,13 @@ class SystemImage {
val IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val VIDEO_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
fun query(
fun cursor(
ctx: Context,
collection: Uri,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): List<SystemImage> {
val list = mutableListOf<SystemImage>()
) = sequence {
// Base fields common for videos and images
val projection = arrayListOf(
MediaStore.Images.Media._ID,
@ -122,18 +120,16 @@ class SystemImage {
}
// Add to main list
list.add(image)
yield(image)
}
}
return list
}
fun getByIds(ctx: Context, ids: List<Long>): List<SystemImage> {
val selection = MediaStore.Images.Media._ID + " IN (" + ids.joinToString(",") + ")"
val images = query(ctx, IMAGE_URI, selection, null, null)
val images = cursor(ctx, IMAGE_URI, selection, null, null).toList()
if (images.size == ids.size) return images
return images + query(ctx, VIDEO_URI, selection, null, null)
return images + cursor(ctx, VIDEO_URI, selection, null, null).toList()
}
}

View File

@ -12,6 +12,7 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.exifinterface.media.ExifInterface
import androidx.media3.common.util.UnstableApi
import gallery.memories.MainActivity
import gallery.memories.R
import gallery.memories.dao.AppDatabase
@ -25,7 +26,7 @@ import java.io.IOException
import java.time.Instant
import java.util.concurrent.CountDownLatch
class TimelineQuery(private val mCtx: MainActivity) {
@UnstableApi class TimelineQuery(private val mCtx: MainActivity) {
private val TAG = TimelineQuery::class.java.simpleName
private val mConfigService = ConfigService(mCtx)
@ -254,11 +255,20 @@ class TimelineQuery(private val mCtx: MainActivity) {
selectionArgs = arrayOf(startTime.toString())
}
// Iterate all images and videos from system store
val files =
SystemImage.query(mCtx, SystemImage.IMAGE_URI, selection, selectionArgs, null) +
SystemImage.query(mCtx, SystemImage.VIDEO_URI, selection, selectionArgs, null)
files.forEach { insertItemDb(it) }
// Count number of updates
var updates = 0
// Iterate all images from system store
for (image in SystemImage.cursor(mCtx, SystemImage.IMAGE_URI, selection, selectionArgs, null)) {
insertItemDb(image)
updates++
}
// Iterate all videos from system store
for (video in SystemImage.cursor(mCtx, SystemImage.VIDEO_URI, selection, selectionArgs, null)) {
insertItemDb(video)
updates++
}
// Store last sync time
mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit()
@ -266,7 +276,7 @@ class TimelineQuery(private val mCtx: MainActivity) {
.apply()
// Number of updated files
return files.size
return updates
}
fun syncDeltaDb(): Int {