Skip to content

Commit 90334f2

Browse files
fix: speech to text button on chat activity not working #2469
Add restoreActivityState() in STTFragment to make invisible views visible and perform other tasks
1 parent 17b05dc commit 90334f2

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,8 @@ class ChatActivity : AppCompatActivity(), IChatView {
639639
companion object {
640640
val ALARM = "ALARM"
641641
}
642+
643+
fun setChatSearchInputGone() {
644+
chatSearchInput.visibility = View.GONE
645+
}
642646
}

app/src/main/java/org/fossasia/susi/ai/chat/STTfragment.kt

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,7 @@ class STTFragment : Fragment() {
113113
val thisActivity = activity
114114
if (thisActivity is ChatActivity) thisActivity.setText(voiceResults[0])
115115
recognizer.destroy()
116-
if ((activity as ChatActivity).recordingThread != null) {
117-
chatPresenter.startHotwordDetection()
118-
}
119-
(activity as ChatActivity).fabsetting.show()
120-
activity?.searchChat?.show()
121-
activity?.voiceSearchChat?.show()
122-
activity?.btnSpeak?.isEnabled = true
116+
restoreActivityState()
123117
activity?.supportFragmentManager?.popBackStackImmediate()
124118
}
125119

@@ -128,15 +122,14 @@ class STTFragment : Fragment() {
128122
}
129123

130124
override fun onError(error: Int) {
131-
Timber.d("Error listening for speech: %s", error)
132-
Toast.makeText(activity?.applicationContext, "Could not recognize speech, try again.", Toast.LENGTH_SHORT).show()
133-
speechProgress?.onResultOrOnError()
134-
recognizer.destroy()
135-
activity?.fabsetting?.show()
136-
activity?.searchChat?.show()
137-
activity?.voiceSearchChat?.show()
138-
activity?.btnSpeak?.isEnabled = true
139-
activity?.supportFragmentManager?.popBackStackImmediate()
125+
if(activity != null) {
126+
Timber.d("Error listening for speech: %s", error)
127+
Toast.makeText(activity?.applicationContext, "Could not recognize speech, try again.", Toast.LENGTH_SHORT).show()
128+
speechProgress?.onResultOrOnError()
129+
recognizer.destroy()
130+
restoreActivityState()
131+
activity?.supportFragmentManager?.popBackStackImmediate()
132+
}
140133
}
141134

142135
override fun onBeginningOfSpeech() {
@@ -170,6 +163,18 @@ class STTFragment : Fragment() {
170163
recognizer.startListening(intent)
171164
}
172165

166+
private fun restoreActivityState() {
167+
if ((activity as ChatActivity).recordingThread != null) {
168+
chatPresenter.startHotwordDetection()
169+
}
170+
(activity as ChatActivity).fabsetting.show()
171+
activity?.searchChat?.show()
172+
activity?.voiceSearchChat?.show()
173+
activity?.btnSpeak?.isEnabled = true
174+
(activity as ChatActivity)?.setChatSearchInputGone()
175+
recognizer?.destroy()
176+
}
177+
173178
override fun onPause() {
174179
super.onPause()
175180
if (thisActivity is ChatActivity) {
@@ -185,6 +190,8 @@ class STTFragment : Fragment() {
185190

186191
override fun onDestroyView() {
187192
super.onDestroyView()
193+
restoreActivityState()
194+
activity?.supportFragmentManager?.popBackStack()
188195
mainHandler.removeCallbacks(runnable)
189196
subHandler.removeCallbacks(delayRunnable)
190197
}

0 commit comments

Comments
 (0)