@Inject the MediaWikiApi where needed to reduce coupling between classes.

This commit is contained in:
Paul Hawke 2017-08-25 22:05:09 -05:00
parent ed1ae98d8e
commit 647cc166ef
16 changed files with 85 additions and 82 deletions

View file

@ -40,6 +40,7 @@ import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.modifications.TemplateRemoveModifier;
import fr.free.nrw.commons.mwapi.EventLog;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import timber.log.Timber;
public class MultipleShareActivity
@ -49,8 +50,8 @@ public class MultipleShareActivity
FragmentManager.OnBackStackChangedListener,
MultipleUploadListFragment.OnMultipleUploadInitiatedHandler,
OnCategoriesSaveHandler {
@Inject
CommonsApplication application;
@Inject CommonsApplication application;
@Inject MediaWikiApi mwApi;
private ArrayList<Contribution> photosList = null;
@ -181,7 +182,7 @@ public class MultipleShareActivity
// FIXME: Make sure that the content provider is up
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
ContentResolver.setSyncAutomatically(application.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("categories-count", categories.size())
.param("files-count", photosList.size())
@ -248,7 +249,7 @@ public class MultipleShareActivity
@Override
protected void onAuthCookieAcquired(String authCookie) {
application.getMWApi().setAuthCookie(authCookie);
mwApi.setAuthCookie(authCookie);
Intent intent = getIntent();
if(intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
@ -291,7 +292,7 @@ public class MultipleShareActivity
public void onBackPressed() {
super.onBackPressed();
if(categorizationFragment != null && categorizationFragment.isVisible()) {
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("categories-count", categorizationFragment.getCurrentSelectedCount())
.param("files-count", photosList.size())
@ -299,7 +300,7 @@ public class MultipleShareActivity
.param("result", "cancelled")
.log();
} else {
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("source", getIntent().getStringExtra(UploadService.EXTRA_SOURCE))
.param("multiple", true)

View file

@ -45,6 +45,7 @@ import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.modifications.TemplateRemoveModifier;
import fr.free.nrw.commons.mwapi.EventLog;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import timber.log.Timber;
import static fr.free.nrw.commons.upload.ExistingFileAsync.Result.DUPLICATE_PROCEED;
@ -66,6 +67,7 @@ public class ShareActivity
private CategorizationFragment categorizationFragment;
@Inject CommonsApplication application;
@Inject MediaWikiApi mwApi;
private String source;
private String mimeType;
@ -169,7 +171,7 @@ public class ShareActivity
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
ContentResolver.setSyncAutomatically(application.getCurrentAccount(), ModificationsContentProvider.AUTHORITY, true); // Enable sync by default!
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("categories-count", categories.size())
.param("files-count", 1)
@ -191,7 +193,7 @@ public class ShareActivity
public void onBackPressed() {
super.onBackPressed();
if(categorizationFragment != null && categorizationFragment.isVisible()) {
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_CATEGORIZATION_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("categories-count", categorizationFragment.getCurrentSelectedCount())
.param("files-count", 1)
@ -199,7 +201,7 @@ public class ShareActivity
.param("result", "cancelled")
.log();
} else {
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("source", getIntent().getStringExtra(UploadService.EXTRA_SOURCE))
.param("multiple", true)
@ -210,8 +212,7 @@ public class ShareActivity
@Override
protected void onAuthCookieAcquired(String authCookie) {
application.getMWApi().setAuthCookie(authCookie);
mwApi.setAuthCookie(authCookie);
}
@Override
@ -385,7 +386,7 @@ public class ShareActivity
Timber.d("%s duplicate check: %s", mediaUri.toString(), result);
duplicateCheckPassed = (result == DUPLICATE_PROCEED
|| result == NO_DUPLICATE);
}, application.getMWApi());
}, mwApi);
fileAsyncTask.execute();
} catch (IOException e) {
Timber.d(e, "IO Exception: ");

View file

@ -48,6 +48,7 @@ public class UploadService extends HandlerService<Contribution> {
public static final String EXTRA_CAMPAIGN = EXTRA_PREFIX + ".campaign";
@Inject CommonsApplication application;
@Inject MediaWikiApi mwApi;
private NotificationManager notificationManager;
private ContentProviderClient contributionsProviderClient;
@ -182,8 +183,6 @@ public class UploadService extends HandlerService<Contribution> {
@SuppressLint("StringFormatInvalid")
private void uploadContribution(Contribution contribution) {
MediaWikiApi api = application.getMWApi();
InputStream file = null;
String notificationTag = contribution.getLocalUri().toString();
@ -222,7 +221,7 @@ public class UploadService extends HandlerService<Contribution> {
filename = findUniqueFilename(filename);
unfinishedUploads.add(filename);
}
if (!api.validateLogin()) {
if (!mwApi.validateLogin()) {
// Need to revalidate!
if (application.revalidateAuthToken()) {
Timber.d("Successfully revalidated token!");
@ -240,7 +239,7 @@ public class UploadService extends HandlerService<Contribution> {
getString(R.string.upload_progress_notification_title_finishing, contribution.getDisplayTitle()),
contribution
);
UploadResult uploadResult = api.uploadFile(filename, file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
UploadResult uploadResult = mwApi.uploadFile(filename, file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
Timber.d("Response is %s", uploadResult.toString());
@ -249,7 +248,7 @@ public class UploadService extends HandlerService<Contribution> {
String resultStatus = uploadResult.getResultStatus();
if (!resultStatus.equals("Success")) {
showFailedNotification(contribution);
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("source", contribution.getSource())
.param("multiple", contribution.getMultiple())
@ -263,7 +262,7 @@ public class UploadService extends HandlerService<Contribution> {
contribution.setDateUploaded(uploadResult.getDateUploaded());
contribution.save();
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application)
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT, application, mwApi)
.param("username", application.getCurrentAccount().name)
.param("source", contribution.getSource()) //FIXME
.param("filename", contribution.getFilename())
@ -304,7 +303,6 @@ public class UploadService extends HandlerService<Contribution> {
}
private String findUniqueFilename(String fileName) throws IOException {
MediaWikiApi api = application.getMWApi();
String sequenceFileName;
for (int sequenceNumber = 1; true; sequenceNumber++) {
if (sequenceNumber == 1) {
@ -320,7 +318,7 @@ public class UploadService extends HandlerService<Contribution> {
sequenceFileName = regexMatcher.replaceAll("$1 " + sequenceNumber + "$2");
}
}
if (!api.fileExistsWithName(sequenceFileName)
if (!mwApi.fileExistsWithName(sequenceFileName)
&& !unfinishedUploads.contains(sequenceFileName)) {
break;
}