1
1
package fr .free .nrw .commons .contributions ;
2
2
3
+ import android .graphics .Color ;
4
+ import android .graphics .drawable .ColorDrawable ;
5
+ import android .net .Uri ;
6
+ import android .text .TextUtils ;
3
7
import android .view .View ;
4
8
import android .widget .LinearLayout ;
5
9
import android .widget .ProgressBar ;
6
10
import android .widget .TextView ;
7
11
8
- import androidx .collection . LruCache ;
12
+ import androidx .annotation . Nullable ;
9
13
import androidx .recyclerview .widget .RecyclerView ;
10
14
11
15
import com .facebook .drawee .view .SimpleDraweeView ;
12
16
13
- import org .apache .commons .lang3 .StringUtils ;
17
+ import com .facebook .imagepipeline .request .ImageRequest ;
18
+ import com .facebook .imagepipeline .request .ImageRequestBuilder ;
14
19
15
- import javax .inject .Inject ;
16
- import javax .inject .Named ;
17
20
18
21
import butterknife .BindView ;
19
22
import butterknife .ButterKnife ;
20
23
import butterknife .OnClick ;
21
- import fr .free .nrw .commons .MediaDataExtractor ;
22
24
import fr .free .nrw .commons .R ;
23
25
import fr .free .nrw .commons .contributions .ContributionsListAdapter .Callback ;
24
- import fr .free .nrw .commons .contributions .model .DisplayableContribution ;
25
- import fr .free .nrw .commons .di .ApplicationlessInjection ;
26
- import fr .free .nrw .commons .upload .FileUtils ;
27
- import io .reactivex .android .schedulers .AndroidSchedulers ;
28
- import io .reactivex .disposables .CompositeDisposable ;
29
- import io .reactivex .disposables .Disposable ;
30
- import io .reactivex .schedulers .Schedulers ;
31
- import timber .log .Timber ;
26
+ import java .util .HashMap ;
27
+ import java .util .Random ;
32
28
33
29
public class ContributionViewHolder extends RecyclerView .ViewHolder {
34
30
@@ -41,32 +37,32 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
41
37
@ BindView (R .id .contributionProgress ) ProgressBar progressView ;
42
38
@ BindView (R .id .failed_image_options ) LinearLayout failedImageOptions ;
43
39
44
- @ Inject
45
- MediaDataExtractor mediaDataExtractor ;
46
-
47
- @ Inject
48
- @ Named ("thumbnail-cache" )
49
- LruCache <String , String > thumbnailCache ;
50
-
51
- private DisplayableContribution contribution ;
52
- private CompositeDisposable compositeDisposable = new CompositeDisposable ();
53
40
private int position ;
41
+ private Contribution contribution ;
42
+ private Random random = new Random ();
54
43
55
44
ContributionViewHolder (View parent , Callback callback ) {
56
45
super (parent );
57
46
ButterKnife .bind (this , parent );
58
47
this .callback =callback ;
59
48
}
60
49
61
- public void init (int position , DisplayableContribution contribution ) {
62
- ApplicationlessInjection .getInstance (itemView .getContext ())
63
- .getCommonsApplicationComponent ().inject (this );
64
- this .position =position ;
50
+ public void init (int position , Contribution contribution ) {
65
51
this .contribution = contribution ;
66
- fetchAndDisplayThumbnail (contribution );
52
+ this .position = position ;
53
+ imageView .getHierarchy ().setPlaceholderImage (new ColorDrawable (
54
+ Color .argb (100 , random .nextInt (256 ), random .nextInt (256 ), random .nextInt (256 ))));
55
+ String imageSource = chooseImageSource (contribution .thumbUrl , contribution .getLocalUri ());
56
+ if (!TextUtils .isEmpty (imageSource )) {
57
+ final ImageRequest imageRequest =
58
+ ImageRequestBuilder .newBuilderWithSource (Uri .parse (imageSource ))
59
+ .setProgressiveRenderingEnabled (true )
60
+ .build ();
61
+ imageView .setImageRequest (imageRequest );
62
+ }
67
63
titleView .setText (contribution .getDisplayTitle ());
68
64
69
- seqNumView .setText (String .valueOf (contribution . getPosition () + 1 ));
65
+ seqNumView .setText (String .valueOf (position + 1 ));
70
66
seqNumView .setVisibility (View .VISIBLE );
71
67
72
68
switch (contribution .getState ()) {
@@ -104,40 +100,18 @@ public void init(int position, DisplayableContribution contribution) {
104
100
}
105
101
106
102
/**
107
- * This method fetches the thumbnail url from file name
108
- * If the thumbnail url is present in cache, then it is used otherwise API call is made to fetch the thumbnail
109
- * This can be removed once #2904 is in place and contribution contains all metadata beforehand
110
- * @param contribution
103
+ * Returns the image source for the image view, first preference is given to thumbUrl if that is
104
+ * null, moves to local uri and if both are null return null
105
+ *
106
+ * @param thumbUrl
107
+ * @param localUri
108
+ * @return
111
109
*/
112
- private void fetchAndDisplayThumbnail (DisplayableContribution contribution ) {
113
- String keyForLRUCache = contribution .getFilename ();
114
- String cacheUrl = thumbnailCache .get (keyForLRUCache );
115
- if (!StringUtils .isBlank (cacheUrl )) {
116
- imageView .setImageURI (cacheUrl );
117
- return ;
118
- }
119
-
120
- imageView .setBackground (null );
121
- if ((contribution .getState () != Contribution .STATE_COMPLETED ) && FileUtils .fileExists (
122
- contribution .getLocalUri ())) {
123
- imageView .setImageURI (contribution .getLocalUri ());
124
- } else {
125
- Timber .d ("Fetching thumbnail for %s" , contribution .getFilename ());
126
- Disposable disposable = mediaDataExtractor
127
- .getMediaFromFileName (contribution .getFilename ())
128
- .subscribeOn (Schedulers .io ())
129
- .observeOn (AndroidSchedulers .mainThread ())
130
- .subscribe (media -> {
131
- thumbnailCache .put (keyForLRUCache , media .getThumbUrl ());
132
- imageView .setImageURI (media .getThumbUrl ());
133
- });
134
- compositeDisposable .add (disposable );
135
- }
136
-
137
- }
138
-
139
- public void clear () {
140
- compositeDisposable .clear ();
110
+ @ Nullable
111
+ private String chooseImageSource (String thumbUrl , Uri localUri ) {
112
+ return !TextUtils .isEmpty (thumbUrl ) ? thumbUrl :
113
+ localUri != null ? localUri .toString () :
114
+ null ;
141
115
}
142
116
143
117
/**
0 commit comments