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

- Refactoring
This commit is contained in:
stefan-niedermann 2020-01-23 11:53:18 +01:00
parent 610eab570d
commit f13ff681b9
5 changed files with 49 additions and 34 deletions

View file

@ -127,11 +127,9 @@ public class EditNoteActivity extends AppCompatActivity implements BaseNoteFragm
if (fragment != null) {
savedState = getSupportFragmentManager().saveFragmentInstanceState(fragment);
}
if (edit) {
fragment = NoteEditFragment.newInstance(accountId, noteId);
} else {
fragment = NotePreviewFragment.newInstance(accountId, noteId);
}
fragment = edit
? NoteEditFragment.newInstance(accountId, noteId)
: NotePreviewFragment.newInstance(accountId, noteId);
if (savedState != null) {
fragment.setInitialSavedState(savedState);

View file

@ -9,12 +9,10 @@ import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -23,7 +21,6 @@ import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
@ -159,14 +156,6 @@ public abstract class BaseNoteFragment extends Fragment implements CategoryDialo
menu.findItem(R.id.menu_delete).setVisible(!isNew);
}
protected abstract ScrollView getScrollView();
protected abstract Layout getLayout();
protected abstract FloatingActionButton getSearchNextButton();
protected abstract FloatingActionButton 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

@ -9,6 +9,7 @@ import android.os.Looper;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.Layout;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
@ -19,9 +20,11 @@ import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.yydcdut.markdown.MarkdownEditText;
@ -34,6 +37,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.util.DisplayUtils;
import it.niedermann.owncloud.notes.util.ICallback;
import it.niedermann.owncloud.notes.util.MarkDownUtil;
import it.niedermann.owncloud.notes.util.NotesTextWatcher;
@ -147,8 +151,6 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
};
if (note != null) {
setActiveTextView(editContent);
if (note.getContent().isEmpty()) {
editContent.requestFocus();
@ -241,4 +243,13 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
}
});
}
@Override
protected void colorWithText(String newText) {
if (editContent != null && ViewCompat.isAttachedToWindow(editContent)) {
editContent.setText(DisplayUtils.searchAndColor(getContent(), new SpannableString
(getContent()), newText, getResources().getColor(R.color.primary)),
TextView.BufferType.SPANNABLE);
}
}
}

View file

@ -7,6 +7,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Layout;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
@ -15,10 +16,12 @@ import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
@ -34,6 +37,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.activity.EditNoteActivity;
import it.niedermann.owncloud.notes.model.LoginStatus;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.util.DisplayUtils;
import it.niedermann.owncloud.notes.util.ICallback;
import it.niedermann.owncloud.notes.util.MarkDownUtil;
import it.niedermann.owncloud.notes.util.NoteLinksUtils;
@ -163,7 +167,6 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment {
}
})
.build());
setActiveTextView(noteContent);
try {
noteContent.setText(markdownProcessor.parse(NoteLinksUtils.replaceNoteLinksWithDummyUrls(note.getContent(), db.getRemoteIds(note.getAccountId()))));
onResume();
@ -207,6 +210,15 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment {
}
}
@Override
protected void colorWithText(String newText) {
if (noteContent != null && ViewCompat.isAttachedToWindow(noteContent)) {
noteContent.setText(markdownProcessor.parse(DisplayUtils.searchAndColor(getContent(), new SpannableString
(getContent()), newText, getResources().getColor(R.color.primary))),
TextView.BufferType.SPANNABLE);
}
}
@Override
protected String getContent() {
return changedText;

View file

@ -1,28 +1,25 @@
package it.niedermann.owncloud.notes.android.fragment;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Layout;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.ViewCompat;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.util.DisplayUtils;
public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
private TextView activeTextView;
private int currentOccurrence = 1;
private int occurrenceCount = 0;
@ -36,6 +33,12 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
if (savedInstanceState != null) {
searchQuery = savedInstanceState.getString("searchQuery", "");
currentOccurrence = savedInstanceState.getInt("currentOccurrence", 1);
// TODO if search is open
if (searchView != null && !TextUtils.isEmpty(searchView.getQuery().toString())) {
colorWithText(searchQuery);
jumpToOccurrence();
}
}
}
@ -128,20 +131,19 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
if (searchView != null && !TextUtils.isEmpty(searchView.getQuery().toString())) {
outState.putString("searchQuery", searchView.getQuery().toString());
outState.putInt("currentOccurrence", currentOccurrence);
}
}
void setActiveTextView(TextView textView) {
activeTextView = textView;
}
protected abstract void colorWithText(String newText);
private void colorWithText(String newText) {
if (activeTextView != null && ViewCompat.isAttachedToWindow(activeTextView)) {
activeTextView.setText(DisplayUtils.searchAndColor(activeTextView.getText().toString(), new SpannableString
(activeTextView.getText()), newText, getResources().getColor(R.color.primary)),
TextView.BufferType.SPANNABLE);
}
}
protected abstract ScrollView getScrollView();
protected abstract Layout getLayout();
protected abstract FloatingActionButton getSearchNextButton();
protected abstract FloatingActionButton getSearchPrevButton();
private void showSearchFabs() {
FloatingActionButton next = getSearchNextButton();
@ -166,6 +168,9 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
}
private void jumpToOccurrence() {
if (getContent() == null || getContent().isEmpty()) {
return;
}
if (searchQuery == null || searchQuery.isEmpty()) {
// No search term
return;