ui scale caching fixes

This commit is contained in:
Yuriy Liskov
2025-06-04 22:09:56 +03:00
parent ff4aec3f77
commit 75018ac4e6
2 changed files with 65 additions and 30 deletions

View File

@@ -12,6 +12,7 @@ import androidx.annotation.NonNull;
import com.liskovsoft.sharedutils.helpers.FileHelpers;
import com.liskovsoft.sharedutils.helpers.MessageHelpers;
import com.liskovsoft.sharedutils.locale.LocaleUpdater;
import com.liskovsoft.sharedutils.misc.WeakHashSet;
import com.liskovsoft.sharedutils.mylogger.Log;
import com.liskovsoft.sharedutils.rx.RxHelper;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.AppDialogPresenter;
@@ -47,6 +48,7 @@ public class ViewManager {
private boolean mIsPlayerOnlyModeEnabled;
private long mPendingActivityMs;
private Class<?> mPendingActivityClass;
private WeakHashSet<Runnable> mOnFinish;
private ViewManager(Context context) {
mContext = context;
@@ -328,6 +330,7 @@ public class ViewManager {
BrowsePresenter.unhold();
AppUpdatePresenter.unhold();
MotherActivity.invalidate();
runOnFinish();
mIsMoveToBackEnabled = false;
}, 1_000);
}
@@ -487,4 +490,20 @@ public class ViewManager {
return null;
}
public void addOnFinish(Runnable onFinish) {
if (mOnFinish == null) {
mOnFinish = new WeakHashSet<>();
}
mOnFinish.add(onFinish);
}
private void runOnFinish() {
if (mOnFinish == null) {
return;
}
mOnFinish.forEach(Runnable::run);
}
}

View File

@@ -4,6 +4,8 @@ import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Pair;
import com.liskovsoft.smartyoutubetv2.common.app.views.ViewManager;
import com.liskovsoft.smartyoutubetv2.common.prefs.MainUIData;
import com.liskovsoft.smartyoutubetv2.tv.R;
@@ -11,8 +13,14 @@ import java.util.HashMap;
import java.util.Map;
public class GridFragmentHelper {
private static final Map<Integer, Integer> sCachedDimens = new HashMap<>();
private static int sCachedDisplayWidth = -1;
private static final Map<Integer, Pair<Integer, Integer>> sCardDimensPx = new HashMap<>();
private static final Map<Integer, Integer> sMaxColsNum = new HashMap<>();
private static final Runnable sInvalidate = GridFragmentHelper::invalidate;
private static void invalidate() {
sCardDimensPx.clear();
sMaxColsNum.clear();
}
/**
* Max number of cards that could fit horizontally
@@ -25,7 +33,19 @@ public class GridFragmentHelper {
* Max number of cards that could fit horizontally
*/
public static int getMaxColsNum(Context context, int cardWidthResId, float cardScale) {
return (int) getMaxColsNumFloat(context, cardWidthResId, cardScale);
Integer maxColsNum = sMaxColsNum.get(cardWidthResId);
if (maxColsNum != null) {
return maxColsNum;
}
ViewManager.instance(context).addOnFinish(sInvalidate);
maxColsNum = (int) getMaxColsNumFloat(context, cardWidthResId, cardScale);
sMaxColsNum.put(cardWidthResId, maxColsNum);
return maxColsNum;
}
private static float getMaxColsNumFloat(Context context, int cardWidthResId, float cardScale) {
@@ -33,18 +53,12 @@ public class GridFragmentHelper {
Resources res = context.getResources();
int displayWidthPx;
DisplayMetrics displayMetrics = res.getDisplayMetrics();
// Take into the account screen orientation (e.g. when running on phone)
int displayWidthPx = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
if (sCachedDisplayWidth == -1) {
DisplayMetrics displayMetrics = res.getDisplayMetrics();
// Take into the account screen orientation (e.g. when running on phone)
displayWidthPx = sCachedDisplayWidth = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
} else {
displayWidthPx = sCachedDisplayWidth;
}
float cardWidthPx = getDimensionPixelSize(res, cardWidthResId) * cardScale;
float cardSpacingPx = getDimensionPixelSize(res, R.dimen.grid_item_horizontal_spacing);
float cardWidthPx = res.getDimensionPixelSize(cardWidthResId) * cardScale;
float cardSpacingPx = res.getDimensionPixelSize(R.dimen.grid_item_horizontal_spacing);
// Get into consideration space from grid sides
return (displayWidthPx - displayWidthPx * 0.1f * uiScale) / (cardWidthPx + cardSpacingPx);
@@ -54,15 +68,28 @@ public class GridFragmentHelper {
return getCardDimensPx(context, cardWidthResId, cardHeightResId, cardScale, false);
}
public static Pair<Integer, Integer> getCardDimensPx(Context context, int cardWidthResId, int cardHeightResId, float cardScale, boolean isSingleColumn) {
Pair<Integer, Integer> cardDimensPx = sCardDimensPx.get(cardWidthResId);
if (cardDimensPx != null) {
return cardDimensPx;
}
cardDimensPx = getCardDimensPxInt(context, cardWidthResId, cardHeightResId, cardScale, isSingleColumn);
sCardDimensPx.put(cardWidthResId, cardDimensPx);
return cardDimensPx;
}
/**
* Calculate card dimension depending on card scale param<br/>
* Trying to not leave empty space (useful in grids).
*/
public static Pair<Integer, Integer> getCardDimensPx(Context context, int cardWidthResId, int cardHeightResId, float cardScale, boolean isSingleColumn) {
private static Pair<Integer, Integer> getCardDimensPxInt(Context context, int cardWidthResId, int cardHeightResId, float cardScale, boolean isSingleColumn) {
Resources res = context.getResources();
float cardWidthPx = getDimensionPixelSize(res, cardWidthResId) * cardScale;
float cardHeightPx = getDimensionPixelSize(res, cardHeightResId) * cardScale;
float cardWidthPx = res.getDimensionPixelSize(cardWidthResId) * cardScale;
float cardHeightPx = res.getDimensionPixelSize(cardHeightResId) * cardScale;
// Get into consideration the space from the grid sides
float colsNum = getMaxColsNumFloat(context, cardWidthResId, cardScale);
@@ -72,21 +99,10 @@ public class GridFragmentHelper {
float width = cardWidthPx + cardWidthPx * colsReminder;
float height = cardHeightPx + cardHeightPx * colsReminder;
if (isSingleColumn) { // single column grids have huge margin
width -= getDimensionPixelSize(res, R.dimen.grid_horizontal_margin);
if (isSingleColumn) { // single column grids have a huge margin
width -= res.getDimensionPixelSize(R.dimen.grid_horizontal_margin);
}
return new Pair<>((int) width, (int) height);
}
private static int getDimensionPixelSize(Resources res, int id) {
Integer size = sCachedDimens.get(id);
if (size == null) {
size = res.getDimensionPixelSize(id);
sCachedDimens.put(id, size);
}
return size;
}
}