From 200622cf7a979f5c77868681506963737521f499 Mon Sep 17 00:00:00 2001 From: johnflynn Date: Wed, 31 Jan 2024 23:51:41 -0500 Subject: [PATCH 1/4] Removed .db from commit history --- .gitignore | 1 + api/pastey.db | Bin 12288 -> 0 bytes 2 files changed, 1 insertion(+) delete mode 100644 api/pastey.db diff --git a/.gitignore b/.gitignore index accd526..96e35b7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ build/ pastey.db +api/pastey.db api/static ### STS ### diff --git a/api/pastey.db b/api/pastey.db deleted file mode 100644 index e1259cb0e54c8bbb1b45da65d0db9bd0d6cd6f4a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI$ze~eF6bJCT`ujj4WN=Y<9Rv#^h?ASG1{G`78bPO$nrl5dliHrxRc9yv1OFFS zhyDu={tdeMQmG&)PCCf<<=!Qidq>D;yF1t~8>gv}w$CF+%VJ7MDOQPu5M9n8&Ou0i z7#onrNvwu9q^euXp6 z>d!^*6DMinbmG38YU{}9I5g*)wklz;5mZ0E@V?ka9e3D@zpYY*x!maViZ6vpV%_ar zSl->&qDz Date: Thu, 1 Feb 2024 00:06:37 -0500 Subject: [PATCH 2/4] Corrected issue with cursor position when changing lines --- ui/lib/screens/bin_screen.dart | 2 ++ ui/lib/widgets/content_text_field.dart | 7 +------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ui/lib/screens/bin_screen.dart b/ui/lib/screens/bin_screen.dart index 39d9740..ce8cb76 100644 --- a/ui/lib/screens/bin_screen.dart +++ b/ui/lib/screens/bin_screen.dart @@ -94,7 +94,9 @@ class _BinScreenState extends State { wschannel.stream.listen((data) { setState(() { message = data; + TextSelection previousSelection = controller.selection; controller.text = data; + controller.selection = previousSelection; }); }); }); diff --git a/ui/lib/widgets/content_text_field.dart b/ui/lib/widgets/content_text_field.dart index fd02a1c..c184647 100644 --- a/ui/lib/widgets/content_text_field.dart +++ b/ui/lib/widgets/content_text_field.dart @@ -22,12 +22,7 @@ class ContentTextField extends StatelessWidget { return PrimaryContainer( radius: 10, child: TextField( - onChanged: (value) { - TextSelection previousSelection = controller.selection; - controller.text = value; - controller.selection = previousSelection; - onChanged(value); - }, + onChanged: onChanged, maxLines: maxLines, style: const TextStyle(fontSize: 16, color: Colors.white), controller: controller, From cae41efa5cc877385c6abe53f9e5c490da0bca96 Mon Sep 17 00:00:00 2001 From: johnflynn Date: Thu, 1 Feb 2024 00:23:05 -0500 Subject: [PATCH 3/4] added _ modifier where possible. Moved Override methods to beginning of classes for consistency --- ui/lib/screens/bin_screen.dart | 95 ++++++++++++++-------------- ui/lib/widgets/current_bin_list.dart | 30 ++++----- ui/lib/widgets/new_bin_button.dart | 18 ++++-- 3 files changed, 74 insertions(+), 69 deletions(-) diff --git a/ui/lib/screens/bin_screen.dart b/ui/lib/screens/bin_screen.dart index ce8cb76..d412129 100644 --- a/ui/lib/screens/bin_screen.dart +++ b/ui/lib/screens/bin_screen.dart @@ -17,15 +17,49 @@ class BinScreen extends StatefulWidget { } class _BinScreenState extends State { - Bin? bin; + Bin? _bin; - late WebSocketChannel channel; + late WebSocketChannel _channel; - String message = 'No data received'; + String _message = 'No data received'; - final TextEditingController controller = TextEditingController(text: ''); + final TextEditingController _controller = TextEditingController(text: ''); - final TextEditingController titleController = TextEditingController(text: ''); + final TextEditingController _titleController = + TextEditingController(text: ''); + + @override + void initState() { + super.initState(); + + _getBin().then((updatedBin) { + setState(() { + _bin = updatedBin; + _titleController.text = updatedBin.title; + }); + }); + + _getWebSocketChannel().then((wschannel) { + setState(() { + _channel = wschannel; + }); + + wschannel.stream.listen((data) { + setState(() { + _message = data; + TextSelection previousSelection = _controller.selection; + _controller.text = data; + _controller.selection = previousSelection; + }); + }); + }); + } + + @override + void dispose() { + _channel.sink.close(); + super.dispose(); + } @override Widget build(BuildContext context) { @@ -40,7 +74,7 @@ class _BinScreenState extends State { children: [ Padding( padding: const EdgeInsets.all(20), - child: bin != null + child: _bin != null ? Focus( onFocusChange: (focused) { if (!focused) { @@ -50,8 +84,8 @@ class _BinScreenState extends State { child: ContentTextField( maxLines: 1, onChanged: (changedTitle) {}, - controller: titleController, - content: bin!.title), + controller: _titleController, + content: _bin!.title), ) : const CircularProgressIndicator(), ), @@ -59,9 +93,9 @@ class _BinScreenState extends State { child: Padding( padding: const EdgeInsets.all(20), child: ContentTextField( - content: message, - onChanged: updateContent, - controller: controller, + content: _message, + onChanged: _updateContent, + controller: _controller, ), ), ), @@ -71,35 +105,8 @@ class _BinScreenState extends State { ); } - void updateContent(String updatedContent) { - channel.sink.add(updatedContent); - } - - @override - void initState() { - super.initState(); - - _getBin().then((_bin) { - setState(() { - bin = _bin; - titleController.text = _bin.title; - }); - }); - - _getWebSocketChannel().then((wschannel) { - setState(() { - channel = wschannel; - }); - - wschannel.stream.listen((data) { - setState(() { - message = data; - TextSelection previousSelection = controller.selection; - controller.text = data; - controller.selection = previousSelection; - }); - }); - }); + void _updateContent(String updatedContent) { + _channel.sink.add(updatedContent); } Future _getWebSocketChannel() async { @@ -122,17 +129,11 @@ class _BinScreenState extends State { final host = await API_HOST; final response = await http.put( Uri.parse('$HTTP_PROTOCOL://$host/bin/${widget.binId}/title'), - body: titleController.text, + body: _titleController.text, ); if (response.statusCode != 200) { throw Exception('Failed to update bin title'); } } - - @override - void dispose() { - channel.sink.close(); - super.dispose(); - } } diff --git a/ui/lib/widgets/current_bin_list.dart b/ui/lib/widgets/current_bin_list.dart index df9c817..e6a3348 100644 --- a/ui/lib/widgets/current_bin_list.dart +++ b/ui/lib/widgets/current_bin_list.dart @@ -21,21 +21,7 @@ class _CurrentBinListState extends State { @override void initState() { super.initState(); - fetchBins(); - } - - Future fetchBins() async { - final host = await API_HOST; - final response = await http.get(Uri.parse('$HTTP_PROTOCOL://$host/bin')); - - if (response.statusCode == 200) { - final List jsonBins = json.decode(response.body); - setState(() { - bins = jsonBins.map((jsonBin) => Bin.fromJson(jsonBin)).toList(); - }); - } else { - throw Exception('Failed to load bins'); - } + _fetchBins(); } @override @@ -61,4 +47,18 @@ class _CurrentBinListState extends State { ), ); } + + Future _fetchBins() async { + final host = await API_HOST; + final response = await http.get(Uri.parse('$HTTP_PROTOCOL://$host/bin')); + + if (response.statusCode == 200) { + final List jsonBins = json.decode(response.body); + setState(() { + bins = jsonBins.map((jsonBin) => Bin.fromJson(jsonBin)).toList(); + }); + } else { + throw Exception('Failed to load bins'); + } + } } diff --git a/ui/lib/widgets/new_bin_button.dart b/ui/lib/widgets/new_bin_button.dart index 7cde9f5..b36b9f4 100644 --- a/ui/lib/widgets/new_bin_button.dart +++ b/ui/lib/widgets/new_bin_button.dart @@ -9,7 +9,16 @@ import 'dart:convert'; class NewBinButton extends StatelessWidget { const NewBinButton({super.key}); - Future addNewBin(GoRouter router) async { + @override + Widget build(BuildContext context) { + return BinButton( + text: 'New Bin', + onPressed: () { + _addNewBin(GoRouter.of(context)); + }); + } + + Future _addNewBin(GoRouter router) async { final host = await API_HOST; final response = await http.post(Uri.parse('$HTTP_PROTOCOL://$host/bin')); @@ -20,9 +29,4 @@ class NewBinButton extends StatelessWidget { router.push('/bin/${bin.id}'); } } - - @override - Widget build(BuildContext context) { - return BinButton(text: 'New Bin', onPressed: () { addNewBin(GoRouter.of(context)); }); - } -} \ No newline at end of file +} From d36a726d36d785d092dbbdf30d9c46dfcae5c893 Mon Sep 17 00:00:00 2001 From: johnflynn Date: Tue, 20 Feb 2024 23:25:57 -0500 Subject: [PATCH 4/4] Updated bin screen to changed updating bin content to occur every .5 seconds if the content locally has changed. Prevents lag when typing fast. --- ui/lib/screens/bin_screen.dart | 45 +++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/ui/lib/screens/bin_screen.dart b/ui/lib/screens/bin_screen.dart index d412129..cf864e1 100644 --- a/ui/lib/screens/bin_screen.dart +++ b/ui/lib/screens/bin_screen.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:ui/main.dart'; @@ -21,10 +22,20 @@ class _BinScreenState extends State { late WebSocketChannel _channel; - String _message = 'No data received'; + // Timer function for sending data to the server. + late Timer _timer; - final TextEditingController _controller = TextEditingController(text: ''); + // The Websocket message. + var _message = ''; + /// true if data has been recieved from the web socket. Prevents data from being overwritten when initialized. + var _dataRecieved = false; + + /// Controller for the content of the bin + final TextEditingController _contentController = + TextEditingController(text: ''); + + /// Controller for the title of the bin final TextEditingController _titleController = TextEditingController(text: ''); @@ -32,6 +43,14 @@ class _BinScreenState extends State { void initState() { super.initState(); + /// Initialize timer with function to update bin content if bin was initialized and if the content has changed. + _timer = Timer.periodic(const Duration(milliseconds: 500), (timer) { + if (_dataRecieved && _message != _contentController.text) { + _channel.sink.add(_contentController.text); + } + }); + + /// Retrieves the bin title. _getBin().then((updatedBin) { setState(() { _bin = updatedBin; @@ -39,18 +58,21 @@ class _BinScreenState extends State { }); }); + /// Initialize websocket channel. _getWebSocketChannel().then((wschannel) { setState(() { _channel = wschannel; }); + /// Listen to websocket events. wschannel.stream.listen((data) { setState(() { _message = data; - TextSelection previousSelection = _controller.selection; - _controller.text = data; - _controller.selection = previousSelection; + TextSelection previousSelection = _contentController.selection; + _contentController.text = data; + _contentController.selection = previousSelection; }); + _dataRecieved = true; }); }); } @@ -58,6 +80,7 @@ class _BinScreenState extends State { @override void dispose() { _channel.sink.close(); + _timer.cancel(); super.dispose(); } @@ -93,9 +116,9 @@ class _BinScreenState extends State { child: Padding( padding: const EdgeInsets.all(20), child: ContentTextField( - content: _message, - onChanged: _updateContent, - controller: _controller, + content: !_dataRecieved ? 'No Message Recieved' : _message, + onChanged: (val) {}, + controller: _contentController, ), ), ), @@ -105,16 +128,15 @@ class _BinScreenState extends State { ); } - void _updateContent(String updatedContent) { - _channel.sink.add(updatedContent); - } + /// Returns the Websocket Channel. Future _getWebSocketChannel() async { final host = await API_HOST; return WebSocketChannel.connect( Uri.parse('$WS_PROTOCOL://$host/bin/${widget.binId}/ws')); } + /// Retrieves the current bin from the server. Future _getBin() async { final host = await API_HOST; final response = @@ -125,6 +147,7 @@ class _BinScreenState extends State { throw Exception('Failed to load bin'); } + /// Updates the title of the bin based on the state of _titleController Future _updateBinTitle() async { final host = await API_HOST; final response = await http.put(