Skip to content

Commit 7f755b0

Browse files
committed
refactor highlighting
1 parent db583c5 commit 7f755b0

File tree

4 files changed

+49
-147
lines changed

4 files changed

+49
-147
lines changed

extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/preference/CustomDialogListPreference.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class CustomDialogListPreference extends ListPreference {
3636
getResourceIdentifier("revanced_custom_list_item_checked", "layout");
3737

3838
private String staticSummary = null;
39-
private CharSequence[] originalEntries = null;
4039

4140
/**
4241
* Set a static summary that will not be overwritten by value changes.
@@ -64,29 +63,6 @@ public String getStaticSummary() {
6463
return staticSummary;
6564
}
6665

67-
/**
68-
* Override setEntries to store original entries for restoration.
69-
*/
70-
@Override
71-
public void setEntries(CharSequence[] entries) {
72-
// Store original entries only on first call.
73-
// This is necessary because during onPause, entries are cached and after resuming,
74-
// the search result highlight is retained in the entries.
75-
if (originalEntries == null && entries != null) {
76-
originalEntries = entries.clone();
77-
}
78-
super.setEntries(entries);
79-
}
80-
81-
/**
82-
* Restore original entries, clearing any search highlighting.
83-
*/
84-
public void restoreOriginalEntries() {
85-
if (originalEntries != null) {
86-
super.setEntries(originalEntries);
87-
}
88-
}
89-
9066
/**
9167
* Custom ArrayAdapter to handle checkmark visibility.
9268
*/

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/search/SearchResultItem.java

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import app.revanced.extension.shared.Utils;
2020
import app.revanced.extension.shared.settings.preference.ColorPickerPreference;
21-
import app.revanced.extension.shared.settings.preference.CustomDialogListPreference;
2221
import app.revanced.extension.youtube.settings.preference.UrlLinkPreference;
2322
import app.revanced.extension.youtube.sponsorblock.objects.SegmentCategoryListPreference;
2423

@@ -122,37 +121,41 @@ public static class PreferenceSearchItem extends SearchResultItem {
122121
final Preference preference;
123122
final String searchableText;
124123
final CharSequence originalTitle;
125-
CharSequence originalSummary;
126-
CharSequence originalSummaryOn;
127-
CharSequence originalSummaryOff;
128-
CharSequence[] originalEntries;
124+
final CharSequence originalSummary;
125+
final CharSequence originalSummaryOn;
126+
final CharSequence originalSummaryOff;
127+
final CharSequence[] originalEntries;
128+
CharSequence[] highlightedEntries;
129129

130130
@ColorInt
131131
private int color;
132132

133-
// Store last applied highlighting pattern to reapply when needed.
134-
Pattern lastQueryPattern;
135-
136133
PreferenceSearchItem(Preference pref, String navPath, List<String> navKeys) {
137134
super(navPath, navKeys, determineType(pref));
138135
this.preference = pref;
139136
this.originalTitle = pref.getTitle() != null ? pref.getTitle() : "";
140137
this.originalSummary = pref.getSummary();
141138
this.highlightedTitle = this.originalTitle;
142139
this.highlightedSummary = this.originalSummary != null ? this.originalSummary : "";
143-
this.originalSummaryOn = null;
144-
this.originalSummaryOff = null;
145-
this.originalEntries = null;
146140
this.color = 0;
147-
this.lastQueryPattern = null;
148141

149-
// Initialize type-specific fields.
150-
initTypeSpecificFields(pref);
142+
// Initialize type-specific fields and create immutable backups.
143+
FieldInitializationResult result = initTypeSpecificFields(pref);
144+
this.originalSummaryOn = result.summaryOn;
145+
this.originalSummaryOff = result.summaryOff;
146+
this.originalEntries = result.entries;
147+
this.highlightedEntries = result.entries != null ? result.entries.clone() : null;
151148

152149
// Build searchable text.
153150
this.searchableText = buildSearchableText(pref);
154151
}
155152

153+
private static class FieldInitializationResult {
154+
CharSequence summaryOn = null;
155+
CharSequence summaryOff = null;
156+
CharSequence[] entries = null;
157+
}
158+
156159
private static ViewType determineType(Preference pref) {
157160
if (pref instanceof SwitchPreference) return ViewType.SWITCH;
158161
if (pref instanceof ListPreference && !(pref instanceof SegmentCategoryListPreference)) return ViewType.LIST;
@@ -163,19 +166,20 @@ private static ViewType determineType(Preference pref) {
163166
return ViewType.REGULAR;
164167
}
165168

166-
private void initTypeSpecificFields(Preference pref) {
169+
private FieldInitializationResult initTypeSpecificFields(Preference pref) {
170+
FieldInitializationResult result = new FieldInitializationResult();
171+
167172
if (pref instanceof SwitchPreference switchPref) {
168-
this.originalSummaryOn = switchPref.getSummaryOn();
169-
this.originalSummaryOff = switchPref.getSummaryOff();
170-
} else if (pref instanceof ListPreference listPref && !(pref instanceof SegmentCategoryListPreference)) {
171-
this.originalEntries = listPref.getEntries();
173+
result.summaryOn = switchPref.getSummaryOn();
174+
result.summaryOff = switchPref.getSummaryOff();
172175
} else if (pref instanceof ColorPickerPreference colorPref) {
173176
String colorString = colorPref.getText();
174177
this.color = TextUtils.isEmpty(colorString) ? 0 : (Color.parseColor(colorString) | 0xFF000000);
175178
} else if (pref instanceof SegmentCategoryListPreference segmentPref) {
176-
this.originalEntries = segmentPref.getEntries();
177179
this.color = segmentPref.getColorWithOpacity();
178180
}
181+
182+
return result;
179183
}
180184

181185
private String buildSearchableText(Preference pref) {
@@ -194,16 +198,15 @@ private String buildSearchableText(Preference pref) {
194198
appendText(searchBuilder, originalSummary);
195199

196200
// Add type-specific searchable content.
197-
if (pref instanceof ListPreference listPref) {
198-
CharSequence[] entries = listPref.getEntries();
199-
if (entries != null) {
200-
for (CharSequence entry : entries) {
201+
if (pref instanceof ListPreference) {
202+
if (originalEntries != null) {
203+
for (CharSequence entry : originalEntries) {
201204
appendText(searchBuilder, entry);
202205
}
203206
}
204-
} else if (pref instanceof SwitchPreference switchPref) {
205-
appendText(searchBuilder, switchPref.getSummaryOn());
206-
appendText(searchBuilder, switchPref.getSummaryOff());
207+
} else if (pref instanceof SwitchPreference) {
208+
appendText(searchBuilder, originalSummaryOn);
209+
appendText(searchBuilder, originalSummaryOff);
207210
} else if (pref instanceof ColorPickerPreference) {
208211
appendText(searchBuilder, ColorPickerPreference.getColorString(color));
209212
}
@@ -228,10 +231,10 @@ public CharSequence getCurrentEffectiveSummary() {
228231
if (preference instanceof SwitchPreference switchPref) {
229232
boolean currentState = switchPref.isChecked();
230233
return currentState
231-
? (switchPref.getSummaryOn() != null ? switchPref.getSummaryOn() :
232-
switchPref.getSummary() != null ? switchPref.getSummary() : "")
233-
: (switchPref.getSummaryOff() != null ? switchPref.getSummaryOff() :
234-
switchPref.getSummary() != null ? switchPref.getSummary() : "");
234+
? (originalSummaryOn != null ? originalSummaryOn :
235+
originalSummary != null ? originalSummary : "")
236+
: (originalSummaryOff != null ? originalSummaryOff :
237+
originalSummary != null ? originalSummary : "");
235238
}
236239
return originalSummary != null ? originalSummary : "";
237240
}
@@ -246,35 +249,30 @@ boolean matchesQuery(String query) {
246249
}
247250

248251
/**
249-
* Highlights the search query in the title, summary, and entries.
250-
* Applies adjust background color span to matching text.
252+
* Highlights the search query in the title, summary and entries.
251253
*/
252254
@Override
253255
void applyHighlighting(Pattern queryPattern) {
254-
this.lastQueryPattern = queryPattern;
255-
256256
// Highlight the title.
257257
highlightedTitle = highlightSearchQuery(originalTitle, queryPattern);
258258

259259
// Get the current effective summary and highlight it.
260260
CharSequence currentSummary = getCurrentEffectiveSummary();
261261
highlightedSummary = highlightSearchQuery(currentSummary, queryPattern);
262262

263-
// Highlight the entries.
264-
if (preference instanceof CustomDialogListPreference listPref && originalEntries != null) {
265-
CharSequence[] highlightedEntries = new CharSequence[originalEntries.length];
263+
// Highlight entries.
264+
if (originalEntries != null) {
265+
highlightedEntries = new CharSequence[originalEntries.length];
266266
for (int i = 0; i < originalEntries.length; i++) {
267267
highlightedEntries[i] = highlightSearchQuery(originalEntries[i], queryPattern);
268268
}
269-
listPref.setEntries(highlightedEntries);
270269
}
271270

272271
highlightingApplied = true;
273272
}
274273

275274
/**
276-
* Clears all search query highlighting from the highlighted fields.
277-
* Restores original text for display purposes.
275+
* Clears all search query highlighting and restores original state completely.
278276
*/
279277
@Override
280278
void clearHighlighting() {
@@ -287,37 +285,15 @@ void clearHighlighting() {
287285
highlightedSummary = getCurrentEffectiveSummary();
288286

289287
// Restore original entries.
290-
if (preference instanceof CustomDialogListPreference listPref) {
291-
listPref.restoreOriginalEntries();
292-
}
293-
294-
highlightingApplied = false;
295-
lastQueryPattern = null;
296-
}
288+
if (originalEntries != null) {
289+
highlightedEntries = originalEntries.clone();
297290

298-
/**
299-
* Updates the original summary and reapplies highlighting if currently applied.
300-
*/
301-
void updateOriginalSummary(CharSequence newSummary) {
302-
this.originalSummary = newSummary;
303-
304-
// If highlighting was previously applied, reapply it to the new summary.
305-
if (highlightingApplied && lastQueryPattern != null) {
306-
highlightedSummary = highlightSearchQuery(newSummary != null ? newSummary : "", lastQueryPattern);
307-
} else {
308-
this.highlightedSummary = newSummary != null ? newSummary : "";
291+
if (preference instanceof ListPreference listPref) {
292+
listPref.setEntries(originalEntries);
293+
}
309294
}
310-
}
311295

312-
/**
313-
* Refreshes highlighting for dynamic summaries (like switch preferences).
314-
* Should be called when the preference state changes.
315-
*/
316-
public void refreshHighlighting() {
317-
if (highlightingApplied && lastQueryPattern != null) {
318-
CharSequence currentSummary = getCurrentEffectiveSummary();
319-
highlightedSummary = highlightSearchQuery(currentSummary, lastQueryPattern);
320-
}
296+
highlightingApplied = false;
321297
}
322298

323299
public void setColor(int newColor) {

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/search/SearchResultsAdapter.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private View createPreferenceView(SearchResultItem item, View convertView,
174174

175175
// Bind data to ViewHolder.
176176
switch (viewType) {
177-
case REGULAR, URL_LINK -> {
177+
case REGULAR, URL_LINK, LIST -> {
178178
RegularViewHolder regularHolder = (RegularViewHolder) holder;
179179
SearchResultItem.PreferenceSearchItem prefItem = (SearchResultItem.PreferenceSearchItem) item;
180180
regularHolder.titleView.setText(item.highlightedTitle);
@@ -184,17 +184,6 @@ private View createPreferenceView(SearchResultItem item, View convertView,
184184
() -> handlePreferenceClick(prefItem.preference),
185185
() -> navigateAndScrollToPreference(item));
186186
}
187-
case LIST -> {
188-
RegularViewHolder listHolder = (RegularViewHolder) holder;
189-
SearchResultItem.PreferenceSearchItem prefItem = (SearchResultItem.PreferenceSearchItem) item;
190-
prefItem.refreshHighlighting();
191-
listHolder.titleView.setText(item.highlightedTitle);
192-
listHolder.summaryView.setText(item.highlightedSummary);
193-
listHolder.summaryView.setVisibility(TextUtils.isEmpty(item.highlightedSummary) ? View.GONE : View.VISIBLE);
194-
setupPreferenceView(view, listHolder.titleView, listHolder.summaryView, prefItem.preference,
195-
() -> handlePreferenceClick(prefItem.preference),
196-
() -> navigateAndScrollToPreference(item));
197-
}
198187
case SWITCH -> {
199188
SwitchViewHolder switchHolder = (SwitchViewHolder) holder;
200189
SearchResultItem.PreferenceSearchItem prefItem = (SearchResultItem.PreferenceSearchItem) item;
@@ -207,18 +196,14 @@ private View createPreferenceView(SearchResultItem item, View convertView,
207196
switchHolder.switchWidget.setChecked(currentState);
208197
switchHolder.switchWidget.jumpDrawablesToCurrentState();
209198
}
210-
// Refresh and use highlighted summary.
211-
prefItem.refreshHighlighting(); // Ensure highlighting is updated for current state.
212199
switchHolder.summaryView.setText(prefItem.highlightedSummary);
213200
switchHolder.summaryView.setVisibility(TextUtils.isEmpty(prefItem.highlightedSummary) ? View.GONE : View.VISIBLE);
214201
setupPreferenceView(view, switchHolder.titleView, switchHolder.summaryView, switchPref,
215202
() -> {
216203
boolean newState = !switchPref.isChecked();
217204
switchPref.setChecked(newState);
218205
switchHolder.switchWidget.setChecked(newState);
219-
// Refresh and update highlighted summary after state change.
220-
prefItem.refreshHighlighting();
221-
switchHolder.summaryView.setText(prefItem.highlightedSummary);
206+
switchHolder.summaryView.setText(prefItem.getCurrentEffectiveSummary());
222207
switchHolder.summaryView.setVisibility(TextUtils.isEmpty(prefItem.highlightedSummary) ? View.GONE : View.VISIBLE);
223208
// Notify preference change.
224209
if (switchPref.getOnPreferenceChangeListener() != null) {

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/search/SearchViewController.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import app.revanced.extension.shared.settings.BaseSettings;
2828
import app.revanced.extension.shared.settings.Setting;
2929
import app.revanced.extension.shared.settings.preference.ColorPickerPreference;
30-
import app.revanced.extension.shared.settings.preference.CustomDialogListPreference;
3130
import app.revanced.extension.shared.settings.preference.NoTitlePreferenceCategory;
3231
import app.revanced.extension.youtube.settings.LicenseActivityHook;
3332
import app.revanced.extension.youtube.sponsorblock.objects.SegmentCategoryListPreference;
@@ -305,8 +304,7 @@ public void initializeSearchData() {
305304
}
306305

307306
/**
308-
* Sets up listeners for preferences (e.g., ColorPickerPreference, CustomDialogListPreference)
309-
* to keep search results in sync when preference values change.
307+
* Sets up listeners for preferences to keep search results in sync when preference values change.
310308
*/
311309
private void setupPreferenceListeners() {
312310
for (SearchResultItem item : allSearchItems) {
@@ -333,33 +331,6 @@ private void setupPreferenceListeners() {
333331
}
334332
return true;
335333
});
336-
} else if (pref instanceof CustomDialogListPreference listPref) {
337-
listPref.setOnPreferenceChangeListener((preference, newValue) -> {
338-
SearchResultItem.PreferenceSearchItem searchItem =
339-
(SearchResultItem.PreferenceSearchItem) keyToSearchItem.get(preference.getKey());
340-
if (searchItem == null) return true;
341-
342-
int index = listPref.findIndexOfValue(newValue.toString());
343-
if (index >= 0) {
344-
// Check if a static summary is set.
345-
boolean isStaticSummary = listPref.getStaticSummary() != null;
346-
347-
if (!isStaticSummary) {
348-
// Only update summary if it is not static.
349-
CharSequence newSummary = listPref.getEntries()[index];
350-
searchItem.updateOriginalSummary(newSummary);
351-
listPref.setSummary(newSummary);
352-
}
353-
354-
// Reapply highlighting to title and summary if previously applied.
355-
if (searchItem.highlightingApplied && searchItem.lastQueryPattern != null) {
356-
searchItem.applyHighlighting(searchItem.lastQueryPattern);
357-
}
358-
}
359-
360-
refreshSearchResults();
361-
return true;
362-
});
363334
}
364335
}
365336
}
@@ -564,15 +535,9 @@ public void closeSearch() {
564535
inputMethodManager.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
565536
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
566537

567-
// Clear highlighting for all search items and force restore original entries.
538+
// Clear highlighting for all search items.
568539
for (SearchResultItem item : allSearchItems) {
569540
item.clearHighlighting();
570-
571-
// Additional explicit restoration to handle cached entries.
572-
if (item instanceof SearchResultItem.PreferenceSearchItem prefItem
573-
&& prefItem.preference instanceof CustomDialogListPreference listPref) {
574-
listPref.restoreOriginalEntries();
575-
}
576541
}
577542

578543
searchResultsAdapter.notifyDataSetChanged();

0 commit comments

Comments
 (0)