Skip to content

Commit

Permalink
Adding a minimum swipe length. Fixes #14 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Mar 17, 2023
1 parent db4088d commit 24fcb57
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 39 deletions.
25 changes: 20 additions & 5 deletions app/src/main/java/com/dessalines/thumbkey/db/AppDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.room.*
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import kotlinx.coroutines.launch
import java.util.concurrent.Executors
Expand All @@ -23,6 +24,7 @@ const val DEFAULT_THEME = 0
const val DEFAULT_THEME_COLOR = 0
const val DEFAULT_VIBRATE_ON_TAP = 1
const val DEFAULT_SOUND_ON_TAP = 0
const val DEFAULT_MIN_SWIPE_LENGTH = 40

const val UPDATE_APP_CHANGELOG_UNVIEWED = "UPDATE AppSettings SET viewed_changelog = 0"

Expand Down Expand Up @@ -83,7 +85,12 @@ data class AppSettings(
name = "viewed_changelog",
defaultValue = "0"
)
val viewedChangelog: Int
val viewedChangelog: Int,
@ColumnInfo(
name = "min_swipe_length",
defaultValue = DEFAULT_MIN_SWIPE_LENGTH.toString()
)
val minSwipeLength: Int
)

@Dao
Expand Down Expand Up @@ -117,8 +124,16 @@ class AppSettingsRepository(private val appSettingsDao: AppSettingsDao) {
}
}

val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"alter table AppSettings add column min_swipe_length INTEGER NOT NULL default $DEFAULT_MIN_SWIPE_LENGTH"
)
}
}

