From ea875c59af7c7b9d629b9d5d9c2ae8605c1f5752 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 27 Oct 2022 14:10:04 +0200 Subject: [PATCH] Deduplicate isGridLayout calls --- .../fragments/list/BaseListFragment.java | 13 ++------- .../subscription/SubscriptionViewModel.kt | 28 +++---------------- .../org/schabi/newpipe/util/ThemeHelper.java | 2 +- 3 files changed, 7 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java index 9e7cb757c..633ba5d78 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java @@ -5,7 +5,6 @@ import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingSc import android.content.Context; import android.content.SharedPreferences; -import android.content.res.Configuration; import android.content.res.Resources; import android.os.Bundle; import android.util.Log; @@ -31,6 +30,7 @@ import org.schabi.newpipe.info_list.dialog.InfoItemDialog; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.OnClickGesture; import org.schabi.newpipe.util.StateSaver; +import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.views.SuperScrollLayoutManager; import java.util.List; @@ -476,15 +476,6 @@ public abstract class BaseListFragment extends BaseStateFragment } protected boolean isGridLayout() { - final String listMode = PreferenceManager.getDefaultSharedPreferences(activity) - .getString(getString(R.string.list_view_mode_key), - getString(R.string.list_view_mode_value)); - if ("auto".equals(listMode)) { - final Configuration configuration = getResources().getConfiguration(); - return configuration.orientation == Configuration.ORIENTATION_LANDSCAPE - && configuration.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE); - } else { - return "grid".equals(listMode); - } + return ThemeHelper.shouldUseGridLayout(activity); } } diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt index 814e697ce..cb14b33a6 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionViewModel.kt @@ -1,21 +1,19 @@ package org.schabi.newpipe.local.subscription import android.app.Application -import android.content.res.Configuration import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import androidx.preference.PreferenceManager import com.xwray.groupie.Group import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.processors.BehaviorProcessor import io.reactivex.rxjava3.schedulers.Schedulers -import org.schabi.newpipe.R import org.schabi.newpipe.local.feed.FeedDatabaseManager import org.schabi.newpipe.local.subscription.item.ChannelItem import org.schabi.newpipe.local.subscription.item.FeedGroupCardGridItem import org.schabi.newpipe.local.subscription.item.FeedGroupCardItem import org.schabi.newpipe.util.DEFAULT_THROTTLE_TIMEOUT +import org.schabi.newpipe.util.ThemeHelper import java.util.concurrent.TimeUnit class SubscriptionViewModel(application: Application) : AndroidViewModel(application) { @@ -23,7 +21,9 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica private var subscriptionManager = SubscriptionManager(application) // true -> list view, false -> grid view - private val listViewMode = BehaviorProcessor.createDefault(!isGridLayout(application)) + private val listViewMode = BehaviorProcessor.createDefault( + !ThemeHelper.shouldUseGridLayout(application) + ) private val listViewModeFlowable = listViewMode.distinctUntilChanged() private val mutableStateLiveData = MutableLiveData() @@ -74,24 +74,4 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica data class LoadedState(val subscriptions: List) : SubscriptionState() data class ErrorState(val error: Throwable? = null) : SubscriptionState() } - - companion object { - private fun isGridLayout(application: Application): Boolean { - val listMode = PreferenceManager.getDefaultSharedPreferences(application) - .getString( - application.getString(R.string.list_view_mode_key), - application.getString(R.string.list_view_mode_value) - ) - - return if ("auto" == listMode) { - val configuration: Configuration = application.resources.configuration - ( - configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && - configuration.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE) - ) - } else { - "grid" == listMode - } - } - } } diff --git a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java index 389af80ee..ea22e9368 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java @@ -349,7 +349,7 @@ public final class ThemeHelper { return false; } else if (listMode.equals(context.getString(R.string.list_view_mode_grid_key))) { return true; - } else { + } else /* listMode.equals("auto") */ { final Configuration configuration = context.getResources().getConfiguration(); return configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && configuration.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);