Skip to content

Commit d56e08c

Browse files
committed
Allow modifying RequestBuilders after calling into().
Fixes an issue where setting options on the generated GlideRequest after having used it to load one image would throw an exception because the internal RequestOptions object was locked when the first load was started. Now we apply autoClone() to make sure that the options object for the first load isn’t modified by subsequent changes to options, but also allowing those changes to occur.
1 parent c3479c4 commit d56e08c

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

library/src/main/java/com/bumptech/glide/RequestBuilder.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,18 @@ public RequestBuilder<TranscodeType> clone() {
349349
* @see RequestManager#clear(Target)
350350
*/
351351
public <Y extends Target<TranscodeType>> Y into(@NonNull Y target) {
352+
return into(target, getMutableOptions());
353+
}
354+
355+
private <Y extends Target<TranscodeType>> Y into(@NonNull Y target, RequestOptions options) {
352356
Util.assertMainThread();
353357
Preconditions.checkNotNull(target);
354358
if (!isModelSet) {
355359
throw new IllegalArgumentException("You must call #load() before calling #into()");
356360
}
357361

358-
requestOptions.lock();
359-
Request request = buildRequest(target);
362+
options = options.autoClone();
363+
Request request = buildRequest(target, options);
360364

361365
Request previous = target.getRequest();
362366
if (request.isEquivalentTo(previous)) {
@@ -378,6 +382,7 @@ public <Y extends Target<TranscodeType>> Y into(@NonNull Y target) {
378382
return target;
379383
}
380384

385+
381386
/**
382387
* Sets the {@link ImageView} the resource will be loaded into, cancels any existing loads into
383388
* the view, and frees any resources Glide may have previously loaded into the view so they may be
@@ -393,26 +398,27 @@ public Target<TranscodeType> into(ImageView view) {
393398
Util.assertMainThread();
394399
Preconditions.checkNotNull(view);
395400

401+
RequestOptions requestOptions = this.requestOptions;
396402
if (!requestOptions.isTransformationSet()
397403
&& requestOptions.isTransformationAllowed()
398404
&& view.getScaleType() != null) {
399-
if (requestOptions.isLocked()) {
400-
requestOptions = requestOptions.clone();
401-
}
405+
// Clone in this method so that if we use this RequestBuilder to load into a View and then
406+
// into a different target, we don't retain the transformation applied based on the previous
407+
// View's scale type.
402408
switch (view.getScaleType()) {
403409
case CENTER_CROP:
404-
requestOptions.optionalCenterCrop();
410+
requestOptions.clone().optionalCenterCrop();
405411
break;
406412
case CENTER_INSIDE:
407-
requestOptions.optionalCenterInside();
413+
requestOptions.clone().optionalCenterInside();
408414
break;
409415
case FIT_CENTER:
410416
case FIT_START:
411417
case FIT_END:
412-
requestOptions.optionalFitCenter();
418+
requestOptions.clone().optionalFitCenter();
413419
break;
414420
case FIT_XY:
415-
requestOptions.optionalCenterInside();
421+
requestOptions.clone().optionalCenterInside();
416422
break;
417423
case CENTER:
418424
case MATRIX:
@@ -421,7 +427,7 @@ public Target<TranscodeType> into(ImageView view) {
421427
}
422428
}
423429

424-
return into(context.buildImageViewTarget(view, transcodeClass));
430+
return into(context.buildImageViewTarget(view, transcodeClass), requestOptions);
425431
}
426432

427433
/**
@@ -578,15 +584,15 @@ private Priority getThumbnailPriority(Priority current) {
578584
}
579585
}
580586

581-
private Request buildRequest(Target<TranscodeType> target) {
587+
private Request buildRequest(Target<TranscodeType> target, RequestOptions requestOptions) {
582588
return buildRequestRecursive(target, null, transitionOptions, requestOptions.getPriority(),
583-
requestOptions.getOverrideWidth(), requestOptions.getOverrideHeight());
589+
requestOptions.getOverrideWidth(), requestOptions.getOverrideHeight(), requestOptions);
584590
}
585591

586592
private Request buildRequestRecursive(Target<TranscodeType> target,
587593
@Nullable ThumbnailRequestCoordinator parentCoordinator,
588594
TransitionOptions<?, ? super TranscodeType> transitionOptions,
589-
Priority priority, int overrideWidth, int overrideHeight) {
595+
Priority priority, int overrideWidth, int overrideHeight, RequestOptions requestOptions) {
590596
if (thumbnailBuilder != null) {
591597
// Recursive case: contains a potentially recursive thumbnail request builder.
592598
if (isThumbnailBuilt) {
@@ -619,8 +625,15 @@ private Request buildRequestRecursive(Target<TranscodeType> target,
619625
transitionOptions, priority, overrideWidth, overrideHeight);
620626
isThumbnailBuilt = true;
621627
// Recursively generate thumbnail requests.
622-
Request thumbRequest = thumbnailBuilder.buildRequestRecursive(target, coordinator,
623-
thumbTransitionOptions, thumbPriority, thumbOverrideWidth, thumbOverrideHeight);
628+
Request thumbRequest =
629+
thumbnailBuilder.buildRequestRecursive(
630+
target,
631+
coordinator,
632+
thumbTransitionOptions,
633+
thumbPriority,
634+
thumbOverrideWidth,
635+
thumbOverrideHeight,
636+
requestOptions);
624637
isThumbnailBuilt = false;
625638
coordinator.setRequests(fullRequest, thumbRequest);
626639
return coordinator;
@@ -648,8 +661,6 @@ private Request obtainRequest(Target<TranscodeType> target,
648661
RequestOptions requestOptions, RequestCoordinator requestCoordinator,
649662
TransitionOptions<?, ? super TranscodeType> transitionOptions, Priority priority,
650663
int overrideWidth, int overrideHeight) {
651-
requestOptions.lock();
652-
653664
return SingleRequest.obtain(
654665
context,
655666
model,

0 commit comments

Comments
 (0)