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:
parent
6e546703a9
commit
b125ff702a
9 changed files with 75 additions and 14 deletions
|
|
@ -98,6 +98,7 @@ public class App extends Application {
|
|||
NewPipe.init(getDownloader(),
|
||||
Localization.getPreferredLocalization(this),
|
||||
Localization.getPreferredContentCountry(this));
|
||||
Localization.init();
|
||||
|
||||
StateSaver.init(this);
|
||||
initNotificationChannel();
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue