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 IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val VIDEO_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI val VIDEO_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
fun query( fun cursor(
ctx: Context, ctx: Context,
collection: Uri, collection: Uri,
selection: String?, selection: String?,
selectionArgs: Array<String>?, selectionArgs: Array<String>?,
sortOrder: String? sortOrder: String?
): List<SystemImage> { ) = sequence {
val list = mutableListOf<SystemImage>()
// Base fields common for videos and images // Base fields common for videos and images
val projection = arrayListOf( val projection = arrayListOf(
MediaStore.Images.Media._ID, MediaStore.Images.Media._ID,
@ -122,18 +120,16 @@ class SystemImage {
} }
// Add to main list // Add to main list
list.add(image) yield(image)
} }
} }
return list
} }
fun getByIds(ctx: Context, ids: List<Long>): List<SystemImage> { fun getByIds(ctx: Context, ids: List<Long>): List<SystemImage> {
val selection = MediaStore.Images.Media._ID + " IN (" + ids.joinToString(",") + ")" 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 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.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.exifinterface.media.ExifInterface import androidx.exifinterface.media.ExifInterface
import androidx.media3.common.util.UnstableApi
import gallery.memories.MainActivity import gallery.memories.MainActivity
import gallery.memories.R import gallery.memories.R
import gallery.memories.dao.AppDatabase import gallery.memories.dao.AppDatabase
@ -25,7 +26,7 @@ import java.io.IOException
import java.time.Instant import java.time.Instant
import java.util.concurrent.CountDownLatch 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 TAG = TimelineQuery::class.java.simpleName
private val mConfigService = ConfigService(mCtx) private val mConfigService = ConfigService(mCtx)
@ -254,11 +255,20 @@ class TimelineQuery(private val mCtx: MainActivity) {
selectionArgs = arrayOf(startTime.toString()) selectionArgs = arrayOf(startTime.toString())
} }
// Iterate all images and videos from system store // Count number of updates
val files = var updates = 0
SystemImage.query(mCtx, SystemImage.IMAGE_URI, selection, selectionArgs, null) +
SystemImage.query(mCtx, SystemImage.VIDEO_URI, selection, selectionArgs, null) // Iterate all images from system store
files.forEach { insertItemDb(it) } 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 // Store last sync time
mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit() mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit()
@ -266,7 +276,7 @@ class TimelineQuery(private val mCtx: MainActivity) {
.apply() .apply()
// Number of updated files // Number of updated files
return files.size return updates
} }
fun syncDeltaDb(): Int { fun syncDeltaDb(): Int {