refactor and format code

This commit is contained in:
Peter Ombodi 2025-09-24 18:07:24 +03:00
parent bec1b30554
commit ccc86d8440
3 changed files with 27 additions and 32 deletions

View file

@ -83,15 +83,16 @@ class NativeSyncApiImpl30(context: Context) : NativeSyncApiImplBase(context), Na
storedGen.toString(), storedGen.toString(),
storedGen.toString() storedGen.toString()
) )
if (isTrashed) { val cursor = if (isTrashed) {
val uri = MediaStore.Files.getContentUri(volume)
val queryArgs = Bundle().apply { val queryArgs = Bundle().apply {
putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection) putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection)
putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs) putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs)
putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY) putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
} }
getCursor(volume, queryArgs)
ctx.contentResolver.query(uri, ASSET_PROJECTION, queryArgs, null).use { cursor -> } else {
getCursor(volume, selection, selectionArgs)
}
getAssets(cursor).forEach { getAssets(cursor).forEach {
when (it) { when (it) {
is AssetResult.ValidAsset -> { is AssetResult.ValidAsset -> {
@ -103,19 +104,6 @@ class NativeSyncApiImpl30(context: Context) : NativeSyncApiImplBase(context), Na
} }
} }
} }
} else {
getAssets(getCursor(volume, selection, selectionArgs)).forEach {
when (it) {
is AssetResult.ValidAsset -> {
changed.add(it.asset)
assetAlbums[it.asset.id] = listOf(it.albumId)
}
is AssetResult.InvalidAsset -> deleted.add(it.assetId)
}
}
}
}
// Unmounted volumes are handled in dart when the album is removed // Unmounted volumes are handled in dart when the album is removed
return SyncDelta(hasChanges, changed, deleted, assetAlbums) return SyncDelta(hasChanges, changed, deleted, assetAlbums)
} }
@ -130,19 +118,16 @@ class NativeSyncApiImpl30(context: Context) : NativeSyncApiImplBase(context), Na
val selectionArgs = mutableListOf(albumId, *MEDIA_SELECTION_ARGS) val selectionArgs = mutableListOf(albumId, *MEDIA_SELECTION_ARGS)
for (volume in volumes) { for (volume in volumes) {
val uri = MediaStore.Files.getContentUri(volume) val cursor = getCursor(volume, Bundle().apply {
val queryArgs = Bundle().apply {
putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection) putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection)
putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs.toTypedArray()) putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs.toTypedArray())
putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY) putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
} })
ctx.contentResolver.query(uri, ASSET_PROJECTION, queryArgs, null).use { cursor ->
getAssets(cursor).forEach { res -> getAssets(cursor).forEach { res ->
if (res is AssetResult.ValidAsset) trashed += res.asset if (res is AssetResult.ValidAsset) trashed += res.asset
} }
} }
}
return trashed return trashed
} }

View file

@ -5,6 +5,7 @@ import android.content.ContentUris
import android.content.Context import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Base64 import android.util.Base64
import androidx.core.database.getStringOrNull import androidx.core.database.getStringOrNull
@ -83,6 +84,16 @@ open class NativeSyncApiImplBase(context: Context) {
sortOrder, sortOrder,
) )
protected fun getCursor(
volume: String,
queryArgs: Bundle
): Cursor? = ctx.contentResolver.query(
MediaStore.Files.getContentUri(volume),
ASSET_PROJECTION,
queryArgs,
null
)
protected fun getAssets(cursor: Cursor?): Sequence<AssetResult> { protected fun getAssets(cursor: Cursor?): Sequence<AssetResult> {
return sequence { return sequence {
cursor?.use { c -> cursor?.use { c ->

View file

@ -22,7 +22,6 @@ class TrashedLocalAssetEntity extends Table with DriftDefaultsMixin, AssetEntity
@override @override
Set<Column> get primaryKey => {id, albumId}; Set<Column> get primaryKey => {id, albumId};
} }
extension TrashedLocalAssetEntityDataDomainExtension on TrashedLocalAssetEntityData { extension TrashedLocalAssetEntityDataDomainExtension on TrashedLocalAssetEntityData {