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