18
18
19
19
import app .revanced .extension .shared .Utils ;
20
20
import app .revanced .extension .shared .settings .preference .ColorPickerPreference ;
21
- import app .revanced .extension .shared .settings .preference .CustomDialogListPreference ;
22
21
import app .revanced .extension .youtube .settings .preference .UrlLinkPreference ;
23
22
import app .revanced .extension .youtube .sponsorblock .objects .SegmentCategoryListPreference ;
24
23
@@ -122,37 +121,41 @@ public static class PreferenceSearchItem extends SearchResultItem {
122
121
final Preference preference ;
123
122
final String searchableText ;
124
123
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 ;
129
129
130
130
@ ColorInt
131
131
private int color ;
132
132
133
- // Store last applied highlighting pattern to reapply when needed.
134
- Pattern lastQueryPattern ;
135
-
136
133
PreferenceSearchItem (Preference pref , String navPath , List <String > navKeys ) {
137
134
super (navPath , navKeys , determineType (pref ));
138
135
this .preference = pref ;
139
136
this .originalTitle = pref .getTitle () != null ? pref .getTitle () : "" ;
140
137
this .originalSummary = pref .getSummary ();
141
138
this .highlightedTitle = this .originalTitle ;
142
139
this .highlightedSummary = this .originalSummary != null ? this .originalSummary : "" ;
143
- this .originalSummaryOn = null ;
144
- this .originalSummaryOff = null ;
145
- this .originalEntries = null ;
146
140
this .color = 0 ;
147
- this .lastQueryPattern = null ;
148
141
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 ;
151
148
152
149
// Build searchable text.
153
150
this .searchableText = buildSearchableText (pref );
154
151
}
155
152
153
+ private static class FieldInitializationResult {
154
+ CharSequence summaryOn = null ;
155
+ CharSequence summaryOff = null ;
156
+ CharSequence [] entries = null ;
157
+ }
158
+
156
159
private static ViewType determineType (Preference pref ) {
157
160
if (pref instanceof SwitchPreference ) return ViewType .SWITCH ;
158
161
if (pref instanceof ListPreference && !(pref instanceof SegmentCategoryListPreference )) return ViewType .LIST ;
@@ -163,19 +166,20 @@ private static ViewType determineType(Preference pref) {
163
166
return ViewType .REGULAR ;
164
167
}
165
168
166
- private void initTypeSpecificFields (Preference pref ) {
169
+ private FieldInitializationResult initTypeSpecificFields (Preference pref ) {
170
+ FieldInitializationResult result = new FieldInitializationResult ();
171
+
167
172
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 ();
172
175
} else if (pref instanceof ColorPickerPreference colorPref ) {
173
176
String colorString = colorPref .getText ();
174
177
this .color = TextUtils .isEmpty (colorString ) ? 0 : (Color .parseColor (colorString ) | 0xFF000000 );
175
178
} else if (pref instanceof SegmentCategoryListPreference segmentPref ) {
176
- this .originalEntries = segmentPref .getEntries ();
177
179
this .color = segmentPref .getColorWithOpacity ();
178
180
}
181
+
182
+ return result ;
179
183
}
180
184
181
185
private String buildSearchableText (Preference pref ) {
@@ -194,16 +198,15 @@ private String buildSearchableText(Preference pref) {
194
198
appendText (searchBuilder , originalSummary );
195
199
196
200
// 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 ) {
201
204
appendText (searchBuilder , entry );
202
205
}
203
206
}
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 );
207
210
} else if (pref instanceof ColorPickerPreference ) {
208
211
appendText (searchBuilder , ColorPickerPreference .getColorString (color ));
209
212
}
@@ -228,10 +231,10 @@ public CharSequence getCurrentEffectiveSummary() {
228
231
if (preference instanceof SwitchPreference switchPref ) {
229
232
boolean currentState = switchPref .isChecked ();
230
233
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 : "" );
235
238
}
236
239
return originalSummary != null ? originalSummary : "" ;
237
240
}
@@ -246,35 +249,30 @@ boolean matchesQuery(String query) {
246
249
}
247
250
248
251
/**
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.
251
253
*/
252
254
@ Override
253
255
void applyHighlighting (Pattern queryPattern ) {
254
- this .lastQueryPattern = queryPattern ;
255
-
256
256
// Highlight the title.
257
257
highlightedTitle = highlightSearchQuery (originalTitle , queryPattern );
258
258
259
259
// Get the current effective summary and highlight it.
260
260
CharSequence currentSummary = getCurrentEffectiveSummary ();
261
261
highlightedSummary = highlightSearchQuery (currentSummary , queryPattern );
262
262
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 ];
266
266
for (int i = 0 ; i < originalEntries .length ; i ++) {
267
267
highlightedEntries [i ] = highlightSearchQuery (originalEntries [i ], queryPattern );
268
268
}
269
- listPref .setEntries (highlightedEntries );
270
269
}
271
270
272
271
highlightingApplied = true ;
273
272
}
274
273
275
274
/**
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.
278
276
*/
279
277
@ Override
280
278
void clearHighlighting () {
@@ -287,37 +285,15 @@ void clearHighlighting() {
287
285
highlightedSummary = getCurrentEffectiveSummary ();
288
286
289
287
// 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 ();
297
290
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
+ }
309
294
}
310
- }
311
295
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 ;
321
297
}
322
298
323
299
public void setColor (int newColor ) {
0 commit comments