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
//////////////////////////////////////////////////////////////////////////*/

View File

@ -182,8 +182,9 @@
<string name="enable_lock_screen_video_thumbnail_key" translatable="false">enable_lock_screen_video_thumbnail</string>
<string name="feed_update_threshold_key" translatable="false">feed_update_threshold_key</string>
<string name="feed_update_threshold_default_value" translatable="false">5</string>
<string name="feed_update_threshold_default_value" translatable="false">300</string>
<!-- Values will be localized in runtime -->
<string-array name="feed_update_threshold_options" translatable="false">
<item>@string/feed_update_threshold_option_always_update</item>
<item>5 minutes</item>
@ -193,14 +194,15 @@
<item>12 hours</item>
<item>1 day</item>
</string-array>
<!-- Threshold values in seconds -->
<string-array name="feed_update_threshold_values" translatable="false">
<item>0</item>
<item>5</item>
<item>15</item>
<item>60</item>
<item>360</item>
<item>720</item>
<item>1440</item>
<item>300</item>
<item>900</item>
<item>3600</item>
<item>21600</item>
<item>43200</item>
<item>86400</item>
</string-array>
<string name="feed_use_dedicated_fetch_method_key" translatable="false">feed_use_dedicated_fetch_method</string>

View File

@ -598,6 +598,27 @@
<item quantity="other">%s seconds</item>
</plurals>
<!-- Time duration plurals -->
<plurals name="seconds">
<item quantity="one">%d second</item>
<item quantity="other">%d seconds</item>
</plurals>
<plurals name="minutes">
<item quantity="one">%d minute</item>
<item quantity="other">%d minutes</item>
</plurals>
<plurals name="hours">
<item quantity="one">%d hour</item>
<item quantity="other">%d hours</item>
</plurals>
<plurals name="days">
<item quantity="one">%d day</item>
<item quantity="other">%d days</item>
</plurals>
<!-- Feed -->
<string name="fragment_feed_title">What\'s New</string>
<string name="feed_groups_header_title">Feed groups</string>

View File

@ -94,7 +94,7 @@
android:layout="@layout/settings_category_header_layout"
android:title="@string/settings_category_feed_title">
<ListPreference
<org.schabi.newpipe.settings.custom.DurationListPreference
app:iconSpaceReserved="false"
android:key="@string/feed_update_threshold_key"
android:defaultValue="@string/feed_update_threshold_default_value"