Migrated the following files in util module to Kotlin

- AbstractTextWatcher
- ActivityUtils
- CommonsDateUtil
- DateUtil
This commit is contained in:
Saifuddin 2024-11-16 18:48:30 +05:30
parent d37257b53b
commit 9c534b1929
12 changed files with 96 additions and 97 deletions

View file

@ -12,15 +12,15 @@ import androidx.annotation.Nullable;
import fr.free.nrw.commons.campaigns.models.Campaign;
import fr.free.nrw.commons.databinding.LayoutCampaginBinding;
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.DateUtil;
import fr.free.nrw.commons.utils.CommonsDateUtil;
import fr.free.nrw.commons.utils.DateUtil;
import java.text.ParseException;
import java.util.Date;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.contributions.MainActivity;
import fr.free.nrw.commons.utils.CommonsDateUtil;
import fr.free.nrw.commons.utils.SwipableCardView;
import fr.free.nrw.commons.utils.ViewUtil;

View file

@ -3,6 +3,7 @@ package fr.free.nrw.commons.campaigns;
import android.annotation.SuppressLint;
import fr.free.nrw.commons.campaigns.models.Campaign;
import fr.free.nrw.commons.utils.CommonsDateUtil;
import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
@ -14,7 +15,6 @@ import javax.inject.Singleton;
import fr.free.nrw.commons.BasePresenter;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import fr.free.nrw.commons.utils.CommonsDateUtil;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.reactivex.SingleObserver;

View file

@ -3,7 +3,6 @@ package fr.free.nrw.commons.delete;
import android.content.Context;
import fr.free.nrw.commons.utils.DateUtil;
import java.util.Date;
import java.util.Locale;

View file

@ -11,7 +11,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import com.google.android.material.tabs.TabLayout;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.ViewPagerAdapter;
import fr.free.nrw.commons.contributions.MainActivity;

View file

@ -55,7 +55,6 @@ import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.actions.ThanksClient;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient;
import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException;
import fr.free.nrw.commons.category.CategoryClient;
import fr.free.nrw.commons.category.CategoryDetailsActivity;

View file

@ -22,7 +22,6 @@ import android.widget.ListView;
import android.widget.TextView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

View file

