Skip to content

Commit

Permalink
tidy up ws reconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisme committed Oct 30, 2021
1 parent 2d4809c commit 7cca7fd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
56 changes: 32 additions & 24 deletions lib/screens/utils/alert.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:notifi/user.dart';
import 'package:notifi/utils/icons.dart';
import 'package:notifi/utils/pallete.dart';
import 'package:provider/provider.dart';

Future<void> showAlert(BuildContext context, String title, String description,
{int duration, int gravity, VoidCallback onOkPressed}) {
Expand Down Expand Up @@ -52,31 +54,37 @@ void showAlertSnackBar(BuildContext context, String message) {
duration: const Duration(days: 1),
backgroundColor: MyColour.transparent,
elevation: 0,
content: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: const EdgeInsets.all(5.0),
decoration: const BoxDecoration(
color: MyColour.black,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
const Icon(
Akaricons.triangleAlert,
color: MyColour.white,
size: 10,
),
Text(' $message',
style: const TextStyle(
color: MyColour.white,
fontSize: 10,
fontWeight: FontWeight.w400)),
],
content: Listener(
onPointerDown: (_) async {
await Provider.of<User>(context, listen: false).initWSS();
ScaffoldMessenger.of(context).removeCurrentSnackBar();
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: const EdgeInsets.all(5.0),
decoration: const BoxDecoration(
color: MyColour.black,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
const Icon(
Akaricons.triangleAlert,
color: MyColour.white,
size: 13,
),
Text(' $message',
style: const TextStyle(
color: MyColour.white,
fontSize: 13,
fontWeight: FontWeight.w400)),
],
),
),
),
],
],
),
));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
25 changes: 10 additions & 15 deletions lib/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ class User with ChangeNotifier {
////////
// ws //
////////
Future<void> initWSS() async {
await closeWS();
Future<void> initWSS({bool shouldDelay = false}) async {
await closeWS(shouldDelay: shouldDelay);
await connectToWS();
_ws.sink.add('.');
}
Expand All @@ -124,33 +124,28 @@ class User with ChangeNotifier {
headers: headers, pingInterval: const Duration(seconds: 3));

setErr(hasErr: false);
bool _wsError = false;
_ws.stream.listen((dynamic streamData) async {
_wsError = false;

final List<String> receivedMsgUUIDs = await _handleMessage(streamData);
if (receivedMsgUUIDs != null && _ws != null) {
_ws.sink.add(jsonEncode(receivedMsgUUIDs));
}
// ignore: always_specify_types
}, onError: (e) async {
setErr(hasErr: true);
_wsError = true;
L.w('Problem with WS: $e');
await initWSS(shouldDelay: true);
}, onDone: () async {
L.i('WS connection closed. ${_user.credentials} error: $_wsError');
await closeWS(shouldDelay: true);
await connectToWS();
L.i('WS connection closed. ${_user.credentials}');
setErr(hasErr: true);
}, cancelOnError: true);
}

Future<void> closeWS({bool shouldDelay = false}) async {
Future<void> closeWS({bool shouldDelay}) async {
if (_ws != null) {
L.i('Closing already open WS...');
await _ws.sink.close(status.normalClosure, 'new code!');
_ws.sink.close(status.normalClosure, 'new code!');
_ws = null;
if (shouldDelay)
await Future<dynamic>.delayed(const Duration(seconds: 2));
await Future<dynamic>.delayed(Duration(seconds: shouldDelay ? 5 : 1));
}
}

Expand Down Expand Up @@ -258,9 +253,9 @@ class User with ChangeNotifier {
_tmpErr = hasErr;
Future<dynamic>.delayed(const Duration(seconds: 1), () {
if (_tmpErr == hasErr) {
if (_tmpErr && !Globals.isIntegration) {
if (_tmpErr) {
MenuBarIcon.setErr();
showAlertSnackBar(_snackContext, 'Network Error!');
showAlertSnackBar(_snackContext, 'Network Error! Tap to try again.');
} else {
MenuBarIcon.revertErr();
ScaffoldMessenger.of(_snackContext).clearSnackBars();
Expand Down
18 changes: 9 additions & 9 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ void showToast(String msg, BuildContext context, {int duration, int gravity}) {

Future<String> getDeviceUUID() async {
if (Platform.isLinux || Globals.isIntegration) {
Uuid uuid = Uuid();
return uuid.v4();
return Uuid().v4();
}
return platform.invokeMethod('UUID');
}
Expand All @@ -122,23 +121,24 @@ bool get shouldUseFirebase {

class L {
static void d(String msg) {
// ignore: avoid_print
print(msg);
_print(msg);
}

static void i(String msg) {
// ignore: avoid_print
print(msg);
_print(msg);
}

static void w(String msg) {
// ignore: avoid_print
print(msg);
_print(msg);
}

static void e(String msg) {
_print(msg);
}

static void _print(String msg) {
// ignore: avoid_print
print(msg);
print(DateTime.now().toString() + ': ' + msg);
}
}

Expand Down

0 comments on commit 7cca7fd

Please sign in to comment.