Skip to content

Commit 24b4cf9

Browse files
committed
#1064 fix: wait for switch keyboard action to complete before doing next action
1 parent dd3ea73 commit 24b4cf9

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
- #1818 the Key Mapper GUI Keyboard is no longer mentioned in the app. It still works but PRO mode
4343
and the auto switching feature are the preferred way to work around the limitations of the Key
4444
Mapper keyboard.
45-
- Allow selecting notification and alarm sound and not just ringtones for Sound action
45+
- Allow selecting notification and alarm sound and not just ringtones for Sound action.
46+
- #1064 wait for switch keyboard action to complete before doing next action.
4647

4748
## [3.2.1](https://github.com/sds100/KeyMapper/releases/tag/v3.2.1)
4849

base/src/main/java/io/github/sds100/keymapper/base/actions/PerformActionsUseCase.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import io.github.sds100.keymapper.common.utils.onSuccess
3636
import io.github.sds100.keymapper.common.utils.otherwise
3737
import io.github.sds100.keymapper.common.utils.success
3838
import io.github.sds100.keymapper.common.utils.then
39-
import io.github.sds100.keymapper.common.utils.valueOrNull
4039
import io.github.sds100.keymapper.common.utils.withFlag
4140
import io.github.sds100.keymapper.data.Keys
4241
import io.github.sds100.keymapper.data.PreferenceDefaults
@@ -77,10 +76,12 @@ import kotlinx.coroutines.delay
7776
import kotlinx.coroutines.flow.Flow
7877
import kotlinx.coroutines.flow.SharingStarted
7978
import kotlinx.coroutines.flow.StateFlow
79+
import kotlinx.coroutines.flow.filterNotNull
8080
import kotlinx.coroutines.flow.first
8181
import kotlinx.coroutines.flow.map
8282
import kotlinx.coroutines.flow.stateIn
8383
import kotlinx.coroutines.runBlocking
84+
import kotlinx.coroutines.withTimeoutOrNull
8485
import timber.log.Timber
8586

8687
class PerformActionsUseCaseImpl @AssistedInject constructor(
@@ -310,19 +311,18 @@ class PerformActionsUseCaseImpl @AssistedInject constructor(
310311
}
311312

312313
is ActionData.SwitchKeyboard -> {
313-
result = switchImeInterface
314-
.switchIme(action.imeId)
315-
.onSuccess { imeId ->
316-
val imeInfo = inputMethodAdapter.getInfoById(action.imeId).valueOrNull()
317-
?: return@onSuccess
318-
319-
val message = resourceProvider.getString(
320-
R.string.toast_chose_keyboard,
321-
imeInfo.label,
322-
)
314+
switchImeInterface.switchIme(action.imeId)
323315

324-
toastAdapter.show(message)
325-
}
316+
// See issue #1064. Wait for the input method to finish switching before returning.
317+
val chosenIme = withTimeoutOrNull(2000) {
318+
inputMethodAdapter.chosenIme.filterNotNull().first { it.id == action.imeId }
319+
}
320+
321+
if (chosenIme == null) {
322+
result = KMError.SwitchImeFailed
323+
} else {
324+
result = Success(Unit)
325+
}
326326
}
327327

328328
is ActionData.Volume.Down -> {

0 commit comments

Comments
 (0)