Skip to content

Commit

Permalink
🚀 Returns non-nullable results as much as possible (#1192)
Browse files Browse the repository at this point in the history
Fixes #1007
  • Loading branch information
AlexV525 authored Oct 2, 2024
2 parents 0829de3 + 37bc1f7 commit b1217fb
Show file tree
Hide file tree
Showing 18 changed files with 368 additions and 403 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ class PhotoManager(private val context: Context) {
try {
val asset = dbUtils.getAssetEntity(context, id)
if (asset == null) {
resultHandler.replyError("The asset not found!")
return
resultHandler.replyError("201", "Failed to find the asset $id")
return;
}
ThumbnailUtil.getThumbnail(
context,
asset,
option.width,
option.height,
width,
height,
format,
quality,
frame,
Expand All @@ -115,8 +115,8 @@ class PhotoManager(private val context: Context) {
fun getOriginBytes(id: String, resultHandler: ResultHandler, needLocationPermission: Boolean) {
val asset = dbUtils.getAssetEntity(context, id)
if (asset == null) {
resultHandler.replyError("The asset not found")
return
resultHandler.replyError("202", "Failed to find the asset $id")
return;
}
try {
val byteArray = dbUtils.getOriginBytes(context, asset, needLocationPermission)
Expand All @@ -135,28 +135,24 @@ class PhotoManager(private val context: Context) {
fun fetchPathProperties(id: String, type: Int, option: FilterOption): AssetPathEntity? {
if (id == ALL_ID) {
val allGalleryList = dbUtils.getAssetPathList(context, type, option)
return if (allGalleryList.isEmpty()) {
null
} else {
// make is all to the gallery list
allGalleryList.run {
var assetCount = 0
for (item in this) {
assetCount += item.assetCount
}
AssetPathEntity(ALL_ID, ALL_ALBUM_NAME, assetCount, type, true).apply {
if (option.containsPathModified) {
dbUtils.injectModifiedDate(context, this)
}
return if (allGalleryList.isEmpty()) null
else allGalleryList.run {
var assetCount = 0
for (item in this) {
assetCount += item.assetCount
}
AssetPathEntity(ALL_ID, ALL_ALBUM_NAME, assetCount, type, true).apply {
if (option.containsPathModified) {
dbUtils.injectModifiedDate(context, this)
}
}
}
}
val galleryEntity = dbUtils.getAssetPathEntityFromId(context, id, type, option)
if (galleryEntity != null && option.containsPathModified) {
val galleryEntity =
dbUtils.getAssetPathEntityFromId(context, id, type, option) ?: return null
if (option.containsPathModified) {
dbUtils.injectModifiedDate(context, galleryEntity)
}

return galleryEntity
}

Expand All @@ -172,8 +168,16 @@ class PhotoManager(private val context: Context) {
description: String,
relativePath: String,
orientation: Int?
): AssetEntity? {
return dbUtils.saveImage(context, bytes, filename, title, description, relativePath, orientation)
): AssetEntity {
return dbUtils.saveImage(
context,
bytes,
filename,
title,
description,
relativePath,
orientation
)
}

fun saveImage(
Expand All @@ -182,7 +186,7 @@ class PhotoManager(private val context: Context) {
description: String,
relativePath: String,
orientation: Int?
): AssetEntity? {
): AssetEntity {
return dbUtils.saveImage(context, filePath, title, description, relativePath, orientation)
}

Expand All @@ -192,7 +196,7 @@ class PhotoManager(private val context: Context) {
desc: String,
relativePath: String,
orientation: Int?
): AssetEntity? {
): AssetEntity {
return dbUtils.saveVideo(context, filePath, title, desc, relativePath, orientation)
}

Expand All @@ -218,10 +222,6 @@ class PhotoManager(private val context: Context) {
fun copyToGallery(assetId: String, galleryId: String, resultHandler: ResultHandler) {
try {
val assetEntity = dbUtils.copyToGallery(context, assetId, galleryId)
if (assetEntity == null) {
resultHandler.reply(null)
return
}
resultHandler.reply(ConvertUtils.convertAsset(assetEntity))
} catch (e: Exception) {
LogUtils.error(e)
Expand All @@ -232,10 +232,6 @@ class PhotoManager(private val context: Context) {
fun moveToGallery(assetId: String, albumId: String, resultHandler: ResultHandler) {
try {
val assetEntity = dbUtils.moveToGallery(context, assetId, albumId)
if (assetEntity == null) {
resultHandler.reply(null)
return
}
resultHandler.reply(ConvertUtils.convertAsset(assetEntity))
} catch (e: Exception) {
LogUtils.error(e)
Expand All @@ -252,9 +248,10 @@ class PhotoManager(private val context: Context) {
return dbUtils.getAssetEntity(context, id)
}

fun getUri(id: String): Uri? {
fun getUri(id: String): Uri {
val asset = dbUtils.getAssetEntity(context, id)
return asset?.getUri()
?: throw RuntimeException("Failed to find asset $id")
return asset.getUri()
}

private val cacheFutures = ArrayList<FutureTarget<Bitmap>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,6 @@ class PhotoManagerPlugin(
relativePath,
orientation,
)
if (entity == null) {
resultHandler.reply(null)
return
}
val map = ConvertUtils.convertAsset(entity)
resultHandler.reply(map)
} catch (e: Exception) {
Expand All @@ -515,10 +511,6 @@ class PhotoManagerPlugin(
relativePath,
orientation,
)
if (entity == null) {
resultHandler.reply(null)
return
}
val map = ConvertUtils.convertAsset(entity)
resultHandler.reply(map)
} catch (e: Exception) {
Expand All @@ -541,10 +533,6 @@ class PhotoManagerPlugin(
relativePath,
orientation,
)
if (entity == null) {
resultHandler.reply(null)
return
}
val map = ConvertUtils.convertAsset(entity)
resultHandler.reply(map)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import android.os.Build
import androidx.annotation.RequiresApi
import com.fluttercandies.photo_manager.core.entity.AssetEntity
import com.fluttercandies.photo_manager.core.utils.AndroidQDBUtils
import com.fluttercandies.photo_manager.core.utils.AndroidQDBUtils.throwIdNotFound
import com.fluttercandies.photo_manager.util.LogUtils
import java.io.File
import java.io.FileOutputStream

@RequiresApi(Build.VERSION_CODES.Q)
class ScopedCache {
companion object {
private const val filenamePrefix = "pm_"
private const val FILENAME_PREFIX = "pm_"
}

fun getCacheFileFromEntity(
context: Context,
assetEntity: AssetEntity,
isOrigin: Boolean
): File? {
): File {
val assetId = assetEntity.id
val targetFile = getCacheFile(context, assetEntity, isOrigin)
if (targetFile.exists()) {
Expand All @@ -29,7 +30,7 @@ class ScopedCache {
val contentResolver = context.contentResolver
val uri = AndroidQDBUtils.getUri(assetId, assetEntity.type, isOrigin)
if (uri == Uri.EMPTY) {
return null
throwIdNotFound(assetId)
}
try {
LogUtils.info(
Expand All @@ -42,21 +43,21 @@ class ScopedCache {
}
} catch (e: Exception) {
LogUtils.error("Caching $assetId [origin: $isOrigin] error", e)
return null
throw e
}
return targetFile
}

private fun getCacheFile(context: Context, assetEntity: AssetEntity, isOrigin: Boolean): File {
val originString = if (isOrigin) "_o" else ""
val name = "$filenamePrefix${assetEntity.id}${originString}_${assetEntity.displayName}"
val name = "$FILENAME_PREFIX${assetEntity.id}${originString}_${assetEntity.displayName}"
return File(context.cacheDir, name)
}

fun clearFileCache(context: Context) {
val files = context.cacheDir?.listFiles()?.filterNotNull() ?: return
for (file in files) {
if (file.name.startsWith(filenamePrefix)) {
if (file.name.startsWith(FILENAME_PREFIX)) {
file.delete()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ class FilterCond {
lateinit var durationConstraint: DurationConstraint

companion object {
private const val widthKey = MediaStore.Files.FileColumns.WIDTH
private const val heightKey = MediaStore.Files.FileColumns.HEIGHT
private const val WIDTH_KEY = MediaStore.Files.FileColumns.WIDTH
private const val HEIGHT_KEY = MediaStore.Files.FileColumns.HEIGHT

@SuppressLint("InlinedApi")
private const val durationKey = MediaStore.Video.VideoColumns.DURATION
private const val DURATION_KEY = MediaStore.Video.VideoColumns.DURATION
}

fun sizeCond(): String =
"$widthKey >= ? AND $widthKey <= ? AND $heightKey >= ? AND $heightKey <=?"
"$WIDTH_KEY >= ? AND $WIDTH_KEY <= ? AND $HEIGHT_KEY >= ? AND $HEIGHT_KEY <=?"

fun sizeArgs(): Array<String> {
return arrayOf(
Expand All @@ -192,9 +192,9 @@ class FilterCond {
}

fun durationCond(): String {
val baseCond = "$durationKey >=? AND $durationKey <=?"
val baseCond = "$DURATION_KEY >=? AND $DURATION_KEY <=?"
if (durationConstraint.allowNullable) {
return "( $durationKey IS NULL OR ( $baseCond ) )"
return "( $DURATION_KEY IS NULL OR ( $baseCond ) )"
}
return baseCond
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ class CustomOption(private val map: Map<*, *>) : FilterOption() {
}
return "( $where )"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ abstract class FilterOption {
args: ArrayList<String>,
needAnd: Boolean = true
): String
}
}
Loading

0 comments on commit b1217fb

Please sign in to comment.