Progressbar in Data Loader

Hi,

I am loading data from an external service (loader based on @stukalov’s GitHub - aleksey-stukalov/github-statistics: The project demonstrates how to use external objects provided via REST API in a CUBA Application). All is good.

Now, there is quite some data to load as I have to do a list, then a get for each item. I was wondering about showing a progress bar…

The obvious answer would be to just start the background task in the data loader load delegate, but that would block the UI as I have to return the list from there.

How would one, appropiately, defer the data loading to a background task?
Alternatively… how can I give CUBA hints on the nice progress bar it already shows in the top of the window?

I found this re the old data source API, but things have changed a lot since then: Progressbar for load datasource in screen - #4 от пользователя artamonov - CUBA.Platform

Hi Marc,

If you want to load data asynchronously, you cannot use declarative data loaders. Instead, use a background task and set the result to data containers programmatically.

An example based on the github-statistics project:

public class ContributorBrowse extends StandardLookup<Contributor> {
    @Inject
    private GithubInfoService githubInfoService;
    @Inject
    private TextField<String> repoOwnerField;
    @Inject
    private LookupField<Repository> repoNameField;
    @Inject
    private CollectionContainer<Repository> reposDc;
    // ...

    private class RepositoryLoadTask extends BackgroundTask<Integer, List<Repository>> {

        protected RepositoryLoadTask() {
            super(10000, ContributorBrowse.this);
        }

        @Override
        public List<Repository> run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
            List<Repository> repos = githubInfoService.getRepos(repoOwnerField.getValue());
            repos.sort((o1, o2) -> o2.getStargazersCount().compareTo(o1.getStargazersCount()));
            return repos;
        }

        @Override
        public void done(List<Repository> result) {
            reposDc.setItems(result);
            if (result.size() > 0)
                repoNameField.setValue(result.get(0));
        }
    }
    // ...

    @Subscribe("repoOwnerField")
    public void onRepoOwnerFieldValueChange(HasValue.ValueChangeEvent<String> event) {
        RepositoryLoadTask repositoryLoadTask = new RepositoryLoadTask();
        BackgroundWorkWindow.show(repositoryLoadTask, true);
    }

You can see the whole changeset here.

Of course, instead of BackgroundWorkWindow with indefinite progress bar you can use BackgroundWorkProgressWindow or BackgroundWorker with a custom progress bar.

Regards,
Konstantin

1 Like

Thank you,
that’s very useful!