Skip to content

Commit 5691143

Browse files
authored
fix: reset search state on page change (#216)
Refs: #190
1 parent 15e69dc commit 5691143

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Fixed crash in search ([#190])
810

911
## [1.4.1] - 2025-09-01
1012
### Changed
@@ -80,6 +82,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8082
[#157]: https://github.com/FossifyOrg/Notes/issues/157
8183
[#164]: https://github.com/FossifyOrg/Notes/issues/164
8284
[#178]: https://github.com/FossifyOrg/Notes/issues/178
85+
[#190]: https://github.com/FossifyOrg/Notes/issues/190
8386
[#201]: https://github.com/FossifyOrg/Notes/issues/201
8487

8588
[Unreleased]: https://github.com/FossifyOrg/Notes/compare/1.4.1...HEAD

app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import org.fossify.notes.extensions.config
107107
import org.fossify.notes.extensions.getPercentageFontSize
108108
import org.fossify.notes.extensions.notesDB
109109
import org.fossify.notes.extensions.parseChecklistItems
110+
import org.fossify.notes.extensions.safeSetSelection
110111
import org.fossify.notes.extensions.updateWidgets
111112
import org.fossify.notes.extensions.widgetsDB
112113
import org.fossify.notes.fragments.TextFragment
@@ -608,6 +609,9 @@ class MainActivity : SimpleActivity() {
608609
}
609610

610611
binding.viewPager.onPageChangeListener {
612+
searchIndex = 0
613+
searchMatches = emptyList()
614+
611615
currentTextFragment?.removeTextWatcher()
612616
currentNotesView()?.let { noteView ->
613617
noteView.text!!.clearBackgroundSpans()
@@ -641,7 +645,7 @@ class MainActivity : SimpleActivity() {
641645

642646
if (searchMatches.isNotEmpty()) {
643647
noteView.requestFocus()
644-
noteView.setSelection(searchMatches.getOrNull(searchIndex) ?: 0)
648+
noteView.safeSetSelection(searchMatches.getOrNull(searchIndex) ?: 0)
645649
}
646650

647651
searchQueryET.postDelayed({
@@ -681,7 +685,7 @@ class MainActivity : SimpleActivity() {
681685
private fun selectSearchMatch(editText: MyEditText) {
682686
if (searchMatches.isNotEmpty()) {
683687
editText.requestFocus()
684-
editText.setSelection(searchMatches.getOrNull(searchIndex) ?: 0)
688+
editText.safeSetSelection(searchMatches.getOrNull(searchIndex) ?: 0)
685689
} else {
686690
hideKeyboard()
687691
}
@@ -694,7 +698,7 @@ class MainActivity : SimpleActivity() {
694698

695699
currentNotesView()?.let { noteView ->
696700
noteView.requestFocus()
697-
noteView.setSelection(0)
701+
noteView.safeSetSelection(0)
698702
}
699703

700704
searchQueryET.postDelayed({

app/src/main/kotlin/org/fossify/notes/extensions/EditText.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ fun MyEditText.enforcePlainText() {
1616
}
1717
filters = (filters ?: emptyArray()) + stripSpans
1818
}
19+
20+
fun MyEditText.safeSetSelection(position: Int) {
21+
val length = text?.length ?: 0
22+
setSelection(position.coerceIn(0, length))
23+
}
24+

0 commit comments

Comments
 (0)