781flyingdutchman / background_downloader

Flutter plugin for file downloads and uploads
Other
162 stars 76 forks source link

Any way to pause or resume a download task with ID? #381

Closed htetlynnhtun closed 1 month ago

htetlynnhtun commented 1 month ago

Is your feature request related to a problem? Please describe. In my app, I have a screen to manage uncompleted and completed tasks. The screen's state is driven by FileDownloader.updates stream and TrackRecords from FileDownloader.database. The task type from these two data sources is of type Task. In one of the on tap handlers, I need to pause or resume a task. But I can't, because FileDownloader.pause and FileDownloader.resume accepts only DownloadTask type.

Describe the solution you'd like It would be nice if we could pause ore resume using just an ID.

Describe alternatives you've considered I was trying to recreate download task from task, but it's so inconvenience.

781flyingdutchman commented 1 month ago

It is done to help you write type-safe code :) in that it requires you to be sure the task is indeed pausable or resumable.

The task type you get from these data sources is indeed Task but they are also the proper descendant, so they will be a DownloadTask if all you ever enqueue is those. To pause you should therefore test that the task is indeed a DownloadTask and in my opinion Dart offers nice ways to do this using pattern matching or simple if statements. For example, if you had a button to pause download then you could simply make it's onPressed parameter () => (task is DownloadTask) ? FileDownloader().pause(task) : null. The call to pause now is ensured to be done with a task that is a DownloadTask and if it isn't then the button is disabled. Or, if you are 100% sure the task is a DownloadTask you could make it () => FileDownloader().pause(task as DownloadTask) to force the type cast and get a compile time error if it is not right.

I know opinions differ on this, but I believe the type system is very helpful in writing safe code, so try to make it work for me.