Skip to content

Commit daf07e6

Browse files
committed
exercises are downloaded in batches, and opened as they are done
1 parent d698622 commit daf07e6

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/actions/DownloadExercisesAction.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ public class DownloadExercisesAction {
3535
private List<Exercise> exercisesToDownload;
3636
private TmcCore tmcCore;
3737
private NBTmcSettings settings;
38+
private int batchSize = 3;
3839

3940
/**
40-
* Downloads all exercises of the list from TmcServer, unzips and opens them and
41-
* saves the checksums of each Exercise to courseDb.
41+
* Downloads all exercises of the list from TmcServer, unzips and opens them
42+
* and saves the checksums of each Exercise to courseDb.
4243
*/
4344
public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
4445
this.projectMediator = ProjectMediator.getInstance();
@@ -50,30 +51,43 @@ public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
5051
}
5152

5253
public void run() throws TmcCoreException {
54+
55+
int start = 0;
56+
int end = Math.min(exercisesToDownload.size(), batchSize);
57+
58+
List<Exercise> batch = exercisesToDownload.subList(start, end);
59+
5360
ProgressHandle exerciseDownload = ProgressHandleFactory.createSystemHandle(
54-
"Downloading " + exercisesToDownload.size() + " exercises.");
61+
"Downloading " + batch.size() + " exercises.");
5562
exerciseDownload.start();
56-
ListenableFuture<List<Exercise>> dlFuture = tmcCore.downloadExercises(exercisesToDownload, settings);
5763

58-
Futures.addCallback(dlFuture, new ProjectOpener(exerciseDownload));
64+
ListenableFuture<List<Exercise>> dlFuture = tmcCore.downloadExercises(batch, settings);
65+
66+
Futures.addCallback(dlFuture, new ProjectOpener(exerciseDownload, end));
5967
}
6068

6169
private class ProjectOpener implements FutureCallback<List<Exercise>> {
6270

6371
private ProgressHandle lastAction;
64-
72+
private int start;
73+
6574
/**
66-
* Converts Exercise objects to TmcProjectInfo objects.
67-
* Saves them to CourseDb and opens them.
68-
* @param lastAction
75+
* Converts Exercise objects to TmcProjectInfo objects. Saves them to
76+
* CourseDb and opens them.
77+
*
78+
* @param lastAction
6979
*/
70-
public ProjectOpener(ProgressHandle lastAction) {
80+
public ProjectOpener(ProgressHandle lastAction, int start) {
7181
this.lastAction = lastAction;
82+
this.start = start;
7283
}
7384

7485
@Override
7586
public void onSuccess(List<Exercise> downloadedExercises) {
7687
lastAction.finish();
88+
89+
downloadNextBatch();
90+
7791
List<TmcProjectInfo> projects = new ArrayList<TmcProjectInfo>();
7892
for (Exercise exercise : downloadedExercises) {
7993
TmcProjectInfo info = projectMediator.tryGetProjectForExercise(exercise);
@@ -86,6 +100,30 @@ public void onSuccess(List<Exercise> downloadedExercises) {
86100
projectMediator.openProjects(projects);
87101
}
88102

103+
private void downloadNextBatch() {
104+
int end = Math.min(exercisesToDownload.size(), start + batchSize);
105+
List<Exercise> batch = exercisesToDownload.subList(start, end);
106+
107+
if (batch.isEmpty()) {
108+
return;
109+
}
110+
111+
ProgressHandle exerciseDownload = ProgressHandleFactory.createSystemHandle(
112+
"Downloading " + batch.size() + " exercises.");
113+
exerciseDownload.start();
114+
115+
ListenableFuture<List<Exercise>> dlFuture;
116+
try {
117+
dlFuture = tmcCore.downloadExercises(batch, settings);
118+
} catch (TmcCoreException ex) {
119+
List<Exercise> empty = new ArrayList<Exercise>();
120+
dlFuture = Futures.immediateFuture(empty);
121+
dialogs.displayError("Could not download exercises: " + batch, ex);
122+
}
123+
124+
Futures.addCallback(dlFuture, new ProjectOpener(exerciseDownload, end));
125+
}
126+
89127
private void saveDownloadedExercisesToCourseDb(final List<Exercise> downloadedExercises) {
90128
try {
91129
SwingUtilities.invokeAndWait(new Runnable() {

0 commit comments

Comments
 (0)