Apply layout ordering to subpages
This commit is contained in:
@@ -48,6 +48,7 @@ public final class LayoutFragment extends Fragment {
|
||||
SharedPreferences prefs = requireContext()
|
||||
.getSharedPreferences(SbtSettings.PREFS_NAME, Context.MODE_PRIVATE);
|
||||
hideUnlockedSubpageObsoleteControls(root);
|
||||
reorderItemCards(root, prefs);
|
||||
|
||||
SwitchMaterial enabledSwitch = root.findViewById(R.id.clock_enabled_switch);
|
||||
enabledSwitch.setChecked(prefs.getBoolean(
|
||||
@@ -274,7 +275,6 @@ public final class LayoutFragment extends Fragment {
|
||||
LinearLayout cardsParent = sourceCard != null && sourceCard.getParent() instanceof LinearLayout parent
|
||||
? parent
|
||||
: null;
|
||||
int sourceCardIndex = cardsParent != null ? cardsParent.indexOfChild(sourceCard) : -1;
|
||||
String copyTag = "layout-copy-" + countKey;
|
||||
|
||||
LinearLayout countContainer = new LinearLayout(requireContext());
|
||||
@@ -286,6 +286,7 @@ public final class LayoutFragment extends Fragment {
|
||||
countContainer.removeAllViews();
|
||||
removeGeneratedCopyCards(cardsParent, copyTag);
|
||||
int count = iconContainerCount(prefs, countKey, countDefault, legacyEnabledKey, legacyEnabledDefault);
|
||||
updateIconCardTitle(sourceCard, sectionTitleId, 0, count);
|
||||
countContainer.addView(iconContainerCountControl(
|
||||
prefs,
|
||||
countKey,
|
||||
@@ -296,6 +297,7 @@ public final class LayoutFragment extends Fragment {
|
||||
rebuild[0].run();
|
||||
}
|
||||
}));
|
||||
int sourceCardIndex = cardsParent != null ? cardsParent.indexOfChild(sourceCard) : -1;
|
||||
if (cardsParent == null || sourceCardIndex < 0) {
|
||||
return;
|
||||
}
|
||||
@@ -437,6 +439,71 @@ public final class LayoutFragment extends Fragment {
|
||||
return card;
|
||||
}
|
||||
|
||||
private void reorderItemCards(View root, SharedPreferences prefs) {
|
||||
View clock = root.findViewById(R.id.layout_clock_card);
|
||||
View chip = root.findViewById(R.id.layout_chip_card);
|
||||
View status = root.findViewById(R.id.layout_status_card);
|
||||
View notifications = root.findViewById(R.id.layout_notif_card);
|
||||
if (!(clock != null
|
||||
&& clock.getParent() instanceof LinearLayout parent
|
||||
&& chip != null
|
||||
&& status != null
|
||||
&& notifications != null)) {
|
||||
return;
|
||||
}
|
||||
int insertIndex = minIndex(parent, clock, chip, status, notifications);
|
||||
parent.removeView(clock);
|
||||
parent.removeView(chip);
|
||||
parent.removeView(status);
|
||||
parent.removeView(notifications);
|
||||
for (String key : LayoutOrderingUi.orderedKeys(prefs)) {
|
||||
View card = cardForOrderKey(key, clock, chip, status, notifications);
|
||||
if (card != null) {
|
||||
parent.addView(card, Math.min(insertIndex++, parent.getChildCount()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int minIndex(LinearLayout parent, View... views) {
|
||||
int result = parent.getChildCount();
|
||||
for (View view : views) {
|
||||
int index = parent.indexOfChild(view);
|
||||
if (index >= 0) {
|
||||
result = Math.min(result, index);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private View cardForOrderKey(String key, View clock, View chip, View status, View notifications) {
|
||||
if ("clock".equals(key)) {
|
||||
return clock;
|
||||
}
|
||||
if ("chip".equals(key)) {
|
||||
return chip;
|
||||
}
|
||||
if ("status".equals(key)) {
|
||||
return status;
|
||||
}
|
||||
if ("notifications".equals(key)) {
|
||||
return notifications;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void updateIconCardTitle(@Nullable MaterialCardView card, int titleId, int index, int count) {
|
||||
TextView title = findCardTitle(card);
|
||||
if (title == null) {
|
||||
return;
|
||||
}
|
||||
if (count > 1) {
|
||||
title.setText(getString(titleId) + " " + (index + 1));
|
||||
} else {
|
||||
title.setText(titleId);
|
||||
}
|
||||
}
|
||||
|
||||
private MaterialButton smallButton(String text) {
|
||||
MaterialButton button = new MaterialButton(requireContext(), null, com.google.android.material.R.attr.materialButtonOutlinedStyle);
|
||||
button.setText(text);
|
||||
|
||||
@@ -76,6 +76,13 @@ final class LayoutOrderingUi {
|
||||
return card(context, R.string.layout_ordering_title, content);
|
||||
}
|
||||
|
||||
static ArrayList<String> orderedKeys(SharedPreferences prefs) {
|
||||
String encoded = prefs != null
|
||||
? prefs.getString(SbtSettings.KEY_LAYOUT_ITEM_ORDER, SbtDefaults.LAYOUT_ITEM_ORDER_DEFAULT)
|
||||
: SbtDefaults.LAYOUT_ITEM_ORDER_DEFAULT;
|
||||
return orderedKeysFromEncoded(encoded);
|
||||
}
|
||||
|
||||
private static void bindOrderingList(Context context, SharedPreferences prefs, RecyclerView list) {
|
||||
ArrayList<OrderingItem> items = parseOrderingItems(
|
||||
context,
|
||||
@@ -126,18 +133,38 @@ final class LayoutOrderingUi {
|
||||
}
|
||||
|
||||
private static ArrayList<OrderingItem> parseOrderingItems(Context context, @Nullable String encoded) {
|
||||
ArrayList<OrderingItem> items = new ArrayList<>();
|
||||
for (String key : orderedKeysFromEncoded(encoded)) {
|
||||
addOrderingItemIfValid(context, items, key);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private static ArrayList<String> orderedKeysFromEncoded(@Nullable String encoded) {
|
||||
if (encoded == null || encoded.isEmpty() || "clock,notifications,status".equals(encoded)) {
|
||||
encoded = SbtDefaults.LAYOUT_ITEM_ORDER_DEFAULT;
|
||||
}
|
||||
ArrayList<OrderingItem> items = new ArrayList<>();
|
||||
ArrayList<String> keys = new ArrayList<>();
|
||||
for (String token : encoded.split(",")) {
|
||||
addOrderingItemIfValid(context, items, token != null ? token.trim() : "");
|
||||
addKeyIfValid(keys, token != null ? token.trim() : "");
|
||||
}
|
||||
addKeyIfValid(keys, "clock");
|
||||
addKeyIfValid(keys, "chip");
|
||||
addKeyIfValid(keys, "status");
|
||||
addKeyIfValid(keys, "notifications");
|
||||
return keys;
|
||||
}
|
||||
|
||||
private static void addKeyIfValid(ArrayList<String> keys, String key) {
|
||||
if (!"clock".equals(key)
|
||||
&& !"chip".equals(key)
|
||||
&& !"status".equals(key)
|
||||
&& !"notifications".equals(key)) {
|
||||
return;
|
||||
}
|
||||
if (!keys.contains(key)) {
|
||||
keys.add(key);
|
||||
}
|
||||
addOrderingItemIfValid(context, items, "clock");
|
||||
addOrderingItemIfValid(context, items, "chip");
|
||||
addOrderingItemIfValid(context, items, "status");
|
||||
addOrderingItemIfValid(context, items, "notifications");
|
||||
return items;
|
||||
}
|
||||
|
||||
private static void addOrderingItemIfValid(Context context, ArrayList<OrderingItem> items, String key) {
|
||||
|
||||
+27
-12
@@ -22,6 +22,8 @@ import androidx.fragment.app.Fragment;
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
import com.google.android.material.card.MaterialCardView;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import se.ajpanton.statusbartweak.R;
|
||||
import se.ajpanton.statusbartweak.shell.settings.SbtDefaults;
|
||||
import se.ajpanton.statusbartweak.shell.settings.SbtSettings;
|
||||
@@ -84,8 +86,9 @@ abstract class LockedSceneLayoutFragment extends Fragment {
|
||||
|| LockedNotificationModeUi.MODE_NONE.equals(mode)) {
|
||||
return;
|
||||
}
|
||||
LinkedHashMap<String, View> cardsByOrderKey = new LinkedHashMap<>();
|
||||
if (!aod) {
|
||||
View clockCard = configuredPositionCardFromLayout(
|
||||
cardsByOrderKey.put("clock", configuredPositionCardFromLayout(
|
||||
context,
|
||||
prefs,
|
||||
R.id.layout_clock_card,
|
||||
@@ -111,10 +114,9 @@ abstract class LockedSceneLayoutFragment extends Fragment {
|
||||
sceneKey("clock_middle_side"),
|
||||
SbtDefaults.CLOCK_MIDDLE_CUTOUT_SIDE_DEFAULT,
|
||||
sceneKey("clock_vertical_offset_px"),
|
||||
SbtDefaults.CLOCK_VERTICAL_OFFSET_PX_DEFAULT);
|
||||
dynamicContent.addView(clockCard);
|
||||
SbtDefaults.CLOCK_VERTICAL_OFFSET_PX_DEFAULT));
|
||||
}
|
||||
dynamicContent.addView(configuredIconContainersCardFromLayout(
|
||||
cardsByOrderKey.put("status", configuredIconContainersCardFromLayout(
|
||||
context,
|
||||
prefs,
|
||||
R.id.layout_status_card,
|
||||
@@ -140,9 +142,9 @@ abstract class LockedSceneLayoutFragment extends Fragment {
|
||||
SbtDefaults.LAYOUT_STATUS_MIDDLE_SIDE_DEFAULT,
|
||||
SbtDefaults.LAYOUT_STATUS_VERTICAL_OFFSET_PX_DEFAULT));
|
||||
if (LockedNotificationModeUi.MODE_CARDS.equals(mode)) {
|
||||
dynamicContent.addView(cardsNotificationCard(context, prefs));
|
||||
cardsByOrderKey.put("notifications", cardsNotificationCard(context, prefs));
|
||||
} else {
|
||||
dynamicContent.addView(configuredIconContainersCardFromLayout(
|
||||
cardsByOrderKey.put("notifications", configuredIconContainersCardFromLayout(
|
||||
context,
|
||||
prefs,
|
||||
R.id.layout_notif_card,
|
||||
@@ -168,6 +170,12 @@ abstract class LockedSceneLayoutFragment extends Fragment {
|
||||
SbtDefaults.LAYOUT_NOTIF_MIDDLE_SIDE_DEFAULT,
|
||||
SbtDefaults.LAYOUT_NOTIF_VERTICAL_OFFSET_PX_DEFAULT));
|
||||
}
|
||||
for (String key : LayoutOrderingUi.orderedKeys(prefs)) {
|
||||
View card = cardsByOrderKey.remove(key);
|
||||
if (card != null) {
|
||||
dynamicContent.addView(card);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private View configuredPositionCardFromLayout(
|
||||
@@ -373,12 +381,7 @@ abstract class LockedSceneLayoutFragment extends Fragment {
|
||||
iconContainerCountControl(context, prefs, countKey, legacyEnabledKey, count, onCountChanged),
|
||||
insertIndex);
|
||||
}
|
||||
if (index > 0) {
|
||||
TextView title = findCardTitle(card);
|
||||
if (title != null) {
|
||||
title.setText(getString(titleId) + " " + (index + 1));
|
||||
}
|
||||
}
|
||||
updateIconCardTitle(card, titleId, index, count);
|
||||
bindXmlPositionSection(
|
||||
card,
|
||||
prefs,
|
||||
@@ -409,6 +412,18 @@ abstract class LockedSceneLayoutFragment extends Fragment {
|
||||
return card;
|
||||
}
|
||||
|
||||
private void updateIconCardTitle(View card, int titleId, int index, int count) {
|
||||
TextView title = findCardTitle(card);
|
||||
if (title == null) {
|
||||
return;
|
||||
}
|
||||
if (count > 1) {
|
||||
title.setText(getString(titleId) + " " + (index + 1));
|
||||
} else {
|
||||
title.setText(titleId);
|
||||
}
|
||||
}
|
||||
|
||||
private View inflateLayoutCard(Context context, int cardId) {
|
||||
View fullLayout = LayoutInflater.from(context).inflate(R.layout.fragment_layout, dynamicContent, false);
|
||||
View card = fullLayout.findViewById(cardId);
|
||||
|
||||
Reference in New Issue
Block a user