13
13
import fr .free .nrw .commons .upload .WikidataItem ;
14
14
import fr .free .nrw .commons .upload .WikidataPlace ;
15
15
import fr .free .nrw .commons .upload .mediaDetails .CaptionInterface ;
16
- import fr .free .nrw .commons .upload .structure .depictions .DepictedItem ;
17
16
import fr .free .nrw .commons .utils .ConfigUtils ;
18
17
import fr .free .nrw .commons .utils .ViewUtil ;
19
18
import io .reactivex .Observable ;
20
19
import io .reactivex .ObservableSource ;
21
20
import io .reactivex .android .schedulers .AndroidSchedulers ;
22
21
import io .reactivex .schedulers .Schedulers ;
23
- import java .util .HashMap ;
24
22
import java .util .Locale ;
25
23
import java .util .Map ;
26
24
import java .util .concurrent .Callable ;
29
27
import javax .inject .Singleton ;
30
28
import org .jetbrains .annotations .NotNull ;
31
29
import org .wikipedia .csrf .CsrfTokenClient ;
30
+ import org .wikipedia .dataclient .mwapi .MwPostResponse ;
32
31
import timber .log .Timber ;
33
32
34
33
/**
@@ -157,12 +156,12 @@ private void showSuccessToast(final String wikiItemName) {
157
156
*/
158
157
@ SuppressLint ("CheckResult" )
159
158
public void createCaptions (final UploadResult uploadResult , final Map <String , String > captions ) {
160
- Observable . fromCallable (() -> wikiBaseClient .getFileEntityId (uploadResult ) )
159
+ wikiBaseClient .getFileEntityId (uploadResult )
161
160
.subscribeOn (Schedulers .io ())
162
161
.subscribe (fileEntityId -> {
163
162
if (fileEntityId != null ) {
164
163
for (final Map .Entry <String , String > entry : captions .entrySet ()) {
165
- wikidataAddLabels (fileEntityId . toString () , entry .getKey (), entry .getValue ());
164
+ wikidataAddLabels (fileEntityId , entry .getKey (), entry .getValue ());
166
165
}
167
166
} else {
168
167
Timber .d ("Error acquiring EntityId for image" );
@@ -180,43 +179,38 @@ public void createCaptions(final UploadResult uploadResult, final Map<String, St
180
179
*/
181
180
182
181
@ SuppressLint ("CheckResult" )
183
- private void wikidataAddLabels (final String fileEntityId , final String languageCode , final String captionValue ) {
184
- Observable .fromCallable (() -> {
185
- try {
186
- return csrfTokenClient .getTokenBlocking ();
187
- } catch (Throwable throwable ) {
188
- throwable .printStackTrace ();
189
- return null ;
190
- }
191
- })
192
- .subscribeOn (Schedulers .io ())
193
- .observeOn (AndroidSchedulers .mainThread ())
194
- .subscribe (editToken -> {
195
- if (editToken != null ) {
196
- Observable .fromCallable (() -> captionInterface .addLabelstoWikidata (fileEntityId , editToken , languageCode , captionValue ))
197
- .subscribeOn (Schedulers .io ())
198
- .observeOn (AndroidSchedulers .mainThread ())
199
- .subscribe (revisionId ->
200
- {
201
- if (revisionId != null ) {
202
- Timber .d ("Caption successfully set, revision id = %s" , revisionId );
203
- } else {
204
- Timber .d ("Error occurred while setting Captions, fileEntityId = %s" , fileEntityId );
205
- }
182
+ private void wikidataAddLabels (final Long fileEntityId , final String languageCode ,
183
+ final String captionValue ) {
184
+
185
+ csrfToken ()
186
+ .subscribeOn (Schedulers .io ())
187
+ .switchMap (editToken -> captionInterface .addLabelstoWikidata (fileEntityId , editToken , languageCode , captionValue ))
188
+ .subscribe (mwPostResponse -> onAddCaptionResponse (fileEntityId , mwPostResponse ),
189
+ throwable -> {
190
+ Timber .e (throwable , "Error occurred while setting Captions" );
191
+ ViewUtil .showLongToast (context , context .getString (R .string .wikidata_edit_failure ));
192
+ }
193
+ );
194
+ }
206
195
207
- },
208
- throwable -> {
209
- Timber .e (throwable , "Error occurred while setting Captions" );
210
- ViewUtil .showLongToast (context , context .getString (R .string .wikidata_edit_failure ));
211
- });
212
- }else {
213
- Timber .d ("Error acquiring EntityId for image" );
214
- }
215
- }, throwable -> {
216
- Timber .e (throwable , "Error occurred while getting EntityID for the File" );
217
- ViewUtil .showLongToast (context , context .getString (R .string .wikidata_edit_failure ));
218
- });
196
+ private void onAddCaptionResponse (Long fileEntityId , MwPostResponse revisionId ) {
197
+ if (revisionId != null ) {
198
+ Timber .d ("Caption successfully set, revision id = %s" , revisionId );
199
+ } else {
200
+ Timber .d ("Error occurred while setting Captions, fileEntityId = %s" , fileEntityId );
219
201
}
202
+ }
203
+
204
+ private Observable <String > csrfToken () {
205
+ return Observable .fromCallable (() -> {
206
+ try {
207
+ return csrfTokenClient .getTokenBlocking ();
208
+ } catch (Throwable throwable ) {
209
+ throwable .printStackTrace ();
210
+ return null ;
211
+ }
212
+ });
213
+ }
220
214
221
215
public void createImageClaim (@ Nullable final WikidataPlace wikidataPlace , final UploadResult imageUpload ) {
222
216
if (!(directKvStore .getBoolean ("Picture_Has_Correct_Location" , true ))) {
0 commit comments