feat(flutter): 增加目录加载诊断与前台重试
This commit is contained in:
1 parent
d68734cdab
commit
730a96559b
10 files changed
+268
-16
No files matched your search
@@ -2,15 +2,20 @@
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../core/config/app_config.dart';
|
||||
import '../../core/data/models/drama.dart';
|
||||
import '../../core/data/services/catalog_controller.dart';
|
||||
import '../../core/data/services/catalog_retry.dart';
|
||||
import '../../core/data/services/category_controller.dart';
|
||||
import '../../core/platform/haptics.dart';
|
||||
import '../../core/theme/app_theme.dart';
|
||||
import '../../core/widgets/app_bottom_nav.dart';
|
||||
import '../../core/widgets/catalog_debug_info.dart';
|
||||
import '../../core/widgets/empty_state.dart';
|
||||
import '../../l10n/app_localizations.dart';
|
||||
import '../discover/discover_types.dart';
|
||||
@@ -23,9 +28,57 @@ import '../mine/mine_view.dart';
|
||||
import 'home_binding.dart';
|
||||
import 'home_logic.dart';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
class HomePage extends StatefulWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
State<HomePage> createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
||||
bool _resumeRetryInFlight = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state != AppLifecycleState.resumed || _resumeRetryInFlight) return;
|
||||
final catalog = context.read<CatalogController>();
|
||||
final categories = context.read<CategoryController>();
|
||||
if (catalog.error == null && categories.error == null) return;
|
||||
unawaited(_retryAfterResume(catalog, categories));
|
||||
}
|
||||
|
||||
Future<void> _retryAfterResume(
|
||||
CatalogController catalog,
|
||||
CategoryController categories,
|
||||
) async {
|
||||
_resumeRetryInFlight = true;
|
||||
try {
|
||||
await AppConfig.probeAndApplyBaseUrl();
|
||||
await Future.wait([
|
||||
if (catalog.error != null) catalog.load(),
|
||||
if (categories.error != null) categories.load(),
|
||||
]);
|
||||
} finally {
|
||||
_resumeRetryInFlight = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _retryHome(CatalogController catalog) {
|
||||
return retryCatalogLoad(reload: catalog.load);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final catalog = context.watch<CatalogController>();
|
||||
@@ -51,7 +104,10 @@ class HomePage extends StatelessWidget {
|
||||
if (catalog.error != null) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.transparent,
|
||||
body: HomeFailureView(onRetry: catalog.load),
|
||||
body: HomeFailureView(
|
||||
error: catalog.debugError ?? catalog.error,
|
||||
onRetry: () => unawaited(_retryHome(catalog)),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (catalog.dramas.isEmpty) {
|
||||
@@ -78,8 +134,9 @@ class HomePage extends StatelessWidget {
|
||||
|
||||
/// 首页目录加载失败与重试入口。
|
||||
class HomeFailureView extends StatelessWidget {
|
||||
const HomeFailureView({super.key, required this.onRetry});
|
||||
const HomeFailureView({super.key, this.error, required this.onRetry});
|
||||
|
||||
final String? error;
|
||||
final VoidCallback onRetry;
|
||||
|
||||
@override
|
||||
@@ -90,6 +147,7 @@ class HomeFailureView extends StatelessWidget {
|
||||
title: l10n.catalogError,
|
||||
actionLabel: l10n.retry,
|
||||
onAction: onRetry,
|
||||
footer: kDebugMode ? CatalogDebugInfo(error: error) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user