-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Keep Main Fragment until user's input when searching in settings #12345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VougJo23
wants to merge
1
commit into
TeamNewPipe:dev
Choose a base branch
from
VougJo23:settingssearchfix
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−37
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
import androidx.preference.PreferenceFragmentCompat; | ||
|
||
import com.evernote.android.state.State; | ||
import com.jakewharton.rxbinding4.widget.RxTextView; | ||
import com.livefront.bridge.Bridge; | ||
|
||
import org.schabi.newpipe.MainActivity; | ||
|
@@ -43,6 +42,10 @@ | |
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.reactivex.rxjava3.disposables.Disposable; | ||
|
||
import com.jakewharton.rxbinding4.widget.RxTextView; | ||
|
||
/* | ||
* Created by Christian Schabesberger on 31.08.15. | ||
* | ||
|
@@ -86,6 +89,8 @@ public class SettingsActivity extends AppCompatActivity implements | |
@State | ||
boolean wasSearchActive; | ||
|
||
private Disposable searchDisposable; | ||
|
||
@Override | ||
protected void onCreate(final Bundle savedInstanceBundle) { | ||
setTheme(ThemeHelper.getSettingsThemeStyle(this)); | ||
|
@@ -111,9 +116,7 @@ protected void onCreate(final Bundle savedInstanceBundle) { | |
} | ||
} | ||
} else { | ||
getSupportFragmentManager().beginTransaction() | ||
.replace(R.id.settings_fragment_holder, new MainSettingsFragment()) | ||
.commit(); | ||
showMainSettingsFragment(); | ||
} | ||
|
||
if (DeviceUtils.isTv(this)) { | ||
|
@@ -189,8 +192,18 @@ private void showSettingsFragment(final Fragment fragment) { | |
.commit(); | ||
} | ||
|
||
private void showMainSettingsFragment() { | ||
getSupportFragmentManager().beginTransaction() | ||
.replace(R.id.settings_fragment_holder, new MainSettingsFragment()) | ||
.commit(); | ||
} | ||
|
||
|
||
@Override | ||
protected void onDestroy() { | ||
if (searchDisposable != null && !searchDisposable.isDisposed()) { | ||
searchDisposable.dispose(); | ||
} | ||
setMenuSearchItem(null); | ||
searchFragment = null; | ||
super.onDestroy(); | ||
|
@@ -205,16 +218,43 @@ private void initSearch( | |
final SettingsLayoutBinding settingsLayoutBinding, | ||
final boolean restored | ||
) { | ||
|
||
searchContainer = | ||
settingsLayoutBinding.settingsToolbarLayout.toolbar | ||
.findViewById(R.id.toolbar_search_container); | ||
|
||
// Configure input field for search | ||
searchEditText = searchContainer.findViewById(R.id.toolbar_search_edit_text); | ||
RxTextView.textChanges(searchEditText) | ||
searchDisposable = RxTextView.textChanges(searchEditText) | ||
// Ignore the initial empty state | ||
.skipInitialValue() | ||
// Wait some time after the last input before actually searching | ||
.debounce(200, TimeUnit.MILLISECONDS) | ||
.subscribe(v -> runOnUiThread(this::onSearchChanged)); | ||
.subscribe(charSequence -> runOnUiThread(() -> { | ||
// Change Fragment based on search text | ||
if (charSequence.length() > 0) { | ||
if (searchFragment != null) { | ||
if (!searchFragment.isAdded()) { | ||
getSupportFragmentManager().beginTransaction() | ||
.replace(R.id.settings_fragment_holder, searchFragment, | ||
PreferenceSearchFragment.NAME) | ||
.addToBackStack(PreferenceSearchFragment.NAME) | ||
.commit(); | ||
} else { | ||
showSettingsFragment(searchFragment); | ||
} | ||
onSearchChanged(); | ||
} | ||
} else { | ||
final Fragment current = getSupportFragmentManager() | ||
.findFragmentById(R.id.settings_fragment_holder); | ||
if (!(current instanceof MainSettingsFragment)) { | ||
getSupportFragmentManager() | ||
.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); | ||
showMainSettingsFragment(); | ||
} | ||
} | ||
})); | ||
|
||
// Configure clear button | ||
searchContainer.findViewById(R.id.toolbar_search_clear) | ||
|
@@ -294,25 +334,36 @@ public void setSearchActive(final boolean active) { | |
Log.d(TAG, "setSearchActive called active=" + active); | ||
} | ||
|
||
// Only reset the text when activating search from the toolbar | ||
if (active && !isSearchActive()) { | ||
resetSearchText(); | ||
} | ||
|
||
// Ignore if search is already in correct state | ||
if (isSearchActive() == active) { | ||
return; | ||
} | ||
|
||
wasSearchActive = active; | ||
if (wasSearchActive) { | ||
searchEditText.post(() -> { | ||
searchEditText.requestFocus(); | ||
KeyboardUtil.showKeyboard(this, searchEditText); | ||
}); | ||
} | ||
|
||
searchContainer.setVisibility(active ? View.VISIBLE : View.GONE); | ||
if (menuSearchItem != null) { | ||
menuSearchItem.setVisible(!active); | ||
} | ||
|
||
if (active) { | ||
getSupportFragmentManager() | ||
.beginTransaction() | ||
.add(FRAGMENT_HOLDER_ID, searchFragment, PreferenceSearchFragment.NAME) | ||
.addToBackStack(PreferenceSearchFragment.NAME) | ||
.commit(); | ||
|
||
if (active && searchText == null) { | ||
showMainSettingsFragment(); | ||
} else if (active) { | ||
// Only add if not already added | ||
if (!searchFragment.isAdded()) { | ||
showSettingsFragment(searchFragment); | ||
} | ||
KeyboardUtil.showKeyboard(this, searchEditText); | ||
} else if (searchFragment != null) { | ||
hideSearchFragment(); | ||
|
@@ -323,8 +374,6 @@ public void setSearchActive(final boolean active) { | |
|
||
KeyboardUtil.hideKeyboard(this, searchEditText); | ||
} | ||
|
||
resetSearchText(); | ||
} | ||
|
||
private void hideSearchFragment() { | ||
|
@@ -388,5 +437,18 @@ public void onSearchResultClicked(@NonNull final PreferenceSearchItem result) { | |
} | ||
} | ||
|
||
// Functions needed for testing | ||
protected EditText getSearchEditText() { | ||
return searchEditText; | ||
} | ||
|
||
public void setMockEditText(final EditText editText) { | ||
this.searchEditText = editText; | ||
} | ||
|
||
protected static int getFragmentHolderId() { | ||
return FRAGMENT_HOLDER_ID; | ||
} | ||
|
||
Comment on lines
+440
to
+452
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to mock these? |
||
//endregion | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/test/java/org/schabi/newpipe/settings/SettingsActivityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.schabi.newpipe.settings; | ||
|
||
import android.widget.EditText; | ||
|
||
import androidx.fragment.app.Fragment; | ||
import androidx.fragment.app.FragmentManager; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class SettingsActivityTest { | ||
|
||
private SettingsActivity activity; | ||
private FragmentManager fragmentManager; | ||
|
||
@Before | ||
public void setUp() { | ||
activity = Mockito.spy(new SettingsActivity()); | ||
fragmentManager = Mockito.mock(FragmentManager.class); | ||
Mockito.doReturn(fragmentManager).when(activity).getSupportFragmentManager(); | ||
// Mock the searchEditText | ||
final EditText editText = Mockito.mock(EditText.class); | ||
activity.setMockEditText(editText); | ||
} | ||
|
||
@Test | ||
public void mainSettingsFragmentDisplayedWhenSearchInactive() { | ||
when(fragmentManager.findFragmentById(anyInt())) | ||
.thenReturn(new MainSettingsFragment()); | ||
|
||
activity.setSearchActive(false); | ||
|
||
final Fragment fragment = activity.getSupportFragmentManager() | ||
.findFragmentById(SettingsActivity.getFragmentHolderId()); | ||
assertTrue(fragment instanceof MainSettingsFragment); | ||
} | ||
|
||
@Test | ||
public void preferenceSearchFragmentDisplayedWhenSearchActiveAndTextNotEmpty() { | ||
when(fragmentManager.findFragmentById(anyInt())) | ||
.thenReturn(new PreferenceSearchFragment()); | ||
|
||
activity.setSearchActive(true); | ||
Mockito.when(activity.getSearchEditText().getText()) | ||
.thenReturn(new android.text.SpannableStringBuilder("query")); | ||
|
||
final Fragment fragment = activity.getSupportFragmentManager() | ||
.findFragmentById(SettingsActivity.getFragmentHolderId()); | ||
assertTrue(fragment instanceof PreferenceSearchFragment); | ||
} | ||
|
||
@Test | ||
public void mainSettingsFragmentDisplayedWhenSearchActiveButTextEmpty() { | ||
when(fragmentManager.findFragmentById(anyInt())) | ||
.thenReturn(new MainSettingsFragment()); | ||
|
||
activity.setSearchActive(true); | ||
Mockito.when(activity.getSearchEditText().getText()) | ||
.thenReturn(new android.text.SpannableStringBuilder("")); | ||
|
||
final Fragment fragment = activity.getSupportFragmentManager() | ||
.findFragmentById(SettingsActivity.getFragmentHolderId()); | ||
assertTrue(fragment instanceof MainSettingsFragment); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes in this class should be reverted as they are caused by different indentation settings in the IDE