feat(client): 网络恢复自动刷新 + 离线嗅探 + 连通性感知登录
网络嗅探与自动恢复: - ConnectivityNotifier:在线时 30s 检测,离线时切换为 1min 嗅探 - 新增 networkRecoveryCountProvider:网络恢复时自增,各数据 provider 通过 ref.watch 实现自动重新加载 - inventory/product/stock_in/stock_out/partner provider 均已接入 FutureBuilder 类屏幕: - finance_screen + batch_tracking_screen 通过 ref.listen 监听恢复事件 修复问题 1:API 超时 10s/30s → 5s/15s,减少无网络时等待 修复问题 2:offline banner 文字由"写操作已禁用"改为实际行为描述 连通性感知: - app 启动时(_AppBootstrap)立即 forceCheck,路由跳转前状态已就绪 - 登录页:watch connectivityProvider,离线时显示警告 banner - 登录按钮:离线状态下直接提示,不等待超时 测试: - login_screen_test + login_flow_test 通过 ConnectivityNotifier(skipInit: true) 覆盖 provider,避免测试环境中 pending HTTP timer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,34 +3,67 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../core/config/app_config.dart';
|
||||
|
||||
/// 每次从离线恢复到在线时自增。
|
||||
/// 数据 provider 通过 watch 此值实现网络恢复后自动刷新。
|
||||
final networkRecoveryCountProvider = StateProvider<int>((ref) => 0);
|
||||
|
||||
final connectivityProvider =
|
||||
StateNotifierProvider<ConnectivityNotifier, bool>((ref) {
|
||||
return ConnectivityNotifier();
|
||||
return ConnectivityNotifier(
|
||||
onRecovered: () {
|
||||
ref.read(networkRecoveryCountProvider.notifier).update((s) => s + 1);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
class ConnectivityNotifier extends StateNotifier<bool> {
|
||||
ConnectivityNotifier() : super(true) {
|
||||
_check(); // immediate first check
|
||||
_timer = Timer.periodic(const Duration(seconds: 30), (_) => _check());
|
||||
final void Function()? onRecovered;
|
||||
|
||||
ConnectivityNotifier({this.onRecovered, bool skipInit = false}) : super(true) {
|
||||
if (!skipInit) {
|
||||
_check();
|
||||
_startOnlineTimer();
|
||||
}
|
||||
}
|
||||
|
||||
Timer? _timer;
|
||||
|
||||
// Dedicated lightweight Dio — short timeouts, no interceptors
|
||||
// 独立轻量 Dio:短超时,无拦截器
|
||||
final _dio = Dio(BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 3),
|
||||
receiveTimeout: const Duration(seconds: 3),
|
||||
));
|
||||
|
||||
/// 立即触发一次检测(供外部调用,如 API 请求失败时)
|
||||
/// 在线时:每 30 秒检测一次
|
||||
void _startOnlineTimer() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer.periodic(const Duration(seconds: 30), (_) => _check());
|
||||
}
|
||||
|
||||
/// 离线时:每 1 分钟嗅探一次,降低频率节省资源
|
||||
void _startOfflineTimer() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer.periodic(const Duration(minutes: 1), (_) => _check());
|
||||
}
|
||||
|
||||
/// 立即触发一次检测(供外部调用,如 API 请求失败 / 启动时)
|
||||
Future<void> forceCheck() => _check();
|
||||
|
||||
Future<void> _check() async {
|
||||
try {
|
||||
await _dio.get(AppConfig.healthUrl);
|
||||
if (!state) state = true;
|
||||
if (!state) {
|
||||
// 离线 → 在线:切回高频检测,广播恢复事件
|
||||
state = true;
|
||||
onRecovered?.call();
|
||||
_startOnlineTimer();
|
||||
}
|
||||
} catch (_) {
|
||||
if (state) state = false;
|
||||
if (state) {
|
||||
// 在线 → 离线:切换为低频嗅探
|
||||
state = false;
|
||||
_startOfflineTimer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import '../core/auth/auth_state.dart';
|
||||
import '../core/models/page_result.dart';
|
||||
import '../models/inventory.dart';
|
||||
import '../repositories/inventory_repository.dart';
|
||||
import 'connectivity_provider.dart';
|
||||
|
||||
final inventoryRepositoryProvider = Provider<InventoryRepository>((ref) {
|
||||
return InventoryRepository(ref.watch(apiClientProvider));
|
||||
@@ -23,6 +24,7 @@ class InventoryListNotifier extends AsyncNotifier<PageResult<Inventory>> {
|
||||
@override
|
||||
Future<PageResult<Inventory>> build() async {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
ref.watch(networkRecoveryCountProvider); // 网络恢复时自动刷新
|
||||
try {
|
||||
final result = await _fetch();
|
||||
_cache = result;
|
||||
@@ -86,6 +88,7 @@ class InventoryLogNotifier extends AsyncNotifier<PageResult<InventoryLog>> {
|
||||
@override
|
||||
Future<PageResult<InventoryLog>> build() async {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
ref.watch(networkRecoveryCountProvider);
|
||||
try {
|
||||
final result = await _fetch();
|
||||
_cache = result;
|
||||
|
||||
@@ -4,6 +4,7 @@ import '../core/auth/auth_state.dart';
|
||||
import '../core/models/page_result.dart';
|
||||
import '../models/partner.dart';
|
||||
import '../repositories/partner_repository.dart';
|
||||
import 'connectivity_provider.dart';
|
||||
|
||||
final partnerRepositoryProvider = Provider<PartnerRepository>((ref) {
|
||||
return PartnerRepository(ref.watch(apiClientProvider));
|
||||
@@ -32,6 +33,7 @@ class PartnerListNotifier extends AsyncNotifier<PageResult<Partner>> {
|
||||
@override
|
||||
Future<PageResult<Partner>> build() async {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
ref.watch(networkRecoveryCountProvider);
|
||||
try {
|
||||
final result = await _fetch();
|
||||
_cache = result;
|
||||
|
||||
@@ -4,6 +4,7 @@ import '../core/auth/auth_state.dart';
|
||||
import '../core/models/page_result.dart';
|
||||
import '../models/product.dart';
|
||||
import '../repositories/product_repository.dart';
|
||||
import 'connectivity_provider.dart';
|
||||
|
||||
final productRepositoryProvider = Provider<ProductRepository>((ref) {
|
||||
return ProductRepository(ref.watch(apiClientProvider));
|
||||
@@ -23,6 +24,7 @@ class ProductListNotifier extends AsyncNotifier<PageResult<Product>> {
|
||||
@override
|
||||
Future<PageResult<Product>> build() async {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
ref.watch(networkRecoveryCountProvider);
|
||||
try {
|
||||
final result = await _fetch();
|
||||
_cache = result;
|
||||
|
||||
@@ -4,6 +4,7 @@ import '../core/auth/auth_state.dart';
|
||||
import '../core/models/page_result.dart';
|
||||
import '../models/stock_in.dart';
|
||||
import '../repositories/stock_in_repository.dart';
|
||||
import 'connectivity_provider.dart';
|
||||
import 'inventory_provider.dart';
|
||||
|
||||
final stockInRepositoryProvider = Provider<StockInRepository>((ref) {
|
||||
@@ -25,6 +26,7 @@ class StockInListNotifier extends AsyncNotifier<PageResult<StockInOrder>> {
|
||||
@override
|
||||
Future<PageResult<StockInOrder>> build() async {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
ref.watch(networkRecoveryCountProvider);
|
||||
try {
|
||||
final result = await _fetch();
|
||||
_cache = result;
|
||||
|
||||
@@ -4,6 +4,7 @@ import '../core/auth/auth_state.dart';
|
||||
import '../core/models/page_result.dart';
|
||||
import '../models/stock_out.dart';
|
||||
import '../repositories/stock_out_repository.dart';
|
||||
import 'connectivity_provider.dart';
|
||||
import 'inventory_provider.dart';
|
||||
|
||||
final stockOutRepositoryProvider = Provider<StockOutRepository>((ref) {
|
||||
@@ -25,6 +26,7 @@ class StockOutListNotifier extends AsyncNotifier<PageResult<StockOutOrder>> {
|
||||
@override
|
||||
Future<PageResult<StockOutOrder>> build() async {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
ref.watch(networkRecoveryCountProvider);
|
||||
try {
|
||||
final result = await _fetch();
|
||||
_cache = result;
|
||||
|
||||
Reference in New Issue
Block a user