Migrated helper modules to kotlin (#6007)

* Rename .java to .kt

* Migrated delete and description module to kotlin (WIP)

* Fix: Unit tests

* Fix: Unit tests

* Rename .java to .kt

* Migrated data, db, and converter module to kotlin

* Fix: Unit tests

* Fix: Unit tests

---------

Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
Saifuddin Adenwala 2024-12-11 04:13:36 +05:30 committed by GitHub
parent 73311970c5
commit 3030a6fca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 1168 additions and 973 deletions

View file

@ -272,7 +272,7 @@ class DescriptionEditActivity :
.addCaption(
applicationContext,
media,
mediaDetail.languageCode,
mediaDetail.languageCode!!,
mediaDetail.captionText,
).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

View file

@ -1,137 +0,0 @@
package fr.free.nrw.commons.description;
import static fr.free.nrw.commons.notification.NotificationHelper.NOTIFICATION_EDIT_DESCRIPTION;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.actions.PageEditClient;
import fr.free.nrw.commons.notification.NotificationHelper;
import io.reactivex.Single;
import java.util.Objects;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;
/**
* Helper class for edit and update given descriptions and showing notification upgradation
*/
public class DescriptionEditHelper {
/**
* notificationHelper: helps creating notification
*/
private final NotificationHelper notificationHelper;
/**
* * pageEditClient: methods provided by this member posts the edited descriptions
* to the Media wiki api
*/
public final PageEditClient pageEditClient;
@Inject
public DescriptionEditHelper(final NotificationHelper notificationHelper,
@Named("commons-page-edit") final PageEditClient pageEditClient) {
this.notificationHelper = notificationHelper;
this.pageEditClient = pageEditClient;
}
/**
* Replaces new descriptions
*
* @param context context
* @param media to be added
* @param appendText to be added
* @return Observable<Boolean>
*/
public Single<Boolean> addDescription(final Context context, final Media media,
final String appendText) {
Timber.d("thread is description adding %s", Thread.currentThread().getName());
final String summary = "Updating Description";
return pageEditClient.edit(Objects.requireNonNull(media.getFilename()),
appendText, summary)
.flatMapSingle(result -> Single.just(showDescriptionEditNotification(context,
media, result)))
.firstOrError();
}
/**
* Adds new captions
*
* @param context context
* @param media to be added
* @param language to be added
* @param value to be added
* @return Observable<Boolean>
*/
public Single<Boolean> addCaption(final Context context, final Media media,
final String language, final String value) {
Timber.d("thread is caption adding %s", Thread.currentThread().getName());
final String summary = "Updating Caption";
return pageEditClient.setCaptions(summary, Objects.requireNonNull(media.getFilename()),
language, value)
.flatMapSingle(result -> Single.just(showCaptionEditNotification(context,
media, result)))
.firstOrError();
}
/**
* Update captions and shows notification about captions update
* @param context to be added
* @param media to be added
* @param result to be added
* @return boolean
*/
private boolean showCaptionEditNotification(final Context context, final Media media,
final int result) {
final String message;
String title = context.getString(R.string.caption_edit_helper_show_edit_title);
if (result == 1) {
title += ": " + context
.getString(R.string.coordinates_edit_helper_show_edit_title_success);
message = context.getString(R.string.caption_edit_helper_show_edit_message);
} else {
title += ": " + context.getString(R.string.caption_edit_helper_show_edit_title);
message = context.getString(R.string.caption_edit_helper_edit_message_else) ;
}
final String urlForFile = BuildConfig.COMMONS_URL + "/wiki/" + media.getFilename();
final Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlForFile));
notificationHelper.showNotification(context, title, message, NOTIFICATION_EDIT_DESCRIPTION,
browserIntent);
return result == 1;
}
/**
* Update descriptions and shows notification about descriptions update
* @param context to be added
* @param media to be added
* @param result to be added
* @return boolean
*/
private boolean showDescriptionEditNotification(final Context context, final Media media,
final boolean result) {
final String message;
String title = context.getString(R.string.description_edit_helper_show_edit_title);
if (result) {
title += ": " + context
.getString(R.string.coordinates_edit_helper_show_edit_title_success);
message = context.getString(R.string.description_edit_helper_show_edit_message);
} else {
title += ": " + context.getString(R.string.description_edit_helper_show_edit_title);
message = context.getString(R.string.description_edit_helper_edit_message_else) ;
}
final String urlForFile = BuildConfig.COMMONS_URL + "/wiki/" + media.getFilename();
final Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlForFile));
notificationHelper.showNotification(context, title, message, NOTIFICATION_EDIT_DESCRIPTION,
browserIntent);
return result;
}
}

