forked from commons-app/apps-android-commons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UploadPresenter.java
400 lines (345 loc) · 13.8 KB
/
UploadPresenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package fr.free.nrw.commons.upload;
import android.annotation.SuppressLint;
import android.content.Context;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.category.CategoriesModel;
import fr.free.nrw.commons.contributions.Contribution;
import fr.free.nrw.commons.filepicker.UploadableFile;
import fr.free.nrw.commons.kvstore.BasicKvStore;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.utils.StringUtils;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
import static fr.free.nrw.commons.upload.UploadModel.UploadItem;
import static fr.free.nrw.commons.utils.ImageUtils.EMPTY_TITLE;
import static fr.free.nrw.commons.utils.ImageUtils.FILE_NAME_EXISTS;
import static fr.free.nrw.commons.utils.ImageUtils.IMAGE_KEEP;
import static fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK;
import static fr.free.nrw.commons.utils.ImageUtils.getErrorMessageForResult;
/**
* The MVP pattern presenter of Upload GUI
*/
@Singleton
public class UploadPresenter {
private static final UploadView DUMMY = (UploadView) Proxy.newProxyInstance(UploadView.class.getClassLoader(),
new Class[]{UploadView.class}, (proxy, method, methodArgs) -> null);
private UploadView view = DUMMY;
private static final SimilarImageInterface SIMILAR_IMAGE = (SimilarImageInterface) Proxy.newProxyInstance(SimilarImageInterface.class.getClassLoader(),
new Class[]{SimilarImageInterface.class}, (proxy, method, methodArgs) -> null);
private SimilarImageInterface similarImageInterface = SIMILAR_IMAGE;
@UploadView.UploadPage
private int currentPage = UploadView.PLEASE_WAIT;
private final UploadModel uploadModel;
private final UploadController uploadController;
private final Context context;
private final BasicKvStore defaultKvStore;
private final JsonKvStore directKvStore;
@Inject
UploadPresenter(UploadModel uploadModel,
UploadController uploadController,
Context context,
@Named("default_preferences") BasicKvStore defaultKvStore,
@Named("direct_nearby_upload_prefs") JsonKvStore directKvStore) {
this.uploadModel = uploadModel;
this.uploadController = uploadController;
this.context = context;
this.defaultKvStore = defaultKvStore;
this.directKvStore = directKvStore;
}
/**
* Passes the items received to {@link #uploadModel} and displays the items.
*
* @param media The Uri's of the media being uploaded.
* @param source File source from {@link Contribution.FileSource}
*/
@SuppressLint("CheckResult")
void receive(List<UploadableFile> media,
@Contribution.FileSource String source,
Place place) {
Observable<UploadItem> uploadItemObservable = uploadModel
.preProcessImages(media, place, source, similarImageInterface);
uploadItemObservable
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(uploadItems -> onImagesProcessed(uploadItems, place),
throwable -> Timber.e(throwable, "Error occurred in processing images"));
}
private void onImagesProcessed(List<UploadItem> uploadItems, Place place) {
uploadModel.onItemsProcessed(place, uploadItems);
updateCards();
updateLicenses();
updateContent();
uploadModel.subscribeBadPicture(this::handleBadImage, false);
}
/**
* Sets the license to parameter and updates {@link UploadActivity}
*
* @param licenseName license name
*/
void selectLicense(String licenseName) {
uploadModel.setSelectedLicense(licenseName);
view.updateLicenseSummary(uploadModel.getSelectedLicense(), uploadModel.getCount());
}
//region Wizard step management
/**
* Called by the next button in {@link UploadActivity}
*/
@SuppressLint("CheckResult")
void handleNext(Title title,
List<Description> descriptions) {
Timber.e("Inside handleNext");
view.showProgressDialog();
uploadModel.getImageQuality(uploadModel.getCurrentItem(), true)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(imageResult -> handleImage(title, descriptions, imageResult),
throwable -> Timber.e(throwable, "Error occurred while handling image"));
}
private void handleImage(Title title, List<Description> descriptions, Integer imageResult) {
view.hideProgressDialog();
if (imageResult == IMAGE_KEEP || imageResult == IMAGE_OK) {
Timber.d("Set title and desc; Show next uploaded item");
setTitleAndDescription(title, descriptions);
directKvStore.putBoolean("Picture_Has_Correct_Location", true);
nextUploadedItem();
} else {
handleBadImage(imageResult);
}
}
/**
* Called by the next button in {@link UploadActivity}
*/
@SuppressLint("CheckResult")
void handleCategoryNext(CategoriesModel categoriesModel,
boolean noCategoryWarningShown) {
if (categoriesModel.selectedCategoriesCount() < 1 && !noCategoryWarningShown) {
view.showNoCategorySelectedWarning();
} else {
nextUploadedItem();
}
}
private void handleBadImage(Integer errorCode) {
Timber.d("Handle bad picture with error code %d", errorCode);
if (errorCode >= 8) { // If location of image and nearby does not match, then set shared preferences to disable wikidata edits
directKvStore.putBoolean("Picture_Has_Correct_Location", false);
}
switch (errorCode) {
case EMPTY_TITLE:
Timber.d("Title is empty. Showing toast");
view.showErrorMessage(R.string.add_title_toast);
break;
case FILE_NAME_EXISTS:
Timber.d("Trying to show duplicate picture popup");
view.showDuplicatePicturePopup();
break;
default:
String errorMessageForResult = getErrorMessageForResult(context, errorCode);
if (StringUtils.isNullOrWhiteSpace(errorMessageForResult)) {
return;
}
view.showBadPicturePopup(errorMessageForResult);
}
}
private void nextUploadedItem() {
Timber.d("Trying to show next uploaded item");
uploadModel.next();
updateContent();
uploadModel.subscribeBadPicture(this::handleBadImage, false);
view.dismissKeyboard();
}
private void setTitleAndDescription(Title title, List<Description> descriptions) {
Timber.d("setTitleAndDescription: Setting title and desc");
uploadModel.setCurrentTitleAndDescriptions(title, descriptions);
}
String getCurrentImageFileName() {
UploadItem currentItem = getCurrentItem();
return currentItem.getFileName();
}
/**
* Called by the previous button in {@link UploadActivity}
*/
void handlePrevious() {
uploadModel.previous();
updateContent();
uploadModel.subscribeBadPicture(this::handleBadImage, false);
view.dismissKeyboard();
}
/**
* Called when one of the pictures on the top card is clicked on in {@link UploadActivity}
*/
void thumbnailClicked(UploadItem item) {
uploadModel.jumpTo(item);
updateContent();
}
/**
* Called by the submit button in {@link UploadActivity}
*/
@SuppressLint("CheckResult")
void handleSubmit(CategoriesModel categoriesModel) {
if (view.checkIfLoggedIn())
uploadModel.buildContributions(categoriesModel.getCategoryStringList())
.observeOn(Schedulers.io())
.subscribe(uploadController::startUpload);
}
/**
* Called by the map button on the right card in {@link UploadActivity}
*/
void openCoordinateMap() {
GPSExtractor gpsObj = uploadModel.getCurrentItem().getGpsCoords();
if (gpsObj != null && gpsObj.imageCoordsExists) {
view.launchMapActivity(gpsObj.getDecLatitude() + "," + gpsObj.getDecLongitude());
}
}
void keepPicture() {
uploadModel.keepPicture();
}
void deletePicture() {
if (uploadModel.getCount() == 1)
view.finish();
else {
uploadModel.deletePicture();
updateCards();
updateContent();
uploadModel.subscribeBadPicture(this::handleBadImage, false);
view.dismissKeyboard();
}
}
//endregion
//region Top Bottom and Right card state management
/**
* Toggles the top card's state between open and closed.
*/
void toggleTopCardState() {
uploadModel.setTopCardState(!uploadModel.isTopCardState());
view.setTopCardState(uploadModel.isTopCardState());
}
/**
* Toggles the bottom card's state between open and closed.
*/
void toggleBottomCardState() {
uploadModel.setBottomCardState(!uploadModel.isBottomCardState());
view.setBottomCardState(uploadModel.isBottomCardState());
}
/**
* Sets all the cards' states to closed.
*/
void closeAllCards() {
if (uploadModel.isTopCardState()) {
uploadModel.setTopCardState(false);
view.setTopCardState(false);
}
if (uploadModel.isRightCardState()) {
uploadModel.setRightCardState(false);
}
if (uploadModel.isBottomCardState()) {
uploadModel.setBottomCardState(false);
view.setBottomCardState(false);
}
}
//endregion
//region View / Lifecycle management
public void init() {
uploadController.prepareService();
}
void cleanup() {
uploadController.cleanup();
}
void removeView() {
this.view = DUMMY;
}
void addView(UploadView view) {
this.view = view;
updateCards();
updateLicenses();
updateContent();
}
/**
* Updates the cards for when there is a change to the amount of items being uploaded.
*/
private void updateCards() {
Timber.i("uploadModel.getCount():" + uploadModel.getCount());
view.updateThumbnails(uploadModel.getUploads());
view.setTopCardVisibility(uploadModel.getCount() > 1);
view.setBottomCardVisibility(uploadModel.getCount() > 0);
view.setTopCardState(uploadModel.isTopCardState());
view.setBottomCardState(uploadModel.isBottomCardState());
}
/**
* Sets the list of licences and the default license.
*/
private void updateLicenses() {
String selectedLicense = defaultKvStore.getString(Prefs.DEFAULT_LICENSE, Prefs.Licenses.CC_BY_SA_3);
view.updateLicenses(uploadModel.getLicenses(), selectedLicense);
view.updateLicenseSummary(selectedLicense, uploadModel.getCount());
}
/**
* Updates the cards and the background when a new currentPage is selected.
*/
private void updateContent() {
Timber.i("Updating content for currentPage" + uploadModel.getCurrentStep());
view.setNextEnabled(uploadModel.isNextAvailable());
view.setPreviousEnabled(uploadModel.isPreviousAvailable());
view.setSubmitEnabled(uploadModel.isSubmitAvailable());
view.setBackground(uploadModel.getCurrentItem().getMediaUri());
view.updateBottomCardContent(uploadModel.getCurrentStep(),
uploadModel.getStepCount(),
uploadModel.getCurrentItem(),
uploadModel.isShowingItem());
view.updateTopCardContent();
GPSExtractor gpsObj = uploadModel.getCurrentItem().getGpsCoords();
view.updateRightCardContent(gpsObj != null && gpsObj.imageCoordsExists);
view.updateSubtitleVisibility(uploadModel.getCount());
showCorrectCards(uploadModel.getCurrentStep(), uploadModel.getCount());
}
/**
* Updates the layout to show the correct bottom card.
*
* @param currentStep the current step
* @param uploadCount how many items are being uploaded
*/
private void showCorrectCards(int currentStep, int uploadCount) {
if (uploadCount == 0) {
currentPage = UploadView.PLEASE_WAIT;
} else if (currentStep <= uploadCount) {
currentPage = UploadView.TITLE_CARD;
view.setTopCardVisibility(uploadModel.getCount() > 1);
} else if (currentStep == uploadCount + 1) {
currentPage = UploadView.CATEGORIES;
view.setTopCardVisibility(false);
view.setRightCardVisibility(false);
view.initDefaultCategories();
} else {
currentPage = UploadView.LICENSE;
view.setTopCardVisibility(false);
view.setRightCardVisibility(false);
}
view.setBottomCardVisibility(currentPage, uploadCount);
}
//endregion
/**
* @return the item currently being displayed
*/
private UploadItem getCurrentItem() {
return uploadModel.getCurrentItem();
}
List<String> getImageTitleList() {
List<String> titleList = new ArrayList<>();
for (UploadItem item : uploadModel.getUploads()) {
if (item.getTitle().isSet()) {
titleList.add(item.getTitle().toString());
}
}
return titleList;
}
}