Skip to content

Commit

Permalink
VersionCode: 51
Browse files Browse the repository at this point in the history
Fixed delete text after selection issues
Optimize edit text when cursor below view
  • Loading branch information
Z-Siqi committed Jan 26, 2024
1 parent 063306a commit f6c5c84
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 35 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.sqz.writingboard"
minSdk 29
targetSdk 34
versionCode 50
versionName "0.5.0"
versionCode 51
versionName "0.5.1-Preview"
}

buildTypes {
Expand Down Expand Up @@ -50,8 +50,8 @@ dependencies {
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3-android:1.2.0-beta02'
implementation 'androidx.compose.foundation:foundation-android:1.6.0-rc01'
implementation 'androidx.compose.material3:material3-android:1.2.0-rc01'
implementation 'androidx.compose.foundation:foundation-android:1.7.0-alpha01'
debugImplementation 'androidx.compose.ui:ui-tooling:1.5.4'
//ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0"
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 50,
"versionName": "0.5.0",
"versionCode": 51,
"versionName": "0.5.1-Preview",
"outputFile": "app-release.apk"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView

@Composable
Expand Down Expand Up @@ -37,4 +44,55 @@ fun KeyboardVisibilityObserver(
rootView.viewTreeObserver.removeOnGlobalLayoutListener(listener)
}
}
}

@Suppress("unused")
class KeyboardHeight {
companion object {
val currentPx @Composable get() = KeyboardHeight().currentPx()
val screenHigh @Composable get() = KeyboardHeight().screenHigh()
}

@Composable
private fun screenHigh(): Int {
return (LocalConfiguration.current.screenHeightDp * LocalDensity.current.density).toInt()
}

@Composable
private fun currentPx(): Int {
val context = LocalContext.current
val localScreenHeight =
(LocalConfiguration.current.screenHeightDp * LocalDensity.current.density).toInt()
val rootView = LocalView.current
val inputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val returnZero = 0
var height by remember { mutableIntStateOf(0) }
var notZero by remember { mutableStateOf(false) }
var calculateNotZeroValue by remember { mutableIntStateOf(0) }
DisposableEffect(rootView, inputMethodManager) {
val listener = ViewTreeObserver.OnGlobalLayoutListener {
val rect = Rect()
rootView.getWindowVisibleDisplayFrame(rect)
val keyboardNowHeight = localScreenHeight - rect.bottom
val keyboardHeight = if (keyboardNowHeight < 0) {
notZero = true
calculateNotZeroValue = keyboardNowHeight * -1
returnZero
} else {
if (notZero) {
keyboardNowHeight + calculateNotZeroValue
} else {
keyboardNowHeight
}
}
height = keyboardHeight
}
rootView.viewTreeObserver.addOnGlobalLayoutListener(listener)
onDispose {
rootView.viewTreeObserver.removeOnGlobalLayoutListener(listener)
}
}
return height
}
}
94 changes: 69 additions & 25 deletions app/src/main/java/com/sqz/writingboard/ui/WritingBoardText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.ContentValues
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.MotionEvent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.gestures.detectVerticalDragGestures
Expand All @@ -27,11 +28,19 @@ import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextRange
Expand All @@ -51,6 +60,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.sqz.writingboard.R
import com.sqz.writingboard.classes.ValueState
import com.sqz.writingboard.component.KeyboardHeight
import com.sqz.writingboard.dataStore
import com.sqz.writingboard.glance.WritingBoardWidget
import com.sqz.writingboard.settingState
Expand All @@ -68,7 +78,7 @@ class WritingBoard : ViewModel() {
var textState by mutableStateOf(TextFieldValue())
}

