Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Apr 7, 2018
1 parent bfab562 commit cd1f6c5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface DatabaseManager : Closeable {
fun deleteAll()
fun update(downloadInfo: DownloadInfo)
fun update(downloadInfoList: List<DownloadInfo>)
fun updateNoLock(downloadInfoList: List<DownloadInfo>)
fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo)
fun get(): List<DownloadInfo>
fun get(id: Int): DownloadInfo?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ class DatabaseManagerImpl constructor(context: Context,

override fun update(downloadInfoList: List<DownloadInfo>) {
synchronized(lock) {
throwExceptionIfClosed()
requestDatabase.requestDao().update(downloadInfoList)
updateNoLock(downloadInfoList)
}
}

override fun updateNoLock(downloadInfoList: List<DownloadInfo>) {
throwExceptionIfClosed()
requestDatabase.requestDao().update(downloadInfoList)
}

override fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo) {
synchronized(lock) {
throwExceptionIfClosed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ fun DatabaseManager.sanitize(downloads: List<DownloadInfo>, 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
Expand All @@ -53,19 +55,6 @@ fun DatabaseManager.sanitize(downloads: List<DownloadInfo>, 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
Expand All @@ -75,21 +64,14 @@ fun DatabaseManager.sanitize(downloads: List<DownloadInfo>, 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)
}
Expand Down

0 comments on commit cd1f6c5

Please sign in to comment.