From 9b6a30acbf57aa74b84deb29def05fc58fcec669 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 17 Jul 2024 16:18:09 -0400 Subject: [PATCH] history controller fix for example code --- veilid-flutter/example/lib/app.dart | 18 +++-- .../example/lib/history_wrapper.dart | 66 ------------------- 2 files changed, 8 insertions(+), 76 deletions(-) delete mode 100644 veilid-flutter/example/lib/history_wrapper.dart diff --git a/veilid-flutter/example/lib/app.dart b/veilid-flutter/example/lib/app.dart index 4515d99b..6b735765 100644 --- a/veilid-flutter/example/lib/app.dart +++ b/veilid-flutter/example/lib/app.dart @@ -6,7 +6,7 @@ import 'package:flutter/material.dart'; import 'package:loggy/loggy.dart'; import 'package:veilid/veilid.dart'; -import 'history_wrapper.dart'; +import 'history_text_editing_controller.dart'; import 'log.dart'; import 'log_terminal.dart'; import 'veilid_theme.dart'; @@ -24,13 +24,13 @@ class _MyAppState extends State with UiLoggy { bool _startedUp = false; Stream? _updateStream; Future? _updateProcessor; - final _debugHistoryWrapper = HistoryWrapper(); + late final HistoryTextEditingController _historyController; String? _errorText; @override void initState() { super.initState(); - + _historyController = HistoryTextEditingController(setState: setState); unawaited(initPlatformState()); } @@ -177,10 +177,10 @@ class _MyAppState extends State with UiLoggy { padding: const EdgeInsets.all(5), child: Row(children: [ Expanded( - child: pad(_debugHistoryWrapper.wrap( - setState, + child: pad( TextField( - controller: _debugHistoryWrapper.controller, + controller: _historyController.controller, + focusNode: _historyController.focusNode, decoration: newInputDecoration( 'Debug Command', _errorText, _startedUp), textInputAction: TextInputAction.unspecified, @@ -197,16 +197,14 @@ class _MyAppState extends State with UiLoggy { } final res = await Veilid.instance.debug(v); loggy.info(res); - setState(() { - _debugHistoryWrapper.submit(v); - }); + _historyController.submit(v); } on VeilidAPIException catch (e) { setState(() { _errorText = e.toDisplayError(); }); } }), - ))), + )), pad( Column(children: [ const Text('Startup'), diff --git a/veilid-flutter/example/lib/history_wrapper.dart b/veilid-flutter/example/lib/history_wrapper.dart deleted file mode 100644 index ab87ffd8..00000000 --- a/veilid-flutter/example/lib/history_wrapper.dart +++ /dev/null @@ -1,66 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; - -// TextField History Wrapper -class HistoryWrapper { - final List _history = []; - int _historyPosition = 0; - final _historyTextEditingController = TextEditingController(); - String _historyCurrentEdit = ''; - - TextEditingController get controller => _historyTextEditingController; - - void submit(String v) { - // add to history - if (_history.isEmpty || _history.last != v) { - _history.add(v); - if (_history.length > 100) { - _history.removeAt(0); - } - } - _historyPosition = _history.length; - _historyTextEditingController.text = ''; - } - - Widget wrap( - void Function(void Function())? stateSetter, TextField textField) { - final setState = stateSetter ?? (x) => x(); - return KeyboardListener( - onKeyEvent: (event) { - setState(() { - if (event.runtimeType == KeyDownEvent && - event.logicalKey == LogicalKeyboardKey.arrowUp) { - if (_historyPosition > 0) { - if (_historyPosition == _history.length) { - _historyCurrentEdit = _historyTextEditingController.text; - } - _historyPosition -= 1; - _historyTextEditingController.text = _history[_historyPosition]; - } - } else if (event.runtimeType == KeyDownEvent && - event.logicalKey == LogicalKeyboardKey.arrowDown) { - if (_historyPosition < _history.length) { - _historyPosition += 1; - if (_historyPosition == _history.length) { - _historyTextEditingController.text = _historyCurrentEdit; - } else { - _historyTextEditingController.text = _history[_historyPosition]; - } - } - } else if (event.runtimeType == KeyDownEvent) { - _historyPosition = _history.length; - _historyCurrentEdit = _historyTextEditingController.text; - } - }); - }, - focusNode: FocusNode(onKeyEvent: (node, event) { - if (event.logicalKey == LogicalKeyboardKey.arrowDown || - event.logicalKey == LogicalKeyboardKey.arrowUp) { - return KeyEventResult.handled; - } - return KeyEventResult.ignored; - }), - child: textField, - ); - } -}