|
26 | 26 | import com.vaadin.flow.function.SerializableBiConsumer;
|
27 | 27 | import com.vaadin.flow.function.SerializableConsumer;
|
28 | 28 | import com.vaadin.flow.function.SerializableRunnable;
|
| 29 | +import com.vaadin.flow.function.SerializableTriConsumer; |
29 | 30 | import com.vaadin.flow.server.Command;
|
30 | 31 | import com.vaadin.flow.shared.Registration;
|
31 | 32 |
|
@@ -112,7 +113,96 @@ public void onStart(TransferContext context) {
|
112 | 113 | }
|
113 | 114 |
|
114 | 115 | /**
|
115 |
| - * Adds a listener to be notified of transfer progress. |
| 116 | + * Adds a listener to be notified when the transfer starts that receives the |
| 117 | + * transfer context as input. |
| 118 | + * <p> |
| 119 | + * The call of the given callback is wrapped by the |
| 120 | + * {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes |
| 121 | + * defined here when the download or upload request is being handled. This |
| 122 | + * needs {@link com.vaadin.flow.component.page.Push} to be enabled in the |
| 123 | + * application to properly send the UI changes to client. |
| 124 | + * |
| 125 | + * @param startHandler |
| 126 | + * the handler to be called when the transfer starts |
| 127 | + * @return this instance for method chaining |
| 128 | + */ |
| 129 | + public R whenStart(SerializableConsumer<TransferContext> startHandler) { |
| 130 | + Objects.requireNonNull(startHandler, "Start handler cannot be null"); |
| 131 | + addTransferProgressListenerInternal(new TransferProgressListener() { |
| 132 | + @Override |
| 133 | + public void onStart(TransferContext context) { |
| 134 | + context.getUI().access(() -> { |
| 135 | + startHandler.accept(context); |
| 136 | + }); |
| 137 | + } |
| 138 | + }); |
| 139 | + return (R) this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Adds a listener to be notified of transfer progress with giving the |
| 144 | + * transfer context object and with the given interval. |
| 145 | + * <p> |
| 146 | + * The call of the given callback is wrapped by the |
| 147 | + * {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes |
| 148 | + * defined here when the download or upload request is being handled. This |
| 149 | + * needs {@link com.vaadin.flow.component.page.Push} to be enabled in the |
| 150 | + * application to properly send the UI changes to client. |
| 151 | + * |
| 152 | + * @param progressHandler |
| 153 | + * the handler to be called with the transfer context, current |
| 154 | + * and total bytes |
| 155 | + * @param progressIntervalInBytes |
| 156 | + * the interval in bytes for reporting progress |
| 157 | + * @return this instance for method chaining |
| 158 | + */ |
| 159 | + public R onProgress( |
| 160 | + SerializableTriConsumer<TransferContext, Long, Long> progressHandler, |
| 161 | + long progressIntervalInBytes) { |
| 162 | + Objects.requireNonNull(progressHandler, |
| 163 | + "Progress handler cannot be null"); |
| 164 | + addTransferProgressListenerInternal(new TransferProgressListener() { |
| 165 | + @Override |
| 166 | + public void onProgress(TransferContext context, |
| 167 | + long transferredBytes, long totalBytes) { |
| 168 | + context.getUI().access(() -> { |
| 169 | + progressHandler.accept(context, transferredBytes, |
| 170 | + totalBytes); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public long progressReportInterval() { |
| 176 | + return progressIntervalInBytes; |
| 177 | + } |
| 178 | + }); |
| 179 | + return (R) this; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Adds a listener to be notified of transfer progress with giving the |
| 184 | + * transfer context object and with the default progress interval. |
| 185 | + * <p> |
| 186 | + * The call of the given callback is wrapped by the |
| 187 | + * {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes |
| 188 | + * defined here when the download or upload request is being handled. This |
| 189 | + * needs {@link com.vaadin.flow.component.page.Push} to be enabled in the |
| 190 | + * application to properly send the UI changes to client. |
| 191 | + * |
| 192 | + * @param progressHandler |
| 193 | + * the handler to be called with the transfer context, current |
| 194 | + * and total bytes |
| 195 | + * @return this instance for method chaining |
| 196 | + */ |
| 197 | + public R onProgress( |
| 198 | + SerializableTriConsumer<TransferContext, Long, Long> progressHandler) { |
| 199 | + return onProgress(progressHandler, |
| 200 | + TransferProgressListener.DEFAULT_PROGRESS_REPORT_INTERVAL_IN_BYTES); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Adds a listener to be notified of transfer progress with the given |
| 205 | + * interval. |
116 | 206 | * <p>
|
117 | 207 | * The call of the given callback is wrapped by the
|
118 | 208 | * {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
|
@@ -174,8 +264,10 @@ public R onProgress(SerializableBiConsumer<Long, Long> progressHandler) {
|
174 | 264 |
|
175 | 265 | /**
|
176 | 266 | * Adds a listener to be notified when the transfer is completed
|
177 |
| - * successfully or with an error. Gives a <code>Boolean</code> indicating |
178 |
| - * whether the transfer was completed successfully (true) or not (false). |
| 267 | + * successfully or with an error. |
| 268 | + * <p> |
| 269 | + * Gives a <code>Boolean</code> indicating whether the transfer was |
| 270 | + * completed successfully (true) or not (false). |
179 | 271 | * <p>
|
180 | 272 | * The call of the given callback is wrapped by the
|
181 | 273 | * {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
|
@@ -210,6 +302,48 @@ public void onComplete(TransferContext context,
|
210 | 302 | return (R) this;
|
211 | 303 | }
|
212 | 304 |
|
| 305 | + /** |
| 306 | + * Adds a listener to be notified when the transfer is completed |
| 307 | + * successfully or with an error with the trasfer context object given as an |
| 308 | + * input. |
| 309 | + * <p> |
| 310 | + * Gives a <code>Boolean</code> indicating whether the transfer was |
| 311 | + * completed successfully (true) or not (false) and transfer context to |
| 312 | + * obtain more meta-data. |
| 313 | + * <p> |
| 314 | + * The call of the given callback is wrapped by the |
| 315 | + * {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes |
| 316 | + * defined here when the download or upload request is being handled. This |
| 317 | + * needs {@link com.vaadin.flow.component.page.Push} to be enabled in the |
| 318 | + * application to properly send the UI changes to client. |
| 319 | + * |
| 320 | + * @param completeOrTerminateHandler |
| 321 | + * the handler to be called when the transfer is completed |
| 322 | + * @return this instance for method chaining |
| 323 | + */ |
| 324 | + public R whenComplete( |
| 325 | + SerializableBiConsumer<TransferContext, Boolean> completeOrTerminateHandler) { |
| 326 | + Objects.requireNonNull(completeOrTerminateHandler, |
| 327 | + "Complete or terminate handler cannot be null"); |
| 328 | + addTransferProgressListenerInternal(new TransferProgressListener() { |
| 329 | + @Override |
| 330 | + public void onError(TransferContext context, IOException reason) { |
| 331 | + context.getUI().access(() -> { |
| 332 | + completeOrTerminateHandler.accept(context, false); |
| 333 | + }); |
| 334 | + } |
| 335 | + |
| 336 | + @Override |
| 337 | + public void onComplete(TransferContext context, |
| 338 | + long transferredBytes) { |
| 339 | + context.getUI().access(() -> { |
| 340 | + completeOrTerminateHandler.accept(context, true); |
| 341 | + }); |
| 342 | + } |
| 343 | + }); |
| 344 | + return (R) this; |
| 345 | + } |
| 346 | + |
213 | 347 | /**
|
214 | 348 | * Get the listeners that are registered to this handler.
|
215 | 349 | * <p>
|
|
0 commit comments