Skip to content

Commit

Permalink
Code cleanup: Getting rid of null asserts. (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Oct 14, 2023
1 parent 413122b commit 8ad6a45
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions app/src/main/java/com/dessalines/thumbkey/ComposeKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,34 @@ class ComposeKeyboardView(
onSwitchLanguage = {
ctx.lifecycleScope.launch {
// Cycle to the next keyboard
val s = settingsState.value!!
val layouts = keyboardLayoutsSetFromString(s.keyboardLayouts).toList()
val currentLayout = s.keyboardLayout
val index = layouts.indexOf(currentLayout)
val nextIndex = (index + 1).mod(layouts.size)
val nextLayout = layouts.getOrNull(nextIndex)
nextLayout?.let { layout ->
val s2 = s.copy(keyboardLayout = layout)
settingsRepo.update(s2)
val state = settingsState.value
state?.let { s ->

val layouts = keyboardLayoutsSetFromString(s.keyboardLayouts).toList()
val currentLayout = s.keyboardLayout
val index = layouts.indexOf(currentLayout)
val nextIndex = (index + 1).mod(layouts.size)
val nextLayout = layouts.getOrNull(nextIndex)
nextLayout?.let { layout ->
val s2 = s.copy(keyboardLayout = layout)
settingsRepo.update(s2)

// Display the new layout's name on the screen
val layoutName = KeyboardLayout.entries.find { it.index == nextLayout }?.keyboardDefinition!!.title
Toast.makeText(context, "$layoutName", Toast.LENGTH_SHORT).show()
// Display the new layout's name on the screen
val layoutName = KeyboardLayout.entries.find { it.index == nextLayout }?.keyboardDefinition?.title
Toast.makeText(context, layoutName, Toast.LENGTH_SHORT).show()
}
}
}
},
onSwitchPosition = {
ctx.lifecycleScope.launch {
// Cycle to the next position
val s = settingsState.value!!
val nextPosition = (s.position + 1).mod(3)
val s2 = s.copy(position = nextPosition)
settingsRepo.update(s2)
val state = settingsState.value
state?.let { s ->
val nextPosition = (s.position + 1).mod(3)
val s2 = s.copy(position = nextPosition)
settingsRepo.update(s2)
}
}
},
)
Expand Down

0 comments on commit 8ad6a45

Please sign in to comment.