forked from eatvio/eatvio_app
43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
import 'package:dio/dio.dart';
|
||
|
||
const notificationSettingsSaveFailedMessage =
|
||
'Не удалось сохранить настройки уведомлений';
|
||
|
||
|
||
String notificationSettingsUpdateAlertMessage(Object error) {
|
||
final validationMessage = _validationMessage(error);
|
||
if (validationMessage != null) return validationMessage;
|
||
return notificationSettingsSaveFailedMessage;
|
||
}
|
||
|
||
String? _validationMessage(Object error) {
|
||
if (error is! DioException || error.response?.statusCode != 422) return null;
|
||
|
||
final data = error.response?.data;
|
||
if (data is! Map) return null;
|
||
|
||
final errors = data['errors'];
|
||
if (errors is Map) {
|
||
for (final value in errors.values) {
|
||
final text = _firstErrorText(value);
|
||
if (text != null) return text;
|
||
}
|
||
}
|
||
|
||
final message = data['message'];
|
||
if (message is String && message.trim().isNotEmpty) return message.trim();
|
||
return null;
|
||
}
|
||
|
||
String? _firstErrorText(Object? value) {
|
||
if (value is List) {
|
||
for (final item in value) {
|
||
final text = item.toString().trim();
|
||
if (text.isNotEmpty) return text;
|
||
}
|
||
return null;
|
||
}
|
||
if (value is String && value.trim().isNotEmpty) return value.trim();
|
||
return null;
|
||
}
|