Show parsed relative times instead of whatever the service gives us

Before, the direct value was given to the user, now it uses the parsed
date so we can match the device's language.

To get the relative time from the parsed dates, we use the PrettyTime
library.

Also introduces a debug option to check the service's original value.
This commit is contained in:
Mauricio Colli 2019-10-28 01:20:06 -03:00
parent 6e546703a9
commit b125ff702a
No known key found for this signature in database
GPG Key ID: F200BFD6F29DDD85
9 changed files with 75 additions and 14 deletions

View File

@ -94,6 +94,7 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
implementation "androidx.room:room-runtime:${roomDbLibVersion}"
implementation "androidx.room:room-rxjava2:${roomDbLibVersion}"

View File

@ -18,6 +18,7 @@
-dontobfuscate
-keep class org.schabi.newpipe.extractor.timeago.patterns.** { *; }
-keep class org.ocpsoft.prettytime.i18n.** { *; }
-keep class org.mozilla.javascript.** { *; }

View File

@ -98,6 +98,7 @@ public class App extends Application {
NewPipe.init(getDownloader(),
Localization.getPreferredLocalization(this),
Localization.getPreferredContentCountry(this));
Localization.init();
StateSaver.init(this);
initNotificationChannel();

View File

@ -109,8 +109,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
}
if (item.getPublishedTime() != null) {
itemPublishedTime.setText(Localization
.formatDate(item.getPublishedTime().date().getTime()));
itemPublishedTime.setText(Localization.relativeTime(item.getPublishedTime().date()));
} else {
itemPublishedTime.setText(item.getTextualPublishedTime());
}

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe.info_list.holder;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.TextView;
@ -12,6 +13,8 @@ import org.schabi.newpipe.info_list.InfoItemBuilder;
import org.schabi.newpipe.local.history.HistoryRecordManager;
import org.schabi.newpipe.util.Localization;
import static org.schabi.newpipe.MainActivity.DEBUG;
/*
* Created by Christian Schabesberger on 01.08.16.
* <p>
@ -62,13 +65,30 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder {
viewsAndDate = Localization.shortViewCount(itemBuilder.getContext(), infoItem.getViewCount());
}
}
if (!TextUtils.isEmpty(infoItem.getTextualUploadDate())) {
final String uploadDate = getFormattedRelativeUploadDate(infoItem);
if (!TextUtils.isEmpty(uploadDate)) {
if (viewsAndDate.isEmpty()) {
viewsAndDate = infoItem.getTextualUploadDate();
} else {
viewsAndDate += "" + infoItem.getTextualUploadDate();
return uploadDate;
}
return Localization.concatenateStrings(viewsAndDate, uploadDate);
}
return viewsAndDate;
}
private String getFormattedRelativeUploadDate(final StreamInfoItem infoItem) {
if (infoItem.getUploadDate() != null) {
String formattedRelativeTime = Localization.relativeTime(infoItem.getUploadDate().date());
if (DEBUG && PreferenceManager.getDefaultSharedPreferences(itemBuilder.getContext())
.getBoolean(itemBuilder.getContext().getString(R.string.show_original_time_ago_key), false)) {
formattedRelativeTime += " (" + infoItem.getTextualUploadDate() + ")";
}
return formattedRelativeTime;
} else {
return infoItem.getTextualUploadDate();
}
}
}

View File

@ -2,25 +2,26 @@ package org.schabi.newpipe.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import androidx.annotation.PluralsRes;
import androidx.annotation.StringRes;
import android.text.TextUtils;
import org.ocpsoft.prettytime.PrettyTime;
import org.ocpsoft.prettytime.units.Decade;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.annotation.PluralsRes;
import androidx.annotation.StringRes;
/*
* Created by chschtsch on 12/29/15.
*
@ -43,11 +44,16 @@ import java.util.Locale;
public class Localization {
public final static String DOT_SEPARATOR = "";
private static PrettyTime prettyTime;
private static final String DOT_SEPARATOR = "";
private Localization() {
}
public static void init() {
initPrettyTime();
}
@NonNull
public static String concatenateStrings(final String... strings) {
return concatenateStrings(Arrays.asList(strings));
@ -188,4 +194,26 @@ public class Localization {
}
return output;
}
/*//////////////////////////////////////////////////////////////////////////
// Pretty Time
//////////////////////////////////////////////////////////////////////////*/
private static void initPrettyTime() {
prettyTime = new PrettyTime(Locale.getDefault());
// Do not use decades as YouTube doesn't either.
prettyTime.removeUnit(Decade.class);
}
private static PrettyTime getPrettyTime() {
// If pretty time's Locale is different, init again with the new one.
if (!prettyTime.getLocale().equals(Locale.getDefault())) {
initPrettyTime();
}
return prettyTime;
}
public static String relativeTime(Calendar calendarTime) {
return getPrettyTime().formatUnrounded(calendarTime);
}
}

View File

@ -111,8 +111,8 @@
<!-- DEBUG ONLY -->
<string name="debug_pref_screen_key" translatable="false">debug_pref_screen_key</string>
<string name="allow_heap_dumping_key" translatable="false">allow_heap_dumping_key</string>
<string name="allow_disposed_exceptions_key" translatable="false">allow_disposed_exceptions_key</string>
<string name="show_original_time_ago_key" translatable="false">show_original_time_ago_text_key</string>
<!-- THEMES -->
<string name="theme_key" translatable="false">theme</string>

View File

@ -458,6 +458,10 @@
<string name="enable_leak_canary_summary">Memory leak monitoring may cause the app to become unresponsive when heap dumping</string>
<string name="enable_disposed_exceptions_title">Report out-of-lifecycle errors</string>
<string name="enable_disposed_exceptions_summary">Force reporting of undeliverable Rx exceptions outside of fragment or activity lifecycle after disposal</string>
<string name="show_original_time_ago_title" translatable="false">Show original time ago on items</string>
<string name="show_original_time_ago_summary" translatable="false">Original texts from services will be visible in stream items</string>
<!-- Subscriptions import/export -->
<string name="import_export_title">Import/export</string>
<string name="import_title">Import</string>

View File

@ -18,4 +18,11 @@
android:key="@string/allow_disposed_exceptions_key"
android:title="@string/enable_disposed_exceptions_title"
android:summary="@string/enable_disposed_exceptions_summary"/>
<SwitchPreference
app:iconSpaceReserved="false"
android:defaultValue="false"
android:key="@string/show_original_time_ago_key"
android:title="@string/show_original_time_ago_title"
android:summary="@string/show_original_time_ago_summary"/>
</PreferenceScreen>