import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; import 'package:isekai_wiki/api/mw/list.dart'; import 'package:isekai_wiki/api/mw/mw_api.dart'; import 'package:isekai_wiki/api/mw/parse.dart'; import 'package:isekai_wiki/api/response/page_info.dart'; import 'package:isekai_wiki/api/response/parse.dart'; import 'package:isekai_wiki/components/bottom_nav_bar.dart'; import 'package:isekai_wiki/components/safearea_builder.dart'; import 'package:isekai_wiki/components/wikipage_parser.dart'; import 'package:isekai_wiki/reactive/reactive.dart'; import 'package:isekai_wiki/utils/dialog.dart'; import 'package:isekai_wiki/utils/error.dart'; import '../components/isekai_nav_bar.dart'; import '../components/isekai_page_scaffold.dart'; class MinimumArticleData { final String title; final String? description; final String? mainCategory; final DateTime? updateTime; MinimumArticleData({required this.title, this.description, this.mainCategory, this.updateTime}); } class ArticleCategoryData { final String name; final String identity; ArticleCategoryData({required this.name, required this.identity}); } class ArticlePageController extends GetxController { var loading = true.obs; var pageTitle = "".obs; var pageId = 0.obs; var pageInfo = Rx(null); var parseInfo = Rx(null); var displayTitle = "".obs; Future loadPageContent() async { if (pageTitle.isNotEmpty || pageId.value != 0) { try { // 加载页面信息 loading.value = true; MWResponse> pageInfoRes; if (pageId.value != 0) { pageInfoRes = await MWApiList.getPageInfoList(pageids: [pageId.value]); } else { pageInfoRes = await MWApiList.getPageInfoList(titles: [pageTitle.value]); } if (pageInfoRes.data.isEmpty) { throw MWApiErrorException(code: 'no-page', info: "页面信息丢失"); } pageInfo.value = pageInfoRes.data[0]; displayTitle.value = pageInfo.value!.mainTitle; pageId.value = pageInfo.value!.pageid; pageTitle.value = pageInfo.value!.title; // 获取页面HTML var parseRes = await MWApiParse.parse(pageId: pageId.value); parseInfo.value = parseRes.data; } catch (err, stack) { alert(Get.overlayContext!, ErrorUtils.getErrorMessage(err), title: "错误"); if (kDebugMode) { print("Exception in page: $err"); stack.printError(); } } finally { loading.value = false; } } } } class ArticlePage extends StatefulWidget { final MinimumArticleData? initialArticleData; final String? targetPage; final int? targetPageId; const ArticlePage({super.key, this.targetPage, this.targetPageId, this.initialArticleData}); @override State createState() => _ArticlePageState(); } class _ArticlePageState extends ReactiveState { var c = ArticlePageController(); @override void initState() { super.initState(); Get.put(c); c.loadPageContent(); } @override void receiveProps() { c.pageTitle.value = widget.targetPage ?? ""; c.pageId.value = widget.targetPageId ?? 0; c.displayTitle.value = widget.initialArticleData?.title ?? "加载中"; } @override Widget render(BuildContext context) { return IsekaiPageScaffold( navigationBar: IsekaiNavigationBar( middle: Obx(() => Text(c.displayTitle.value)), ), bottomNavigationBar: BottomNavigationBar(child: SizedBox()), child: SafeAreaBuilder( builder: (context, padding) => Obx( () => WikiPageParser( padding: padding, pageInfo: c.pageInfo.value, parseInfo: c.parseInfo.value, ), ), ), ); } }