From 0ed8e5a2988c07f3dfbd5dd4e9ef8ed53378fbbe Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 16 Dec 2023 15:22:59 +0400 Subject: [PATCH] fix(YouTube - SponsorBlock): Export local statistics with saved settings (#542) --- .../integrations/settings/SettingsEnum.java | 2 -- .../SponsorBlockSettingsFragment.java | 11 +++----- .../sponsorblock/SponsorBlockUtils.java | 26 +++++++++++++------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java index 154c0c4b..9b4e7c32 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java @@ -597,8 +597,6 @@ public enum SettingsEnum { case SB_LAST_VIP_CHECK: case SB_HIDE_EXPORT_WARNING: case SB_SEEN_GUIDELINES: - case SB_LOCAL_TIME_SAVED_NUMBER_SEGMENTS: - case SB_LOCAL_TIME_SAVED_MILLISECONDS: return false; } return true; diff --git a/app/src/main/java/app/revanced/integrations/settingsmenu/SponsorBlockSettingsFragment.java b/app/src/main/java/app/revanced/integrations/settingsmenu/SponsorBlockSettingsFragment.java index e25ae7a5..2257f82d 100644 --- a/app/src/main/java/app/revanced/integrations/settingsmenu/SponsorBlockSettingsFragment.java +++ b/app/src/main/java/app/revanced/integrations/settingsmenu/SponsorBlockSettingsFragment.java @@ -26,8 +26,6 @@ import android.widget.EditText; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.text.DecimalFormat; - import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.settings.SharedPrefCategory; import app.revanced.integrations.sponsorblock.SegmentPlaybackController; @@ -471,8 +469,6 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment { } } - private static final DecimalFormat statsNumberOfSegmentsSkippedFormatter = new DecimalFormat("#,###,###"); - private void addUserStats(@NonNull Preference loadingPlaceholder, @Nullable UserStats stats) { ReVancedUtils.verifyOnMainThread(); try { @@ -514,7 +510,7 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment { // number of segment submissions (does not include ignored segments) Preference preference = new Preference(context); statsCategory.addPreference(preference); - String formatted = statsNumberOfSegmentsSkippedFormatter.format(stats.segmentCount); + String formatted = SponsorBlockUtils.getNumberOfSkipsString(stats.segmentCount); preference.setTitle(fromHtml(str("sb_stats_submissions", formatted))); if (stats.totalSegmentCountIncludingIgnored == 0) { preference.setSelectable(false); @@ -550,7 +546,8 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment { stats_saved = str("sb_stats_saved_zero"); stats_saved_sum = str("sb_stats_saved_sum_zero"); } else { - stats_saved = str("sb_stats_saved", statsNumberOfSegmentsSkippedFormatter.format(stats.viewCount)); + stats_saved = str("sb_stats_saved", + SponsorBlockUtils.getNumberOfSkipsString(stats.viewCount)); stats_saved_sum = str("sb_stats_saved_sum", SponsorBlockUtils.getTimeSavedString((long) (60 * stats.minutesSaved))); } preference.setTitle(fromHtml(stats_saved)); @@ -573,7 +570,7 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment { statsCategory.addPreference(preference); Runnable updateStatsSelfSaved = () -> { - String formatted = statsNumberOfSegmentsSkippedFormatter.format(SettingsEnum.SB_LOCAL_TIME_SAVED_NUMBER_SEGMENTS.getInt()); + String formatted = SponsorBlockUtils.getNumberOfSkipsString(SettingsEnum.SB_LOCAL_TIME_SAVED_NUMBER_SEGMENTS.getInt()); preference.setTitle(fromHtml(str("sb_stats_self_saved", formatted))); String formattedSaved = SponsorBlockUtils.getTimeSavedString(SettingsEnum.SB_LOCAL_TIME_SAVED_MILLISECONDS.getLong() / 1000); preference.setSummary(fromHtml(str("sb_stats_self_saved_sum", formattedSaved))); diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockUtils.java b/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockUtils.java index 37020fce..ef30eb24 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockUtils.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockUtils.java @@ -12,6 +12,7 @@ import android.widget.EditText; import androidx.annotation.NonNull; import java.lang.ref.WeakReference; +import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Duration; @@ -39,6 +40,7 @@ public class SponsorBlockUtils { private static final SimpleDateFormat manualEditTimeFormatter = new SimpleDateFormat(MANUAL_EDIT_TIME_FORMAT); @SuppressLint("SimpleDateFormat") private static final SimpleDateFormat voteSegmentTimeFormatter = new SimpleDateFormat(); + private static final NumberFormat statsNumberFormatter = NumberFormat.getNumberInstance(); static { TimeZone utc = TimeZone.getTimeZone("UTC"); manualEditTimeFormatter.setTimeZone(utc); @@ -402,19 +404,27 @@ public class SponsorBlockUtils { } } + public static String getNumberOfSkipsString(int viewCount) { + return statsNumberFormatter.format(viewCount); + } + public static String getTimeSavedString(long totalSecondsSaved) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { Duration duration = Duration.ofSeconds(totalSecondsSaved); - final long hoursSaved = duration.toHours(); - final long minutesSaved = duration.toMinutes() % 60; - if (hoursSaved > 0) { - return str("sb_stats_saved_hour_format", hoursSaved, minutesSaved); + final long hours = duration.toHours(); + final long minutes = duration.toMinutes() % 60; + // Format all numbers so non-western numbers use a consistent appearance. + String minutesFormatted = statsNumberFormatter.format(minutes); + if (hours > 0) { + String hoursFormatted = statsNumberFormatter.format(hours); + return str("sb_stats_saved_hour_format", hoursFormatted, minutesFormatted); } - final long secondsSaved = duration.getSeconds() % 60; - if (minutesSaved > 0) { - return str("sb_stats_saved_minute_format", minutesSaved, secondsSaved); + final long seconds = duration.getSeconds() % 60; + String secondsFormatted = statsNumberFormatter.format(seconds); + if (minutes > 0) { + return str("sb_stats_saved_minute_format", minutesFormatted, secondsFormatted); } - return str("sb_stats_saved_second_format", secondsSaved); + return str("sb_stats_saved_second_format", secondsFormatted); } return "error"; // will never be reached. YouTube requires Android O or greater }