1package com.bumptech.glide.request;
2
3/**
4 * A coordinator that coordinates two individual {@link Request}s that load a small thumbnail version of an image and
5 * the full size version of the image at the same time.
6 */
7public class ThumbnailRequestCoordinator implements RequestCoordinator, Request {
8    private Request full;
9    private Request thumb;
10    private RequestCoordinator coordinator;
11
12    public ThumbnailRequestCoordinator() {
13        this(null);
14    }
15
16    public ThumbnailRequestCoordinator(RequestCoordinator coordinator) {
17        this.coordinator = coordinator;
18    }
19
20    public void setRequests(Request full, Request thumb) {
21        this.full = full;
22        this.thumb = thumb;
23    }
24
25    @Override
26    public boolean canSetImage(Request request) {
27        return parentCanSetImage() && (request == full || !full.isComplete());
28    }
29
30    private boolean parentCanSetImage() {
31        return coordinator == null || coordinator.canSetImage(this);
32    }
33
34    @Override
35    public boolean canSetPlaceholder(Request request) {
36        return parentCanSetPlaceholder() && (request == full && !isAnyRequestComplete());
37    }
38
39    private boolean parentCanSetPlaceholder() {
40        return coordinator == null || coordinator.canSetPlaceholder(this);
41    }
42
43    @Override
44    public boolean isAnyRequestComplete() {
45        return parentIsAnyRequestComplete() || full.isComplete() || thumb.isComplete();
46    }
47
48    private boolean parentIsAnyRequestComplete() {
49        return coordinator != null && coordinator.isAnyRequestComplete();
50    }
51
52    @Override
53    public void run() {
54        if (!thumb.isRunning()) {
55            thumb.run();
56        }
57        if (!full.isRunning()) {
58            full.run();
59        }
60    }
61
62    @Override
63    public void clear() {
64        full.clear();
65        thumb.clear();
66    }
67
68    @Override
69    public boolean isRunning() {
70        return full.isRunning();
71    }
72
73    @Override
74    public boolean isComplete() {
75        // TODO: this is a little strange, but we often want to avoid restarting the request or
76        // setting placeholders even if only the thumb is complete.
77        return full.isComplete() || thumb.isComplete();
78    }
79
80    @Override
81    public boolean isFailed() {
82        return full.isFailed();
83    }
84
85    @Override
86    public void recycle() {
87        full.recycle();
88        thumb.recycle();
89    }
90}
91