From 8b20ccde0b39b25c51e575a4d99d49bfb6a72a05 Mon Sep 17 00:00:00 2001 From: Pavol Franek <> Date: Tue, 24 Mar 2020 08:46:59 +0100 Subject: [PATCH] searching for strings in a given note, then highlighting and looping through the string occurrences with some arrows. --- .../notes/pro/activities/MainActivity.kt | 33 ++++++++++++++++--- .../notes/pro/adapters/NotesPagerAdapter.kt | 2 ++ .../notes/pro/fragments/TextFragment.kt | 8 +++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index 40882617..bcf1e712 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.notes.pro.activities import android.content.Intent import android.net.Uri import android.os.Bundle +import android.text.Editable import android.text.Spannable import android.text.SpannableString import android.text.method.ArrowKeyMovementMethod @@ -30,6 +31,7 @@ import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter import com.simplemobiletools.notes.pro.databases.NotesDatabase import com.simplemobiletools.notes.pro.dialogs.* import com.simplemobiletools.notes.pro.extensions.* +import com.simplemobiletools.notes.pro.fragments.TextFragment import com.simplemobiletools.notes.pro.helpers.MIME_TEXT_PLAIN import com.simplemobiletools.notes.pro.helpers.NoteType import com.simplemobiletools.notes.pro.helpers.NotesHelper @@ -84,12 +86,15 @@ class MainActivity : SimpleActivity() { private fun searchListeners() { search_query.onTextChangeListener { query -> currentNotesView()?.let { noteView -> - noteView.setText(noteView.value) + currentTextFragment?.removeTextWatcher() + searchClearSpans(noteView.text) if (query.isNotBlank() && query.length > 1) { searchMatches = searchMatches(query, noteView.value) searchHighLightText(noteView, query) } + + currentTextFragment?.setTextWatcher() } } @@ -127,7 +132,26 @@ class MainActivity : SimpleActivity() { } view_pager.onPageChangeListener { - if (searchIsActive) searchHide() + currentTextFragment?.removeTextWatcher() + currentNotesView()?.let { noteView -> + searchClearSpans(noteView.text) + } + + search_query.text?.clear() + searchHide() + + currentTextFragment?.setTextWatcher() + } + } + + private val currentTextFragment: TextFragment? get() = mAdapter?.textFragment(view_pager.currentItem) + + private fun searchClearSpans(editable: Editable) { + val spans = editable.getSpans(0, editable.length, Any::class.java) + for (span in spans) { + if (span is BackgroundColorSpan) { + editable.removeSpan(span) + } } } @@ -135,10 +159,8 @@ class MainActivity : SimpleActivity() { if (searchMatches.isNotEmpty()) { noteView.requestFocus() noteView.setSelection(searchMatches.getOrNull(searchIndex) ?: 0) - } else { + } else hideKeyboard() - //toast("No matches")//TODO - } } private fun searchMatches(textToHighlight: String, content: String): ArrayList { @@ -245,6 +267,7 @@ class MainActivity : SimpleActivity() { findItem(R.id.open_note).isVisible = shouldBeVisible findItem(R.id.delete_note).isVisible = shouldBeVisible findItem(R.id.export_all_notes).isVisible = shouldBeVisible + findItem(R.id.open_search).isVisible = view_pager.currentItem != 0 saveNoteButton = findItem(R.id.save_note) saveNoteButton!!.isVisible = !config.autosaveNotes && showSaveButton && mCurrentNote.type == NoteType.TYPE_TEXT.value diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt index 1d183faf..87b75758 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt @@ -45,6 +45,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity } } + fun textFragment(position: Int): TextFragment? = (fragments[position] as? TextFragment) + fun getCurrentNotesView(position: Int) = (fragments[position] as? TextFragment)?.getNotesView() fun getCurrentNoteViewText(position: Int) = (fragments[position] as? TextFragment)?.getCurrentNoteViewText() diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt index a813a878..9cb22532 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt @@ -77,7 +77,8 @@ class TextFragment : NoteFragment() { if (config!!.autosaveNotes) { saveText(false) } - view.text_note_view.removeTextChangedListener(textWatcher) + + removeTextWatcher() } override fun setMenuVisibility(menuVisible: Boolean) { @@ -154,9 +155,12 @@ class TextFragment : NoteFragment() { view.notes_counter.beGone() } - view.text_note_view.addTextChangedListener(textWatcher) + setTextWatcher() } + fun setTextWatcher() = view.text_note_view.addTextChangedListener(textWatcher) + fun removeTextWatcher() = view.text_note_view.removeTextChangedListener(textWatcher) + fun updateNoteValue(value: String) { note?.value = value }