@ -1,7 +1,6 @@
package fr.free.nrw.commons.upload.mediaDetails;
import static android.app.Activity.RESULT_OK;
import static fr.free.nrw.commons.utils.ActivityUtils.startActivityWithFlags;
import android.annotation.SuppressLint;
import android.app.Activity;
@ -45,6 +44,7 @@ import fr.free.nrw.commons.upload.UploadBaseFragment;
import fr.free.nrw.commons.upload.UploadItem;
import fr.free.nrw.commons.upload.UploadMediaDetail;
import fr.free.nrw.commons.upload.UploadMediaDetailAdapter;
import fr.free.nrw.commons.utils.ActivityUtils;
import fr.free.nrw.commons.utils.DialogUtil;
import fr.free.nrw.commons.utils.ImageUtils;
import fr.free.nrw.commons.utils.NetworkUtils;
@ -208,7 +208,7 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
try {
if(!presenter.getImageQuality(indexOfFragment, inAppPictureLocation, getActivity())) {
startActivityWithFlags(
ActivityUtils.startActivityWithFlags(
getActivity(), MainActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,
Intent.FLAG_ACTIVITY_SINGLE_TOP);
}

View file

@ -1,31 +1,25 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import android.text.Editable;
import android.text.TextWatcher;
import android.text.Editable
import android.text.TextWatcher
import androidx.annotation.NonNull;
class AbstractTextWatcher(
private val textChange: TextChange
) : TextWatcher {
public class AbstractTextWatcher implements TextWatcher {
private final TextChange textChange;
public AbstractTextWatcher(@NonNull TextChange textChange) {
this.textChange = textChange;
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// No-op
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
textChange.onTextChanged(s.toString())
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textChange.onTextChanged(s.toString());
override fun afterTextChanged(s: Editable?) {
// No-op
}
@Override
public void afterTextChanged(Editable s) {
}
public interface TextChange {
void onTextChanged(String value);
interface TextChange {
fun onTextChanged(value: String)
}
}

View file

@ -1,15 +1,16 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import android.content.Context;
import android.content.Intent;
import android.content.Context
import android.content.Intent
public class ActivityUtils {
object ActivityUtils {
public static <T> void startActivityWithFlags(Context context, Class<T> cls, int... flags) {
Intent intent = new Intent(context, cls);
for (int flag : flags) {
intent.addFlags(flag);
@JvmStatic
fun <T> startActivityWithFlags(context: Context, cls: Class<T>, vararg flags: Int) {
val intent = Intent(context, cls)
for (flag in flags) {
intent.addFlags(flag)
}
context.startActivity(intent);
context.startActivity(intent)
}
}
}

View file

@ -1,44 +1,46 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.TimeZone
/**
* Provides util functions for formatting date time
* Most of our formatting needs are addressed by the data library's DateUtil class
* Methods should be added here only if DateUtil class doesn't provide for it already
* Provides util functions for formatting date time.
* Most of our formatting needs are addressed by the data library's DateUtil class.
* Methods should be added here only if DateUtil class doesn't provide for it already.
*/
public class CommonsDateUtil {
object CommonsDateUtil {
/**
* Gets SimpleDateFormat for short date pattern
* Gets SimpleDateFormat for short date pattern.
* @return simpledateformat
*/
public static SimpleDateFormat getIso8601DateFormatShort() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat;
@JvmStatic
fun getIso8601DateFormatShort(): SimpleDateFormat {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.ROOT)
simpleDateFormat.timeZone = TimeZone.getTimeZone("UTC")
return simpleDateFormat
}
/**
* Gets SimpleDateFormat for date pattern returned by Media object
* Gets SimpleDateFormat for date pattern returned by Media object.
* @return simpledateformat
*/
public static SimpleDateFormat getMediaSimpleDateFormat() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat;
@JvmStatic
fun getMediaSimpleDateFormat(): SimpleDateFormat {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT)
simpleDateFormat.timeZone = TimeZone.getTimeZone("UTC")
return simpleDateFormat
}
/**
* Gets the timestamp pattern for a date
* Gets the timestamp pattern for a date.
* @return timestamp
*/
public static SimpleDateFormat getIso8601DateFormatTimestamp() {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
Locale.ROOT);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat;
@JvmStatic
fun getIso8601DateFormatTimestamp(): SimpleDateFormat {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT)
simpleDateFormat.timeZone = TimeZone.getTimeZone("UTC")
return simpleDateFormat
}
}

View file

@ -1,53 +1,59 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import static android.text.format.DateFormat.getBestDateTimePattern;
import android.text.format.DateFormat.getBestDateTimePattern
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.HashMap
import java.util.Locale
import java.util.TimeZone
import androidx.annotation.NonNull;
/**
* Utility class for date formatting and parsing.
* TODO: Switch to DateTimeFormatter when minSdk = 26.
*/
object DateUtil {
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
private val DATE_FORMATS: MutableMap<String, SimpleDateFormat> = HashMap()
public final class DateUtil {
private static Map<String, SimpleDateFormat> DATE_FORMATS = new HashMap<>();
// TODO: Switch to DateTimeFormatter when minSdk = 26.
public static synchronized String iso8601DateFormat(Date date) {
return getCachedDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT, true).format(date);
@JvmStatic
@Synchronized
fun iso8601DateFormat(date: Date): String {
return getCachedDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT, true).format(date)
}
public static synchronized Date iso8601DateParse(String date) throws ParseException {
return getCachedDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT, true).parse(date);
@JvmStatic
@Synchronized
@Throws(ParseException::class)
fun iso8601DateParse(date: String): Date {
return getCachedDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT, true).parse(date)
}
public static String getMonthOnlyDateString(@NonNull Date date) {
return getDateStringWithSkeletonPattern(date, "MMMM d");
@JvmStatic
fun getMonthOnlyDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMMM d")
}
public static String getExtraShortDateString(@NonNull Date date) {
return getDateStringWithSkeletonPattern(date, "MMM d");
@JvmStatic
fun getExtraShortDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMM d")
}
public static synchronized String getDateStringWithSkeletonPattern(@NonNull Date date, @NonNull String pattern) {
return getCachedDateFormat(getBestDateTimePattern(Locale.getDefault(), pattern), Locale.getDefault(), false).format(date);
@JvmStatic
@Synchronized
fun getDateStringWithSkeletonPattern(date: Date, pattern: String): String {
return getCachedDateFormat(getBestDateTimePattern(Locale.getDefault(), pattern), Locale.getDefault(), false).format(date)
}
private static SimpleDateFormat getCachedDateFormat(String pattern, Locale locale, boolean utc) {
@JvmStatic
private fun getCachedDateFormat(pattern: String, locale: Locale, utc: Boolean): SimpleDateFormat {
if (!DATE_FORMATS.containsKey(pattern)) {
SimpleDateFormat df = new SimpleDateFormat(pattern, locale);
val df = SimpleDateFormat(pattern, locale)
if (utc) {
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.timeZone = TimeZone.getTimeZone("UTC")
}
DATE_FORMATS.put(pattern, df);
DATE_FORMATS[pattern] = df
}
return DATE_FORMATS.get(pattern);
}
private DateUtil() {
return DATE_FORMATS[pattern]!!
}
}

View file

@ -7,8 +7,8 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.StringUtils;
import fr.free.nrw.commons.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import fr.free.nrw.commons.wikidata.GsonUtil;
import java.text.ParseException;