Skip to content

Commit

Permalink
Merge pull request #5 from Neuman968/cursorFix
Browse files Browse the repository at this point in the history
Cursor fix
  • Loading branch information
Neuman968 authored Feb 21, 2024
2 parents 3b4ac21 + d36a726 commit 7589b33
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build/


pastey.db
api/pastey.db

api/static
### STS ###
Expand Down
Binary file removed api/pastey.db
Binary file not shown.
118 changes: 72 additions & 46 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 @@ -17,15 +18,71 @@ class BinScreen extends StatefulWidget {
}

class _BinScreenState extends State<BinScreen> {
Bin? bin;
Bin? _bin;

late WebSocketChannel channel;
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 = '';

final TextEditingController titleController = TextEditingController(text: '');
/// 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 = _contentController.selection;
_contentController.text = data;
_contentController.selection = previousSelection;
});
_dataRecieved = true;
});
});
}

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

@override
Widget build(BuildContext context) {
Expand All @@ -40,7 +97,7 @@ class _BinScreenState extends State<BinScreen> {
children: [
Padding(
padding: const EdgeInsets.all(20),
child: bin != null
child: _bin != null
? Focus(
onFocusChange: (focused) {
if (!focused) {
Expand All @@ -50,18 +107,18 @@ class _BinScreenState extends State<BinScreen> {
child: ContentTextField(
maxLines: 1,
onChanged: (changedTitle) {},
controller: titleController,
content: bin!.title),
controller: _titleController,
content: _bin!.title),
)
: const CircularProgressIndicator(),
),
Expanded(
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 @@ -71,41 +128,15 @@ class _BinScreenState extends State<BinScreen> {
);
}

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;
controller.text = data;
});
});
});
}

/// 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 @@ -116,21 +147,16 @@ 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(
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();
}
}
7 changes: 1 addition & 6 deletions ui/lib/widgets/content_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 15 additions & 15 deletions ui/lib/widgets/current_bin_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,7 @@ class _CurrentBinListState extends State<CurrentBinList> {
@override
void initState() {
super.initState();
fetchBins();
}

Future<void> fetchBins() async {
final host = await API_HOST;
final response = await http.get(Uri.parse('$HTTP_PROTOCOL://$host/bin'));

if (response.statusCode == 200) {
final List<dynamic> jsonBins = json.decode(response.body);
setState(() {
bins = jsonBins.map((jsonBin) => Bin.fromJson(jsonBin)).toList();
});
} else {
throw Exception('Failed to load bins');
}
_fetchBins();
}

@override
Expand All @@ -61,4 +47,18 @@ class _CurrentBinListState extends State<CurrentBinList> {
),
);
}

Future<void> _fetchBins() async {
final host = await API_HOST;
final response = await http.get(Uri.parse('$HTTP_PROTOCOL://$host/bin'));

if (response.statusCode == 200) {
final List<dynamic> jsonBins = json.decode(response.body);
setState(() {
bins = jsonBins.map((jsonBin) => Bin.fromJson(jsonBin)).toList();
});
} else {
throw Exception('Failed to load bins');
}
}
}
18 changes: 11 additions & 7 deletions ui/lib/widgets/new_bin_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ import 'dart:convert';
class NewBinButton extends StatelessWidget {
const NewBinButton({super.key});

Future<void> addNewBin(GoRouter router) async {
@override
Widget build(BuildContext context) {
return BinButton(
text: 'New Bin',
onPressed: () {
_addNewBin(GoRouter.of(context));
});
}

Future<void> _addNewBin(GoRouter router) async {
final host = await API_HOST;
final response = await http.post(Uri.parse('$HTTP_PROTOCOL://$host/bin'));

Expand All @@ -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)); });
}
}
}

0 comments on commit 7589b33

Please sign in to comment.