781flyingdutchman / background_downloader

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

Method to get TaskUpdate of a Task #331

Closed Nico04 closed 5 months ago

Nico04 commented 5 months ago

Currently we can listen to updates using the updates stream or using callbacks, that great. I trying to manage download progression & status that is displayed on a list of widgets. But because updates stream emits TaskUpdate objects for all tasks, it's currently not direct to get the status of all actives Task, or of a specific Task (using id ?), so widgets can display current download status, while being stateless (stateless is important so status is hanlded globally and works even after app restarts).

So it could be very handy to get a TaskUpdate object of a specific Task (or a list for all active tasks), that works even after a cold app start, don't you think ?

(Meanwhile I'll try to develop a system to keep updates in memory for all tasks, so it can be read anytime)

781flyingdutchman commented 5 months ago

If I understand you correctly, I think you can accomplish that relatively easily by creating a global object containing a map of String (taskId) and ValueNotifier. In the updates listener you then set the value of the notifier with the taskId of the incoming update, and in your widget list you use a ValueListenableBuilder to listen to updates for one specific taskId and update the display accordingly. That seems to me the most "reactive" way to do it and you can make it even more robust using, for example, riverPod.

If you want a more conventional polling approach you can use the database (see docs).

Nico04 commented 5 months ago

Thanks for your quick answer. That's exactly what I have done (Map<String, Status>), it works quite well while app is running. But after a cold start, because no updates are emited right away (for instance, progress can be long at a certain state), widget status is incorrect (there is a notification running, but map is empty). Any idea how to handle that case ?

781flyingdutchman commented 5 months ago

Take a look at the docs on tracking tasks in a database. On startup, call trackTasks and make sure to also call resumeFromBackground to fetch pending updates. Once they have completed, your database will be an accurate reflection of current state, and you can set your Map to align with that, then update your map based on incoming status updates as you already do.

Nico04 commented 5 months ago

You're right, that was the solution. I managed to restore full tasks statuses with theses methods, thanks. I knew database existed, but I thought it was useful only when needed to keep track of completed tasks, whereas it's also needed for running tasks. I think the text "even after they have completed" lost me here. Anyway : thanks again, your lib is awesome !