Improve error handling in message view
This commit is contained in:
parent
57cbd4370b
commit
5d5b01b8a9
5 changed files with 232 additions and 202 deletions
|
@ -124,10 +124,13 @@ class ApiClient {
|
|||
if (response.statusCode == 403) {
|
||||
tryCachedLogin();
|
||||
// TODO: Show the login screen again if cached login was unsuccessful.
|
||||
throw "You are not authorized to do that.";
|
||||
throw "You are not authorized to do that";
|
||||
}
|
||||
if (response.statusCode == 500) {
|
||||
throw "Internal server error";
|
||||
}
|
||||
if (response.statusCode != 200) {
|
||||
throw "Unknown Error${kDebugMode ? ": ${response.statusCode}|${response.body}" : ""}";
|
||||
throw "Unknown Error: ${response.statusCode}${kDebugMode ? "|${response.body}" : ""}";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -196,6 +196,10 @@ class MessagingClient extends ChangeNotifier {
|
|||
|
||||
MessageCache _createUserMessageCache(String userId) => MessageCache(apiClient: _apiClient, userId: userId);
|
||||
|
||||
void deleteUserMessageCache(String userId) {
|
||||
_messageCache.remove(userId);
|
||||
}
|
||||
|
||||
Future<void> loadUserMessageCache(String userId) async {
|
||||
final cache = getUserMessageCache(userId) ?? _createUserMessageCache(userId);
|
||||
await cache.loadMessages();
|
||||
|
|
|
@ -114,7 +114,7 @@ class MessageCache {
|
|||
final List<Message> _messages = [];
|
||||
final ApiClient _apiClient;
|
||||
final String _userId;
|
||||
Future? currentOperation;
|
||||
Object? error;
|
||||
|
||||
List<Message> get messages => _messages;
|
||||
|
||||
|
@ -155,9 +155,14 @@ class MessageCache {
|
|||
}
|
||||
|
||||
Future<void> loadMessages() async {
|
||||
error = null;
|
||||
try {
|
||||
final messages = await MessageApi.getUserMessages(_apiClient, userId: _userId);
|
||||
_messages.addAll(messages);
|
||||
_ensureIntegrity();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
}
|
||||
|
||||
void _ensureIntegrity() {
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:contacts_plus_plus/client_holder.dart';
|
|||
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||
import 'package:contacts_plus_plus/models/friend.dart';
|
||||
import 'package:contacts_plus_plus/models/message.dart';
|
||||
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
|
||||
import 'package:contacts_plus_plus/widgets/friends/friend_online_status_indicator.dart';
|
||||
import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
@ -78,6 +79,9 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
|||
.of(context)
|
||||
.colorScheme
|
||||
.surfaceVariant;
|
||||
return Consumer<MessagingClient>(
|
||||
builder: (context, mClient, _) {
|
||||
final cache = mClient.getUserMessageCache(widget.friend.id);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Row(
|
||||
|
@ -143,9 +147,8 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
|||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Consumer<MessagingClient>(
|
||||
builder: (context, mClient, _) {
|
||||
final cache = mClient.getUserMessageCache(widget.friend.id);
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
if (cache == null) {
|
||||
return const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
|
@ -154,6 +157,17 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
|||
],
|
||||
);
|
||||
}
|
||||
if (cache.error != null) {
|
||||
return DefaultErrorWidget(
|
||||
message: cache.error.toString(),
|
||||
onRetry: () {
|
||||
setState(() {
|
||||
mClient.deleteUserMessageCache(widget.friend.id);
|
||||
});
|
||||
mClient.loadUserMessageCache(widget.friend.id);
|
||||
},
|
||||
);
|
||||
}
|
||||
if (cache.messages.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
|
@ -212,6 +226,7 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
|||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: TextField(
|
||||
enabled: cache != null && cache.error == null,
|
||||
autocorrect: true,
|
||||
controller: _messageTextController,
|
||||
maxLines: 4,
|
||||
|
@ -229,8 +244,9 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
|||
},
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
hintText: "Send a message to ${widget.friend
|
||||
hintText: "Message ${widget.friend
|
||||
.username}...",
|
||||
hintMaxLines: 1,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(24)
|
||||
|
@ -287,4 +303,6 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
|||
),
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.2.1+1
|
||||
version: 1.2.2+1
|
||||
|
||||
environment:
|
||||
sdk: '>=3.0.0'
|
||||
|
|
Loading…
Reference in a new issue