@@ -30,7 +30,6 @@ public final class RefreshCoursesAction {
30
30
private CourseDb courseDb ;
31
31
private ConvenientDialogDisplayer dialogs ;
32
32
33
- //private BgTaskListenerList<List<Course>> listeners;
34
33
private FutureCallbackList <List <Course >> callbacks ;
35
34
private final TmcCore tmcCore ;
36
35
private final NBTmcSettings tmcSettings ;
@@ -61,47 +60,62 @@ public RefreshCoursesAction addListener(FutureCallback<List<Course>> callbacks)
61
60
return this ;
62
61
}
63
62
64
- //HUOM metodissa oldRun on vanha toteutus. Se hakee palvelmelta nykyisen kurssin "fullcourse info".
63
+ /**
64
+ * Starts downloading course-jsons from TMC-server.
65
+ * Url of TMC-server is defined in TmcSettings object.
66
+ * TmcCore includes all logic, callbacks here are run after core-futures are ready.
67
+ */
65
68
public void run () {
66
69
try {
67
70
ListenableFuture <List <Course >> listCourses = this .tmcCore .listCourses (tmcSettings );
68
- Futures .addCallback (listCourses , new FutureCallback <List <Course >>() {
69
- @ Override
70
- public void onSuccess (final List <Course > courses ) {
71
- Course currentCourse = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
72
- if (currentCourse != null ) {
73
- try {
74
- ListenableFuture <Course > courseFuture = tmcCore .getCourse (tmcSettings , currentCourse .getDetailsUrl ());
75
- Futures .addCallback (courseFuture , new UpdateCourse (courses ));
76
- } catch (TmcCoreException ex ) {
77
- Exceptions .printStackTrace (ex );
78
- callbacks .onFailure (ex );
79
- }
80
- } else {
81
- callbacks .onSuccess (courses );
82
- }
83
- }
71
+ Futures .addCallback (listCourses , new LoadCourses ());
72
+ } catch (TmcCoreException ex ) {
73
+ Exceptions .printStackTrace (ex );
74
+ callbacks .onFailure (ex );
75
+ }
76
+ }
84
77
85
- @ Override
86
- public void onFailure (Throwable ex ) {
87
- log .log (Level .INFO , "Failed to download current course info." , ex );
78
+ /**
79
+ * This callBack is run when ListenableFuture (to witch this is attached) is done.
80
+ * On success method takes list of Course-objects, searches the current course and starts uploading
81
+ * the details of the course.
82
+ * If no currentCourse found, no need to update details.
83
+ */
84
+ class LoadCourses implements FutureCallback <List <Course >> {
85
+ @ Override
86
+ public void onSuccess (final List <Course > courses ) {
87
+ Course currentCourse = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
88
+ if (currentCourse != null ) {
89
+ try {
90
+ ListenableFuture <Course > courseFuture = tmcCore .getCourse (tmcSettings , currentCourse .getDetailsUrl ());
91
+ Futures .addCallback (courseFuture , new UpdateCourse (courses ));
92
+ } catch (TmcCoreException ex ) {
93
+ Exceptions .printStackTrace (ex );
88
94
callbacks .onFailure (ex );
89
95
}
96
+ } else {
97
+ callbacks .onSuccess (courses );
90
98
}
91
- );
92
- } catch (TmcCoreException ex ) {
93
- Exceptions .printStackTrace (ex );
99
+ }
100
+
101
+ @ Override
102
+ public void onFailure (Throwable ex ) {
103
+ log .log (Level .INFO , "Failed to download current course info." , ex );
94
104
callbacks .onFailure (ex );
95
105
}
96
106
}
97
107
108
+ /**
109
+ * When detailed current course is present, courses will be given to FutureCallbackList,
110
+ * that shares the result to every callback that is attached to that list.
111
+ */
98
112
class UpdateCourse implements FutureCallback <Course > {
99
-
100
113
List <Course > courses ;
101
- public UpdateCourse (List <Course > courses ){
114
+
115
+ public UpdateCourse (List <Course > courses ) {
102
116
this .courses = courses ;
103
117
}
104
-
118
+
105
119
@ Override
106
120
public void onSuccess (Course detailedCourse ) {
107
121
detailedCourse .setExercisesLoaded (true );
@@ -123,6 +137,9 @@ public void onFailure(Throwable ex) {
123
137
}
124
138
}
125
139
140
+ /**
141
+ * Updates the courseDb after all course-jsons are downloaded.
142
+ */
126
143
private class DefaultListener implements FutureCallback <List <Course >> {
127
144
128
145
private final boolean showDialogOnError ;
@@ -132,7 +149,7 @@ public DefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
132
149
this .showDialogOnError = showDialogOnError ;
133
150
this .updateCourseDb = updateCourseDb ;
134
151
}
135
-
152
+
136
153
@ Override
137
154
public void onSuccess (List <Course > result ) {
138
155
if (updateCourseDb ) {
0 commit comments