6
6
import android .view .ViewGroup ;
7
7
import android .widget .AdapterView ;
8
8
import android .widget .AdapterView .OnItemSelectedListener ;
9
+ import android .widget .ImageView ;
10
+ import android .widget .Spinner ;
9
11
import androidx .annotation .NonNull ;
10
12
import androidx .annotation .Nullable ;
11
13
import androidx .appcompat .widget .AppCompatEditText ;
12
14
import androidx .appcompat .widget .AppCompatSpinner ;
13
15
import androidx .recyclerview .widget .RecyclerView ;
14
16
import butterknife .BindView ;
15
17
import butterknife .ButterKnife ;
18
+ import com .google .android .material .textfield .TextInputEditText ;
16
19
import com .google .android .material .textfield .TextInputLayout ;
17
20
import fr .free .nrw .commons .R ;
18
21
import fr .free .nrw .commons .utils .AbstractTextWatcher ;
@@ -72,24 +75,38 @@ public void addDescription(UploadMediaDetail uploadMediaDetail) {
72
75
notifyItemInserted (uploadMediaDetails .size ());
73
76
}
74
77
78
+ /**
79
+ * Remove description based on position from the list and notifies the RecyclerView Adapter that
80
+ * data in adapter has been removed at that particular position.
81
+ * @param uploadMediaDetail
82
+ * @param position
83
+ */
84
+ public void removeDescription (final UploadMediaDetail uploadMediaDetail , final int position ) {
85
+ this .uploadMediaDetails .remove (uploadMediaDetail );
86
+ notifyItemRemoved (position );
87
+ }
88
+
75
89
public class ViewHolder extends RecyclerView .ViewHolder {
76
90
77
91
@ Nullable
78
92
@ BindView (R .id .spinner_description_languages )
79
- AppCompatSpinner spinnerDescriptionLanguages ;
93
+ Spinner spinnerDescriptionLanguages ;
80
94
81
95
@ BindView (R .id .description_item_edit_text )
82
- AppCompatEditText descItemEditText ;
96
+ TextInputEditText descItemEditText ;
83
97
84
98
@ BindView (R .id .description_item_edit_text_input_layout )
85
99
TextInputLayout descInputLayout ;
86
100
87
101
@ BindView (R .id .caption_item_edit_text )
88
- AppCompatEditText captionItemEditText ;
102
+ TextInputEditText captionItemEditText ;
89
103
90
104
@ BindView (R .id .caption_item_edit_text_input_layout )
91
105
TextInputLayout captionInputLayout ;
92
106
107
+ @ BindView (R .id .btn_remove )
108
+ ImageView removeButton ;
109
+
93
110
public ViewHolder (View itemView ) {
94
111
super (itemView );
95
112
ButterKnife .bind (this , itemView );
@@ -110,6 +127,7 @@ public void bind(int position) {
110
127
descItemEditText .setText (uploadMediaDetail .getDescriptionText ());
111
128
112
129
if (position == 0 ) {
130
+ removeButton .setVisibility (View .GONE );
113
131
captionInputLayout .setEndIconMode (TextInputLayout .END_ICON_CUSTOM );
114
132
captionInputLayout .setEndIconDrawable (R .drawable .mapbox_info_icon_default );
115
133
captionInputLayout .setEndIconOnClickListener (v ->
@@ -121,10 +139,13 @@ public void bind(int position) {
121
139
callback .showAlert (R .string .media_detail_description , R .string .description_info ));
122
140
123
141
} else {
142
+ removeButton .setVisibility (View .VISIBLE );
124
143
captionInputLayout .setEndIconDrawable (null );
125
144
descInputLayout .setEndIconDrawable (null );
126
145
}
127
146
147
+ removeButton .setOnClickListener (v -> removeDescription (uploadMediaDetail , position ));
148
+
128
149
captionItemEditText .addTextChangedListener (new AbstractTextWatcher (
129
150
captionText -> uploadMediaDetails .get (position ).setCaptionText (captionText )));
130
151
initLanguageSpinner (position , uploadMediaDetail );
@@ -175,45 +196,36 @@ public void onNothingSelected(AdapterView<?> adapterView) {
175
196
});
176
197
177
198
178
- if (description .getCaptionText ().isEmpty () == false
179
- && languagesAdapter .getIndexOfLanguageCode (description .getLanguageCode ()) != -1 ){
180
- // If the user selects a nearby pin or location bookmark to upload a picture and language is present in spinner we set the language.
181
- spinnerDescriptionLanguages .setSelection (languagesAdapter .getIndexOfLanguageCode (description .getLanguageCode ()));
182
- }
183
- else {
184
- // This is a contribution upload or the language from description is not present in spinner.
185
- if (description .getSelectedLanguageIndex () == -1 ) {
186
- if (!TextUtils .isEmpty (savedLanguageValue )) {
187
- // If user has chosen a default language from settings activity savedLanguageValue is not null
188
- spinnerDescriptionLanguages
189
- .setSelection (
190
- languagesAdapter .getIndexOfLanguageCode (savedLanguageValue ));
199
+ if (description .getSelectedLanguageIndex () == -1 ) {
200
+ if (!TextUtils .isEmpty (savedLanguageValue )) {
201
+ // If user has chosen a default language from settings activity
202
+ // savedLanguageValue is not null
203
+ spinnerDescriptionLanguages .setSelection (languagesAdapter
204
+ .getIndexOfLanguageCode (savedLanguageValue ));
205
+ } else {
206
+ //Checking whether Language Code attribute is null or not.
207
+ if (uploadMediaDetails .get (position ).getLanguageCode () != null ) {
208
+ //If it is not null that means it is fetching details from the previous
209
+ // upload (i.e. when user has pressed copy previous caption & description)
210
+ //hence providing same language code for the current upload.
211
+ spinnerDescriptionLanguages .setSelection (languagesAdapter
212
+ .getIndexOfLanguageCode (uploadMediaDetails .get (position )
213
+ .getLanguageCode ()), true );
191
214
} else {
192
- //Checking whether Language Code attribute is null or not.
193
- if (uploadMediaDetails .get (position ).getLanguageCode () != null ) {
194
- //If it is not null that means it is fetching details from the previous upload (i.e. when user has pressed copy previous caption & description)
195
- //hence providing same language code for the current upload.
196
- spinnerDescriptionLanguages .setSelection (languagesAdapter
197
- .getIndexOfLanguageCode (
198
- uploadMediaDetails .get (position ).getLanguageCode ()), true );
215
+ if (position == 0 ) {
216
+ final int defaultLocaleIndex = languagesAdapter
217
+ .getIndexOfUserDefaultLocale (spinnerDescriptionLanguages
218
+ .getContext ());
219
+ spinnerDescriptionLanguages .setSelection (defaultLocaleIndex , true );
199
220
} else {
200
- if (position == 0 ) {
201
- int defaultLocaleIndex = languagesAdapter
202
- .getIndexOfUserDefaultLocale (
203
- spinnerDescriptionLanguages .getContext ());
204
- spinnerDescriptionLanguages .setSelection (defaultLocaleIndex , true );
205
- } else {
206
- spinnerDescriptionLanguages .setSelection (0 , true );
207
- }
221
+ spinnerDescriptionLanguages .setSelection (0 , true );
208
222
}
209
223
}
210
-
211
- } else {
212
- spinnerDescriptionLanguages
213
- .setSelection (description .getSelectedLanguageIndex ());
214
- selectedLanguages
215
- .put (spinnerDescriptionLanguages , description .getLanguageCode ());
216
224
}
225
+
226
+ } else {
227
+ spinnerDescriptionLanguages .setSelection (description .getSelectedLanguageIndex ());
228
+ selectedLanguages .put (spinnerDescriptionLanguages , description .getLanguageCode ());
217
229
}
218
230
}
219
231
}
0 commit comments