diff --git a/lib/screens/utils/alert.dart b/lib/screens/utils/alert.dart index 851d1c37..b52fa4ba 100644 --- a/lib/screens/utils/alert.dart +++ b/lib/screens/utils/alert.dart @@ -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 showAlert(BuildContext context, String title, String description, {int duration, int gravity, VoidCallback onOkPressed}) { @@ -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: [ - Container( - padding: const EdgeInsets.all(5.0), - decoration: const BoxDecoration( - color: MyColour.black, - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - 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(context, listen: false).initWSS(); + ScaffoldMessenger.of(context).removeCurrentSnackBar(); + }, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Container( + padding: const EdgeInsets.all(5.0), + decoration: const BoxDecoration( + color: MyColour.black, + ), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + 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); } diff --git a/lib/user.dart b/lib/user.dart index 1ceaff21..bb0c45d7 100644 --- a/lib/user.dart +++ b/lib/user.dart @@ -98,8 +98,8 @@ class User with ChangeNotifier { //////// // ws // //////// - Future initWSS() async { - await closeWS(); + Future initWSS({bool shouldDelay = false}) async { + await closeWS(shouldDelay: shouldDelay); await connectToWS(); _ws.sink.add('.'); } @@ -124,10 +124,7 @@ 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 receivedMsgUUIDs = await _handleMessage(streamData); if (receivedMsgUUIDs != null && _ws != null) { _ws.sink.add(jsonEncode(receivedMsgUUIDs)); @@ -135,22 +132,20 @@ class User with ChangeNotifier { // 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 closeWS({bool shouldDelay = false}) async { + Future 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.delayed(const Duration(seconds: 2)); + await Future.delayed(Duration(seconds: shouldDelay ? 5 : 1)); } } @@ -258,9 +253,9 @@ class User with ChangeNotifier { _tmpErr = hasErr; Future.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(); diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 4c2c7012..8a41cd27 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -110,8 +110,7 @@ void showToast(String msg, BuildContext context, {int duration, int gravity}) { Future getDeviceUUID() async { if (Platform.isLinux || Globals.isIntegration) { - Uuid uuid = Uuid(); - return uuid.v4(); + return Uuid().v4(); } return platform.invokeMethod('UUID'); } @@ -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); } }