I'm trying to test my fork but it won't build because of:
AGPBI: {"kind":"error","text":"Program type already present: com.tonyodev.fetch2.BuildConfig","sources":[{}],"tool":"D8"}
Regardless maybe you could look at my commit, https://github.com/sethchhim/Fetch/commit/9d4da298eae05895c49e894ac8dea0f29e556166
I need a way to know when download is REPLACED. Now that I think about it maybe onDeleted is the correct place to update instead of adding a new one, probably add an isReplaced boolean. Let me know your thoughts, thanks!
@sethchhim the updateRequest method already has a callback for success and failure. Is it not easier to update the views using the callback? I don't see the need to add a replace method to the FetchListener. Examples?
I think I need the new method or add a modified onDeleted because when a new request is added with EnqueueAction.REPLACE_EXISTING, the listener is never updated when a request has actually been replaced.
What do you think about:
private fun prepareDownloadInfoForEnqueue(downloadInfo: DownloadInfo) {
val existingDownload = databaseManager.getByFile(downloadInfo.file)
if (existingDownload == null) {
createFileIfPossible(File(downloadInfo.file))
}
if (downloadInfo.enqueueAction == EnqueueAction.DO_NOT_ENQUEUE_IF_EXISTING && existingDownload != null) {
throw FetchException(REQUEST_WITH_FILE_PATH_ALREADY_EXIST)
} else if (downloadInfo.enqueueAction == EnqueueAction.REPLACE_EXISTING && existingDownload != null) {
deleteDownloads(listOf(downloadInfo.id))
listenerCoordinator.mainListener.onDeleted(download = downloadInfo, replaced = true)
} else if (downloadInfo.enqueueAction == EnqueueAction.INCREMENT_FILE_NAME && existingDownload != null) {
val file = getIncrementedFileIfOriginalExists(downloadInfo.file)
downloadInfo.file = file.absolutePath
downloadInfo.id = getUniqueId(downloadInfo.url, downloadInfo.file)
createFileIfPossible(file)
}
}
My issue is that without an update that a download has been removed (technically replaced), my ui is not updated via the listener and I have a duplicate of the download. It is not duplicated in the Fetch database but it is duplicated in the app's database.
Ok I think just the standard onDeleted would work, no need to edit it. I just want some update after Fetch removes that download during the REPLACE_EXISTINGprocess.
I will try a workaround using the enqueue callback, thanks for the consideration. Also your code is so clean, I'm going to study it more because I rarely use interface contracts. It looks good.
I'm reopening this issue because you did bring up some good points. Let me have a look at the flow again and I will post back here.
Just had a look at the logic for update request. It works similarly to enqueue request minus the callbacks. I think my idea behind the replace method was to update parts of a request silently in the background. Eg. urls and extra data without dispatching update calls. Your use case seems more in-lined with the enqueue method. Can you share sample code via email? I would like to have a better look at the use case. I don't want to add callbacks to the library unless they are truly needed.
internal fun download(login: Login, downloadList: List<Book>, savePath: String) {
downloadList.forEach {
val request = createRequest(login, it, savePath)
deleteMatchingRequestsWithDifferentFileName(request)
fetch.enqueue(request, Func { r -> onRequestQueueSuccess(r) }, Func { e -> onRequestQueueFail(e) })
}
}
private fun deleteMatchingRequestsWithDifferentFileName(request: Request) {
getDownloads().observeForever {
it?.let {
it.forEach {
val isRequestExistWithDifferentFileName = it.url == request.url && it.file != request.file
if (isRequestExistWithDifferentFileName) delete(it)
}
}
}
}
Here is my solution, I went nuclear and just deleted the download if there was a matching request with a different filename. I think this is fine because changing save path is not a frequent event and I will warn the user. I apologize, I misunderstood REPLACE_EXISTING. I didn't read the documentation and assumed it meant replace if existing "url" but it means replace if existing "filename". With this cleared up, your method of deleting the file and not updating the listener is correct. The ui now works as intended, you can close if you want and thanks for the help.
Glad you found a proper solution.