2222import androidx .preference .PreferenceFragmentCompat ;
2323
2424import com .evernote .android .state .State ;
25- import com .jakewharton .rxbinding4 .widget .RxTextView ;
2625import com .livefront .bridge .Bridge ;
2726
2827import org .schabi .newpipe .MainActivity ;
4342
4443import java .util .concurrent .TimeUnit ;
4544
45+ import io .reactivex .rxjava3 .disposables .Disposable ;
46+
47+ import com .jakewharton .rxbinding4 .widget .RxTextView ;
48+
4649/*
4750 * Created by Christian Schabesberger on 31.08.15.
4851 *
@@ -86,6 +89,8 @@ public class SettingsActivity extends AppCompatActivity implements
8689 @ State
8790 boolean wasSearchActive ;
8891
92+ private Disposable searchDisposable ;
93+
8994 @ Override
9095 protected void onCreate (final Bundle savedInstanceBundle ) {
9196 setTheme (ThemeHelper .getSettingsThemeStyle (this ));
@@ -111,9 +116,7 @@ protected void onCreate(final Bundle savedInstanceBundle) {
111116 }
112117 }
113118 } else {
114- getSupportFragmentManager ().beginTransaction ()
115- .replace (R .id .settings_fragment_holder , new MainSettingsFragment ())
116- .commit ();
119+ showMainSettingsFragment ();
117120 }
118121
119122 if (DeviceUtils .isTv (this )) {
@@ -189,8 +192,18 @@ private void showSettingsFragment(final Fragment fragment) {
189192 .commit ();
190193 }
191194
195+ private void showMainSettingsFragment () {
196+ getSupportFragmentManager ().beginTransaction ()
197+ .replace (R .id .settings_fragment_holder , new MainSettingsFragment ())
198+ .commit ();
199+ }
200+
201+
192202 @ Override
193203 protected void onDestroy () {
204+ if (searchDisposable != null && !searchDisposable .isDisposed ()) {
205+ searchDisposable .dispose ();
206+ }
194207 setMenuSearchItem (null );
195208 searchFragment = null ;
196209 super .onDestroy ();
@@ -205,16 +218,43 @@ private void initSearch(
205218 final SettingsLayoutBinding settingsLayoutBinding ,
206219 final boolean restored
207220 ) {
221+
208222 searchContainer =
209223 settingsLayoutBinding .settingsToolbarLayout .toolbar
210224 .findViewById (R .id .toolbar_search_container );
211225
212226 // Configure input field for search
213227 searchEditText = searchContainer .findViewById (R .id .toolbar_search_edit_text );
214- RxTextView .textChanges (searchEditText )
228+ searchDisposable = RxTextView .textChanges (searchEditText )
229+ // Ignore the initial empty state
230+ .skipInitialValue ()
215231 // Wait some time after the last input before actually searching
216232 .debounce (200 , TimeUnit .MILLISECONDS )
217- .subscribe (v -> runOnUiThread (this ::onSearchChanged ));
233+ .subscribe (charSequence -> runOnUiThread (() -> {
234+ // Change Fragment based on search text
235+ if (charSequence .length () > 0 ) {
236+ if (searchFragment != null ) {
237+ if (!searchFragment .isAdded ()) {
238+ getSupportFragmentManager ().beginTransaction ()
239+ .replace (R .id .settings_fragment_holder , searchFragment ,
240+ PreferenceSearchFragment .NAME )
241+ .addToBackStack (PreferenceSearchFragment .NAME )
242+ .commit ();
243+ } else {
244+ showSettingsFragment (searchFragment );
245+ }
246+ onSearchChanged ();
247+ }
248+ } else {
249+ final Fragment current = getSupportFragmentManager ()
250+ .findFragmentById (R .id .settings_fragment_holder );
251+ if (!(current instanceof MainSettingsFragment )) {
252+ getSupportFragmentManager ()
253+ .popBackStack (null , FragmentManager .POP_BACK_STACK_INCLUSIVE );
254+ showMainSettingsFragment ();
255+ }
256+ }
257+ }));
218258
219259 // Configure clear button
220260 searchContainer .findViewById (R .id .toolbar_search_clear )
@@ -294,25 +334,36 @@ public void setSearchActive(final boolean active) {
294334 Log .d (TAG , "setSearchActive called active=" + active );
295335 }
296336
337+ // Only reset the text when activating search from the toolbar
338+ if (active && !isSearchActive ()) {
339+ resetSearchText ();
340+ }
341+
297342 // Ignore if search is already in correct state
298343 if (isSearchActive () == active ) {
299344 return ;
300345 }
301346
302347 wasSearchActive = active ;
348+ if (wasSearchActive ) {
349+ searchEditText .post (() -> {
350+ searchEditText .requestFocus ();
351+ KeyboardUtil .showKeyboard (this , searchEditText );
352+ });
353+ }
303354
304355 searchContainer .setVisibility (active ? View .VISIBLE : View .GONE );
305356 if (menuSearchItem != null ) {
306357 menuSearchItem .setVisible (!active );
307358 }
308359
309- if (active ) {
310- getSupportFragmentManager ()
311- . beginTransaction ()
312- . add ( FRAGMENT_HOLDER_ID , searchFragment , PreferenceSearchFragment . NAME )
313- . addToBackStack ( PreferenceSearchFragment . NAME )
314- . commit ( );
315-
360+ if (active && searchText == null ) {
361+ showMainSettingsFragment ();
362+ } else if ( active ) {
363+ // Only add if not already added
364+ if (! searchFragment . isAdded ()) {
365+ showSettingsFragment ( searchFragment );
366+ }
316367 KeyboardUtil .showKeyboard (this , searchEditText );
317368 } else if (searchFragment != null ) {
318369 hideSearchFragment ();
@@ -323,8 +374,6 @@ public void setSearchActive(final boolean active) {
323374
324375 KeyboardUtil .hideKeyboard (this , searchEditText );
325376 }
326-
327- resetSearchText ();
328377 }
329378
330379 private void hideSearchFragment () {
@@ -388,5 +437,18 @@ public void onSearchResultClicked(@NonNull final PreferenceSearchItem result) {
388437 }
389438 }
390439
440+ // Functions needed for testing
441+ protected EditText getSearchEditText () {
442+ return searchEditText ;
443+ }
444+
445+ public void setMockEditText (final EditText editText ) {
446+ this .searchEditText = editText ;
447+ }
448+
449+ protected static int getFragmentHolderId () {
450+ return FRAGMENT_HOLDER_ID ;
451+ }
452+
391453 //endregion
392454}
0 commit comments