Skip to content

Commit 463eee3

Browse files
authored
Fix crash when long pressing omnibar text
1 parent 74bd1f7 commit 463eee3

File tree

4 files changed

+19
-39
lines changed

4 files changed

+19
-39
lines changed

app/src/androidTest/java/com/duckduckgo/app/browser/BrowserViewModelTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class BrowserViewModelTest {
9999
}
100100

101101
@Test
102-
fun whenViewModelNotifiedThatUrlFocusChangedGotFocusThenViewStateIsUpdated() {
103-
testee.urlFocusChanged(true)
102+
fun whenViewModelNotifiedThatUrlGotFocusThenViewStateIsUpdated() {
103+
testee.onUrlInputStateChanged("", true)
104104
assertTrue(testee.viewState.value!!.isEditing)
105105
}
106106

107107
@Test
108-
fun whenViewModelNotifiedThatUrlFocusChangedLostFocusThenViewStateIsUpdated() {
109-
testee.urlFocusChanged(false)
108+
fun whenViewModelNotifiedThatUrlLostFocusThenViewStateIsUpdated() {
109+
testee.onUrlInputStateChanged("", false)
110110
assertFalse(testee.viewState.value!!.isEditing)
111111
}
112112

app/src/main/java/com/duckduckgo/app/browser/BrowserActivity.kt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,17 @@ class BrowserActivity : DuckDuckGoActivity() {
110110
}
111111

112112
private fun showClearButton() {
113-
clearUrlButton.show()
114-
urlInput.updatePadding(paddingEnd = 40.toPx())
113+
urlInput.post {
114+
clearUrlButton.show()
115+
urlInput.updatePadding(paddingEnd = 40.toPx())
116+
}
115117
}
116118

117119
private fun hideClearButton() {
118-
clearUrlButton.hide()
119-
urlInput.updatePadding(paddingEnd = 10.toPx())
120+
urlInput.post {
121+
clearUrlButton.hide()
122+
urlInput.updatePadding(paddingEnd = 10.toPx())
123+
}
120124
}
121125

122126
private fun shouldUpdateUrl(viewState: BrowserViewModel.ViewState, url: String?) =
@@ -131,17 +135,13 @@ class BrowserActivity : DuckDuckGoActivity() {
131135
}
132136

133137
private fun configureUrlInput() {
134-
urlInput.onFocusChangeListener = View.OnFocusChangeListener { _: View, hasFocus: Boolean ->
135-
viewModel.urlFocusChanged(hasFocus)
136-
137-
if (hasFocus) {
138-
viewModel.onUrlInputValueChanged(urlInput.text.toString(), urlInput.hasFocus())
139-
}
138+
urlInput.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus: Boolean ->
139+
viewModel.onUrlInputStateChanged(urlInput.text.toString(), hasFocus)
140140
}
141141

142142
urlInput.addTextChangedListener(object : TextChangedWatcher() {
143143
override fun afterTextChanged(editable: Editable) {
144-
viewModel.onUrlInputValueChanged(urlInput.text.toString(), urlInput.hasFocus())
144+
viewModel.onUrlInputStateChanged(urlInput.text.toString(), urlInput.hasFocus())
145145
}
146146
})
147147

@@ -152,12 +152,6 @@ class BrowserActivity : DuckDuckGoActivity() {
152152
}
153153
}
154154

155-
urlInput.addTextChangedListener(object : TextChangedWatcher() {
156-
override fun afterTextChanged(editable: Editable) {
157-
viewModel.onUrlInputValueChanged(urlInput.text.toString(), urlInput.hasFocus())
158-
}
159-
})
160-
161155
clearUrlButton.setOnClickListener { urlInput.setText("") }
162156
}
163157

app/src/main/java/com/duckduckgo/app/browser/BrowserViewModel.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,7 @@ class BrowserViewModel(
127127

128128
private fun currentViewState(): ViewState = viewState.value!!
129129

130-
131-
fun urlFocusChanged(hasFocus: Boolean) {
132-
if (!hasFocus) {
133-
viewState.value = currentViewState().copy(isEditing = hasFocus, showClearButton = false)
134-
} else {
135-
viewState.value = currentViewState().copy(isEditing = hasFocus)
136-
}
137-
}
138-
139-
fun onUrlInputValueChanged(query: String, hasFocus: Boolean) {
130+
fun onUrlInputStateChanged(query: String, hasFocus: Boolean) {
140131
val showClearButton = hasFocus && query.isNotEmpty()
141132
viewState.value = currentViewState().copy(isEditing = hasFocus, showClearButton = showClearButton)
142133
}

app/src/main/java/com/duckduckgo/app/browser/omnibar/KeyboardAwareEditText.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ class KeyboardAwareEditText : AppCompatEditText {
3939
override fun onKeyPreIme(keyCode: Int, event: KeyEvent): Boolean {
4040

4141
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
42-
43-
if (onBackKeyListener != null) {
44-
return onBackKeyListener!!.onBackKey()
45-
}
46-
47-
return false
42+
return onBackKeyListener?.onBackKey() ?: false
4843
}
4944

5045
return super.onKeyPreIme(keyCode, event)
@@ -53,10 +48,10 @@ class KeyboardAwareEditText : AppCompatEditText {
5348
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
5449
super.onLayout(changed, left, top, right, bottom)
5550
if (showImeAfterFirstLayout) {
56-
post({
51+
post {
5752
showKeyboard()
5853
showImeAfterFirstLayout = false
59-
})
54+
}
6055
}
6156
}
6257

0 commit comments

Comments
 (0)