From b55f165022b52c5b43a41e1ab89cd6d61f37b8c1 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 7 Oct 2019 21:44:02 +0300 Subject: [PATCH] Relative time for KitKat --- .../main/recent/RecentFragment.java | 5 +- .../main/unread/UnreadFragment.java | 5 +- .../gr/thmmy/mthmmy/utils/DateTimeUtils.java | 14 +--- .../utils/parsing/ThmmyDateTimeParser.java | 21 ++++-- .../res/xml-v21/app_preferences_guest.xml | 43 ------------ .../main/res/xml-v21/app_preferences_user.xml | 66 ------------------- .../main/res/xml/app_preferences_guest.xml | 6 ++ app/src/main/res/xml/app_preferences_user.xml | 6 ++ .../thmmy/mthmmy/utils/DateTimeUtilsTest.java | 4 +- 9 files changed, 34 insertions(+), 136 deletions(-) delete mode 100644 app/src/main/res/xml-v21/app_preferences_guest.xml delete mode 100644 app/src/main/res/xml-v21/app_preferences_user.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index a1f21281..6eff1d27 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -35,7 +35,7 @@ import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.Response; import timber.log.Timber; -import static gr.thmmy.mthmmy.utils.DateTimeUtils.convertDateTime; +import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertDateTime; import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertToTimestamp; @@ -192,8 +192,7 @@ public class RecentFragment extends BaseFragment { matcher = pattern.matcher(dateTime); if (matcher.find()){ dateTime = matcher.group(1); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP - && BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { + if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { dateTime=convertDateTime(dateTime, false); String timestamp = convertToTimestamp(dateTime); if(timestamp!=null) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 950ca27a..53b7d20b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -38,7 +38,7 @@ import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; -import static gr.thmmy.mthmmy.utils.DateTimeUtils.convertDateTime; +import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertDateTime; import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertToTimestamp; /** @@ -220,8 +220,7 @@ public class UnreadFragment extends BaseFragment { dateTime = dateTime.replace("", ""); dateTime = dateTime.replace("", ""); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP - && BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { + if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) { dateTime=convertDateTime(dateTime, false); String timestamp = convertToTimestamp(dateTime); if(timestamp!=null) diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java b/app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java index b5c5528e..0c7a3591 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java @@ -8,19 +8,7 @@ import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import static android.text.format.DateUtils.SECOND_IN_MILLIS; import static android.text.format.DateUtils.YEAR_IN_MILLIS; -public class DateTimeUtils { - //TODO: move this function to ThmmyDateTimeParser class once KitKat support is dropped - public static String convertDateTime(String dateTime, boolean removeSeconds){ - //Convert e.g. Today at 12:16:48 -> 12:16:48, but October 03, 2019, 16:40:18 remains as is - if (!dateTime.contains(",")) - dateTime = dateTime.replaceAll(".+? ([0-9])", "$1"); - - //Remove seconds - if(removeSeconds) - dateTime = dateTime.replaceAll("(.+?)(:[0-5][0-9])($|\\s)", "$1$3"); - - return dateTime; - } +class DateTimeUtils { private static final long MONTH_IN_MILLIS = 30*DAY_IN_MILLIS; private static final long DECADE_IN_MILLIS = 10*YEAR_IN_MILLIS; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java index e0f8da2b..4a7f58ed 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java @@ -1,8 +1,5 @@ package gr.thmmy.mthmmy.utils.parsing; -import android.os.Build; - -import androidx.annotation.RequiresApi; import androidx.annotation.VisibleForTesting; import org.joda.time.DateTime; @@ -21,7 +18,6 @@ import java.util.regex.Pattern; import gr.thmmy.mthmmy.base.BaseApplication; import timber.log.Timber; -@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class ThmmyDateTimeParser { private static final DateTimeParser[] parsers = { DateTimeFormat.forPattern("HH:mm:ss").getParser(), @@ -38,8 +34,9 @@ public class ThmmyDateTimeParser { .append(null, parsers) .toFormatter(); - private static final Locale greekLocale = Locale.forLanguageTag("el-GR"); - private static final Locale englishLocale = Locale.forLanguageTag("en-US"); + //TODO: Replace with Locale.forLanguageTag() (with "el-GR","en-US") when KitKat support is dropped + private static final Locale greekLocale = new Locale("el", "GR"); + private static final Locale englishLocale = new Locale("en", "US"); private static final Pattern pattern = Pattern.compile("\\s(1[3-9]|2[0-3]:)"); @@ -84,6 +81,18 @@ public class ThmmyDateTimeParser { return timestamp; } + public static String convertDateTime(String dateTime, boolean removeSeconds){ + //Convert e.g. Today at 12:16:48 -> 12:16:48, but October 03, 2019, 16:40:18 remains as is + if (!dateTime.contains(",")) + dateTime = dateTime.replaceAll(".+? ([0-9])", "$1"); + + //Remove seconds + if(removeSeconds) + dateTime = dateTime.replaceAll("(.+?)(:[0-5][0-9])($|\\s)", "$1$3"); + + return dateTime; + } + @VisibleForTesting private static DateTimeZone getDtz(){ if(!BaseApplication.getInstance().getSessionManager().isLoggedIn()) diff --git a/app/src/main/res/xml-v21/app_preferences_guest.xml b/app/src/main/res/xml-v21/app_preferences_guest.xml deleted file mode 100644 index 78c7c8ca..00000000 --- a/app/src/main/res/xml-v21/app_preferences_guest.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - diff --git a/app/src/main/res/xml-v21/app_preferences_user.xml b/app/src/main/res/xml-v21/app_preferences_user.xml deleted file mode 100644 index c132270f..00000000 --- a/app/src/main/res/xml-v21/app_preferences_user.xml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/xml/app_preferences_guest.xml b/app/src/main/res/xml/app_preferences_guest.xml index 30e86769..240f048d 100644 --- a/app/src/main/res/xml/app_preferences_guest.xml +++ b/app/src/main/res/xml/app_preferences_guest.xml @@ -14,6 +14,12 @@ android:title="@string/pref_title_app_main_default_tab" android:summary="@string/pref_summary_app_main_default_tab" app:iconSpaceReserved="false" /> + +