Skip to content

Commit 1217b0b

Browse files
committed
fix: an error would be shown if trying to choose an input method and the ime picker had to be shown to do this.
1 parent 3ea35cd commit 1217b0b

File tree

10 files changed

+91
-66
lines changed

10 files changed

+91
-66
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,16 @@ class PerformActionsUseCaseImpl(
196196

197197
is SwitchKeyboardSystemAction -> {
198198
coroutineScope.launch {
199-
inputMethodAdapter.chooseIme(
200-
action.imeId,
201-
fromForeground = false
202-
).onSuccess {
203-
val message = resourceProvider.getString(
204-
R.string.toast_chose_keyboard,
205-
it.label
206-
)
207-
popupMessageAdapter.showPopupMessage(message)
208-
}.showErrorMessageOnFail()
199+
inputMethodAdapter
200+
.chooseImeWithoutUserInput(action.imeId)
201+
.onSuccess {
202+
val message = resourceProvider.getString(
203+
R.string.toast_chose_keyboard,
204+
it.label
205+
)
206+
popupMessageAdapter.showPopupMessage(message)
207+
}
208+
.showErrorMessageOnFail()
209209
}
210210

211211
result = null
@@ -678,7 +678,13 @@ class PerformActionsUseCaseImpl(
678678

679679
when (result) {
680680
null, is Success -> Timber.d("Performed action $action, input event type: $inputEventType, key meta state: $keyMetaState")
681-
is Error -> Timber.d("Failed to perform action $action, reason: ${result.getFullMessage(resourceProvider)}, action: $action, input event type: $inputEventType, key meta state: $keyMetaState")
681+
is Error -> Timber.d(
682+
"Failed to perform action $action, reason: ${
683+
result.getFullMessage(
684+
resourceProvider
685+
)
686+
}, action: $action, input event type: $inputEventType, key meta state: $keyMetaState"
687+
)
682688
}
683689

684690
result?.showErrorMessageOnFail()

app/src/main/java/io/github/sds100/keymapper/mappings/DisplayMappingUseCase.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import io.github.sds100.keymapper.system.apps.PackageManagerAdapter
1010
import io.github.sds100.keymapper.system.inputmethod.InputMethodAdapter
1111
import io.github.sds100.keymapper.system.inputmethod.KeyMapperImeHelper
1212
import io.github.sds100.keymapper.system.permissions.PermissionAdapter
13-
import io.github.sds100.keymapper.util.Error
14-
import io.github.sds100.keymapper.util.Result
15-
import io.github.sds100.keymapper.util.Success
16-
import io.github.sds100.keymapper.util.then
13+
import io.github.sds100.keymapper.util.*
1714
import kotlinx.coroutines.flow.Flow
18-
import kotlinx.coroutines.flow.drop
1915
import kotlinx.coroutines.flow.map
20-
import kotlinx.coroutines.flow.merge
2116

2217
/**
2318
* Created by sds100 on 03/04/2021.
@@ -54,9 +49,10 @@ class DisplaySimpleMappingUseCaseImpl(
5449
Error.AccessibilityServiceCrashed -> serviceAdapter.restartService()
5550
is Error.AppDisabled -> packageManager.enableApp(error.packageName)
5651
is Error.AppNotFound -> packageManager.installApp(error.packageName)
57-
Error.NoCompatibleImeChosen -> keyMapperImeHelper.chooseCompatibleInputMethod(
58-
fromForeground = true
59-
)
52+
Error.NoCompatibleImeChosen -> keyMapperImeHelper.chooseCompatibleInputMethod()
53+
.otherwise {
54+
inputMethodAdapter.showImePicker(fromForeground = true)
55+
}
6056
Error.NoCompatibleImeEnabled -> keyMapperImeHelper.enableCompatibleInputMethods()
6157
is Error.ImeDisabled -> inputMethodAdapter.enableIme(error.ime.id)
6258
is Error.PermissionDenied -> permissionAdapter.request(error.permission)

app/src/main/java/io/github/sds100/keymapper/settings/ConfigSettingsUseCase.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ class ConfigSettingsUseCaseImpl(
5757
}
5858

5959
override suspend fun chooseCompatibleIme(): Result<ImeInfo> {
60-
return imeHelper.chooseCompatibleInputMethod(fromForeground = true)
60+
return imeHelper.chooseCompatibleInputMethod()
61+
}
62+
63+
override suspend fun showImePicker(): Result<*> {
64+
return inputMethodAdapter.showImePicker(fromForeground = true)
6165
}
6266

6367
override fun <T> getPreference(key: Preferences.Key<T>) =
@@ -139,6 +143,7 @@ interface ConfigSettingsUseCase {
139143

140144
fun enableCompatibleIme()
141145
suspend fun chooseCompatibleIme(): Result<ImeInfo>
146+
suspend fun showImePicker(): Result<*>
142147

143148
val defaultLongPressDelay: Flow<Int>
144149
val defaultDoublePressDelay: Flow<Int>

app/src/main/java/io/github/sds100/keymapper/settings/SettingsViewModel.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.ViewModelProvider
55
import androidx.lifecycle.viewModelScope
66
import io.github.sds100.keymapper.R
7-
import io.github.sds100.keymapper.util.SharedPrefsDataStoreWrapper
8-
import io.github.sds100.keymapper.util.getFullMessage
9-
import io.github.sds100.keymapper.util.onFailure
10-
import io.github.sds100.keymapper.util.onSuccess
7+
import io.github.sds100.keymapper.util.*
118
import io.github.sds100.keymapper.util.ui.*
129
import kotlinx.coroutines.flow.*
1310
import kotlinx.coroutines.launch
@@ -49,15 +46,26 @@ class SettingsViewModel(
4946

5047
fun onChooseCompatibleImeClick() {
5148
viewModelScope.launch {
52-
useCase.chooseCompatibleIme().onSuccess { ime ->
53-
val snackBar =
54-
PopupUi.SnackBar(message = getString(R.string.toast_chose_keyboard, ime.label))
55-
showPopup("chose_ime_success", snackBar)
56-
}.onFailure { error ->
57-
val snackBar =
58-
PopupUi.SnackBar(message = error.getFullMessage(this@SettingsViewModel))
59-
showPopup("chose_ime_error", snackBar)
60-
}
49+
useCase
50+
.chooseCompatibleIme()
51+
.onSuccess { ime ->
52+
val snackBar =
53+
PopupUi.SnackBar(
54+
message = getString(
55+
R.string.toast_chose_keyboard,
56+
ime.label
57+
)
58+
)
59+
showPopup("chose_ime_success", snackBar)
60+
}
61+
.otherwise {
62+
useCase.showImePicker()
63+
}
64+
.onFailure { error ->
65+
val snackBar =
66+
PopupUi.SnackBar(message = error.getFullMessage(this@SettingsViewModel))
67+
showPopup("chose_ime_error", snackBar)
68+
}
6169
}
6270
}
6371

app/src/main/java/io/github/sds100/keymapper/system/inputmethod/AndroidInputMethodAdapter.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ class AndroidInputMethodAdapter(
171171
return suAdapter.execute("ime enable $imeId")
172172
}
173173

174-
override suspend fun chooseIme(imeId: String, fromForeground: Boolean): Result<ImeInfo> {
174+
override suspend fun chooseImeWithoutUserInput(imeId: String): Result<ImeInfo> {
175175

176176
getInfoById(imeId).onSuccess {
177-
0
178177
if (!it.isEnabled) {
179178
return Error.ImeDisabled(it)
180179
}
@@ -200,9 +199,8 @@ class AndroidInputMethodAdapter(
200199
failed = false
201200
}
202201

203-
//show the ime picker as a last resort
204202
if (failed) {
205-
showImePicker(fromForeground).onFailure { return it }
203+
return Error.FailedToChangeIme
206204
}
207205

208206
//wait for the ime to change and then return the info of the ime

app/src/main/java/io/github/sds100/keymapper/system/inputmethod/AutoSwitchImeController.kt

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ class AutoSwitchImeController(
4646
if (!toggleKeyboardOnToggleKeymaps) return@onEach
4747

4848
if (isPaused) {
49-
imeHelper.chooseLastUsedIncompatibleInputMethod(fromForeground = false)
49+
imeHelper.chooseLastUsedIncompatibleInputMethod().otherwise {
50+
inputMethodAdapter.showImePicker(fromForeground = false)
51+
}
5052
} else {
51-
imeHelper.chooseCompatibleInputMethod(fromForeground = false)
53+
imeHelper.chooseCompatibleInputMethod().otherwise {
54+
inputMethodAdapter.showImePicker(fromForeground = false)
55+
}
5256
}
5357
}.launchIn(coroutineScope)
5458

@@ -58,14 +62,18 @@ class AutoSwitchImeController(
5862
}
5963

6064
if (changeImeOnDeviceConnect && devicesThatToggleKeyboard.contains(device.descriptor)) {
61-
val result = imeHelper.chooseCompatibleInputMethod(fromForeground = false)
62-
63-
result.onSuccess { ime ->
64-
val message = resourceProvider.getString(R.string.toast_chose_keyboard, ime.label)
65-
popupMessageAdapter.showPopupMessage(message)
66-
}.onFailure { error ->
67-
popupMessageAdapter.showPopupMessage(error.getFullMessage(resourceProvider))
68-
}
65+
imeHelper.chooseCompatibleInputMethod()
66+
.onSuccess { ime ->
67+
val message =
68+
resourceProvider.getString(R.string.toast_chose_keyboard, ime.label)
69+
popupMessageAdapter.showPopupMessage(message)
70+
}
71+
.otherwise {
72+
inputMethodAdapter.showImePicker(fromForeground = false)
73+
}
74+
.onFailure { error ->
75+
popupMessageAdapter.showPopupMessage(error.getFullMessage(resourceProvider))
76+
}
6977
}
7078
}.launchIn(coroutineScope)
7179

@@ -75,14 +83,18 @@ class AutoSwitchImeController(
7583
}
7684

7785
if (changeImeOnDeviceConnect && devicesThatToggleKeyboard.contains(device.descriptor)) {
78-
val result = imeHelper.chooseLastUsedIncompatibleInputMethod(fromForeground = false)
79-
80-
result.onSuccess { ime ->
81-
val message = resourceProvider.getString(R.string.toast_chose_keyboard, ime.label)
82-
popupMessageAdapter.showPopupMessage(message)
83-
}.onFailure { error ->
84-
popupMessageAdapter.showPopupMessage(error.getFullMessage(resourceProvider))
85-
}
86+
imeHelper.chooseLastUsedIncompatibleInputMethod()
87+
.onSuccess { ime ->
88+
val message =
89+
resourceProvider.getString(R.string.toast_chose_keyboard, ime.label)
90+
popupMessageAdapter.showPopupMessage(message)
91+
}
92+
.otherwise {
93+
inputMethodAdapter.showImePicker(fromForeground = false)
94+
}
95+
.onFailure { error ->
96+
popupMessageAdapter.showPopupMessage(error.getFullMessage(resourceProvider))
97+
}
8698
}
8799
}.launchIn(coroutineScope)
88100
}

app/src/main/java/io/github/sds100/keymapper/system/inputmethod/InputMethodAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface InputMethodAdapter {
1313
fun showImePicker(fromForeground: Boolean): Result<*>
1414
fun enableIme(imeId: String): Result<*>
1515

16-
suspend fun chooseIme(imeId: String, fromForeground: Boolean): Result<ImeInfo>
16+
suspend fun chooseImeWithoutUserInput(imeId: String): Result<ImeInfo>
1717

1818
fun getInfoById(imeId: String): Result<ImeInfo>
1919
fun getInfoByPackageName(packageName: String): Result<ImeInfo>

app/src/main/java/io/github/sds100/keymapper/system/inputmethod/KeyMapperImeHelper.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ class KeyMapperImeHelper(private val imeAdapter: InputMethodAdapter) {
3030
}
3131
}
3232

33-
suspend fun chooseCompatibleInputMethod(fromForeground: Boolean): Result<ImeInfo> {
33+
suspend fun chooseCompatibleInputMethod(): Result<ImeInfo> {
3434
return getLastUsedCompatibleImeId().suspendThen {
35-
imeAdapter.chooseIme(it, fromForeground)
35+
imeAdapter.chooseImeWithoutUserInput(it)
3636
}
3737
}
3838

39-
suspend fun chooseLastUsedIncompatibleInputMethod(fromForeground: Boolean): Result<ImeInfo> {
39+
suspend fun chooseLastUsedIncompatibleInputMethod(): Result<ImeInfo> {
4040
return getLastUsedIncompatibleImeId().then {
41-
imeAdapter.chooseIme(it, fromForeground)
41+
imeAdapter.chooseImeWithoutUserInput(it)
4242
}
4343
}
4444

45-
suspend fun toggleCompatibleInputMethod(fromForeground: Boolean): Result<ImeInfo> {
45+
suspend fun toggleCompatibleInputMethod(): Result<ImeInfo> {
4646
return if (isCompatibleImeChosen()) {
47-
chooseLastUsedIncompatibleInputMethod(fromForeground)
47+
chooseLastUsedIncompatibleInputMethod()
4848
} else {
49-
chooseCompatibleInputMethod(fromForeground)
49+
chooseCompatibleInputMethod()
5050
}
5151
}
5252

app/src/main/java/io/github/sds100/keymapper/system/inputmethod/ToggleCompatibleImeUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ToggleCompatibleImeUseCaseImpl(
1616
inputMethodAdapter.isUserInputRequiredToChangeIme
1717

1818
override suspend fun toggle(): Result<ImeInfo> {
19-
return keyMapperImeHelper.toggleCompatibleInputMethod(fromForeground = false)
19+
return keyMapperImeHelper.toggleCompatibleInputMethod()
2020
}
2121
}
2222

app/src/main/java/io/github/sds100/keymapper/util/Result.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ suspend infix fun <T, U> Result<T>.suspendThen(f: suspend (T) -> Result<U>) =
141141
is Error -> this
142142
}
143143

144-
infix fun <T> Result<T>.otherwise(f: (error: Error) -> Result<T>) =
144+
inline infix fun <T> Result<T>.otherwise(f: (error: Error) -> Result<T>) =
145145
when (this) {
146146
is Success -> this
147147
is Error -> f(this)

0 commit comments

Comments
 (0)