@Database(
version = 1,
version = 2,
entities = [AppSettings::class],
exportSchema = true
)
Expand All @@ -141,9 +156,9 @@ abstract class AppDB : RoomDatabase() {
"thumbkey"
)
.allowMainThreadQueries()
// .addMigrations(
// MIGRATION_1_2,
// )
.addMigrations(
MIGRATION_1_2
)
// Necessary because it can't insert data on creation
.addCallback(object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ fun KeyboardKey(
vibrateOnTap: Boolean,
soundOnTap: Boolean,
keySize: Int,
minSwipeLength: Int,
onToggleShiftMode: (enable: Boolean) -> Unit,
onToggleNumericMode: (enable: Boolean) -> Unit
) {
val id = key.toString() + animationHelperSpeed + animationSpeed + keySize
// Necessary for swipe settings to get updated correctly
val id = key.toString() + animationHelperSpeed + animationSpeed + autoCapitalize + vibrateOnTap + soundOnTap + keySize + minSwipeLength

val ctx = LocalContext.current
val ime = ctx as IMEService
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -147,10 +150,9 @@ fun KeyboardKey(
offsetY += y
},
onDragEnd = {
val swipeDirection = swipeDirection(offsetX, offsetY)
val swipeDirection = swipeDirection(offsetX, offsetY, minSwipeLength)
val action = key.swipes?.get(swipeDirection)?.action ?: key.center.action

val swipeKey = key.swipes?.get(swipeDirection)
val action = swipeKey?.action ?: run { key.center.action }
performKeyAction(
action = action,
ime = ime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.dessalines.thumbkey.db.DEFAULT_ANIMATION_SPEED
import com.dessalines.thumbkey.db.DEFAULT_AUTO_CAPITALIZE
import com.dessalines.thumbkey.db.DEFAULT_KEYBOARD_LAYOUT
import com.dessalines.thumbkey.db.DEFAULT_KEY_SIZE
import com.dessalines.thumbkey.db.DEFAULT_MIN_SWIPE_LENGTH
import com.dessalines.thumbkey.db.DEFAULT_POSITION
import com.dessalines.thumbkey.db.DEFAULT_SOUND_ON_TAP
import com.dessalines.thumbkey.db.DEFAULT_VIBRATE_ON_TAP
Expand Down Expand Up @@ -76,6 +77,7 @@ fun KeyboardScreen(settings: AppSettings?) {
?: DEFAULT_ANIMATION_SPEED,
animationHelperSpeed = settings?.animationHelperSpeed
?: DEFAULT_ANIMATION_HELPER_SPEED,
minSwipeLength = settings?.minSwipeLength ?: DEFAULT_MIN_SWIPE_LENGTH,
onToggleShiftMode = { enable ->
if (mode !== KeyboardMode.NUMERIC) {
mode = if (enable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.dessalines.thumbkey.db.DEFAULT_ANIMATION_SPEED
import com.dessalines.thumbkey.db.DEFAULT_AUTO_CAPITALIZE
import com.dessalines.thumbkey.db.DEFAULT_KEYBOARD_LAYOUT
import com.dessalines.thumbkey.db.DEFAULT_KEY_SIZE
import com.dessalines.thumbkey.db.DEFAULT_MIN_SWIPE_LENGTH
import com.dessalines.thumbkey.db.DEFAULT_SOUND_ON_TAP
import com.dessalines.thumbkey.db.DEFAULT_THEME
import com.dessalines.thumbkey.db.DEFAULT_THEME_COLOR
Expand Down Expand Up @@ -67,6 +68,9 @@ fun LookAndFeelActivity(
val animationHelperSpeedState = rememberFloatSettingState(
(settings?.animationHelperSpeed ?: DEFAULT_ANIMATION_HELPER_SPEED).toFloat()
)
val minSwipeLengthState = rememberFloatSettingState(
(settings?.minSwipeLength ?: DEFAULT_MIN_SWIPE_LENGTH).toFloat()
)
val positionState = rememberIntSettingState(
settings?.position ?: com.dessalines.thumbkey.db.DEFAULT_POSITION
)
Expand Down Expand Up @@ -120,6 +124,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
vibrateOnTapState,
soundOnTapState,
Expand Down Expand Up @@ -148,6 +153,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -176,6 +182,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -204,6 +211,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -231,6 +239,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -258,6 +267,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -285,6 +295,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -313,6 +324,36 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
soundOnTapState,
keyboardLayoutState,
themeState,
themeColorState
)
}
)
SettingsSlider(
valueRange = 0f..200f,
state = minSwipeLengthState,
icon = {
Icon(
imageVector = Icons.Outlined.Swipe,
contentDescription = "TODO"
)
},
title = {
Text(text = "Minimum Swipe Length: ${minSwipeLengthState.value.toInt()}")
},
onValueChangeFinished = {
updateAppSettings(
appSettingsViewModel,
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -341,6 +382,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -369,6 +411,7 @@ fun LookAndFeelActivity(
keySizeState,
animationSpeedState,
animationHelperSpeedState,
minSwipeLengthState,
positionState,
autoCapitalizeState,
vibrateOnTapState,
Expand Down Expand Up @@ -398,6 +441,7 @@ private fun updateAppSettings(
keySizeState: SettingValueState<Float>,
animationSpeedState: SettingValueState<Float>,
animationHelperSpeedState: SettingValueState<Float>,
minSwipeLengthState: SettingValueState<Float>,
positionState: SettingValueState<Int>,
autoCapitalizeState: SettingValueState<Boolean>,
vibrateOnTapState: SettingValueState<Boolean>,
Expand All @@ -412,6 +456,7 @@ private fun updateAppSettings(
keySize = keySizeState.value.toInt(),
animationSpeed = animationSpeedState.value.toInt(),
animationHelperSpeed = animationHelperSpeedState.value.toInt(),
minSwipeLength = minSwipeLengthState.value.toInt(),
position = positionState.value,
autoCapitalize = autoCapitalizeState.value.compareTo(false),
vibrateOnTap = vibrateOnTapState.value.compareTo(false),
Expand Down
81 changes: 51 additions & 30 deletions app/src/main/java/com/dessalines/thumbkey/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.atan2
import kotlin.math.pow
import kotlin.math.sqrt

const val TAG = "com.thumbkey"

Expand Down Expand Up @@ -73,39 +75,58 @@ fun keyboardPositionToAlignment(position: KeyboardPosition): Alignment {
}
}

fun swipeDirection(x: Float, y: Float): SwipeDirection {
val angleDir = (atan2(x.toDouble(), y.toDouble()) / Math.PI * 180)
val angle = if (angleDir < 0) {
360 + angleDir
} else {
angleDir
}
/**
* If this doesn't meet the minimum swipe length, it returns null
*/
fun swipeDirection(x: Float, y: Float, minSwipeLength: Int): SwipeDirection? {
val xD = x.toDouble()
val yD = y.toDouble()

return when (angle) {
in 22.5..67.5 -> {
SwipeDirection.BOTTOM_RIGHT
}
in 67.5..112.5 -> {
SwipeDirection.RIGHT
}
in 112.5..157.5 -> {
SwipeDirection.TOP_RIGHT
}
in 157.5..202.5 -> {
SwipeDirection.TOP
}
in 202.5..247.5 -> {
SwipeDirection.TOP_LEFT
}
in 247.5..292.5 -> {
SwipeDirection.LEFT
}
in 292.5..337.5 -> {
SwipeDirection.BOTTOM_LEFT
val swipeLength = sqrt(xD.pow(2) + yD.pow(2))

if (swipeLength > minSwipeLength) {
val angleDir = (atan2(xD, yD) / Math.PI * 180)
val angle = if (angleDir < 0) {
360 + angleDir
} else {
angleDir
}
else -> {
SwipeDirection.BOTTOM

return when (angle) {
in 22.5..67.5 -> {
SwipeDirection.BOTTOM_RIGHT
}

in 67.5..112.5 -> {
SwipeDirection.RIGHT
}

in 112.5..157.5 -> {
SwipeDirection.TOP_RIGHT
}

in 157.5..202.5 -> {
SwipeDirection.TOP
}

in 202.5..247.5 -> {
SwipeDirection.TOP_LEFT
}

in 247.5..292.5 -> {
SwipeDirection.LEFT
}

in 292.5..337.5 -> {
SwipeDirection.BOTTOM_LEFT
}

else -> {
SwipeDirection.BOTTOM
}
}
} else {
return null
}
}

Expand Down

0 comments on commit 24fcb57

Please sign in to comment.