Send Android draft on button press
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -43,7 +42,6 @@ class _InputSyncPageState extends State<InputSyncPage> {
|
||||
final inputFocusNode = FocusNode();
|
||||
|
||||
WebSocketChannel? channel;
|
||||
Timer? syncDebounce;
|
||||
String lastSyncedText = '';
|
||||
bool connected = false;
|
||||
|
||||
@@ -51,12 +49,10 @@ class _InputSyncPageState extends State<InputSyncPage> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSavedConnection();
|
||||
inputController.addListener(_syncTextChange);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
syncDebounce?.cancel();
|
||||
channel?.sink.close();
|
||||
hostController.dispose();
|
||||
portController.dispose();
|
||||
@@ -88,12 +84,10 @@ class _InputSyncPageState extends State<InputSyncPage> {
|
||||
setState(() {
|
||||
connected = true;
|
||||
});
|
||||
_sendCurrentText(force: true);
|
||||
inputFocusNode.requestFocus();
|
||||
}
|
||||
|
||||
void _disconnect() {
|
||||
syncDebounce?.cancel();
|
||||
channel?.sink.close();
|
||||
channel = null;
|
||||
setState(() {
|
||||
@@ -101,41 +95,14 @@ class _InputSyncPageState extends State<InputSyncPage> {
|
||||
});
|
||||
}
|
||||
|
||||
void _syncTextChange() {
|
||||
void _sendCurrentText() {
|
||||
if (!connected || channel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final value = inputController.value;
|
||||
if (value.composing.isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
final currentText = value.text;
|
||||
if (currentText == lastSyncedText) {
|
||||
return;
|
||||
}
|
||||
|
||||
syncDebounce?.cancel();
|
||||
syncDebounce = Timer(const Duration(milliseconds: 800), _sendCurrentText);
|
||||
}
|
||||
|
||||
void _sendCurrentText({bool force = false}) {
|
||||
if (!connected || channel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final latestValue = inputController.value;
|
||||
if (latestValue.composing.isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!force && latestValue.text == lastSyncedText) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastSyncedText = latestValue.text;
|
||||
_send('replaceAll', text: latestValue.text);
|
||||
lastSyncedText = inputController.text;
|
||||
_send('replaceAll', text: inputController.text);
|
||||
inputFocusNode.requestFocus();
|
||||
}
|
||||
|
||||
void _send(String action, {String? text}) {
|
||||
@@ -182,6 +149,12 @@ class _InputSyncPageState extends State<InputSyncPage> {
|
||||
onPressed: connected ? _disconnect : _connect,
|
||||
icon: Icon(connected ? Icons.link_off : Icons.link),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
IconButton.filled(
|
||||
tooltip: 'Send',
|
||||
onPressed: connected ? _sendCurrentText : null,
|
||||
icon: const Icon(Icons.send),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user