mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-11-23 13:26:15 +03:00
#550 In-note-search doesn't jump to occurrence of searchstring
- Jump to last occurrence when previously selected occurrence was the first one - Hide search fabs, if only one occurrecen
This commit is contained in:
parent
01e01192d5
commit
e4059efaea
1 changed files with 66 additions and 26 deletions
|
@ -190,7 +190,8 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int occurrence = 1;
|
private int currentOccurrence = 1;
|
||||||
|
private int occurrenceCount = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPrepareOptionsMenu(@NonNull Menu menu) {
|
public void onPrepareOptionsMenu(@NonNull Menu menu) {
|
||||||
|
@ -215,9 +216,6 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
final LinearLayout searchEditFrame = searchView.findViewById(R.id
|
final LinearLayout searchEditFrame = searchView.findViewById(R.id
|
||||||
.search_edit_frame);
|
.search_edit_frame);
|
||||||
|
|
||||||
FloatingActionButton next = getSearchNextButton();
|
|
||||||
FloatingActionButton prev = getSearchPrevButton();
|
|
||||||
|
|
||||||
searchEditFrame.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
searchEditFrame.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||||
int oldVisibility = -1;
|
int oldVisibility = -1;
|
||||||
|
|
||||||
|
@ -229,19 +227,9 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
if (currentVisibility != View.VISIBLE) {
|
if (currentVisibility != View.VISIBLE) {
|
||||||
colorWithText("");
|
colorWithText("");
|
||||||
searchQuery = "";
|
searchQuery = "";
|
||||||
if (prev != null) {
|
hideSearchFabs();
|
||||||
prev.hide();
|
|
||||||
}
|
|
||||||
if (next != null) {
|
|
||||||
next.hide();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (prev != null) {
|
showSearchFabs();
|
||||||
prev.show();
|
|
||||||
}
|
|
||||||
if (next != null) {
|
|
||||||
next.show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oldVisibility = currentVisibility;
|
oldVisibility = currentVisibility;
|
||||||
|
@ -249,16 +237,20 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
FloatingActionButton next = getSearchNextButton();
|
||||||
|
FloatingActionButton prev = getSearchPrevButton();
|
||||||
|
|
||||||
if (next != null) {
|
if (next != null) {
|
||||||
next.setOnClickListener(v -> {
|
next.setOnClickListener(v -> {
|
||||||
occurrence++;
|
currentOccurrence++;
|
||||||
jumpToOccurrence();
|
jumpToOccurrence();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prev != null) {
|
if (prev != null) {
|
||||||
prev.setOnClickListener(v -> {
|
prev.setOnClickListener(v -> {
|
||||||
occurrence--;
|
currentOccurrence--;
|
||||||
jumpToOccurrence();
|
jumpToOccurrence();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -266,7 +258,7 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onQueryTextSubmit(String query) {
|
public boolean onQueryTextSubmit(String query) {
|
||||||
occurrence++;
|
currentOccurrence++;
|
||||||
jumpToOccurrence();
|
jumpToOccurrence();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -275,30 +267,59 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
public boolean onQueryTextChange(String newText) {
|
public boolean onQueryTextChange(String newText) {
|
||||||
searchQuery = newText;
|
searchQuery = newText;
|
||||||
colorWithText(newText);
|
colorWithText(newText);
|
||||||
occurrence = 1;
|
occurrenceCount = countOccurrences(getContent(), searchQuery);
|
||||||
|
if(occurrenceCount > 1) {
|
||||||
|
showSearchFabs();
|
||||||
|
} else {
|
||||||
|
hideSearchFabs();
|
||||||
|
}
|
||||||
|
currentOccurrence = 1;
|
||||||
jumpToOccurrence();
|
jumpToOccurrence();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void hideSearchFabs() {
|
||||||
|
FloatingActionButton next = getSearchNextButton();
|
||||||
|
FloatingActionButton prev = getSearchPrevButton();
|
||||||
|
if (prev != null) {
|
||||||
|
prev.hide();
|
||||||
|
}
|
||||||
|
if (next != null) {
|
||||||
|
next.hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showSearchFabs() {
|
||||||
|
FloatingActionButton next = getSearchNextButton();
|
||||||
|
FloatingActionButton prev = getSearchPrevButton();
|
||||||
|
if (prev != null) {
|
||||||
|
prev.show();
|
||||||
|
}
|
||||||
|
if (next != null) {
|
||||||
|
next.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void jumpToOccurrence() {
|
private void jumpToOccurrence() {
|
||||||
if (searchQuery == null || searchQuery.isEmpty()) {
|
if (searchQuery == null || searchQuery.isEmpty()) {
|
||||||
// No search term
|
// No search term
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (occurrence < 1) {
|
if (currentOccurrence < 1) {
|
||||||
// TODO find last occurrence
|
// if currentOccurrence is lower than 1, jump to last occurrence
|
||||||
occurrence = 1;
|
currentOccurrence = occurrenceCount;
|
||||||
|
jumpToOccurrence();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String currentContent = getContent().toLowerCase();
|
String currentContent = getContent().toLowerCase();
|
||||||
int indexOfNewText = indexOfNth(currentContent, searchQuery.toLowerCase(), 0, occurrence);
|
int indexOfNewText = indexOfNth(currentContent, searchQuery.toLowerCase(), 0, currentOccurrence);
|
||||||
if (indexOfNewText <= 0) {
|
if (indexOfNewText <= 0) {
|
||||||
// Search term is not n times in text
|
// Search term is not n times in text
|
||||||
// Go back to first search result
|
// Go back to first search result
|
||||||
if (occurrence != 1) {
|
if (currentOccurrence != 1) {
|
||||||
occurrence = 1;
|
currentOccurrence = 1;
|
||||||
jumpToOccurrence();
|
jumpToOccurrence();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -322,6 +343,25 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
|
||||||
return indexOfNth(input, value, idx + 1, --nth);
|
return indexOfNth(input, value, idx + 1, --nth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int countOccurrences(String haystack, String needle) {
|
||||||
|
if(haystack == null || haystack.isEmpty() || needle == null || needle.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
haystack = haystack.toLowerCase();
|
||||||
|
needle = needle.toLowerCase();
|
||||||
|
int lastIndex = 0;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
while (lastIndex != -1) {
|
||||||
|
lastIndex = haystack.indexOf(needle, lastIndex);
|
||||||
|
if (lastIndex != -1) {
|
||||||
|
count++;
|
||||||
|
lastIndex += needle.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract ScrollView getScrollView();
|
protected abstract ScrollView getScrollView();
|
||||||
|
|
||||||
protected abstract Layout getLayout();
|
protected abstract Layout getLayout();
|
||||||
|
|
Loading…
Reference in a new issue