@@ -2,8 +2,8 @@ package contributors
2
2
3
3
import contributors.Contributors.LoadingStatus.*
4
4
import contributors.Variant.*
5
+ import io.reactivex.disposables.Disposable
5
6
import kotlinx.coroutines.*
6
- import kotlinx.coroutines.swing.Swing
7
7
import tasks.*
8
8
import java.awt.event.ActionListener
9
9
import javax.swing.SwingUtilities
@@ -111,18 +111,30 @@ interface Contributors: CoroutineScope {
111
111
}.setUpCancellation()
112
112
}
113
113
RX -> { // Using RxJava
114
- loadContributorsReactive(service, req) { users, completed ->
115
- SwingUtilities .invokeLater {
116
- updateResults(users, startTime, completed)
117
- }
118
- }
114
+ loadContributorsReactive(service, req)
115
+ .subscribe { users ->
116
+ SwingUtilities .invokeLater {
117
+ updateResults(users, startTime)
118
+ }
119
+ }.setUpCancellation()
119
120
}
120
- RX_PROGRESS -> { // Using RxJava and showing progress
121
- loadContributorsReactiveProgress(service, req) { users, completed ->
122
- SwingUtilities .invokeLater {
123
- updateResults(users, startTime, completed)
124
- }
125
- }
121
+ RX_PROGRESS -> { // Using RxJava and showing progress { users, completed ->
122
+ loadContributorsReactiveProgress(service, req)
123
+ .subscribe({
124
+ SwingUtilities .invokeLater {
125
+ updateResults(it, startTime, false )
126
+ }
127
+ }, {
128
+ SwingUtilities .invokeLater {
129
+ setLoadingStatus(" error ${it.message} " , false )
130
+ setActionsStatus(newLoadingEnabled = true )
131
+ }
132
+ }, {
133
+ SwingUtilities .invokeLater {
134
+ updateLoadingStatus(COMPLETED , startTime)
135
+ setActionsStatus(newLoadingEnabled = true )
136
+ }
137
+ }).setUpCancellation()
126
138
}
127
139
}
128
140
}
@@ -186,6 +198,22 @@ interface Contributors: CoroutineScope {
186
198
}
187
199
}
188
200
201
+ private fun Disposable.setUpCancellation () {
202
+ // make active the 'cancel' button
203
+ setActionsStatus(newLoadingEnabled = false , cancellationEnabled = true )
204
+
205
+ val loadingDisposable = this
206
+
207
+ // cancel the loading job if the 'cancel' button was clicked
208
+ var listener: ActionListener
209
+ listener = ActionListener {
210
+ loadingDisposable.dispose()
211
+ updateLoadingStatus(CANCELED )
212
+ setActionsStatus(newLoadingEnabled = true )
213
+ }
214
+ addCancelListener(listener)
215
+ }
216
+
189
217
fun loadInitialParams () {
190
218
setParams(loadStoredParams())
191
219
}
0 commit comments