Add first-load indicator and error handling

This commit is contained in:
Nutcake 2023-05-06 16:57:08 +02:00
parent 766f5a94e2
commit 33e46c9f22
3 changed files with 20 additions and 11 deletions

View file

@ -62,10 +62,9 @@ class MessagingClient extends ChangeNotifier {
int _attempts = 0; int _attempts = 0;
WebSocket? _wsChannel; WebSocket? _wsChannel;
bool _isConnecting = false; bool _isConnecting = false;
String _initError = ""; String? _initStatus;
bool _initDone = false;
String get initError => _initError; String? get initStatus => _initStatus;
MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient}) MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient})
: _apiClient = apiClient, _notificationClient = notificationClient { : _apiClient = apiClient, _notificationClient = notificationClient {
@ -86,14 +85,18 @@ class MessagingClient extends ChangeNotifier {
_wsChannel!.add(jsonEncode(data)+eofChar); _wsChannel!.add(jsonEncode(data)+eofChar);
} }
void resetStatus() {
_initStatus = null;
notifyListeners();
}
void refreshFriendsListWithErrorHandler () async { void refreshFriendsListWithErrorHandler () async {
try { try {
await refreshFriendsList(); await refreshFriendsList();
_initDone = true;
} catch (e) { } catch (e) {
_initError = "$e"; _initStatus = "$e";
notifyListeners();
} }
notifyListeners();
} }
Future<void> refreshFriendsList() async { Future<void> refreshFriendsList() async {
@ -121,7 +124,7 @@ class MessagingClient extends ChangeNotifier {
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2; aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
return aVal.compareTo(bVal); return aVal.compareTo(bVal);
})); }));
_initError = ""; _initStatus = "";
notifyListeners(); notifyListeners();
} }

View file

@ -254,19 +254,24 @@ class _FriendsListState extends State<FriendsList> {
], ],
), ),
body: Stack( body: Stack(
alignment: Alignment.topCenter,
children: [ children: [
Consumer<MessagingClient>( Consumer<MessagingClient>(
builder: (context, mClient, _) { builder: (context, mClient, _) {
if (mClient.initError.isNotEmpty) { if (mClient.initStatus == null) {
return const LinearProgressIndicator();
} else if (mClient.initStatus!.isNotEmpty) {
return Column( return Column(
children: [ children: [
Expanded( Expanded(
child: DefaultErrorWidget( child: DefaultErrorWidget(
message: mClient.initError, message: mClient.initStatus,
onRetry: () async { onRetry: () async {
mClient.resetStatus();
mClient.refreshFriendsListWithErrorHandler(); mClient.refreshFriendsListWithErrorHandler();
}, },
)), ),
),
], ],
); );
} else { } else {

View file

@ -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 # 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 # 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. # 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: environment:
sdk: '>=2.19.6 <3.0.0' sdk: '>=2.19.6 <3.0.0'
@ -52,6 +52,7 @@ dependencies:
flutter_local_notifications: ^14.0.0+1 flutter_local_notifications: ^14.0.0+1
collection: ^1.17.0 collection: ^1.17.0
package_info_plus: ^3.1.2 package_info_plus: ^3.1.2
provider: ^6.0.5
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: