Skip to content

Commit

Permalink
Adding multiple default languages. Fixes #42 (#123)
Browse files Browse the repository at this point in the history
* Adding multiple default languages. Fixes #42

* Fixing room migration.
  • Loading branch information
dessalines authored Apr 4, 2023
1 parent 58dd168 commit 26720a5
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 49 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ This project is a follow-up to the now unmaintained (and closed-source) [Message
- Tap, or slide / swipe to type a letter.
- Swipe up or down on `A` to capitalize. <img width=60px height=60px src="https://i.postimg.cc/Znt2Ft9G/thumbkey-1-1.png">
- Double tap the space bar to type a comma, triple tap to type a period. More taps have more punctuation.
- Slide left on the backspace key to delete whole words.
- Slide left or right on the spacebar to move the cursor.
- Press the `#` key to see a numeric keypad, and additional symbols.
- Press the Settings Gear key to configure Thumb-Key.
- Slide left on the backspace key to delete whole words.
- Slide left or right on the spacebar to move the cursor.
- Slide left on the `#` key to switch to a default language.
- Slide up on the `#` key to select and copy all.
- Slide down on the `#` key to paste.
- Practice typing, and check your words per minute, using [monkeytype.com](https://monkeytype.com)

## Thumb-Key Design
Expand Down
32 changes: 26 additions & 6 deletions app/src/main/java/com/dessalines/thumbkey/ComposeKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,54 @@ import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.platform.AbstractComposeView
import androidx.lifecycle.LiveData
import com.dessalines.thumbkey.db.AppSettings
import androidx.lifecycle.lifecycleScope
import com.dessalines.thumbkey.db.AppSettingsRepository
import com.dessalines.thumbkey.ui.components.keyboard.KeyboardScreen
import com.dessalines.thumbkey.ui.theme.ThumbkeyTheme
import com.dessalines.thumbkey.utils.getKeyboardMode
import com.dessalines.thumbkey.utils.keyboardLayoutsSetFromString
import com.dessalines.thumbkey.utils.toBool
import kotlinx.coroutines.launch

@SuppressLint("ViewConstructor")
class ComposeKeyboardView(
context: Context,
private val liveSettings: LiveData<AppSettings>
private val settingsRepo: AppSettingsRepository
) :
AbstractComposeView
(context) {

@Composable
override fun Content() {
val settings = liveSettings.observeAsState().value
val settingsState = settingsRepo.appSettings.observeAsState()
val settings = settingsState.value
val ctx = context as IMEService

val startMode = getKeyboardMode(
ime = context as IMEService,
ime = ctx,
autoCapitalize = settings?.autoCapitalize?.toBool() ?: false
)

ThumbkeyTheme(
settings = settings
) {
KeyboardScreen(settings, startMode)
KeyboardScreen(
settings = settings,
startMode = startMode,
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.get(nextIndex)
val s2 = s.copy(keyboardLayout = nextLayout)
settingsRepo.update(s2)
}
}
)
}
}
}
5 changes: 2 additions & 3 deletions app/src/main/java/com/dessalines/thumbkey/IMEService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ class IMEService :
SavedStateRegistryOwner {

private fun setupView(): View {
val liveSettings = (application as ThumbkeyApplication).appSettingsRepository
.appSettings
val settingsRepo = (application as ThumbkeyApplication).appSettingsRepository

val view = ComposeKeyboardView(this, liveSettings)
val view = ComposeKeyboardView(this, settingsRepo)
window?.window?.decorView?.let { decorView ->
decorView.setViewTreeLifecycleOwner(this)
decorView.setViewTreeViewModelStoreOwner(this)
Expand Down
20 changes: 17 additions & 3 deletions app/src/main/java/com/dessalines/thumbkey/db/AppDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ data class AppSettings(
name = "hide_letters",
defaultValue = DEFAULT_HIDE_LETTERS.toString()
)
val hideLetters: Int
val hideLetters: Int,
@ColumnInfo(
name = "keyboard_layouts",
defaultValue = "$DEFAULT_KEYBOARD_LAYOUT"
)
val keyboardLayouts: String
)

@Dao
Expand Down Expand Up @@ -160,8 +165,16 @@ val MIGRATION_3_4 = object : Migration(3, 4) {
}
}

val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"alter table AppSettings add column keyboard_layouts TEXT NOT NULL default '$DEFAULT_KEYBOARD_LAYOUT'"
)
}
}

@Database(
version = 4,
version = 5,
entities = [AppSettings::class],
exportSchema = true
)
Expand All @@ -187,7 +200,8 @@ abstract class AppDB : RoomDatabase() {
.addMigrations(
MIGRATION_1_2,
MIGRATION_2_3,
MIGRATION_3_4
MIGRATION_3_4,
MIGRATION_4_5
)
// Necessary because it can't insert data on creation
.addCallback(object : Callback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ContentPaste
import androidx.compose.material.icons.outlined.KeyboardBackspace
import androidx.compose.material.icons.outlined.KeyboardReturn
import androidx.compose.material.icons.outlined.Language
import androidx.compose.material.icons.outlined.Numbers
import androidx.compose.material.icons.outlined.SelectAll
import androidx.compose.material.icons.outlined.Settings
Expand Down Expand Up @@ -45,6 +46,11 @@ val NUMERIC_KEY_ITEM =
display = KeyDisplay.IconDisplay(Icons.Outlined.ContentPaste),
action = KeyAction.Paste,
color = ColorVariant.MUTED
),
SwipeDirection.LEFT to KeyC(
display = KeyDisplay.IconDisplay(Icons.Outlined.Language),
action = KeyAction.SwitchLanguage,
color = ColorVariant.MUTED
)
),
backgroundColor = ColorVariant.SURFACE_VARIANT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ fun KeyboardKey(
onToggleShiftMode: (enable: Boolean) -> Unit,
onToggleNumericMode: (enable: Boolean) -> Unit,
onToggleCapsLock: () -> Unit,
onAutoCapitalize: (enable: Boolean) -> Unit
onAutoCapitalize: (enable: Boolean) -> Unit,
onSwitchLanguage: () -> Unit
) {
// Necessary for swipe settings to get updated correctly
val id = key.toString() + animationHelperSpeed + animationSpeed + autoCapitalize + vibrateOnTap + soundOnTap + keySize + minSwipeLength
Expand Down Expand Up @@ -139,7 +140,8 @@ fun KeyboardKey(
onToggleShiftMode = onToggleShiftMode,
onToggleNumericMode = onToggleNumericMode,
onToggleCapsLock = onToggleCapsLock,
onAutoCapitalize = onAutoCapitalize
onAutoCapitalize = onAutoCapitalize,
onSwitchLanguage = onSwitchLanguage
)
doneKeyAction(scope, action, isDragged, releasedKey, animationHelperSpeed)
}
Expand All @@ -166,7 +168,8 @@ fun KeyboardKey(
onToggleShiftMode = onToggleShiftMode,
onToggleNumericMode = onToggleNumericMode,
onToggleCapsLock = onToggleCapsLock,
onAutoCapitalize = onAutoCapitalize
onAutoCapitalize = onAutoCapitalize,
onSwitchLanguage = onSwitchLanguage
)
lastAction.value = action

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import com.dessalines.thumbkey.utils.toBool
@Composable
fun KeyboardScreen(
settings: AppSettings?,
startMode: KeyboardMode
startMode: KeyboardMode,
onSwitchLanguage: () -> Unit
) {
var mode by remember {
mutableStateOf(startMode)
Expand Down Expand Up @@ -123,7 +124,8 @@ fun KeyboardScreen(
mode = KeyboardMode.MAIN
}
}
}
},
onSwitchLanguage = onSwitchLanguage
)
}
}
Expand Down
Loading

0 comments on commit 26720a5

Please sign in to comment.