Skip to content

Commit

Permalink
Deduplicate isGridLayout calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Oct 27, 2022
1 parent 83d16dc commit ea875c5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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;
Expand All @@ -31,6 +30,7 @@
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;
Expand Down Expand Up @@ -476,15 +476,6 @@ public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
}

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);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
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) {
private var feedDatabaseManager: FeedDatabaseManager = FeedDatabaseManager(application)
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<SubscriptionState>()
Expand Down Expand Up @@ -74,24 +74,4 @@ class SubscriptionViewModel(application: Application) : AndroidViewModel(applica
data class LoadedState(val subscriptions: List<Group>) : 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
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public static boolean shouldUseGridLayout(final Context context) {
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);
Expand Down

0 comments on commit ea875c5

Please sign in to comment.