Skip to content

Commit db04cc2

Browse files
committed
new enqueue option part 1
1 parent 7a96030 commit db04cc2

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

fetch2/src/main/java/com/tonyodev/fetch2/EnqueueAction.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ enum class EnqueueAction(val value: Int) {
1515
INCREMENT_FILE_NAME(1),
1616

1717
/** Fetch will not enqueue the new request if a request already managed by Fetch has the same file path. */
18-
DO_NOT_ENQUEUE_IF_EXISTING(2);
18+
DO_NOT_ENQUEUE_IF_EXISTING(2),
19+
20+
/** If Fetch is already managing an existing request with the same file path do one the following:
21+
* 1: If existing download is completed, Fetch will call onComplete method on attached Fetch Listeners
22+
* 2: If existing download is not completed, resume download normally
23+
* 3: If not downloaded, download will proceed normally.
24+
* Note: If download is existing, Fetch will not update the existing download with the settings from
25+
* the new passed in request.
26+
* */
27+
UPDATE_ACCORDINGLY(3);
1928

2029
companion object {
2130

@@ -24,6 +33,7 @@ enum class EnqueueAction(val value: Int) {
2433
return when (value) {
2534
1 -> INCREMENT_FILE_NAME
2635
2 -> DO_NOT_ENQUEUE_IF_EXISTING
36+
3 -> UPDATE_ACCORDINGLY
2737
else -> REPLACE_EXISTING
2838
}
2939
}

fetch2/src/main/java/com/tonyodev/fetch2/database/DownloadInfo.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,27 @@ class DownloadInfo : Download {
175175
dest.writeSerializable(HashMap(extras.map))
176176
}
177177

178+
fun copyFrom(downloadInfo: DownloadInfo) {
179+
id = downloadInfo.id
180+
namespace = downloadInfo.namespace
181+
url = downloadInfo.url
182+
file = downloadInfo.file
183+
group = downloadInfo.group
184+
priority = downloadInfo.priority
185+
headers = downloadInfo.headers
186+
downloaded = downloadInfo.downloaded
187+
total = downloadInfo.total
188+
status = downloadInfo.status
189+
error = downloadInfo.error
190+
networkType = downloadInfo.networkType
191+
created = downloadInfo.created
192+
tag = downloadInfo.tag
193+
enqueueAction = downloadInfo.enqueueAction
194+
identifier = downloadInfo.identifier
195+
downloadOnEnqueue = downloadInfo.downloadOnEnqueue
196+
extras = downloadInfo.extras
197+
}
198+
178199
override fun describeContents(): Int {
179200
return 0
180201
}

fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,46 @@ class FetchHandlerImpl(private val namespace: String,
4848
}
4949

5050
private fun enqueueRequests(requests: List<Request>): List<Download> {
51-
val results = requests.distinctBy { it.file }
51+
val results = requests.asSequence().distinctBy { it.file }
5252
.map {
5353
val downloadInfo = it.toDownloadInfo()
5454
downloadInfo.namespace = namespace
5555
prepareDownloadInfoForEnqueue(downloadInfo)
56-
downloadInfo.status = if (it.downloadOnEnqueue) {
57-
Status.QUEUED
56+
if (downloadInfo.status != Status.COMPLETED) {
57+
downloadInfo.status = if (it.downloadOnEnqueue) {
58+
Status.QUEUED
59+
} else {
60+
Status.ADDED
61+
}
62+
val downloadPair = databaseManager.insert(downloadInfo)
63+
logger.d("Enqueued download ${downloadPair.first}")
64+
downloadPair.first
5865
} else {
59-
Status.ADDED
66+
logger.d("Updating download $downloadInfo")
67+
databaseManager.update(downloadInfo)
68+
downloadInfo
6069
}
61-
val downloadPair = databaseManager.insert(downloadInfo)
62-
logger.d("Enqueued download ${downloadPair.first}")
63-
downloadPair.first
64-
}
70+
}.toList()
6571
startPriorityQueueIfNotStarted()
6672
return results
6773
}
6874

6975
private fun prepareDownloadInfoForEnqueue(downloadInfo: DownloadInfo) {
70-
val existingDownload = databaseManager.getByFile(downloadInfo.file)
76+
var existingDownload = databaseManager.getByFile(downloadInfo.file)
7177
if (existingDownload == null) {
7278
createFileIfPossible(File(downloadInfo.file))
7379
}
74-
if (downloadInfo.enqueueAction == EnqueueAction.DO_NOT_ENQUEUE_IF_EXISTING && existingDownload != null) {
80+
if (downloadInfo.enqueueAction == EnqueueAction.UPDATE_ACCORDINGLY && existingDownload != null) {
81+
cancelDownloadsIfDownloading(listOf(existingDownload.id))
82+
existingDownload = databaseManager.getByFile(downloadInfo.file)
83+
if (existingDownload != null) {
84+
if (existingDownload.status != Status.COMPLETED) {
85+
existingDownload.status = Status.QUEUED
86+
existingDownload.error = defaultNoError
87+
}
88+
downloadInfo.copyFrom(existingDownload)
89+
}
90+
} else if (downloadInfo.enqueueAction == EnqueueAction.DO_NOT_ENQUEUE_IF_EXISTING && existingDownload != null) {
7591
throw FetchException(REQUEST_WITH_FILE_PATH_ALREADY_EXIST)
7692
} else if (downloadInfo.enqueueAction == EnqueueAction.REPLACE_EXISTING && existingDownload != null) {
7793
deleteDownloads(listOf(downloadInfo.id))
@@ -275,14 +291,9 @@ class FetchHandlerImpl(private val namespace: String,
275291
val oldDownloadInfo = databaseManager.get(requestId)
276292
if (oldDownloadInfo != null) {
277293
val newDownloadInfo = newRequest.toDownloadInfo()
278-
newDownloadInfo.status = if (newRequest.downloadOnEnqueue) {
279-
Status.QUEUED
280-
} else {
281-
Status.ADDED
282-
}
283294
newDownloadInfo.namespace = namespace
284295
val enqueueAction = newDownloadInfo.enqueueAction
285-
if (enqueueAction == EnqueueAction.REPLACE_EXISTING) {
296+
if (enqueueAction == EnqueueAction.REPLACE_EXISTING || enqueueAction == EnqueueAction.UPDATE_ACCORDINGLY) {
286297
if (newRequest.file == oldDownloadInfo.file) {
287298
newDownloadInfo.downloaded = oldDownloadInfo.downloaded
288299
newDownloadInfo.total = oldDownloadInfo.total
@@ -291,6 +302,15 @@ class FetchHandlerImpl(private val namespace: String,
291302
} else {
292303
delete(listOf(requestId))
293304
}
305+
if (oldDownloadInfo.status == Status.COMPLETED) {
306+
307+
} else {
308+
newDownloadInfo.status = if (newRequest.downloadOnEnqueue) {
309+
Status.QUEUED
310+
} else {
311+
Status.ADDED
312+
}
313+
}
294314
prepareDownloadInfoForEnqueue(newDownloadInfo)
295315
startPriorityQueueIfNotStarted()
296316
databaseManager.insert(newDownloadInfo)

0 commit comments

Comments
 (0)