From 734b6e2b678fe869eabdfd3020c872ddd974c096 Mon Sep 17 00:00:00 2001 From: TwoAi Date: Fri, 30 Aug 2024 12:23:51 -0400 Subject: [PATCH] Add null-safe SharedPreferences.getStringSafe Null-safe alternative to SharedPreferences.getString that guarantees the return value is non-null when defValue is non-null. --- .../java/org/schabi/newpipe/ktx/SharedPreferences.kt | 7 +++++++ .../newpipe/local/feed/service/FeedLoadManager.kt | 11 +++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/ktx/SharedPreferences.kt diff --git a/app/src/main/java/org/schabi/newpipe/ktx/SharedPreferences.kt b/app/src/main/java/org/schabi/newpipe/ktx/SharedPreferences.kt new file mode 100644 index 000000000..ff406fc91 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/ktx/SharedPreferences.kt @@ -0,0 +1,7 @@ +package org.schabi.newpipe.ktx + +import android.content.SharedPreferences + +fun SharedPreferences.getStringSafe(key: String, defValue: String): String { + return getString(key, null) ?: defValue +} diff --git a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt index 0b6a8068c..901ceadf7 100644 --- a/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt +++ b/app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt @@ -19,6 +19,7 @@ import org.schabi.newpipe.extractor.Info import org.schabi.newpipe.extractor.NewPipe import org.schabi.newpipe.extractor.feed.FeedInfo import org.schabi.newpipe.extractor.stream.StreamInfoItem +import org.schabi.newpipe.ktx.getStringSafe import org.schabi.newpipe.local.feed.FeedDatabaseManager import org.schabi.newpipe.local.subscription.SubscriptionManager import org.schabi.newpipe.util.ChannelTabHelper @@ -69,12 +70,10 @@ class FeedLoadManager(private val context: Context) { val outdatedThreshold = if (ignoreOutdatedThreshold) { OffsetDateTime.now(ZoneOffset.UTC) } else { - val thresholdOutdatedSeconds = ( - defaultSharedPreferences.getString( - context.getString(R.string.feed_update_threshold_key), - context.getString(R.string.feed_update_threshold_default_value) - ) ?: context.getString(R.string.feed_update_threshold_default_value) - ).toInt() + val thresholdOutdatedSeconds = defaultSharedPreferences.getStringSafe( + context.getString(R.string.feed_update_threshold_key), + context.getString(R.string.feed_update_threshold_default_value) + ).toInt() OffsetDateTime.now(ZoneOffset.UTC).minusSeconds(thresholdOutdatedSeconds.toLong()) }