feat: surface read-only load errors
This commit is contained in:
55
lib/app/error_message.dart
Normal file
55
lib/app/error_message.dart
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
|
||||||
|
String describeLoadError(Object error) {
|
||||||
|
if (error is DioException) {
|
||||||
|
final statusCode = error.response?.statusCode;
|
||||||
|
|
||||||
|
if (statusCode == 401 || statusCode == 403) {
|
||||||
|
return 'Нет доступа: API key отклонён ($statusCode).';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_isNetworkFailure(error)) {
|
||||||
|
return 'Backend недоступен: ${error.message ?? error.type.name}.';
|
||||||
|
}
|
||||||
|
|
||||||
|
final responseMessage = _responseMessage(error);
|
||||||
|
if (responseMessage != null) {
|
||||||
|
return 'Backend вернул ошибку $statusCode: $responseMessage';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (statusCode != null) {
|
||||||
|
return 'Backend вернул ошибку $statusCode.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Ошибка запроса: ${error.message ?? error.type.name}.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error is FormatException) {
|
||||||
|
return 'Неожиданный ответ backend: ${error.message}';
|
||||||
|
}
|
||||||
|
|
||||||
|
return error.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _isNetworkFailure(DioException error) {
|
||||||
|
return switch (error.type) {
|
||||||
|
DioExceptionType.connectionTimeout ||
|
||||||
|
DioExceptionType.sendTimeout ||
|
||||||
|
DioExceptionType.receiveTimeout ||
|
||||||
|
DioExceptionType.connectionError ||
|
||||||
|
DioExceptionType.unknown => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
String? _responseMessage(DioException error) {
|
||||||
|
final data = error.response?.data;
|
||||||
|
if (data is Map) {
|
||||||
|
final value = data['detail'] ?? data['message'] ?? data['error'];
|
||||||
|
return value?.toString();
|
||||||
|
}
|
||||||
|
if (data is String && data.trim().isNotEmpty) {
|
||||||
|
return data.trim();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
34
lib/app/load_state.dart
Normal file
34
lib/app/load_state.dart
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
enum LoadStatus { idle, loading, data, empty, error }
|
||||||
|
|
||||||
|
class LoadState<T> {
|
||||||
|
final LoadStatus status;
|
||||||
|
final T data;
|
||||||
|
final String? errorMessage;
|
||||||
|
|
||||||
|
const LoadState.idle(this.data)
|
||||||
|
: status = LoadStatus.idle,
|
||||||
|
errorMessage = null;
|
||||||
|
|
||||||
|
const LoadState.loading(this.data)
|
||||||
|
: status = LoadStatus.loading,
|
||||||
|
errorMessage = null;
|
||||||
|
|
||||||
|
const LoadState.data(this.data)
|
||||||
|
: status = LoadStatus.data,
|
||||||
|
errorMessage = null;
|
||||||
|
|
||||||
|
const LoadState.empty(this.data)
|
||||||
|
: status = LoadStatus.empty,
|
||||||
|
errorMessage = null;
|
||||||
|
|
||||||
|
const LoadState.error(this.data, this.errorMessage)
|
||||||
|
: status = LoadStatus.error;
|
||||||
|
|
||||||
|
bool get isIdle => status == LoadStatus.idle;
|
||||||
|
|
||||||
|
bool get isLoading => status == LoadStatus.loading;
|
||||||
|
|
||||||
|
bool get hasError => status == LoadStatus.error;
|
||||||
|
|
||||||
|
bool get isEmpty => status == LoadStatus.empty;
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ import 'dart:math' as math;
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
|
import '../app/error_message.dart';
|
||||||
|
import '../app/load_state.dart';
|
||||||
import '../models/home_config.dart';
|
import '../models/home_config.dart';
|
||||||
import '../services/api_client.dart';
|
import '../services/api_client.dart';
|
||||||
import '../services/settings_service.dart';
|
import '../services/settings_service.dart';
|
||||||
@@ -694,50 +696,69 @@ class TasksNotifier extends Notifier<List<dynamic>> {
|
|||||||
|
|
||||||
// ─── Статистика ──────────────────────────────────────────────
|
// ─── Статистика ──────────────────────────────────────────────
|
||||||
|
|
||||||
final statsProvider = NotifierProvider<StatsNotifier, Map<String, dynamic>>(
|
final statsProvider =
|
||||||
() => StatsNotifier(),
|
NotifierProvider<StatsNotifier, LoadState<Map<String, dynamic>>>(
|
||||||
);
|
() => StatsNotifier(),
|
||||||
|
);
|
||||||
|
|
||||||
class StatsNotifier extends Notifier<Map<String, dynamic>> {
|
class StatsNotifier extends Notifier<LoadState<Map<String, dynamic>>> {
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> build() => {};
|
LoadState<Map<String, dynamic>> build() =>
|
||||||
|
const LoadState.idle(<String, dynamic>{});
|
||||||
|
|
||||||
Future<void> load({int days = 7}) async {
|
Future<void> load({int days = 7}) async {
|
||||||
|
state = LoadState.loading(state.data);
|
||||||
try {
|
try {
|
||||||
final api = ref.read(apiProvider);
|
final api = ref.read(apiProvider);
|
||||||
final res = await api.getStatsSummary(days: days);
|
final res = await api.getStatsSummary(days: days);
|
||||||
final data = res.data;
|
final data = res.data;
|
||||||
if (data is Map) {
|
if (data is! Map) {
|
||||||
state = Map<String, dynamic>.from(data);
|
throw FormatException('stats summary должен быть объектом');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final stats = Map<String, dynamic>.from(data);
|
||||||
|
final groups = stats['groups'];
|
||||||
|
final hasGroups = groups is List && groups.isNotEmpty;
|
||||||
|
state = hasGroups ? LoadState.data(stats) : LoadState.empty(stats);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint("Ошибка загрузки статистики: $e");
|
state = LoadState.error(state.data, describeLoadError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Лог событий ─────────────────────────────────────────────
|
// ─── Лог событий ─────────────────────────────────────────────
|
||||||
|
|
||||||
final eventLogProvider = NotifierProvider<EventLogNotifier, List<dynamic>>(
|
final eventLogProvider =
|
||||||
() => EventLogNotifier(),
|
NotifierProvider<EventLogNotifier, LoadState<List<dynamic>>>(
|
||||||
);
|
() => EventLogNotifier(),
|
||||||
|
);
|
||||||
|
|
||||||
class EventLogNotifier extends Notifier<List<dynamic>> {
|
class EventLogNotifier extends Notifier<LoadState<List<dynamic>>> {
|
||||||
@override
|
@override
|
||||||
List<dynamic> build() => [];
|
LoadState<List<dynamic>> build() => const LoadState.idle(<dynamic>[]);
|
||||||
|
|
||||||
Future<void> load({int limit = 100}) async {
|
Future<void> load({int limit = 100}) async {
|
||||||
|
state = LoadState.loading(state.data);
|
||||||
try {
|
try {
|
||||||
final api = ref.read(apiProvider);
|
final api = ref.read(apiProvider);
|
||||||
final res = await api.getStatsLog(limit: limit);
|
final res = await api.getStatsLog(limit: limit);
|
||||||
final data = res.data;
|
final data = res.data;
|
||||||
|
late final List<dynamic> events;
|
||||||
if (data is List) {
|
if (data is List) {
|
||||||
state = data;
|
events = List<dynamic>.from(data);
|
||||||
} else if (data is Map) {
|
} else if (data is Map) {
|
||||||
state = data['data'] ?? data['events'] ?? data.values.toList();
|
final value = data['data'] ?? data['events'] ?? data.values.toList();
|
||||||
|
if (value is! List) {
|
||||||
|
throw FormatException('stats log должен быть списком событий');
|
||||||
|
}
|
||||||
|
events = List<dynamic>.from(value);
|
||||||
|
} else {
|
||||||
|
throw FormatException('stats log должен быть списком событий');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = events.isEmpty ? LoadState.empty(events) : LoadState.data(events);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint("Ошибка загрузки логов: $e");
|
state = LoadState.error(state.data, describeLoadError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import '../app/load_state.dart';
|
||||||
import '../providers/providers.dart';
|
import '../providers/providers.dart';
|
||||||
|
import '../widgets/load_error_view.dart';
|
||||||
|
|
||||||
/// Экран просмотра лога событий.
|
/// Экран просмотра лога событий.
|
||||||
class EventLogScreen extends ConsumerStatefulWidget {
|
class EventLogScreen extends ConsumerStatefulWidget {
|
||||||
@@ -11,7 +13,6 @@ class EventLogScreen extends ConsumerStatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _EventLogScreenState extends ConsumerState<EventLogScreen> {
|
class _EventLogScreenState extends ConsumerState<EventLogScreen> {
|
||||||
bool _loading = true;
|
|
||||||
int _limit = 100;
|
int _limit = 100;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -21,14 +22,13 @@ class _EventLogScreenState extends ConsumerState<EventLogScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _load() async {
|
Future<void> _load() async {
|
||||||
setState(() => _loading = true);
|
|
||||||
await ref.read(eventLogProvider.notifier).load(limit: _limit);
|
await ref.read(eventLogProvider.notifier).load(limit: _limit);
|
||||||
if (mounted) setState(() => _loading = false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final events = ref.watch(eventLogProvider);
|
final eventsState = ref.watch(eventLogProvider);
|
||||||
|
final events = eventsState.data;
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
@@ -47,31 +47,68 @@ class _EventLogScreenState extends ConsumerState<EventLogScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: _loading
|
body: _buildContent(eventsState, events),
|
||||||
? const Center(
|
);
|
||||||
child: CircularProgressIndicator(color: Colors.deepOrange),
|
}
|
||||||
)
|
|
||||||
: events.isEmpty
|
Widget _buildContent(
|
||||||
? const Center(
|
LoadState<List<dynamic>> eventsState,
|
||||||
child: Text(
|
List<dynamic> events,
|
||||||
'Нет событий',
|
) {
|
||||||
style: TextStyle(color: Colors.white54),
|
if ((eventsState.isIdle || eventsState.isLoading) && events.isEmpty) {
|
||||||
),
|
return const Center(
|
||||||
)
|
child: CircularProgressIndicator(color: Colors.deepOrange),
|
||||||
: RefreshIndicator(
|
);
|
||||||
color: Colors.deepOrange,
|
}
|
||||||
onRefresh: _load,
|
|
||||||
child: ListView.builder(
|
if (eventsState.hasError && events.isEmpty) {
|
||||||
padding: const EdgeInsets.all(8),
|
return LoadErrorView(
|
||||||
itemCount: events.length,
|
title: 'Не удалось загрузить лог событий',
|
||||||
itemBuilder: (context, index) {
|
message: eventsState.errorMessage,
|
||||||
final event = events[index];
|
icon: Icons.list_alt,
|
||||||
return _EventRow(
|
onRetry: _load,
|
||||||
event: event is Map ? Map<String, dynamic>.from(event) : {},
|
);
|
||||||
);
|
}
|
||||||
},
|
|
||||||
),
|
if (events.isEmpty) {
|
||||||
),
|
return const Center(
|
||||||
|
child: Text('Нет событий', style: TextStyle(color: Colors.white54)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final hasStatusHeader = eventsState.isLoading || eventsState.hasError;
|
||||||
|
final statusHeaderCount = hasStatusHeader ? 1 : 0;
|
||||||
|
|
||||||
|
return RefreshIndicator(
|
||||||
|
color: Colors.deepOrange,
|
||||||
|
onRefresh: _load,
|
||||||
|
child: ListView.builder(
|
||||||
|
physics: const AlwaysScrollableScrollPhysics(),
|
||||||
|
padding: const EdgeInsets.all(8),
|
||||||
|
itemCount: events.length + statusHeaderCount,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
if (hasStatusHeader && index == 0) {
|
||||||
|
if (eventsState.isLoading) {
|
||||||
|
return const Padding(
|
||||||
|
padding: EdgeInsets.only(bottom: 12),
|
||||||
|
child: LinearProgressIndicator(color: Colors.deepOrange),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadErrorBanner(
|
||||||
|
title: 'Не удалось обновить лог событий',
|
||||||
|
message: eventsState.errorMessage,
|
||||||
|
onRetry: _load,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final eventIndex = index - statusHeaderCount;
|
||||||
|
final event = events[eventIndex];
|
||||||
|
return _EventRow(
|
||||||
|
event: event is Map ? Map<String, dynamic>.from(event) : {},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import '../app/load_state.dart';
|
||||||
import '../providers/providers.dart';
|
import '../providers/providers.dart';
|
||||||
|
import '../widgets/load_error_view.dart';
|
||||||
|
|
||||||
/// Экран просмотра статистики.
|
/// Экран просмотра статистики.
|
||||||
/// Показывает сводку по группам за выбранный период.
|
/// Показывает сводку по группам за выбранный период.
|
||||||
@@ -12,7 +14,6 @@ class StatsScreen extends ConsumerStatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _StatsScreenState extends ConsumerState<StatsScreen> {
|
class _StatsScreenState extends ConsumerState<StatsScreen> {
|
||||||
bool _loading = true;
|
|
||||||
int _days = 7;
|
int _days = 7;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -22,14 +23,13 @@ class _StatsScreenState extends ConsumerState<StatsScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _load() async {
|
Future<void> _load() async {
|
||||||
setState(() => _loading = true);
|
|
||||||
await ref.read(statsProvider.notifier).load(days: _days);
|
await ref.read(statsProvider.notifier).load(days: _days);
|
||||||
if (mounted) setState(() => _loading = false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final stats = ref.watch(statsProvider);
|
final statsState = ref.watch(statsProvider);
|
||||||
|
final stats = statsState.data;
|
||||||
final groups = (stats['groups'] as List<dynamic>?) ?? [];
|
final groups = (stats['groups'] as List<dynamic>?) ?? [];
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -62,37 +62,70 @@ class _StatsScreenState extends ConsumerState<StatsScreen> {
|
|||||||
),
|
),
|
||||||
|
|
||||||
// ─── Содержимое ───
|
// ─── Содержимое ───
|
||||||
Expanded(
|
Expanded(child: _buildContent(statsState, groups)),
|
||||||
child: _loading
|
|
||||||
? const Center(
|
|
||||||
child: CircularProgressIndicator(color: Colors.deepOrange),
|
|
||||||
)
|
|
||||||
: groups.isEmpty
|
|
||||||
? const Center(
|
|
||||||
child: Text(
|
|
||||||
'Нет данных',
|
|
||||||
style: TextStyle(color: Colors.white54),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
: RefreshIndicator(
|
|
||||||
color: Colors.deepOrange,
|
|
||||||
onRefresh: _load,
|
|
||||||
child: ListView.builder(
|
|
||||||
padding: const EdgeInsets.all(12),
|
|
||||||
itemCount: groups.length,
|
|
||||||
itemBuilder: (context, index) {
|
|
||||||
final g = groups[index];
|
|
||||||
return _StatsCard(
|
|
||||||
data: g is Map ? Map<String, dynamic>.from(g) : {},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildContent(
|
||||||
|
LoadState<Map<String, dynamic>> statsState,
|
||||||
|
List<dynamic> groups,
|
||||||
|
) {
|
||||||
|
if ((statsState.isIdle || statsState.isLoading) && groups.isEmpty) {
|
||||||
|
return const Center(
|
||||||
|
child: CircularProgressIndicator(color: Colors.deepOrange),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (statsState.hasError && groups.isEmpty) {
|
||||||
|
return LoadErrorView(
|
||||||
|
title: 'Не удалось загрузить статистику',
|
||||||
|
message: statsState.errorMessage,
|
||||||
|
icon: Icons.bar_chart,
|
||||||
|
onRetry: _load,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (groups.isEmpty) {
|
||||||
|
return const Center(
|
||||||
|
child: Text('Нет данных', style: TextStyle(color: Colors.white54)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final hasStatusHeader = statsState.isLoading || statsState.hasError;
|
||||||
|
final statusHeaderCount = hasStatusHeader ? 1 : 0;
|
||||||
|
|
||||||
|
return RefreshIndicator(
|
||||||
|
color: Colors.deepOrange,
|
||||||
|
onRefresh: _load,
|
||||||
|
child: ListView.builder(
|
||||||
|
physics: const AlwaysScrollableScrollPhysics(),
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
itemCount: groups.length + statusHeaderCount,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
if (hasStatusHeader && index == 0) {
|
||||||
|
if (statsState.isLoading) {
|
||||||
|
return const Padding(
|
||||||
|
padding: EdgeInsets.only(bottom: 12),
|
||||||
|
child: LinearProgressIndicator(color: Colors.deepOrange),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadErrorBanner(
|
||||||
|
title: 'Не удалось обновить статистику',
|
||||||
|
message: statsState.errorMessage,
|
||||||
|
onRetry: _load,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final groupIndex = index - statusHeaderCount;
|
||||||
|
final g = groups[groupIndex];
|
||||||
|
return _StatsCard(data: g is Map ? Map<String, dynamic>.from(g) : {});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Карточка статистики одной группы
|
/// Карточка статистики одной группы
|
||||||
|
|||||||
114
lib/widgets/load_error_view.dart
Normal file
114
lib/widgets/load_error_view.dart
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class LoadErrorView extends StatelessWidget {
|
||||||
|
final String title;
|
||||||
|
final String? message;
|
||||||
|
final VoidCallback onRetry;
|
||||||
|
final IconData icon;
|
||||||
|
|
||||||
|
const LoadErrorView({
|
||||||
|
super.key,
|
||||||
|
required this.title,
|
||||||
|
required this.onRetry,
|
||||||
|
this.message,
|
||||||
|
this.icon = Icons.wifi_off_rounded,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(icon, size: 64, color: Colors.deepOrange),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
Text(
|
||||||
|
title,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: const TextStyle(color: Colors.white70, fontSize: 16),
|
||||||
|
),
|
||||||
|
if (message != null) ...[
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
message!,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: const TextStyle(color: Colors.white38, fontSize: 12),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
FilledButton.icon(
|
||||||
|
onPressed: onRetry,
|
||||||
|
icon: const Icon(Icons.refresh),
|
||||||
|
label: const Text('Повторить'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoadErrorBanner extends StatelessWidget {
|
||||||
|
final String title;
|
||||||
|
final String? message;
|
||||||
|
final VoidCallback onRetry;
|
||||||
|
|
||||||
|
const LoadErrorBanner({
|
||||||
|
super.key,
|
||||||
|
required this.title,
|
||||||
|
required this.onRetry,
|
||||||
|
this.message,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Container(
|
||||||
|
margin: const EdgeInsets.fromLTRB(12, 4, 12, 12),
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.deepOrange.withValues(alpha: 0.14),
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
border: Border.all(color: Colors.deepOrange.withValues(alpha: 0.35)),
|
||||||
|
),
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Icon(
|
||||||
|
Icons.warning_amber_rounded,
|
||||||
|
color: Colors.deepOrange,
|
||||||
|
size: 20,
|
||||||
|
),
|
||||||
|
const SizedBox(width: 10),
|
||||||
|
Expanded(
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
title,
|
||||||
|
style: const TextStyle(
|
||||||
|
color: Colors.white70,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
if (message != null) ...[
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
message!,
|
||||||
|
style: const TextStyle(color: Colors.white38, fontSize: 12),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
tooltip: 'Повторить',
|
||||||
|
onPressed: onRetry,
|
||||||
|
icon: const Icon(Icons.refresh),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
121
test/read_only_load_state_test.dart
Normal file
121
test/read_only_load_state_test.dart
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:ignis_app/app/load_state.dart';
|
||||||
|
import 'package:ignis_app/providers/providers.dart';
|
||||||
|
import 'package:ignis_app/services/api_client.dart';
|
||||||
|
|
||||||
|
class FakeIgnisApi extends IgnisApi {
|
||||||
|
Object? statsData;
|
||||||
|
Object? eventLogData;
|
||||||
|
Object? statsError;
|
||||||
|
Object? eventLogError;
|
||||||
|
int? requestedDays;
|
||||||
|
int? requestedLimit;
|
||||||
|
|
||||||
|
FakeIgnisApi({this.statsData, this.eventLogData});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Response> getStatsSummary({int days = 7}) async {
|
||||||
|
requestedDays = days;
|
||||||
|
final error = statsError;
|
||||||
|
if (error != null) throw error;
|
||||||
|
return Response(
|
||||||
|
requestOptions: RequestOptions(path: '/stats/summary'),
|
||||||
|
data: statsData,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Response> getStatsLog({int limit = 100}) async {
|
||||||
|
requestedLimit = limit;
|
||||||
|
final error = eventLogError;
|
||||||
|
if (error != null) throw error;
|
||||||
|
return Response(
|
||||||
|
requestOptions: RequestOptions(path: '/stats/log'),
|
||||||
|
data: eventLogData,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
ProviderContainer containerWith(FakeIgnisApi api) {
|
||||||
|
final container = ProviderContainer(
|
||||||
|
overrides: [apiProvider.overrideWithValue(api)],
|
||||||
|
);
|
||||||
|
addTearDown(container.dispose);
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('stats load exposes data state', () async {
|
||||||
|
final api = FakeIgnisApi(
|
||||||
|
statsData: {
|
||||||
|
'groups': [
|
||||||
|
{'id': 'kitchen', 'total_commands': 3},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(statsProvider.notifier).load(days: 14);
|
||||||
|
|
||||||
|
final state = container.read(statsProvider);
|
||||||
|
expect(state.status, LoadStatus.data);
|
||||||
|
expect(state.data['groups'], hasLength(1));
|
||||||
|
expect(api.requestedDays, 14);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stats load exposes empty state for empty groups', () async {
|
||||||
|
final api = FakeIgnisApi(statsData: {'groups': <Object>[]});
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(statsProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(statsProvider);
|
||||||
|
expect(state.status, LoadStatus.empty);
|
||||||
|
expect(state.data['groups'], isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('event log load accepts map response and exposes data state', () async {
|
||||||
|
final api = FakeIgnisApi(
|
||||||
|
eventLogData: {
|
||||||
|
'events': [
|
||||||
|
{'action': 'toggle', 'target_id': 'kitchen'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(eventLogProvider.notifier).load(limit: 50);
|
||||||
|
|
||||||
|
final state = container.read(eventLogProvider);
|
||||||
|
expect(state.status, LoadStatus.data);
|
||||||
|
expect(state.data, hasLength(1));
|
||||||
|
expect(api.requestedLimit, 50);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('load error keeps previous stats data and exposes message', () async {
|
||||||
|
final api = FakeIgnisApi(
|
||||||
|
statsData: {
|
||||||
|
'groups': [
|
||||||
|
{'id': 'kitchen'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(statsProvider.notifier).load();
|
||||||
|
api.statsError = DioException(
|
||||||
|
requestOptions: RequestOptions(path: '/stats/summary'),
|
||||||
|
type: DioExceptionType.connectionError,
|
||||||
|
message: 'No route to host',
|
||||||
|
);
|
||||||
|
|
||||||
|
await container.read(statsProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(statsProvider);
|
||||||
|
expect(state.status, LoadStatus.error);
|
||||||
|
expect(state.data['groups'], hasLength(1));
|
||||||
|
expect(state.errorMessage, contains('Backend недоступен'));
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user