mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
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:
parent
73311970c5
commit
3030a6fca7
18 changed files with 1168 additions and 973 deletions
|
|
@ -272,7 +272,7 @@ class DescriptionEditActivity :
|
|||
.addCaption(
|
||||
applicationContext,
|
||||
media,
|
||||
mediaDetail.languageCode,
|
||||
mediaDetail.languageCode!!,
|
||||
mediaDetail.captionText,
|
||||
).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue