Skip to content

Commit

Permalink
Updated bin screen to changed updating bin content to occur every .5 …
Browse files Browse the repository at this point in the history
…seconds if the content locally has changed. Prevents lag when typing fast.
  • Loading branch information
jf9327 committed Feb 21, 2024
1 parent cae41ef commit d36a726
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions ui/lib/screens/bin_screen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:ui/main.dart';
Expand All @@ -21,43 +22,65 @@ class _BinScreenState extends State<BinScreen> {

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: '');

@override
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;
_titleController.text = updatedBin.title;
});
});

/// 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;
});
});
}

@override
void dispose() {
_channel.sink.close();
_timer.cancel();
super.dispose();
}

Expand Down Expand Up @@ -93,9 +116,9 @@ class _BinScreenState extends State<BinScreen> {
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,
),
),
),
Expand All @@ -105,16 +128,15 @@ class _BinScreenState extends State<BinScreen> {
);
}

void _updateContent(String updatedContent) {
_channel.sink.add(updatedContent);
}

/// Returns the Websocket Channel.
Future<WebSocketChannel> _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<Bin> _getBin() async {
final host = await API_HOST;
final response =
Expand All @@ -125,6 +147,7 @@ class _BinScreenState extends State<BinScreen> {
throw Exception('Failed to load bin');
}

/// Updates the title of the bin based on the state of _titleController
Future<void> _updateBinTitle() async {
final host = await API_HOST;
final response = await http.put(
Expand Down

0 comments on commit d36a726

Please sign in to comment.