View file

@ -0,0 +1,154 @@
package fr.free.nrw.commons.description
import android.content.Context
import android.content.Intent
import android.net.Uri
import fr.free.nrw.commons.BuildConfig
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.R
import fr.free.nrw.commons.actions.PageEditClient
import fr.free.nrw.commons.notification.NotificationHelper
import fr.free.nrw.commons.notification.NotificationHelper.Companion.NOTIFICATION_EDIT_DESCRIPTION
import io.reactivex.Single
import javax.inject.Inject
import javax.inject.Named
import timber.log.Timber
/**
* Helper class for edit and update given descriptions and showing notification upgradation
*/
class DescriptionEditHelper @Inject constructor(
/**
* notificationHelper: helps creating notification
*/
private val notificationHelper: NotificationHelper,
/**
* pageEditClient: methods provided by this member posts the edited descriptions
* to the Media wiki api
*/
@Named("commons-page-edit") val pageEditClient: PageEditClient
) {
/**
* Replaces new descriptions
*
* @param context context
* @param media to be added
* @param appendText to be added
* @return Single<Boolean>
*/
fun addDescription(context: Context, media: Media, appendText: String): Single<Boolean> {
Timber.d("thread is description adding %s", Thread.currentThread().name)
val summary = "Updating Description"
return pageEditClient.edit(
requireNotNull(media.filename),
appendText,
summary
).flatMapSingle { result ->
Single.just(showDescriptionEditNotification(context, media, result))
}.firstOrError()
}
/**
* Adds new captions
*
* @param context context
* @param media to be added
* @param language to be added
* @param value to be added
* @return Single<Boolean>
*/
fun addCaption(
context: Context,
media: Media,
language: String,
value: String
): Single<Boolean> {
Timber.d("thread is caption adding %s", Thread.currentThread().name)
val summary = "Updating Caption"
return pageEditClient.setCaptions(
summary,
requireNotNull(media.filename),
language,
value
).flatMapSingle { result ->
Single.just(showCaptionEditNotification(context, media, result))
}.firstOrError()
}
/**
* Update captions and shows notification about captions update
* @param context to be added
* @param media to be added
* @param result to be added
* @return boolean
*/
private fun showCaptionEditNotification(context: Context, media: Media, result: Int): Boolean {
val message: String
var title = context.getString(R.string.caption_edit_helper_show_edit_title)
if (result == 1) {
title += ": " + context.getString(
R.string.coordinates_edit_helper_show_edit_title_success
)
message = context.getString(R.string.caption_edit_helper_show_edit_message)
} else {
title += ": " + context.getString(R.string.caption_edit_helper_show_edit_title)
message = context.getString(R.string.caption_edit_helper_edit_message_else)
}
val urlForFile = "${BuildConfig.COMMONS_URL}/wiki/${media.filename}"
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(urlForFile))
notificationHelper.showNotification(
context,
title,
message,
NOTIFICATION_EDIT_DESCRIPTION,
browserIntent
)
return result == 1
}
/**
* Update descriptions and shows notification about descriptions update
* @param context to be added
* @param media to be added
* @param result to be added
* @return boolean
*/
private fun showDescriptionEditNotification(
context: Context,
media: Media,
result: Boolean
): Boolean {
val message: String
var title= context.getString(
R.string.description_edit_helper_show_edit_title
)
if (result) {
title += ": " + context.getString(
R.string.coordinates_edit_helper_show_edit_title_success
)
message = context.getString(R.string.description_edit_helper_show_edit_message)
} else {
title += ": " + context.getString(R.string.description_edit_helper_show_edit_title)
message = context.getString(R.string.description_edit_helper_edit_message_else)
}
val urlForFile = "${BuildConfig.COMMONS_URL}/wiki/${media.filename}"
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(urlForFile))
notificationHelper.showNotification(
context,
title,
message,
NOTIFICATION_EDIT_DESCRIPTION,
browserIntent
)
return result
}
}