Prevent uploading icon in error

This commit is contained in:
J-Jamet
2021-03-02 16:50:35 +01:00
parent 97765d798c
commit eab8cd101f
5 changed files with 30 additions and 2 deletions

View File

@@ -92,6 +92,7 @@ class Database {
return mDatabaseKDB?.loadedCipherKey ?: mDatabaseKDBX?.loadedCipherKey return mDatabaseKDB?.loadedCipherKey ?: mDatabaseKDBX?.loadedCipherKey
} }
// TODO private
val iconsManager: IconsManager val iconsManager: IconsManager
get() { get() {
return mDatabaseKDB?.iconsManager ?: mDatabaseKDBX?.iconsManager ?: IconsManager() return mDatabaseKDB?.iconsManager ?: mDatabaseKDBX?.iconsManager ?: IconsManager()
@@ -102,6 +103,10 @@ class Database {
?: mDatabaseKDBX?.buildNewCustomIcon(cacheDirectory) ?: mDatabaseKDBX?.buildNewCustomIcon(cacheDirectory)
} }
fun removeCustomIcon(iconUUID: UUID) {
iconsManager.removeCustomIcon(iconUUID)
}
val allowName: Boolean val allowName: Boolean
get() = mDatabaseKDBX != null get() = mDatabaseKDBX != null

View File

@@ -78,6 +78,15 @@ abstract class BinaryPool<T> {
return key return key
} }
/**
* Remove a binary from the pool with its [key], the file is not deleted
*/
@Throws(IOException::class)
fun remove(key: T) {
pool.remove(key)
// Don't clear attachment here because a file can be used in many BinaryAttachment
}
/** /**
* Remove a binary from the pool, the file is not deleted * Remove a binary from the pool, the file is not deleted
*/ */

View File

@@ -319,7 +319,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
} }
fun containsCustomIcons(): Boolean { fun containsCustomIcons(): Boolean {
return this.iconsManager.containsCustomIcons() return this.iconsManager.containsAnyCustomIcon()
} }
fun putCustomData(label: String, value: String) { fun putCustomData(label: String, value: String) {

View File

@@ -60,10 +60,20 @@ class IconsManager {
return IconImageCustom(iconUuid) return IconImageCustom(iconUuid)
} }
fun containsCustomIcons(): Boolean { fun containsAnyCustomIcon(): Boolean {
return !customCache.isEmpty() return !customCache.isEmpty()
} }
fun removeCustomIcon(iconUuid: UUID) {
val binary = customCache[iconUuid]
customCache.remove(iconUuid)
try {
binary?.clear()
} catch (e: Exception) {
Log.e(TAG, "Unable to remove custom icon binary", e)
}
}
fun getCustomIconList(): List<IconImage> { fun getCustomIconList(): List<IconImage> {
val list = ArrayList<IconImage>() val list = ArrayList<IconImage>()
customCache.doForEachBinary { key, binary -> customCache.doForEachBinary { key, binary ->

View File

@@ -53,6 +53,10 @@ class IconPickerViewModel: ViewModel() {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
asyncResult.await()?.let { customIcon -> asyncResult.await()?.let { customIcon ->
iconCustomAdded.value = customIcon iconCustomAdded.value = customIcon
// Remove icon if data cannot be saved
if (customIcon.binaryFile.length <= 0) {
database.removeCustomIcon(customIcon.uuid)
}
} }
} }
} }