From cd1f6c55953d80dc054e671553b9c32b09ebf3e2 Mon Sep 17 00:00:00 2001 From: Tonyo Francis Date: Sat, 7 Apr 2018 19:16:06 -0400 Subject: [PATCH] minor fixes --- .../fetch2/database/DatabaseManager.kt | 1 + .../fetch2/database/DatabaseManagerImpl.kt | 8 ++++-- .../fetch2/util/DatabaseExtensions.kt | 28 ++++--------------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManager.kt b/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManager.kt index ce26804a..8acd7496 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManager.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManager.kt @@ -18,6 +18,7 @@ interface DatabaseManager : Closeable { fun deleteAll() fun update(downloadInfo: DownloadInfo) fun update(downloadInfoList: List) + fun updateNoLock(downloadInfoList: List) fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo) fun get(): List fun get(id: Int): DownloadInfo? diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt index ace0b610..0f327c73 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/database/DatabaseManagerImpl.kt @@ -89,11 +89,15 @@ class DatabaseManagerImpl constructor(context: Context, override fun update(downloadInfoList: List) { synchronized(lock) { - throwExceptionIfClosed() - requestDatabase.requestDao().update(downloadInfoList) + updateNoLock(downloadInfoList) } } + override fun updateNoLock(downloadInfoList: List) { + throwExceptionIfClosed() + requestDatabase.requestDao().update(downloadInfoList) + } + override fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo) { synchronized(lock) { throwExceptionIfClosed() diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/util/DatabaseExtensions.kt b/fetch2/src/main/java/com/tonyodev/fetch2/util/DatabaseExtensions.kt index d4636f87..56689f44 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/util/DatabaseExtensions.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/util/DatabaseExtensions.kt @@ -30,8 +30,10 @@ fun DatabaseManager.sanitize(downloads: List, initializing: Boolea Status.PAUSED, Status.COMPLETED, Status.CANCELLED, - Status.REMOVED -> { - if (!fileExist) { + Status.REMOVED, + Status.FAILED, + Status.QUEUED -> { + if (!fileExist && downloadInfo.status != Status.QUEUED) { downloadInfo.status = Status.FAILED downloadInfo.error = Error.FILE_NOT_FOUND downloadInfo.downloaded = 0L @@ -53,19 +55,6 @@ fun DatabaseManager.sanitize(downloads: List, initializing: Boolea } } } - Status.FAILED -> { - if (fileExist) { - if (downloadInfo.downloaded != fileLength) { - downloadInfo.downloaded = fileLength - changedDownloadsList.add(downloadInfo) - } - } else { - downloadInfo.error = Error.FILE_NOT_FOUND - downloadInfo.downloaded = 0L - downloadInfo.total = -1L - changedDownloadsList.add(downloadInfo) - } - } Status.DOWNLOADING -> { if (initializing) { downloadInfo.status = Status.QUEUED @@ -75,21 +64,14 @@ fun DatabaseManager.sanitize(downloads: List, initializing: Boolea changedDownloadsList.add(downloadInfo) } } - Status.QUEUED -> { - if (fileExist && downloadInfo.downloaded != fileLength) { - downloadInfo.downloaded = fileLength - changedDownloadsList.add(downloadInfo) - } - } Status.NONE, Status.DELETED -> { - } } } if (changedDownloadsList.size > 0) { try { - update(changedDownloadsList) + updateNoLock(changedDownloadsList) } catch (e: Exception) { logger.e("Database sanitize update error", e) }