Localize duration strings used in feed settings using plurals

This commit is contained in:
Mauricio Colli 2020-03-05 23:20:55 -03:00
parent b62142db82
commit ac44ed0862
No known key found for this signature in database
GPG key ID: F200BFD6F29DDD85
7 changed files with 119 additions and 14 deletions

View file

@ -121,11 +121,11 @@ class FeedLoadService : Service() {
val useFeedExtractor = defaultSharedPreferences
.getBoolean(getString(R.string.feed_use_dedicated_fetch_method_key), false)
val thresholdOutdatedMinutesString = defaultSharedPreferences
val thresholdOutdatedSecondsString = defaultSharedPreferences
.getString(getString(R.string.feed_update_threshold_key), getString(R.string.feed_update_threshold_default_value))
val thresholdOutdatedMinutes = thresholdOutdatedMinutesString!!.toInt()
val thresholdOutdatedSeconds = thresholdOutdatedSecondsString!!.toInt()
startLoading(groupId, useFeedExtractor, thresholdOutdatedMinutes)
startLoading(groupId, useFeedExtractor, thresholdOutdatedSeconds)
return START_NOT_STICKY
}
@ -166,11 +166,11 @@ class FeedLoadService : Service() {
}
}
private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedMinutes: Int) {
private fun startLoading(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, useFeedExtractor: Boolean, thresholdOutdatedSeconds: Int) {
feedResultsHolder = ResultsHolder()
val outdatedThreshold = Calendar.getInstance().apply {
add(Calendar.MINUTE, -thresholdOutdatedMinutes)
add(Calendar.SECOND, -thresholdOutdatedSeconds)
}.time
val subscriptions = when (groupId) {

View file

@ -23,7 +23,7 @@ package org.schabi.newpipe.settings;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import androidx.preference.PreferenceManager;
import androidx.annotation.NonNull;
import org.schabi.newpipe.R;

View file

@ -0,0 +1,46 @@
package org.schabi.newpipe.settings.custom
import android.content.Context
import android.util.AttributeSet
import androidx.preference.ListPreference
import org.schabi.newpipe.util.Localization
/**
* An extension of a common ListPreference where it sets the duration values to human readable strings.
*
* The values in the entry values array will be interpreted as seconds. If the value of a specific position
* is less than or equals to zero, its original entry title will be used.
*
* If the entry values array have anything other than numbers in it, an exception will be raised.
*/
class DurationListPreference : ListPreference {
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?) : super(context)
override fun onAttached() {
super.onAttached()
val originalEntryTitles = entries
val originalEntryValues = entryValues
val newEntryTitles = arrayOfNulls<CharSequence>(originalEntryValues.size)
for (i in originalEntryValues.indices) {
val currentDurationValue: Int
try {
currentDurationValue = (originalEntryValues[i] as String).toInt()
} catch (e: NumberFormatException) {
throw RuntimeException("Invalid number was set in the preference entry values array", e)
}
if (currentDurationValue <= 0) {
newEntryTitles[i] = originalEntryTitles[i]
} else {
newEntryTitles[i] = Localization.localizeDuration(context, currentDurationValue)
}
}
entries = newEntryTitles
}
}

View file

@ -213,6 +213,42 @@ public class Localization {
return output;
}
/**
* Localize an amount of seconds into a human readable string.
*
* <p>The seconds will be converted to the closest whole time unit.
* <p>For example, 60 seconds would give "1 minute", 119 would also give "1 minute".
*
* @param context used to get plurals resources.
* @param durationInSecs an amount of seconds.
* @return duration in a human readable string.
*/
@NonNull
public static String localizeDuration(Context context, int durationInSecs) {
if (durationInSecs < 0) {
throw new IllegalArgumentException("duration can not be negative");
}
final int days = (int) (durationInSecs / (24 * 60 * 60L)); /* greater than a day */
durationInSecs %= (24 * 60 * 60L);
final int hours = (int) (durationInSecs / (60 * 60L)); /* greater than an hour */
durationInSecs %= (60 * 60L);
final int minutes = (int) (durationInSecs / 60L);
final int seconds = (int) (durationInSecs % 60L);
final Resources resources = context.getResources();
if (days > 0) {
return resources.getQuantityString(R.plurals.days, days, days);
} else if (hours > 0) {
return resources.getQuantityString(R.plurals.hours, hours, hours);
} else if (minutes > 0) {
return resources.getQuantityString(R.plurals.minutes, minutes, minutes);
} else {
return resources.getQuantityString(R.plurals.seconds, seconds, seconds);
}
}
/*//////////////////////////////////////////////////////////////////////////
// Pretty Time
//////////////////////////////////////////////////////////////////////////*/