Skip to content

Commit

Permalink
🚀 Improve type promotions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 1, 2024
1 parent d4854c1 commit 5eed08c
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ class PhotoManager(private val context: Context) {
val frame = option.frame
try {
val asset = dbUtils.getAssetEntity(context, id)
if (asset == null) {
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 @@ -110,6 +114,10 @@ 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("202", "Failed to find the asset $id")
return;
}
try {
val byteArray = dbUtils.getOriginBytes(context, asset, needLocationPermission)
resultHandler.reply(byteArray)
Expand All @@ -127,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)
val galleryEntity =
dbUtils.getAssetPathEntityFromId(context, id, type, option) ?: return null
if (option.containsPathModified) {
dbUtils.injectModifiedDate(context, galleryEntity)
}

return galleryEntity
}

Expand All @@ -165,7 +169,15 @@ class PhotoManager(private val context: Context) {
relativePath: String,
orientation: Int?
): AssetEntity {
return dbUtils.saveImage(context, bytes, filename, title, description, relativePath, orientation)
return dbUtils.saveImage(
context,
bytes,
filename,
title,
description,
relativePath,
orientation
)
}

fun saveImage(
Expand Down Expand Up @@ -232,12 +244,13 @@ class PhotoManager(private val context: Context) {
resultHandler.reply(result)
}

fun fetchEntityProperties(id: String): AssetEntity {
fun fetchEntityProperties(id: String): AssetEntity? {
return dbUtils.getAssetEntity(context, id)
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ class PhotoManagerPlugin(
Methods.fetchEntityProperties -> {
val id = call.argument<String>("id")!!
val asset = photoManager.fetchEntityProperties(id)
val assetResult = ConvertUtils.convertAsset(asset)
val assetResult = if (asset != null) {
ConvertUtils.convertAsset(asset)
} else {
null
}
resultHandler.reply(assetResult)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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
Expand All @@ -20,7 +21,7 @@ class ScopedCache {
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,7 +43,7 @@ class ScopedCache {
}
} catch (e: Exception) {
LogUtils.error("Caching $assetId [origin: $isOrigin] error", e)
return null
throw e
}
return targetFile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ object AndroidQDBUtils : IDBUtils {
context: Context,
id: String,
checkIfExists: Boolean
): AssetEntity {
): AssetEntity? {
val selection = "$_ID = ?"
val args = arrayOf(id)
val cursor = context.contentResolver.logQuery(
Expand All @@ -235,7 +235,7 @@ object AndroidQDBUtils : IDBUtils {
)
cursor.use {
return if (it.moveToNext()) it.toAssetEntity(context, checkIfExists)
else throwMsg("Failed to find asset $id")
else null
}
}

Expand All @@ -244,7 +244,7 @@ object AndroidQDBUtils : IDBUtils {
pathId: String,
type: Int,
option: FilterOption
): AssetPathEntity {
): AssetPathEntity? {
val isAll = pathId == ""
val args = ArrayList<String>()
val where = option.makeWhere(type, args)
Expand All @@ -270,15 +270,15 @@ object AndroidQDBUtils : IDBUtils {
name = it.getString(1) ?: ""
assetCount = it.count
} else {
throwMsg("Failed to find the path $pathId")
return null
}
}
return AssetPathEntity(pathId, name, assetCount, type, isAll)
}

override fun getExif(context: Context, id: String): ExifInterface? {
return try {
val asset = getAssetEntity(context, id)
val asset = getAssetEntity(context, id) ?: return null
val uri = getUri(asset)
val originalUri = MediaStore.setRequireOriginal(uri)
val inputStream = context.contentResolver.openInputStream(originalUri) ?: return null
Expand All @@ -289,11 +289,11 @@ object AndroidQDBUtils : IDBUtils {
}
}

override fun getFilePath(context: Context, id: String, origin: Boolean): String? {
val assetEntity = getAssetEntity(context, id)
override fun getFilePath(context: Context, id: String, origin: Boolean): String {
val assetEntity = getAssetEntity(context, id) ?: throwIdNotFound(id)
val filePath = if (shouldUseScopedCache) {
val file = scopedCache.getCacheFileFromEntity(context, assetEntity, origin)
file?.absolutePath
file.absolutePath
} else {
assetEntity.path
}
Expand Down Expand Up @@ -325,7 +325,7 @@ object AndroidQDBUtils : IDBUtils {
if (galleryId == currentGalleryId) {
throwMsg("No copy required, because the target gallery is the same as the current one.")
}
val asset = getAssetEntity(context, assetId)
val asset = getAssetEntity(context, assetId) ?: throwIdNotFound(assetId)
val copyKeys = arrayListOf(
DISPLAY_NAME,
TITLE,
Expand Down Expand Up @@ -380,7 +380,7 @@ object AndroidQDBUtils : IDBUtils {

val insertedId = insertedUri.lastPathSegment
?: throwMsg("Cannot open output stream for $insertedUri.")
return getAssetEntity(context, insertedId)
return getAssetEntity(context, insertedId) ?: throwIdNotFound(assetId)
}

override fun moveToGallery(context: Context, assetId: String, galleryId: String): AssetEntity {
Expand All @@ -398,7 +398,7 @@ object AndroidQDBUtils : IDBUtils {
}
val count = cr.update(allUri, contentValues, idSelection, arrayOf(assetId))
if (count > 0) {
return getAssetEntity(context, assetId)
return getAssetEntity(context, assetId) ?: throwIdNotFound(assetId)
}
throwMsg("Cannot update $assetId relativePath")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object DBUtils : IDBUtils {
pathId: String,
type: Int,
option: FilterOption
): AssetPathEntity {
): AssetPathEntity? {
val args = ArrayList<String>()
val where = option.makeWhere(type, args)
val idSelection: String
Expand All @@ -125,9 +125,7 @@ object DBUtils : IDBUtils {
val name = it.getString(1) ?: ""
val assetCount = it.getInt(2)
AssetPathEntity(id, name, assetCount, 0)
} else {
throwMsg("Failed to find the path $pathId")
}
} else null
}
}

Expand Down Expand Up @@ -215,7 +213,7 @@ object DBUtils : IDBUtils {
context: Context,
id: String,
checkIfExists: Boolean
): AssetEntity {
): AssetEntity? {
val keys =
(IDBUtils.storeImageKeys + IDBUtils.storeVideoKeys + locationKeys + IDBUtils.typeKeys).distinct()
.toTypedArray()
Expand All @@ -230,11 +228,8 @@ object DBUtils : IDBUtils {
null
)
cursor.use {
return if (it.moveToNext()) {
it.toAssetEntity(context, checkIfExists)
} else {
throwMsg("Failed to find asset $id")
}
return if (it.moveToNext()) it.toAssetEntity(context, checkIfExists)
else null
}
}

Expand All @@ -247,13 +242,13 @@ object DBUtils : IDBUtils {
}

override fun getExif(context: Context, id: String): ExifInterface? {
val asset = getAssetEntity(context, id)
val asset = getAssetEntity(context, id) ?: return null
val file = File(asset.path)
return if (file.exists()) ExifInterface(asset.path) else null
}

override fun getFilePath(context: Context, id: String, origin: Boolean): String {
val assetEntity = getAssetEntity(context, id)
val assetEntity = getAssetEntity(context, id) ?: throwIdNotFound(id)
return assetEntity.path
}

Expand All @@ -267,7 +262,8 @@ object DBUtils : IDBUtils {
)
}
val cr = context.contentResolver
val asset = getAssetEntity(context, assetId)
val asset =
getAssetEntity(context, assetId) ?: throwMsg("Failed to find the asset $assetId")

val copyKeys = arrayListOf(
MediaStore.MediaColumns.DISPLAY_NAME,
Expand All @@ -293,7 +289,7 @@ object DBUtils : IDBUtils {
null
)
if (!cursor.moveToNext()) {
throwMsg("Cannot find asset .")
throwIdNotFound(assetId)
}
val insertUri = MediaStoreUtils.getInsertUri(mediaType)
val galleryInfo = getGalleryInfo(context, galleryId) ?: throwMsg("Cannot find gallery info")
Expand All @@ -319,7 +315,7 @@ object DBUtils : IDBUtils {
cursor.close()
val insertedId = insertedUri.lastPathSegment
?: throwMsg("Cannot open output stream for $insertedUri.")
return getAssetEntity(context, insertedId)
return getAssetEntity(context, insertedId) ?: throwIdNotFound(assetId)
}

override fun moveToGallery(context: Context, assetId: String, galleryId: String): AssetEntity {
Expand Down Expand Up @@ -360,7 +356,7 @@ object DBUtils : IDBUtils {

val count = cr.update(allUri, contentValues, idSelection, arrayOf(assetId))
if (count > 0) {
return getAssetEntity(context, assetId)
return getAssetEntity(context, assetId) ?: throwIdNotFound(assetId)
}
throwMsg("Cannot update $assetId relativePath")
}
Expand Down
Loading

0 comments on commit 5eed08c

Please sign in to comment.