Browse Source

Relative time for KitKat

widgets
Ezerous 5 years ago
parent
commit
b55f165022
  1. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  2. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  3. 14
      app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java
  4. 21
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java
  5. 43
      app/src/main/res/xml-v21/app_preferences_guest.xml
  6. 66
      app/src/main/res/xml-v21/app_preferences_user.xml
  7. 6
      app/src/main/res/xml/app_preferences_guest.xml
  8. 6
      app/src/main/res/xml/app_preferences_user.xml
  9. 4
      app/src/test/java/gr/thmmy/mthmmy/utils/DateTimeUtilsTest.java

5
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 okhttp3.Response;
import timber.log.Timber; 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; import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertToTimestamp;
@ -192,8 +192,7 @@ public class RecentFragment extends BaseFragment {
matcher = pattern.matcher(dateTime); matcher = pattern.matcher(dateTime);
if (matcher.find()){ if (matcher.find()){
dateTime = matcher.group(1); dateTime = matcher.group(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) {
&& BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) {
dateTime=convertDateTime(dateTime, false); dateTime=convertDateTime(dateTime, false);
String timestamp = convertToTimestamp(dateTime); String timestamp = convertToTimestamp(dateTime);
if(timestamp!=null) if(timestamp!=null)

5
app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java

@ -38,7 +38,7 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber; 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; import static gr.thmmy.mthmmy.utils.parsing.ThmmyDateTimeParser.convertToTimestamp;
/** /**
@ -220,8 +220,7 @@ public class UnreadFragment extends BaseFragment {
dateTime = dateTime.replace("<b>", ""); dateTime = dateTime.replace("<b>", "");
dateTime = dateTime.replace("</b>", ""); dateTime = dateTime.replace("</b>", "");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP if (BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) {
&& BaseApplication.getInstance().isDisplayRelativeTimeEnabled()) {
dateTime=convertDateTime(dateTime, false); dateTime=convertDateTime(dateTime, false);
String timestamp = convertToTimestamp(dateTime); String timestamp = convertToTimestamp(dateTime);
if(timestamp!=null) if(timestamp!=null)

14
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.SECOND_IN_MILLIS;
import static android.text.format.DateUtils.YEAR_IN_MILLIS; import static android.text.format.DateUtils.YEAR_IN_MILLIS;
public class DateTimeUtils { 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;
}
private static final long MONTH_IN_MILLIS = 30*DAY_IN_MILLIS; private static final long MONTH_IN_MILLIS = 30*DAY_IN_MILLIS;
private static final long DECADE_IN_MILLIS = 10*YEAR_IN_MILLIS; private static final long DECADE_IN_MILLIS = 10*YEAR_IN_MILLIS;

21
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java

@ -1,8 +1,5 @@
package gr.thmmy.mthmmy.utils.parsing; package gr.thmmy.mthmmy.utils.parsing;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -21,7 +18,6 @@ import java.util.regex.Pattern;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import timber.log.Timber; import timber.log.Timber;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ThmmyDateTimeParser { public class ThmmyDateTimeParser {
private static final DateTimeParser[] parsers = { private static final DateTimeParser[] parsers = {
DateTimeFormat.forPattern("HH:mm:ss").getParser(), DateTimeFormat.forPattern("HH:mm:ss").getParser(),
@ -38,8 +34,9 @@ public class ThmmyDateTimeParser {
.append(null, parsers) .append(null, parsers)
.toFormatter(); .toFormatter();
private static final Locale greekLocale = Locale.forLanguageTag("el-GR"); //TODO: Replace with Locale.forLanguageTag() (with "el-GR","en-US") when KitKat support is dropped
private static final Locale englishLocale = Locale.forLanguageTag("en-US"); 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]:)"); private static final Pattern pattern = Pattern.compile("\\s(1[3-9]|2[0-3]:)");
@ -84,6 +81,18 @@ public class ThmmyDateTimeParser {
return timestamp; 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 @VisibleForTesting
private static DateTimeZone getDtz(){ private static DateTimeZone getDtz(){
if(!BaseApplication.getInstance().getSessionManager().isLoggedIn()) if(!BaseApplication.getInstance().getSessionManager().isLoggedIn())

43
app/src/main/res/xml-v21/app_preferences_guest.xml

@ -1,43 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.PreferenceCategory
android:title="@string/pref_category_app"
app:iconSpaceReserved="false">
<androidx.preference.ListPreference
android:defaultValue="0"
android:dialogTitle="@string/pref_app_main_default_tab_dialog_title"
android:entries="@array/pref_app_main_default_tab_entries"
android:entryValues="@array/pref_app_main_default_tab_values"
android:key="@string/pref_app_main_default_tab_key"
android:title="@string/pref_title_app_main_default_tab"
android:summary="@string/pref_summary_app_main_default_tab"
app:iconSpaceReserved="false" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_app_display_relative_time_key"
android:title="@string/pref_title_display_relative_time"
android:summary="@string/pref_summary_display_relative_time"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory>
<androidx.preference.PreferenceCategory
android:key="@string/pref_category_privacy_key"
android:title="@string/pref_category_privacy"
app:iconSpaceReserved="false">
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_privacy_crashlytics_enable_key"
android:title="@string/pref_title_privacy_crashlytics_enable"
android:summary="@string/pref_summary_privacy_crashlytics_enable"
app:iconSpaceReserved="false" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_privacy_analytics_enable_key"
android:title="@string/pref_title_privacy_analytics_enable"
android:summary="@string/pref_summary_privacy_analytics_enable"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>

66
app/src/main/res/xml-v21/app_preferences_user.xml

@ -1,66 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.PreferenceCategory
android:title="@string/pref_category_app"
app:iconSpaceReserved="false">
<androidx.preference.ListPreference
android:defaultValue="0"
android:dialogTitle="@string/pref_app_main_default_tab_dialog_title"
android:entries="@array/pref_app_main_default_tab_entries"
android:entryValues="@array/pref_app_main_default_tab_values"
android:key="@string/pref_app_main_default_tab_key"
android:title="@string/pref_title_app_main_default_tab"
android:summary="@string/pref_summary_app_main_default_tab"
app:iconSpaceReserved="false" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_app_display_relative_time_key"
android:title="@string/pref_title_display_relative_time"
android:summary="@string/pref_summary_display_relative_time"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory>
<androidx.preference.PreferenceCategory
android:key="@string/pref_category_posting_key"
android:title="@string/pref_category_posting"
app:iconSpaceReserved="false">
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="@string/pref_posting_app_signature_enable_key"
android:title="@string/pref_title_posting_app_signature_enable"
android:summary="@string/pref_summary_posting_app_signature_enable"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory>
<androidx.preference.PreferenceCategory
android:key="@string/pref_category_uploading_key"
android:title="@string/pref_category_uploading"
app:iconSpaceReserved="false">
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="@string/pref_uploading_app_signature_enable_key"
android:title="@string/pref_title_uploading_app_signature_enable"
android:summary="@string/pref_summary_uploading_app_signature_enable"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory>
<androidx.preference.PreferenceCategory
android:key="@string/pref_category_privacy_key"
android:title="@string/pref_category_privacy"
app:iconSpaceReserved="false">
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_privacy_crashlytics_enable_key"
android:title="@string/pref_title_privacy_crashlytics_enable"
android:summary="@string/pref_summary_privacy_crashlytics_enable"
app:iconSpaceReserved="false" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_privacy_analytics_enable_key"
android:title="@string/pref_title_privacy_analytics_enable"
android:summary="@string/pref_summary_privacy_analytics_enable"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>

6
app/src/main/res/xml/app_preferences_guest.xml

@ -14,6 +14,12 @@
android:title="@string/pref_title_app_main_default_tab" android:title="@string/pref_title_app_main_default_tab"
android:summary="@string/pref_summary_app_main_default_tab" android:summary="@string/pref_summary_app_main_default_tab"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_app_display_relative_time_key"
android:title="@string/pref_title_display_relative_time"
android:summary="@string/pref_summary_display_relative_time"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory> </androidx.preference.PreferenceCategory>
<androidx.preference.PreferenceCategory <androidx.preference.PreferenceCategory

6
app/src/main/res/xml/app_preferences_user.xml

@ -14,6 +14,12 @@
android:title="@string/pref_title_app_main_default_tab" android:title="@string/pref_title_app_main_default_tab"
android:summary="@string/pref_summary_app_main_default_tab" android:summary="@string/pref_summary_app_main_default_tab"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/pref_app_display_relative_time_key"
android:title="@string/pref_title_display_relative_time"
android:summary="@string/pref_summary_display_relative_time"
app:iconSpaceReserved="false" />
</androidx.preference.PreferenceCategory> </androidx.preference.PreferenceCategory>
<androidx.preference.PreferenceCategory <androidx.preference.PreferenceCategory

4
app/src/test/java/gr/thmmy/mthmmy/utils/DateTimeUtilsTest.java

@ -33,7 +33,7 @@ public class DateTimeUtilsTest {
"1h", "1h",
"1h 15m", "1h 15m",
"2h", "2h",
"3h 20m", "2h 20m",
"4h", "4h",
"20h", "20h",
"21h", "21h",
@ -66,7 +66,7 @@ public class DateTimeUtilsTest {
newDT().minusHours(1).minusMinutes(4).getMillis(), newDT().minusHours(1).minusMinutes(4).getMillis(),
newDT().minusHours(1).minusMinutes(15).getMillis(), newDT().minusHours(1).minusMinutes(15).getMillis(),
newDT().minusHours(2).minusMinutes(4).getMillis(), newDT().minusHours(2).minusMinutes(4).getMillis(),
newDT().minusHours(3).minusMinutes(20).getMillis(), newDT().minusHours(2).minusMinutes(20).getMillis(),
newDT().minusHours(3).minusMinutes(51).getMillis(), newDT().minusHours(3).minusMinutes(51).getMillis(),
newDT().minusHours(20).minusMinutes(10).getMillis(), newDT().minusHours(20).minusMinutes(10).getMillis(),
newDT().minusHours(20).minusMinutes(30).getMillis(), newDT().minusHours(20).minusMinutes(30).getMillis(),

Loading…
Cancel
Save