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) {
|
if (response.statusCode == 403) {
|
||||||
tryCachedLogin();
|
tryCachedLogin();
|
||||||
// TODO: Show the login screen again if cached login was unsuccessful.
|
// 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) {
|
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);
|
MessageCache _createUserMessageCache(String userId) => MessageCache(apiClient: _apiClient, userId: userId);
|
||||||
|
|
||||||
|
void deleteUserMessageCache(String userId) {
|
||||||
|
_messageCache.remove(userId);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> loadUserMessageCache(String userId) async {
|
Future<void> loadUserMessageCache(String userId) async {
|
||||||
final cache = getUserMessageCache(userId) ?? _createUserMessageCache(userId);
|
final cache = getUserMessageCache(userId) ?? _createUserMessageCache(userId);
|
||||||
await cache.loadMessages();
|
await cache.loadMessages();
|
||||||
|
|
|
@ -114,7 +114,7 @@ class MessageCache {
|
||||||
final List<Message> _messages = [];
|
final List<Message> _messages = [];
|
||||||
final ApiClient _apiClient;
|
final ApiClient _apiClient;
|
||||||
final String _userId;
|
final String _userId;
|
||||||
Future? currentOperation;
|
Object? error;
|
||||||
|
|
||||||
List<Message> get messages => _messages;
|
List<Message> get messages => _messages;
|
||||||
|
|
||||||
|
@ -155,9 +155,14 @@ class MessageCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> loadMessages() async {
|
Future<void> loadMessages() async {
|
||||||
|
error = null;
|
||||||
|
try {
|
||||||
final messages = await MessageApi.getUserMessages(_apiClient, userId: _userId);
|
final messages = await MessageApi.getUserMessages(_apiClient, userId: _userId);
|
||||||
_messages.addAll(messages);
|
_messages.addAll(messages);
|
||||||
_ensureIntegrity();
|
_ensureIntegrity();
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _ensureIntegrity() {
|
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/clients/messaging_client.dart';
|
||||||
import 'package:contacts_plus_plus/models/friend.dart';
|
import 'package:contacts_plus_plus/models/friend.dart';
|
||||||
import 'package:contacts_plus_plus/models/message.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/friends/friend_online_status_indicator.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart';
|
import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
@ -78,6 +79,9 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
||||||
.of(context)
|
.of(context)
|
||||||
.colorScheme
|
.colorScheme
|
||||||
.surfaceVariant;
|
.surfaceVariant;
|
||||||
|
return Consumer<MessagingClient>(
|
||||||
|
builder: (context, mClient, _) {
|
||||||
|
final cache = mClient.getUserMessageCache(widget.friend.id);
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Row(
|
title: Row(
|
||||||
|
@ -143,9 +147,8 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Consumer<MessagingClient>(
|
child: Builder(
|
||||||
builder: (context, mClient, _) {
|
builder: (context) {
|
||||||
final cache = mClient.getUserMessageCache(widget.friend.id);
|
|
||||||
if (cache == null) {
|
if (cache == null) {
|
||||||
return const Column(
|
return const Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
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) {
|
if (cache.messages.isEmpty) {
|
||||||
return Center(
|
return Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
|
@ -212,6 +226,7 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8),
|
padding: const EdgeInsets.all(8),
|
||||||
child: TextField(
|
child: TextField(
|
||||||
|
enabled: cache != null && cache.error == null,
|
||||||
autocorrect: true,
|
autocorrect: true,
|
||||||
controller: _messageTextController,
|
controller: _messageTextController,
|
||||||
maxLines: 4,
|
maxLines: 4,
|
||||||
|
@ -229,8 +244,9 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
|
||||||
},
|
},
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
isDense: true,
|
isDense: true,
|
||||||
hintText: "Send a message to ${widget.friend
|
hintText: "Message ${widget.friend
|
||||||
.username}...",
|
.username}...",
|
||||||
|
hintMaxLines: 1,
|
||||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||||
border: OutlineInputBorder(
|
border: OutlineInputBorder(
|
||||||
borderRadius: BorderRadius.circular(24)
|
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
|
# 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.2.1+1
|
version: 1.2.2+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: '>=3.0.0'
|
sdk: '>=3.0.0'
|
||||||
|
|
Loading…
Reference in a new issue