Some correction in the code

This commit is contained in:
Sujal-Gupta-SG 2025-01-30 09:47:44 +05:30
parent b2092fa751
commit 058c0482da
6 changed files with 32 additions and 50 deletions

View file

@ -2098,6 +2098,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
? descriptionText : descriptionText.replaceFirst(".$", ""));
// Set the short description after we remove place name from long description
binding.bottomSheetDetails.description.setText(descriptionText);
binding.fabCamera.setOnClickListener(view -> {
if (binding.fabCamera.isShown()) {
Timber.d("Camera button tapped. Place: %s", selectedPlace.toString());

View file

@ -15,6 +15,9 @@ class ResultTuple {
}
constructor() {
// Setting the default language to "en" to ensure users
// are aware that they can select a language while uploading.
// This prevents cases where no language is selected.
language = "en"
type = ""
value = ""

View file

@ -261,7 +261,6 @@ class UploadClient
fileKey: String?,
): Observable<UploadResult?> =
try {
Timber.d( "Step 1")
uploadInterface
.uploadFileFromStash(
csrfTokenClient.getTokenBlocking(),
@ -270,10 +269,8 @@ class UploadClient
uniqueFileName!!,
fileKey!!,
).map { uploadResponse: JsonObject? ->
Timber.d("Step 2")
Timber.d(uploadResponse.toString())
val uploadResult = gson.fromJson(uploadResponse, UploadResponse::class.java)
Timber.d("Step 3: Upload result - ${uploadResult.upload}")
if (uploadResult.upload == null) {
// Parse the error to MwException
@ -288,7 +285,6 @@ class UploadClient
throw Exception(exception.errorCode)
}
}
Timber.d("Step 4")
uploadResult.upload
}
} catch (throwable: Throwable) {

View file

@ -480,11 +480,9 @@ class UploadWorker(
uploadResult.filename,
contribution.media.captions,
)
Timber.d(revisionID.toString())
if (revisionID != null) {
Timber.d("Updating WikiItem place with image")
withContext(Dispatchers.IO) {
Timber.d("Updating WikiItem place with image")
// Fetch and update place details
val place = placesRepository.fetchPlace(wikiDataPlace.id)
place.name = wikiDataPlace.name
@ -520,7 +518,6 @@ class UploadWorker(
saveCompletedContribution(contribution, uploadResult)
}
private fun saveCompletedContribution(
contribution: Contribution,
uploadResult: UploadResult,
@ -532,6 +529,7 @@ class UploadWorker(
.blockingGet()
contributionFromUpload.dateModified = Date()
contributionDao.deleteAndSaveContribution(contribution, contributionFromUpload)
// Upload success, save to uploaded status.
saveIntoUploadedStatus(contribution)
}

View file

@ -78,23 +78,23 @@ class WikiBaseClient
.toLong()
}
fun addLabelsToWikidata(
fileEntityId: Long,
languageCode: String?,
captionValue: String?
): Observable<MwPostResponse> {
return csrfToken().switchMap { editToken ->
wikiBaseInterface.addLabelstoWikidata(
PAGE_ID_PREFIX + fileEntityId,
editToken,
languageCode,
captionValue
)
}
}
private fun csrfToken(): Observable<String> =
Observable.fromCallable {
csrfTokenClient.getTokenBlocking()
fun addLabelsToWikidata(
fileEntityId: Long,
languageCode: String?,
captionValue: String?
): Observable<MwPostResponse> {
return csrfToken().switchMap { editToken ->
wikiBaseInterface.addLabelstoWikidata(
PAGE_ID_PREFIX + fileEntityId,
editToken,
languageCode,
captionValue
)
}
}
private fun csrfToken(): Observable<String> =
Observable.fromCallable {
csrfTokenClient.getTokenBlocking()
}
}

View file

@ -37,6 +37,10 @@ import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
/**
* This class is meant to handle the Wikidata edits made through the app It will talk with MediaWiki
* Apis to make the necessary calls, log the edits and fire listeners on successful edits
*/
@Singleton
class WikidataEditService @Inject constructor(
private val context: Context,
@ -50,7 +54,7 @@ class WikidataEditService @Inject constructor(
fileEntityId: String,
depictedItems: List<String>
): Observable<Boolean> {
Timber.d("Entering addDepictsProperty with fileEntityId: %s and depictedItems: %s", fileEntityId, depictedItems)
val data = EditClaim.from(
if (isBetaFlavour) listOf("Q10") else depictedItems, DEPICTS.propertyName
)
@ -68,7 +72,6 @@ class WikidataEditService @Inject constructor(
showLongToast(context, throwable.toString())
}
.subscribeOn(Schedulers.io())
.doOnComplete { Timber.d("Exiting addDepictsProperty") }
}
@SuppressLint("CheckResult")
@ -76,10 +79,11 @@ class WikidataEditService @Inject constructor(
fileEntityId: String?,
depictedItems: List<String>
): Observable<Boolean> {
Timber.d("Entering updateDepictsProperty with fileEntityId: %s and depictedItems: %s", fileEntityId, depictedItems)
val entityId: String = PAGE_ID_PREFIX + fileEntityId
val claimIds = getDepictionsClaimIds(entityId)
/* Please consider removeClaim scenario for BetaDebug */
val data = RemoveClaim.from(if (isBetaFlavour) listOf("Q10") else claimIds)
return wikiBaseClient.postDeleteClaims(entityId, gson.toJson(data))
@ -101,26 +105,22 @@ class WikidataEditService @Inject constructor(
return@switchMap Observable.empty<Boolean>()
}
}
.doOnComplete { Timber.d("Exiting updateDepictsProperty") }
}
@SuppressLint("CheckResult")
private fun getDepictionsClaimIds(entityId: String): List<String> {
Timber.d("Entering getDepictionsClaimIds with entityId: %s", entityId)
val claimIds = wikiBaseClient.getClaimIdsByProperty(entityId, DEPICTS.propertyName)
.subscribeOn(Schedulers.io())
.blockingFirst()
Timber.d("Exiting getDepictionsClaimIds with claimIds: %s", claimIds)
return claimIds
}
@SuppressLint("StringFormatInvalid")
private fun showSuccessToast(wikiItemName: String) {
Timber.d("Entering showSuccessToast with wikiItemName: %s", wikiItemName)
val successStringTemplate = context.getString(R.string.successful_wikidata_edit)
val successMessage = String.format(Locale.getDefault(), successStringTemplate, wikiItemName)
showLongToast(context, successMessage)
Timber.d("Exiting showSuccessToast")
}
@RequiresApi(Build.VERSION_CODES.N)
@ -129,7 +129,6 @@ class WikidataEditService @Inject constructor(
fileEntityId: Long, languageCode: String,
captionValue: String
): Observable<Boolean> {
Timber.d("Entering addCaption with fileEntityId: %s, languageCode: %s, captionValue: %s", fileEntityId, languageCode, captionValue)
return wikiBaseClient.addLabelsToWikidata(fileEntityId, languageCode, captionValue)
.doOnNext { mwPostResponse: MwPostResponse? ->
onAddCaptionResponse(
@ -145,24 +144,20 @@ class WikidataEditService @Inject constructor(
)
}
.map(Objects::nonNull)
.doOnComplete { Timber.d("Exiting addCaption") }
}
private fun onAddCaptionResponse(fileEntityId: Long, response: MwPostResponse?) {
Timber.d("Entering onAddCaptionResponse with fileEntityId: %s and response: %s", fileEntityId, response)
if (response != null) {
Timber.d("Caption successfully set, revision id = %s", response)
} else {
Timber.d("Error occurred while setting Captions, fileEntityId = %s", fileEntityId)
}
Timber.d("Exiting onAddCaptionResponse")
}
fun createClaim(
wikidataPlace: WikidataPlace?, fileName: String,
captions: Map<String, String>
): Long? {
Timber.d("Entering createClaim with wikidataPlace: %s, fileName: %s, captions: %s", wikidataPlace, fileName, captions)
if (!(directKvStore.getBoolean("Picture_Has_Correct_Location", true))) {
Timber.d(
"Image location and nearby place location mismatched, so Wikidata item won't be edited"
@ -170,7 +165,6 @@ class WikidataEditService @Inject constructor(
return null
}
val result = addImageAndMediaLegends(wikidataPlace!!, fileName, captions)
Timber.d("Exiting createClaim with result: %s", result)
return result
}
@ -178,7 +172,6 @@ class WikidataEditService @Inject constructor(
wikidataItem: WikidataItem, fileName: String,
captions: Map<String, String>
): Long {
Timber.d("Entering addImageAndMediaLegends with wikidataItem: %s, fileName: %s, captions: %s", wikidataItem, fileName, captions)
val p18 = SnakPartial(
"value",
IMAGE.propertyName,
@ -205,12 +198,10 @@ class WikidataEditService @Inject constructor(
)
val result = wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle()
Timber.d("Exiting addImageAndMediaLegends with result: %s", result)
return result
}
fun handleImageClaimResult(wikidataItem: WikidataItem, revisionId: Long?) {
Timber.d("Entering handleImageClaimResult with wikidataItem: %s and revisionId: %s", wikidataItem, revisionId)
if (revisionId != null) {
wikidataEditListener?.onSuccessfulWikidataEdit()
showSuccessToast(wikidataItem.name)
@ -218,14 +209,12 @@ class WikidataEditService @Inject constructor(
Timber.d("Unable to make wiki data edit for entity %s", wikidataItem)
showLongToast(context, context.getString(R.string.wikidata_edit_failure))
}
Timber.d("Exiting handleImageClaimResult")
}
fun addDepictionsAndCaptions(
uploadResult: UploadResult,
contribution: Contribution
): Observable<Boolean> {
Timber.d("Entering addDepictionsAndCaptions with uploadResult: %s and contribution: %s", uploadResult, contribution)
return wikiBaseClient.getFileEntityId(uploadResult)
.doOnError { throwable: Throwable? ->
Timber.e(
@ -249,15 +238,12 @@ class WikidataEditService @Inject constructor(
return@switchMap Observable.empty<Boolean>()
}
}
.doOnComplete { Timber.d("Exiting addDepictionsAndCaptions") }
}
@SuppressLint("NewApi")
private fun captionEdits(contribution: Contribution, fileEntityId: Long): Observable<Boolean> {
Timber.d("Entering captionEdits with contribution: %s and fileEntityId: %s", contribution, fileEntityId)
val result = Observable.fromIterable(contribution.media.captions.entries)
.concatMap { addCaption(fileEntityId, it.key, it.value) }
Timber.d("Exiting captionEdits")
return result
}
@ -265,13 +251,11 @@ class WikidataEditService @Inject constructor(
contribution: Contribution,
fileEntityId: Long
): Observable<Boolean> {
Timber.d("Entering depictionEdits with contribution: %s and fileEntityId: %s", contribution, fileEntityId)
val result = addDepictsProperty(fileEntityId.toString(), buildList {
for ((_, _, _, _, _, _, id) in contribution.depictedItems) {
add(id)
}
})
Timber.d("Exiting depictionEdits")
return result
}