5dd7c07138
网络嗅探与自动恢复: - 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>
128 lines
3.1 KiB
Dart
128 lines
3.1 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../core/api/api_client.dart';
|
|
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));
|
|
});
|
|
|
|
final inventoryListProvider =
|
|
AsyncNotifierProvider<InventoryListNotifier, PageResult<Inventory>>(
|
|
InventoryListNotifier.new,
|
|
);
|
|
|
|
class InventoryListNotifier extends AsyncNotifier<PageResult<Inventory>> {
|
|
int _page = 1;
|
|
int? _warehouseId;
|
|
String _keyword = '';
|
|
PageResult<Inventory>? _cache;
|
|
|
|
@override
|
|
Future<PageResult<Inventory>> build() async {
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
ref.watch(networkRecoveryCountProvider); // 网络恢复时自动刷新
|
|
try {
|
|
final result = await _fetch();
|
|
_cache = result;
|
|
return result;
|
|
} catch (_) {
|
|
if (_cache != null) return _cache!;
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<PageResult<Inventory>> _fetch() {
|
|
return ref.read(inventoryRepositoryProvider).listInventory(
|
|
warehouseId: _warehouseId,
|
|
keyword: _keyword.isEmpty ? null : _keyword,
|
|
page: _page,
|
|
pageSize: 50,
|
|
);
|
|
}
|
|
|
|
void setPage(int page) {
|
|
_page = page;
|
|
reload();
|
|
}
|
|
|
|
void setWarehouseId(int? id) {
|
|
_warehouseId = id;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
void setKeyword(String keyword) {
|
|
_keyword = keyword;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
void reload() {
|
|
state = const AsyncValue.loading();
|
|
_fetch().then((result) {
|
|
_cache = result;
|
|
state = AsyncValue.data(result);
|
|
}, onError: (e, st) {
|
|
if (_cache != null) {
|
|
state = AsyncValue.data(_cache!);
|
|
} else {
|
|
state = AsyncValue.error(e, st);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
final inventoryLogProvider =
|
|
AsyncNotifierProvider<InventoryLogNotifier, PageResult<InventoryLog>>(
|
|
InventoryLogNotifier.new,
|
|
);
|
|
|
|
class InventoryLogNotifier extends AsyncNotifier<PageResult<InventoryLog>> {
|
|
int _page = 1;
|
|
PageResult<InventoryLog>? _cache;
|
|
|
|
@override
|
|
Future<PageResult<InventoryLog>> build() async {
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
ref.watch(networkRecoveryCountProvider);
|
|
try {
|
|
final result = await _fetch();
|
|
_cache = result;
|
|
return result;
|
|
} catch (_) {
|
|
if (_cache != null) return _cache!;
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<PageResult<InventoryLog>> _fetch() {
|
|
return ref.read(inventoryRepositoryProvider).listLogs(
|
|
page: _page,
|
|
pageSize: 50,
|
|
);
|
|
}
|
|
|
|
void setPage(int page) {
|
|
_page = page;
|
|
reload();
|
|
}
|
|
|
|
void reload() {
|
|
state = const AsyncValue.loading();
|
|
_fetch().then((result) {
|
|
_cache = result;
|
|
state = AsyncValue.data(result);
|
|
}, onError: (e, st) {
|
|
if (_cache != null) {
|
|
state = AsyncValue.data(_cache!);
|
|
} else {
|
|
state = AsyncValue.error(e, st);
|
|
}
|
|
});
|
|
}
|
|
}
|