#550 In-note-search doesn't jump to occurrence of searchstring

Added previous and next buttons (does not work yet properly)
This commit is contained in:
stefan-niedermann 2020-01-23 00:32:27 +01:00 committed by Niedermann IT-Dienstleistungen
parent d17acb928b
commit f5a2baaf8f
7 changed files with 157 additions and 36 deletions

View file

@ -188,6 +188,8 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
}
}
private int occurence = 1;
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
@ -230,6 +232,16 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
});
View next = getSearchNextButton();
if (next != null) {
next.setOnClickListener(v -> jumpToNthNote(searchQuery, occurence, true));
}
View prev = getSearchPrevButton();
if (prev != null) {
prev.setOnClickListener(v -> jumpToNthNote(searchQuery, occurence, false));
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
@ -238,24 +250,30 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
@Override
public boolean onQueryTextChange(String newText) {
occurence = 1;
searchQuery = newText;
colorWithText(newText);
jumpToFirstNote(newText);
jumpToNthNote(newText);
return true;
}
});
}
private void jumpToFirstNote(String newText) {
private void jumpToNthNote(String newText) {
jumpToNthNote(newText, 1, true);
}
private void jumpToNthNote(String newText, int occurrence, boolean directionForward) {
if (newText == null || newText.isEmpty()) {
// No search term
return;
}
String currentContent = getContent().toLowerCase();
int indexOfNewText = currentContent.indexOf(newText.toLowerCase());
int indexOfNewText = indexOfNth(currentContent, newText.toLowerCase(), 0, occurrence);
if (indexOfNewText <= 0) {
// Search term not in text
occurence = 1;
return;
}
String textUntilFirstOccurrence = currentContent.substring(0, indexOfNewText);
@ -267,16 +285,34 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
}
}
Layout textViewLayout = getLayout();
if (numberLine >= 0) {
getScrollView().smoothScrollTo(0, textViewLayout.getLineTop(numberLine));
getScrollView().smoothScrollTo(0, getLayout().getLineTop(numberLine));
}
if(directionForward) {
this.occurence++;
} else {
this.occurence--;
}
}
private static int indexOfNth(String input, String value, int startIndex, int nth) {
if (nth < 1)
throw new IllegalArgumentException("Param 'nth' must be greater than 0!");
if (nth == 1)
return input.indexOf(value, startIndex);
int idx = input.indexOf(value, startIndex);
if (idx == -1)
return -1;
return indexOfNth(input, value, idx + 1, --nth);
}
protected abstract ScrollView getScrollView();
protected abstract Layout getLayout();
protected abstract View getSearchNextButton();
protected abstract View getSearchPrevButton();
private void prepareFavoriteOption(MenuItem item) {
item.setIcon(note.isFavorite() ? R.drawable.ic_star_white_24dp : R.drawable.ic_star_border_white_24dp);
item.setChecked(note.isFavorite());

View file

@ -22,6 +22,7 @@ import android.widget.ScrollView;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.yydcdut.markdown.MarkdownEditText;
import com.yydcdut.markdown.MarkdownProcessor;
import com.yydcdut.markdown.syntax.edit.EditFactory;
@ -44,6 +45,12 @@ public class NoteEditFragment extends BaseNoteFragment {
private static final long DELAY = 2000; // Wait for this time after typing before saving
private static final long DELAY_AFTER_SYNC = 5000; // Wait for this time after saving before checking for next save
@BindView(R.id.searchNext)
FloatingActionButton searchNext;
@BindView(R.id.searchPrev)
FloatingActionButton searchPrev;
@BindView(R.id.scrollView)
ScrollView scrollView;
@ -105,6 +112,16 @@ public class NoteEditFragment extends BaseNoteFragment {
return editContent.getLayout();
}
@Override
protected View getSearchNextButton() {
return searchNext;
}
@Override
protected View getSearchPrevButton() {
return searchPrev;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

View file

@ -20,6 +20,7 @@ import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.yydcdut.markdown.MarkdownProcessor;
import com.yydcdut.markdown.MarkdownTextView;
import com.yydcdut.markdown.syntax.text.TextFactory;
@ -48,6 +49,12 @@ public class NotePreviewFragment extends BaseNoteFragment {
@BindView(R.id.editContentContainer)
ScrollView scrollView;
@BindView(R.id.searchNext)
FloatingActionButton searchNext;
@BindView(R.id.searchPrev)
FloatingActionButton searchPrev;
@BindView(R.id.single_note_content)
MarkdownTextView noteContent;
@ -72,6 +79,16 @@ public class NotePreviewFragment extends BaseNoteFragment {
return scrollView;
}
@Override
protected View getSearchNextButton() {
return searchNext;
}
@Override
protected View getSearchPrevButton() {
return searchPrev;
}
@Override
protected Layout getLayout() {
return noteContent.getLayout();

View file

@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M7.41,7.84L12,12.42l4.59,-4.58L18,9.25l-6,6 -6,-6z"/>
</vector>

View file

@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>

View file

@ -1,21 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">
android:layout_height="match_parent">
<com.yydcdut.markdown.MarkdownEditText
android:id="@+id/editContent"
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine|textCapSentences"
android:padding="16dp"
android:textColor="@color/fg_default" />
</ScrollView>
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">
<com.yydcdut.markdown.MarkdownEditText
android:id="@+id/editContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine|textCapSentences"
android:padding="16dp"
android:textColor="@color/fg_default" />
</ScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/searchPrev"
style="@style/fab"
android:layout_gravity="bottom|end"
android:translationY="-56dp"
app:fabSize="mini"
app:srcCompat="@drawable/ic_keyboard_arrow_up_white_24dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/searchNext"
style="@style/fab"
android:layout_gravity="bottom|end"
app:fabSize="mini"
app:srcCompat="@drawable/ic_keyboard_arrow_down_white_24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
@ -9,21 +9,41 @@
tools:context="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"
tools:ignore="MergeRootFrame">
<ScrollView
android:id="@+id/editContentContainer"
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">
android:layout_height="match_parent">
<com.yydcdut.markdown.MarkdownTextView
android:id="@+id/single_note_content"
<ScrollView
android:id="@+id/editContentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_normal"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/fg_default"
android:textIsSelectable="true" />
</ScrollView>
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">
<com.yydcdut.markdown.MarkdownTextView
android:id="@+id/single_note_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_normal"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/fg_default"
android:textIsSelectable="true" />
</ScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/searchPrev"
style="@style/fab"
android:layout_gravity="bottom|end"
android:translationY="-56dp"
app:fabSize="mini"
app:srcCompat="@drawable/ic_keyboard_arrow_up_white_24dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/searchNext"
style="@style/fab"
android:layout_gravity="bottom|end"
app:fabSize="mini"
app:srcCompat="@drawable/ic_keyboard_arrow_down_white_24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>