Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 109 additions & 25 deletions app/src/main/java/com/olup/notable/components/EditorGestureReceiver.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.olup.notable

import io.shipbook.shipbooksdk.Log
import androidx.compose.foundation.gestures.*
import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.input.pointer.pointerInput
import com.olup.notable.EditorControlTower
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


Expand All @@ -25,6 +29,10 @@ fun EditorGestureReceiver(
) {

val coroutineScope = rememberCoroutineScope()
val appSettings by AppRepository(LocalContext.current)
.kvProxy
.observeKv("APP_SETTINGS", AppSettings.serializer(), AppSettings(version = 1))
.observeAsState()
Box(
modifier = Modifier
.pointerInput(Unit) {
Expand All @@ -34,7 +42,7 @@ fun EditorGestureReceiver(
val inputId = down.id

val initialPosition = down.position
val initialTimestamp = System.currentTimeMillis();
val initialTimestamp = System.currentTimeMillis()

var lastPosition = initialPosition
var lastTimestamp = initialTimestamp
Expand All @@ -50,16 +58,16 @@ fun EditorGestureReceiver(
val fingerChange = event.changes.filter { it.type == PointerType.Touch }
// is already consumed return
if (fingerChange.find { it.isConsumed } != null) {
Log.i(TAG, "Canceling gesture - already consumed")
return@awaitEachGesture
Log.i(TAG, "Canceling gesture - already consumemd")
}
fingerChange.forEach { it.consume() }

val eventReference =
fingerChange.find { it.id.value == inputId.value } ?: break

lastPosition = eventReference.position
lastTimestamp = System.currentTimeMillis();
lastTimestamp = System.currentTimeMillis()
inputsCount = fingerChange.size

if (fingerChange.any { !it.pressed }) break
Expand All @@ -75,20 +83,36 @@ fun EditorGestureReceiver(
if (withTimeoutOrNull(100) {
awaitFirstDown()
if (inputsCount == 1) {
state.isToolbarOpen = !state.isToolbarOpen
resolveGesture(
settings = appSettings,
default = AppSettings.defaultDoubleTapAction,
override = AppSettings::doubleTapAction,
state = state,
scope = coroutineScope,
previousPage = goToPreviousPage,
nextPage = goToNextPage,
)
}
} != null) return@awaitEachGesture

// in case of single tap
if (inputsCount == 2) {
state.mode = if (state.mode == Mode.Draw) Mode.Erase else Mode.Draw
resolveGesture(
settings = appSettings,
default = AppSettings.defaultTwoFingerTapAction,
override = AppSettings::twoFingerTapAction,
state = state,
scope = coroutineScope,
previousPage = goToPreviousPage,
nextPage = goToNextPage,
)
}
return@awaitEachGesture

}

val verticalDrag = lastPosition.y - initialPosition.y
val horinzontalDrag = lastPosition.x - initialPosition.x
val horizontalDrag = lastPosition.x - initialPosition.x


if (verticalDrag < -200) {
Expand All @@ -113,33 +137,93 @@ fun EditorGestureReceiver(
}
}
}
if (horinzontalDrag < -200) {
if (horizontalDrag < -200) {
if (inputsCount == 1) {
goToNextPage()
resolveGesture(
settings = appSettings,
default = AppSettings.defaultSwipeLeftAction,
override = AppSettings::swipeLeftAction,
state = state,
scope = coroutineScope,
previousPage = goToPreviousPage,
nextPage = goToNextPage,
)
} else if (inputsCount == 2) {
Log.i(TAG, "Redo")
coroutineScope.launch {
History.moveHistory(UndoRedoType.Redo)
DrawCanvas.refreshUi.emit(Unit)
}
resolveGesture(
settings = appSettings,
default = AppSettings.defaultTwoFingerSwipeLeftAction,
override = AppSettings::twoFingerSwipeLeftAction,
state = state,
scope = coroutineScope,
previousPage = goToPreviousPage,
nextPage = goToNextPage,
)
}
}
if (horinzontalDrag > 200) {
if (horizontalDrag > 200) {
if (inputsCount == 1) {
goToPreviousPage()
resolveGesture(
settings = appSettings,
default = AppSettings.defaultSwipeRightAction,
override = AppSettings::swipeRightAction,
state = state,
scope = coroutineScope,
previousPage = goToPreviousPage,
nextPage = goToNextPage,
)
} else if (inputsCount == 2) {
Log.i(TAG, "Undo")
coroutineScope.launch {
History.moveHistory(UndoRedoType.Undo)
DrawCanvas.refreshUi.emit(Unit)
}
resolveGesture(
settings = appSettings,
default = AppSettings.defaultTwoFingerSwipeRightAction,
override = AppSettings::twoFingerSwipeRightAction,
state = state,
scope = coroutineScope,
previousPage = goToPreviousPage,
nextPage = goToNextPage,
)
}

}

}
}
.fillMaxWidth()
.fillMaxHeight()
)
}
}

private fun resolveGesture(
settings: AppSettings?,
default: AppSettings.GestureAction,
override: AppSettings.() -> AppSettings.GestureAction?,
state: EditorState,
scope: CoroutineScope,
previousPage: () -> Unit,
nextPage: () -> Unit,
) {
when (if (settings != null) override(settings) else default) {
null -> Log.i(TAG, "No Action")
AppSettings.GestureAction.PreviousPage -> previousPage()
AppSettings.GestureAction.NextPage -> nextPage()

AppSettings.GestureAction.ChangeTool ->
state.mode = if (state.mode == Mode.Draw) Mode.Erase else Mode.Draw

AppSettings.GestureAction.ToggleZen ->
state.isToolbarOpen = !state.isToolbarOpen

AppSettings.GestureAction.Undo -> {
Log.i(TAG, "Undo")
scope.launch {
History.moveHistory(UndoRedoType.Undo)
DrawCanvas.refreshUi.emit(Unit)
}
}

AppSettings.GestureAction.Redo -> {
Log.i(TAG, "Redo")
scope.launch {
History.moveHistory(UndoRedoType.Redo)
DrawCanvas.refreshUi.emit(Unit)
}
}
}
}
14 changes: 5 additions & 9 deletions app/src/main/java/com/olup/notable/components/Select.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowDropDown
import androidx.compose.material.icons.rounded.Edit
import androidx.compose.material.icons.sharp.Edit
import androidx.compose.material.icons.sharp.KeyboardArrowDown
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -20,11 +17,11 @@ import androidx.compose.ui.window.Popup
import com.olup.notable.noRippleClickable

@Composable
fun SelectMenu(options: List<Pair<String, String>>, value: String, onChange: (String) -> Unit) {
fun <T> SelectMenu(options: List<Pair<T, String>>, value: T, onChange: (T) -> Unit) {
var isExpanded by remember { mutableStateOf(false) }

Box() {
Row() {
Box {
Row {
Text(text = options.find { it.first == value }?.second ?: "Undefined",
fontWeight = FontWeight.Light,
modifier = Modifier.noRippleClickable { isExpanded = true })
Expand Down Expand Up @@ -52,11 +49,10 @@ fun SelectMenu(options: List<Pair<String, String>>, value: String, onChange: (St
.noRippleClickable {
onChange(it.first)
isExpanded = false
})
}
)
}
}

}
}

}
Loading