From 33e46c9f22585046f5c08dc0ddbd52ba7a10bfbc Mon Sep 17 00:00:00 2001 From: Nutcake Date: Sat, 6 May 2023 16:57:08 +0200 Subject: [PATCH] Add first-load indicator and error handling --- lib/clients/messaging_client.dart | 17 ++++++++++------- lib/widgets/friends_list.dart | 11 ++++++++--- pubspec.yaml | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/clients/messaging_client.dart b/lib/clients/messaging_client.dart index 3c7102a..6a9ed1a 100644 --- a/lib/clients/messaging_client.dart +++ b/lib/clients/messaging_client.dart @@ -62,10 +62,9 @@ class MessagingClient extends ChangeNotifier { int _attempts = 0; WebSocket? _wsChannel; bool _isConnecting = false; - String _initError = ""; - bool _initDone = false; + String? _initStatus; - String get initError => _initError; + String? get initStatus => _initStatus; MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient}) : _apiClient = apiClient, _notificationClient = notificationClient { @@ -86,14 +85,18 @@ class MessagingClient extends ChangeNotifier { _wsChannel!.add(jsonEncode(data)+eofChar); } + void resetStatus() { + _initStatus = null; + notifyListeners(); + } + void refreshFriendsListWithErrorHandler () async { try { await refreshFriendsList(); - _initDone = true; } catch (e) { - _initError = "$e"; + _initStatus = "$e"; + notifyListeners(); } - notifyListeners(); } Future refreshFriendsList() async { @@ -121,7 +124,7 @@ class MessagingClient extends ChangeNotifier { aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2; return aVal.compareTo(bVal); })); - _initError = ""; + _initStatus = ""; notifyListeners(); } diff --git a/lib/widgets/friends_list.dart b/lib/widgets/friends_list.dart index 39516e2..8ff52c4 100644 --- a/lib/widgets/friends_list.dart +++ b/lib/widgets/friends_list.dart @@ -254,19 +254,24 @@ class _FriendsListState extends State { ], ), body: Stack( + alignment: Alignment.topCenter, children: [ Consumer( builder: (context, mClient, _) { - if (mClient.initError.isNotEmpty) { + if (mClient.initStatus == null) { + return const LinearProgressIndicator(); + } else if (mClient.initStatus!.isNotEmpty) { return Column( children: [ Expanded( child: DefaultErrorWidget( - message: mClient.initError, + message: mClient.initStatus, onRetry: () async { + mClient.resetStatus(); mClient.refreshFriendsListWithErrorHandler(); }, - )), + ), + ), ], ); } else { diff --git a/pubspec.yaml b/pubspec.yaml index 7ab28f8..1e79b97 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.1+1 +version: 1.0.2+1 environment: sdk: '>=2.19.6 <3.0.0' @@ -52,6 +52,7 @@ dependencies: flutter_local_notifications: ^14.0.0+1 collection: ^1.17.0 package_info_plus: ^3.1.2 + provider: ^6.0.5 dev_dependencies: flutter_test: