From 7040a0497ef3909c50a0460806556bff19c517ae Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Mon, 8 Sep 2025 15:47:18 +0530 Subject: [PATCH 1/2] fix: reset search on page change Refs: https://github.com/FossifyOrg/Notes/issues/190 --- .../org/fossify/notes/activities/MainActivity.kt | 10 +++++++--- .../kotlin/org/fossify/notes/extensions/EditText.kt | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt b/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt index e9e6e66de..b641bd5ea 100644 --- a/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt @@ -107,6 +107,7 @@ import org.fossify.notes.extensions.config import org.fossify.notes.extensions.getPercentageFontSize import org.fossify.notes.extensions.notesDB import org.fossify.notes.extensions.parseChecklistItems +import org.fossify.notes.extensions.safeSetSelection import org.fossify.notes.extensions.updateWidgets import org.fossify.notes.extensions.widgetsDB import org.fossify.notes.fragments.TextFragment @@ -608,6 +609,9 @@ class MainActivity : SimpleActivity() { } binding.viewPager.onPageChangeListener { + searchIndex = 0 + searchMatches = emptyList() + currentTextFragment?.removeTextWatcher() currentNotesView()?.let { noteView -> noteView.text!!.clearBackgroundSpans() @@ -641,7 +645,7 @@ class MainActivity : SimpleActivity() { if (searchMatches.isNotEmpty()) { noteView.requestFocus() - noteView.setSelection(searchMatches.getOrNull(searchIndex) ?: 0) + noteView.safeSetSelection(searchMatches.getOrNull(searchIndex) ?: 0) } searchQueryET.postDelayed({ @@ -681,7 +685,7 @@ class MainActivity : SimpleActivity() { private fun selectSearchMatch(editText: MyEditText) { if (searchMatches.isNotEmpty()) { editText.requestFocus() - editText.setSelection(searchMatches.getOrNull(searchIndex) ?: 0) + editText.safeSetSelection(searchMatches.getOrNull(searchIndex) ?: 0) } else { hideKeyboard() } @@ -694,7 +698,7 @@ class MainActivity : SimpleActivity() { currentNotesView()?.let { noteView -> noteView.requestFocus() - noteView.setSelection(0) + noteView.safeSetSelection(0) } searchQueryET.postDelayed({ diff --git a/app/src/main/kotlin/org/fossify/notes/extensions/EditText.kt b/app/src/main/kotlin/org/fossify/notes/extensions/EditText.kt index 1eac63c47..ff7e3bd44 100644 --- a/app/src/main/kotlin/org/fossify/notes/extensions/EditText.kt +++ b/app/src/main/kotlin/org/fossify/notes/extensions/EditText.kt @@ -16,3 +16,9 @@ fun MyEditText.enforcePlainText() { } filters = (filters ?: emptyArray()) + stripSpans } + +fun MyEditText.safeSetSelection(position: Int) { + val length = text?.length ?: 0 + setSelection(position.coerceIn(0, length)) +} + From 7cbfeacd80c01f3f6e0228790d9a29df8cb58b89 Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Mon, 8 Sep 2025 15:50:04 +0530 Subject: [PATCH 2/2] docs: update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c0e5206d..e024f4294 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fixed crash in search ([#190]) ## [1.4.1] - 2025-09-01 ### Changed @@ -80,6 +82,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#157]: https://github.com/FossifyOrg/Notes/issues/157 [#164]: https://github.com/FossifyOrg/Notes/issues/164 [#178]: https://github.com/FossifyOrg/Notes/issues/178 +[#190]: https://github.com/FossifyOrg/Notes/issues/190 [#201]: https://github.com/FossifyOrg/Notes/issues/201 [Unreleased]: https://github.com/FossifyOrg/Notes/compare/1.4.1...HEAD