@OptIn(ExperimentalFoundationApi::class)
@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
@Composable
fun WritingBoardText(scrollState: ScrollState, modifier: Modifier = Modifier) {

Expand Down Expand Up @@ -269,25 +279,14 @@ fun WritingBoardText(scrollState: ScrollState, modifier: Modifier = Modifier) {
}

if (fixChooseAllWay) {
var yInScreenFromClick by remember { mutableIntStateOf(0) }
val isWindowFocused = LocalWindowInfo.current.isWindowFocused
//fix choose all
var fixChooseAll by remember { mutableStateOf(false) }
if (
(!fixChooseAll) &&
(text2.text.selectionInChars.length == text2.text.length) &&
(valueState.initLayout) &&
(text2.text.isNotEmpty())
) {
text2.edit { placeCursorAtEnd() }
text2.edit { selectAll() }
fixChooseAll = true
} else if (text2.text.selectionInChars.length < text2.text.length) {
fixChooseAll = false
}
val density = LocalDensity.current.density
var initScroll by remember { mutableStateOf(false) }

//opt editing when reopen app
var judgeCondition by remember { mutableStateOf(false) }
if (!settingState.readSwitchState("opt_edit_text", context)) {
//opt editing when back app
var rememberScroll by remember { mutableIntStateOf(0) }
var scrollIt by remember { mutableStateOf(false) }
if (scrollIt && judgeCondition && valueState.isEditing && rememberScroll != 0) {
Expand All @@ -314,19 +313,39 @@ fun WritingBoardText(scrollState: ScrollState, modifier: Modifier = Modifier) {
judgeCondition = false
}
}
} else {
if (valueState.isEditing && valueState.softKeyboard && isWindowFocused) {
//opt edit when scrollable
val keyboardHeight = KeyboardHeight.currentPx
val screenHeight = KeyboardHeight.screenHigh
if (valueState.softKeyboard && !valueState.editingHorizontalScreen) {
val high = screenHeight - (48 * density).toInt()
LaunchedEffect(true) {
delay(200)
text2.edit { insert(text2.text.selectionInChars.start, " ") }
Handler(Looper.getMainLooper()).postDelayed(1) {
text2.edit { placeCursorBeforeCharAt(text2.text.selectionInChars.start -1) }
text2.edit { delete(text2.text.selectionInChars.start, text2.text.selectionInChars.start +1) }
delay(222)
if (yInScreenFromClick >= high - keyboardHeight) {
if (!initScroll) {
yInScreenFromClick += 100
initScroll = true
}
scrollState.scrollTo(scrollState.value + (yInScreenFromClick - (high - keyboardHeight)))
yInScreenFromClick = 0
}
}
}
} else {
//fix choose all (outdated, keep for some device)
var fixChooseAll by remember { mutableStateOf(false) }
if (
(!fixChooseAll) &&
(text2.text.selectionInChars.length == text2.text.length) &&
(valueState.initLayout) &&
(text2.text.isNotEmpty())
) {
text2.edit { placeCursorAtEnd() }
text2.edit { selectAll() }
fixChooseAll = true
} else if (text2.text.selectionInChars.length < text2.text.length) {
fixChooseAll = false
}
}

//catch typing
if (text2.undoState.canUndo) {
autoSaveByChar++
Expand All @@ -353,6 +372,31 @@ fun WritingBoardText(scrollState: ScrollState, modifier: Modifier = Modifier) {
}
.pointerInput(Unit) {
detectVerticalDragGestures { _, _ -> }
}
.pointerInteropFilter { motionEvent: MotionEvent ->
//detect screen y coordinate when click
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
val y = motionEvent.y
yInScreenFromClick = y.toInt() + (48 * density).toInt()
Log.i("WritingBoardTag", "$yInScreenFromClick")
}
}
false
}
.onKeyEvent { keyEvent ->
//fix delete after choose errors
if (keyEvent.type == KeyEventType.KeyDown && keyEvent.key == Key.Backspace) {
text2.edit {
delete(
text2.text.selectionInChars.end,
text2.text.selectionInChars.start
)
}
true
} else {
false
}
},
textStyle = TextStyle.Default.copy(
fontSize = fontSize,
Expand Down Expand Up @@ -396,4 +440,4 @@ fun WritingBoardText(scrollState: ScrollState, modifier: Modifier = Modifier) {
)
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<string name="language_no_support">Android13以下不支持语言设置 \n请尝试更新您的系统!</string>
<string name="request_widget">添加书写板小部件</string>
<string name="text_only">仅文本</string>
<string name="opt_edit_text">优化编辑文本时软键盘 \n遮挡文字的问题</string>
<string name="opt_edit_text">回到上个版本的编辑优化</string>
<string name="not_support">Android 13 以下不允许通过应用程序添加 QS 磁贴! \n\n请按照以下方式添加: \n1, 向下滑动以打开“快速设置”面板。\n2, 点击编辑按钮。\n3, 找到软件图块。\n4, 按住图块并将其拖动到活动图块列表中。</string>
<string name="dismiss">Dismiss</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rSG/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<string name="language_no_support">Android13以下不支持语言设置 \n请尝试更新您的系统!</string>
<string name="request_widget">添加WritingBoard小部件</string>
<string name="text_only">仅文本</string>
<string name="opt_edit_text">优化软键盘遮挡文本时 \n编辑文本的问题</string>
<string name="opt_edit_text">回到上个版本的优化</string>
<string name="not_support">Android 13 以下不允许通过应用程序添加 QS 磁贴! \n\n请按照以下方式添加: \n1, 向下滑动以打开“快速设置”面板。\n2, 点击编辑按钮。\n3, 找到软件图块。\n4, 按住图块并将其拖动到活动图块列表中。</string>
<string name="dismiss">Dismiss</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<string name="language_no_support">Android13以下不支援語言設定 \n請嘗試更新您的系統!</string>
<string name="request_widget">添加書寫板小工具</string>
<string name="text_only">仅文本</string>
<string name="opt_edit_text">優化軟鍵盤遮擋文字時 \n編輯文字的問題</string>
<string name="opt_edit_text">Back to last version of edit optimize</string>
<string name="not_support">Android 13 以下不允許透過應用程式新增 QS 磁貼! \n\n請按照以下方式添加: \n1, 向下滑動以開啟“快速設定”面板。\n2, 點選編輯按鈕。\n3, 找到應用程式圖塊。\n4, 按住圖塊並將其拖曳到活動圖塊清單中。</string>
<string name="dismiss">Dismiss</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<string name="language_no_support">Language setting is not support below Android13 \nPlease try to update your system!</string>
<string name="request_widget">Add WritingBoard Widget</string>
<string name="text_only">Text Only</string>
<string name="opt_edit_text">Optimization edit text when \ntext occlusion by soft keyboard</string>
<string name="opt_edit_text">Back to last version of edit optimize</string>
<string name="not_support">Add QS Tile by app not allow below Android 13! \n\nPlease follow below to add: \n1, Swipe down to open the Quick Settings panel.\n2, Tap the edit button.\n3, Find the app tile.\n4, Hold down the tile and drag it to the list of active tiles.</string>
<string name="dismiss">Dismiss</string>
</resources>

0 comments on commit f6c5c84

Please sign in to comment.