1
1
package fr .free .nrw .commons .contributions ;
2
2
3
- import android .content . Context ;
3
+ import android .net . Uri ;
4
4
import android .view .View ;
5
5
import android .widget .LinearLayout ;
6
6
import android .widget .ProgressBar ;
7
7
import android .widget .TextView ;
8
-
9
8
import androidx .collection .LruCache ;
10
-
11
- import com .facebook .drawee .view .SimpleDraweeView ;
12
-
13
- import org .apache .commons .lang3 .StringUtils ;
14
-
15
- import javax .inject .Inject ;
16
- import javax .inject .Named ;
17
-
9
+ import androidx .recyclerview .widget .RecyclerView ;
18
10
import butterknife .BindView ;
19
11
import butterknife .ButterKnife ;
20
12
import butterknife .OnClick ;
13
+ import com .facebook .drawee .view .SimpleDraweeView ;
21
14
import fr .free .nrw .commons .MediaDataExtractor ;
22
15
import fr .free .nrw .commons .R ;
23
- import fr .free .nrw .commons .ViewHolder ;
16
+ import fr .free .nrw .commons .contributions . ContributionsListAdapter . Callback ;
24
17
import fr .free .nrw .commons .contributions .model .DisplayableContribution ;
25
18
import fr .free .nrw .commons .di .ApplicationlessInjection ;
19
+ import fr .free .nrw .commons .upload .FileUtils ;
26
20
import io .reactivex .android .schedulers .AndroidSchedulers ;
27
21
import io .reactivex .disposables .CompositeDisposable ;
28
22
import io .reactivex .disposables .Disposable ;
29
23
import io .reactivex .schedulers .Schedulers ;
24
+ import javax .inject .Inject ;
25
+ import javax .inject .Named ;
26
+ import org .apache .commons .lang3 .StringUtils ;
30
27
import timber .log .Timber ;
31
28
32
- public class ContributionViewHolder implements ViewHolder <DisplayableContribution > {
29
+ public class ContributionViewHolder extends RecyclerView .ViewHolder {
30
+
31
+ private final Callback callback ;
33
32
@ BindView (R .id .contributionImage )
34
33
SimpleDraweeView imageView ;
35
34
@ BindView (R .id .contributionTitle ) TextView titleView ;
@@ -47,15 +46,18 @@ public class ContributionViewHolder implements ViewHolder<DisplayableContributio
47
46
48
47
private DisplayableContribution contribution ;
49
48
private CompositeDisposable compositeDisposable = new CompositeDisposable ();
49
+ private int position ;
50
50
51
- ContributionViewHolder (View parent ) {
51
+ ContributionViewHolder (View parent , Callback callback ) {
52
+ super (parent );
52
53
ButterKnife .bind (this , parent );
54
+ this .callback =callback ;
53
55
}
54
56
55
- @ Override
56
- public void bindModel (Context context , DisplayableContribution contribution ) {
57
- ApplicationlessInjection .getInstance (context )
57
+ public void init (int position , DisplayableContribution contribution ) {
58
+ ApplicationlessInjection .getInstance (itemView .getContext ())
58
59
.getCommonsApplicationComponent ().inject (this );
60
+ this .position =position ;
59
61
this .contribution = contribution ;
60
62
fetchAndDisplayThumbnail (contribution );
61
63
titleView .setText (contribution .getDisplayTitle ());
@@ -104,19 +106,39 @@ public void bindModel(Context context, DisplayableContribution contribution) {
104
106
* @param contribution
105
107
*/
106
108
private void fetchAndDisplayThumbnail (DisplayableContribution contribution ) {
107
- if (!StringUtils .isBlank (thumbnailCache .get (contribution .getFilename ()))) {
108
- imageView .setImageURI (thumbnailCache .get (contribution .getFilename ()));
109
+ String keyForLRUCache = getKeyForLRUCache (contribution .getContentUri ());
110
+ String cacheUrl = thumbnailCache .get (keyForLRUCache );
111
+ if (!StringUtils .isBlank (cacheUrl )) {
112
+ imageView .setImageURI (cacheUrl );
109
113
return ;
110
114
}
111
- Timber .d ("Fetching thumbnail for %s" , contribution .getFilename ());
112
- Disposable disposable = mediaDataExtractor .getMediaFromFileName (contribution .getFilename ())
113
- .subscribeOn (Schedulers .io ())
114
- .observeOn (AndroidSchedulers .mainThread ())
115
- .subscribe (media -> {
116
- thumbnailCache .put (contribution .getFilename (), media .getThumbUrl ());
117
- imageView .setImageURI (media .getThumbUrl ());
118
- });
119
- compositeDisposable .add (disposable );
115
+
116
+ imageView .setBackground (null );
117
+ if ((contribution .getState () != Contribution .STATE_COMPLETED ) && FileUtils .fileExists (
118
+ contribution .getLocalUri ())) {
119
+ imageView .setImageURI (contribution .getLocalUri ());
120
+ } else {
121
+ Timber .d ("Fetching thumbnail for %s" , contribution .getFilename ());
122
+ Disposable disposable = mediaDataExtractor
123
+ .getMediaFromFileName (contribution .getFilename ())
124
+ .subscribeOn (Schedulers .io ())
125
+ .observeOn (AndroidSchedulers .mainThread ())
126
+ .subscribe (media -> {
127
+ thumbnailCache .put (keyForLRUCache , media .getThumbUrl ());
128
+ imageView .setImageURI (media .getThumbUrl ());
129
+ });
130
+ compositeDisposable .add (disposable );
131
+ }
132
+
133
+ }
134
+
135
+ /**
136
+ * Returns image key for the LRU cache, basically the id of the image, (the content uri is the ""+/id)
137
+ * @param contentUri
138
+ * @return
139
+ */
140
+ private String getKeyForLRUCache (Uri contentUri ) {
141
+ return contentUri .getLastPathSegment ();
120
142
}
121
143
122
144
public void clear () {
@@ -128,28 +150,19 @@ public void clear() {
128
150
*/
129
151
@ OnClick (R .id .retryButton )
130
152
public void retryUpload () {
131
- DisplayableContribution .ContributionActions actions = contribution .getContributionActions ();
132
- if (actions != null ) {
133
- actions .retryUpload ();
134
- }
153
+ callback .retryUpload (contribution );
135
154
}
136
155
137
156
/**
138
157
* Delete a failed upload attempt
139
158
*/
140
159
@ OnClick (R .id .cancelButton )
141
160
public void deleteUpload () {
142
- DisplayableContribution .ContributionActions actions = contribution .getContributionActions ();
143
- if (actions != null ) {
144
- actions .deleteUpload ();
145
- }
161
+ callback .deleteUpload (contribution );
146
162
}
147
163
148
164
@ OnClick (R .id .contributionImage )
149
165
public void imageClicked (){
150
- DisplayableContribution .ContributionActions actions = contribution .getContributionActions ();
151
- if (actions != null ) {
152
- actions .onClick ();
153
- }
166
+ callback .openMediaDetail (position );
154
167
}
155
168
}
0 commit comments