@@ -36,7 +36,6 @@ import io.github.sds100.keymapper.common.utils.onSuccess
3636import io.github.sds100.keymapper.common.utils.otherwise
3737import io.github.sds100.keymapper.common.utils.success
3838import io.github.sds100.keymapper.common.utils.then
39- import io.github.sds100.keymapper.common.utils.valueOrNull
4039import io.github.sds100.keymapper.common.utils.withFlag
4140import io.github.sds100.keymapper.data.Keys
4241import io.github.sds100.keymapper.data.PreferenceDefaults
@@ -77,10 +76,12 @@ import kotlinx.coroutines.delay
7776import kotlinx.coroutines.flow.Flow
7877import kotlinx.coroutines.flow.SharingStarted
7978import kotlinx.coroutines.flow.StateFlow
79+ import kotlinx.coroutines.flow.filterNotNull
8080import kotlinx.coroutines.flow.first
8181import kotlinx.coroutines.flow.map
8282import kotlinx.coroutines.flow.stateIn
8383import kotlinx.coroutines.runBlocking
84+ import kotlinx.coroutines.withTimeoutOrNull
8485import timber.log.Timber
8586
8687class 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