Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
fix(home_page): display error if failed to load
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 14, 2021
1 parent b8eb758 commit 5357844
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions lib/ui/pages/home_page/anime_section.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:yukino_app/ui/components/error_widget.dart';
import '../../../config/defaults.dart';
import '../../../modules/app/state.dart';
import '../../../modules/helpers/assets.dart';
Expand All @@ -9,6 +10,7 @@ import '../../../modules/state/stateful_holder.dart';
import '../../../modules/state/states.dart';
import '../../../modules/trackers/myanimelist/myanimelist.dart';
import '../../../modules/translator/translator.dart';
import '../../../modules/utils/error.dart';
import '../../components/network_image_fallback.dart';

final StatefulValueHolder<MyAnimeListHome?> _cache =
Expand All @@ -28,8 +30,8 @@ class Page extends StatefulWidget {
class _PageState extends State<Page> with HooksMixin {
int? seasonAnimeHoverIndex;
int? recentlyUpdatedHoverIndex;
final Map<int, StatefulValueHolder<MyAnimeListAnimeList?>> mediaCache =
<int, StatefulValueHolder<MyAnimeListAnimeList?>>{};
final Map<int, StatefulValueHolderWithError<MyAnimeListAnimeList?>>
mediaCache = <int, StatefulValueHolderWithError<MyAnimeListAnimeList?>>{};

@override
void initState() {
Expand All @@ -51,6 +53,34 @@ class _PageState extends State<Page> with HooksMixin {
hookState.markReady();
}

Future<void> getNodeId(
final int nodeId,
final StateSetter setState,
) async {
if (!mounted) return;

setState(() {
mediaCache[nodeId]!.resolving(null);
});

try {
final MyAnimeListAnimeList anime =
await MyAnimeListAnimeList.getFromNodeId(nodeId);

if (mounted) {
setState(() {
mediaCache[nodeId]!.resolve(anime);
});
}
} catch (err, stack) {
if (mounted) {
setState(() {
mediaCache[nodeId]!.fail(null, ErrorInfo(err, stack));
});
}
}
}

Widget buildOpenBuilder(final MyAnimeListHomeContent x) => StatefulBuilder(
builder: (
final BuildContext context,
Expand All @@ -64,17 +94,32 @@ class _PageState extends State<Page> with HooksMixin {

if (mediaCache[nodeId] == null) {
mediaCache[nodeId] =
StatefulValueHolder<MyAnimeListAnimeList?>(null);
StatefulValueHolderWithError<MyAnimeListAnimeList?>(null);
getNodeId(nodeId, setState);
}

MyAnimeListAnimeList.getFromNodeId(
nodeId,
).then((final MyAnimeListAnimeList m) {
if (mounted) {
setState(() {
mediaCache[nodeId]!.resolve(m);
});
}
});
if (mediaCache[nodeId]?.state.hasFailed ?? false) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
if (mounted) {
getNodeId(nodeId, setState);
}
},
icon: const Icon(Icons.refresh),
tooltip: Translator.t.refetch(),
),
],
),
body: Center(
child: KawaiiErrorWidget.fromErrorInfo(
message: Translator.t.somethingWentWrong(),
error: mediaCache[nodeId]!.error,
),
),
);
}

return Scaffold(
Expand Down

0 comments on commit 5357844

Please sign in to comment.