Skip to content

Commit

Permalink
Migrated util module files from java to kotlin (#5935)
Browse files Browse the repository at this point in the history
* Rename .java to .kt

* Migrated the following files in util module to Kotlin
- AbstractTextWatcher
- ActivityUtils
- CommonsDateUtil
- DateUtil

* Rename .java to .kt

* Migrated the following files in util module to Kotlin
- DeviceInfoUtil
- ExecutorUtils
- FragmentUtils
  • Loading branch information
Saifuddin53 authored Nov 17, 2024
1 parent 248c7b0 commit 5c8c403
Show file tree
Hide file tree
Showing 22 changed files with 288 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -14,7 +15,6 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;

import fr.free.nrw.commons.utils.DateUtil;

import java.util.Date;
import java.util.Locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -45,6 +44,7 @@
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;
Expand Down Expand Up @@ -208,7 +208,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

try {
if(!presenter.getImageQuality(indexOfFragment, inAppPictureLocation, getActivity())) {
startActivityWithFlags(
ActivityUtils.startActivityWithFlags(
getActivity(), MainActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,
Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
Expand Down

This file was deleted.

25 changes: 25 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/utils/AbstractTextWatcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.free.nrw.commons.utils

import android.text.Editable
import android.text.TextWatcher

class AbstractTextWatcher(
private val textChange: TextChange
) : TextWatcher {

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// No-op
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
textChange.onTextChanged(s.toString())
}

override fun afterTextChanged(s: Editable?) {
// No-op
}

interface TextChange {
fun onTextChanged(value: String)
}
}
15 changes: 0 additions & 15 deletions app/src/main/java/fr/free/nrw/commons/utils/ActivityUtils.java

This file was deleted.

16 changes: 16 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/utils/ActivityUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.free.nrw.commons.utils

import android.content.Context
import android.content.Intent

object ActivityUtils {

@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)
}
}
44 changes: 0 additions & 44 deletions app/src/main/java/fr/free/nrw/commons/utils/CommonsDateUtil.java

This file was deleted.

46 changes: 46 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/utils/CommonsDateUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.free.nrw.commons.utils

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.
*/
object CommonsDateUtil {

/**
* Gets SimpleDateFormat for short date pattern.
* @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.
* @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.
* @return timestamp
*/
@JvmStatic
fun getIso8601DateFormatTimestamp(): SimpleDateFormat {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT)
simpleDateFormat.timeZone = TimeZone.getTimeZone("UTC")
return simpleDateFormat
}
}
53 changes: 0 additions & 53 deletions app/src/main/java/fr/free/nrw/commons/utils/DateUtil.java

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/utils/DateUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fr.free.nrw.commons.utils

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

/**
* Utility class for date formatting and parsing.
* TODO: Switch to DateTimeFormatter when minSdk = 26.
*/
object DateUtil {

private val DATE_FORMATS: MutableMap<String, SimpleDateFormat> = HashMap()

@JvmStatic
@Synchronized
fun iso8601DateFormat(date: Date): String {
return getCachedDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT, true).format(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)
}

@JvmStatic
fun getMonthOnlyDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMMM d")
}

@JvmStatic
fun getExtraShortDateString(date: Date): String {
return getDateStringWithSkeletonPattern(date, "MMM d")
}

@JvmStatic
@Synchronized
fun getDateStringWithSkeletonPattern(date: Date, pattern: String): String {
return getCachedDateFormat(
getBestDateTimePattern(Locale.getDefault(), pattern),
Locale.getDefault(), false
).format(date)
}

@JvmStatic
private fun getCachedDateFormat(pattern: String, locale: Locale, utc: Boolean): SimpleDateFormat {
if (!DATE_FORMATS.containsKey(pattern)) {
val df = SimpleDateFormat(pattern, locale)
if (utc) {
df.timeZone = TimeZone.getTimeZone("UTC")
}
DATE_FORMATS[pattern] = df
}
return DATE_FORMATS[pattern]!!
}
}
Loading

0 comments on commit 5c8c403

Please sign in to comment.