Skip to content

Commit

Permalink
refactor: 重构优化
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint committed Aug 12, 2023
1 parent 47d8c51 commit 9872a3e
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 188 deletions.
4 changes: 2 additions & 2 deletions lib/api/tmdb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TmdbApi {
defaultLanguage: MiruStorage.getSetting(SettingKey.language),
);

static Future<tmdb_model.TMDBDetail> getDetail(String keyword,
static Future<tmdb_model.TMDBDetail?> getDetail(String keyword,
{int page = 1}) async {
final result = await tmdb.v3.search.queryMulti(
keyword,
Expand All @@ -17,7 +17,7 @@ class TmdbApi {
// print(result);
final results = result["results"] as List;
if (results.isEmpty) {
throw Exception("No results");
return null;
}
late Map data;
final mediaType = results[0]["media_type"];
Expand Down
84 changes: 66 additions & 18 deletions lib/pages/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:miru_app/api/tmdb.dart';
import 'package:miru_app/models/index.dart';
Expand Down Expand Up @@ -49,7 +50,7 @@ class DetailPageController extends GetxController {
if (tmdbDetail != null && tmdbDetail!.backdrop != null) {
bg = TmdbApi.getImageUrl(tmdbDetail!.backdrop!) ?? '';
} else {
bg = detail!.cover;
bg = detail?.cover ?? '';
}
return bg;
}
Expand All @@ -64,11 +65,9 @@ class DetailPageController extends GetxController {
}

onRefresh() async {
// 获取收藏状态
runtime.value = ExtensionUtils.runtimes[package];
await refreshFavorite();
try {
// 获取扩展类型
runtime.value = ExtensionUtils.extensions[package];
_miruDetail = await DatabaseUtils.getMiruDetail(package, url);
_tmdbID = _miruDetail?.tmdbID ?? -1;
await getDetail();
Expand All @@ -95,27 +94,50 @@ class DetailPageController extends GetxController {

getRemoteDeatil() async {
try {
detail = await ExtensionUtils.extensions[package]?.detail(url);
detail = await runtime.value!.detail(url);
await DatabaseUtils.putMiruDetail(package, url, detail!, tmdbID: _tmdbID);
} catch (e) {
// 弹出错误信息
showPlatformSnackbar(
context: cuurentContext,
title: 'detail.get-lastest-data-error'.i18n,
content: e.toString().split('\n')[0],
severity: fluent.InfoBarSeverity.error,
);
if (runtime.value == null) {
final content = FlutterI18n.translate(
cuurentContext,
'common.extension-missing',
translationParams: {
'package': package,
},
);
showPlatformSnackbar(
context: cuurentContext,
title: '',
content: content,
severity: fluent.InfoBarSeverity.error,
);
throw content;
} else {
showPlatformSnackbar(
context: cuurentContext,
title: 'detail.get-lastest-data-error'.i18n,
content: e.toString().split('\n')[0],
severity: fluent.InfoBarSeverity.error,
);
}
rethrow;
}
}

getTMDBDetail() async {
tmdbDetail = await DatabaseUtils.getTMDBDetail(_tmdbID);
if (detail == null) {
return;
}
getRemoteTMDBDetail();
}

getRemoteTMDBDetail() async {
tmdbDetail = await TmdbApi.getDetail(detail!.title);
if (tmdbDetail == null) {
return;
}
_tmdbID = tmdbDetail!.id;
DatabaseUtils.putTMDBDetail(
tmdbDetail!.id,
Expand Down Expand Up @@ -151,15 +173,25 @@ class DetailPageController extends GetxController {
}

toggleFavorite() async {
if (isLoading.value) {
if (detail == null) {
return;
}
await DatabaseUtils.toggleFavorite(
package: package,
url: url,
cover: detail!.cover,
name: detail!.title,
);
try {
await DatabaseUtils.toggleFavorite(
package: package,
url: url,
cover: detail!.cover,
name: detail!.title,
);
} catch (e) {
showPlatformSnackbar(
context: cuurentContext,
title: '',
content: e.toString().split('\n')[0],
severity: fluent.InfoBarSeverity.error,
);
rethrow;
}
await refreshFavorite();
Get.find<HomePageController>().onRefresh();
}
Expand All @@ -170,6 +202,22 @@ class DetailPageController extends GetxController {
int index,
int selectEpGroup,
) {
if (runtime.value == null) {
showPlatformSnackbar(
context: cuurentContext,
title: '',
content: FlutterI18n.translate(
cuurentContext,
'common.extension-missing',
translationParams: {
'package': package,
},
),
severity: fluent.InfoBarSeverity.error,
);
return;
}

Navigator.of(context, rootNavigator: true).push(
PageRouteBuilder(
transitionDuration: const Duration(milliseconds: 600),
Expand Down
56 changes: 17 additions & 39 deletions lib/pages/detail/view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:fluent_ui/fluent_ui.dart' as fluent;
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:miru_app/api/tmdb.dart';
import 'package:miru_app/models/extension.dart';
Expand All @@ -13,7 +12,6 @@ import 'package:miru_app/pages/detail/widgets/detail_episodes.dart';
import 'package:miru_app/pages/detail/widgets/detail_extension_tile.dart';
import 'package:miru_app/pages/detail/widgets/detail_favorite_button.dart';
import 'package:miru_app/pages/detail/widgets/detail_overview.dart';
import 'package:miru_app/utils/extension.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/widgets/cache_network_image.dart';
import 'package:miru_app/widgets/card_tile.dart';
Expand Down Expand Up @@ -58,19 +56,6 @@ class _DetailPageState extends State<DetailPage> {
}

Widget _buildAndroidDetail(BuildContext context) {
if (!ExtensionUtils.extensions.containsKey(widget.package)) {
return Scaffold(
body: Center(
child: Text(FlutterI18n.translate(
context,
'common.extension-missing',
translationParams: {
'package': widget.package,
},
)),
),
);
}
return Scaffold(
body: Obx(() {
late String episodesString;
Expand Down Expand Up @@ -169,19 +154,6 @@ class _DetailPageState extends State<DetailPage> {
}

Widget _buildDesktopDetail(BuildContext context) {
if (!ExtensionUtils.extensions.containsKey(widget.package)) {
return Center(
child: Text(
FlutterI18n.translate(
context,
'common.extension-missing',
translationParams: {
'package': widget.package,
},
),
),
);
}
return Obx(() {
if (c.error.value.isNotEmpty) {
return Center(
Expand Down Expand Up @@ -226,15 +198,18 @@ class _DetailPageState extends State<DetailPage> {
child: Row(
children: [
if (constraints.maxWidth > 600) ...[
Container(
width: 230,
height: double.infinity,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
),
child: CacheNetWorkImage(
c.detail!.cover,
Hero(
tag: c.heroTag ?? '',
child: Container(
width: 230,
height: double.infinity,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
),
child: CacheNetWorkImage(
c.detail?.cover ?? '',
),
),
),
const SizedBox(width: 30),
Expand All @@ -245,7 +220,7 @@ class _DetailPageState extends State<DetailPage> {
mainAxisAlignment: MainAxisAlignment.end,
children: [
SelectableText(
c.detail!.title,
c.detail?.title ?? '',
style: const TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
Expand Down Expand Up @@ -292,7 +267,7 @@ class _DetailPageState extends State<DetailPage> {
),
),
const SizedBox(height: 30),
if (c.detail!.episodes != null) const DetailEpisodes(),
if (c.detail?.episodes != null) const DetailEpisodes(),
const SizedBox(height: 16),
Obx(
() {
Expand Down Expand Up @@ -418,6 +393,9 @@ class _DetailPageState extends State<DetailPage> {
padding: const EdgeInsets.symmetric(vertical: 8),
child: SelectableText(
c.tmdbDetail?.overview ?? c.detail?.desc ?? '',
style: const TextStyle(
height: 2,
),
),
),
),
Expand Down
9 changes: 8 additions & 1 deletion lib/pages/detail/widgets/detail_extension_tile.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:miru_app/pages/detail/controller.dart';
import 'package:miru_app/utils/extension.dart';
Expand All @@ -12,7 +13,13 @@ class DetailExtensionTile extends StatelessWidget {
final c = Get.find<DetailPageController>();
return Obx(() {
if (c.extension == null) {
return const SizedBox.shrink();
return Text(FlutterI18n.translate(
context,
'common.extension-missing',
translationParams: {
'package': c.package,
},
));
}
return Row(
children: [
Expand Down
6 changes: 3 additions & 3 deletions lib/pages/extension/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:miru_app/utils/extension.dart';
import 'package:miru_app/utils/extension_runtime.dart';

class ExtensionPageController extends GetxController {
RxMap<String, ExtensionRuntime> extensions = <String, ExtensionRuntime>{}.obs;
RxMap<String, ExtensionRuntime> runtimes = <String, ExtensionRuntime>{}.obs;
RxMap<String, String> errors = <String, String>{}.obs;
RxBool isInstallloading = false.obs;

Expand All @@ -14,9 +14,9 @@ class ExtensionPageController extends GetxController {
}

onRefresh() async {
extensions.clear();
runtimes.clear();
errors.clear();
extensions.addAll(ExtensionUtils.extensions);
runtimes.addAll(ExtensionUtils.runtimes);
errors.addAll(ExtensionUtils.extensionErrorMap);
}
}
13 changes: 7 additions & 6 deletions lib/pages/extension/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ class _ExtensionPageState extends State<ExtensionPage> {
// 定位目录
final dir = await ExtensionUtils.getExtensionsDir;
if (Platform.isAndroid) {
// 复制 dir
Clipboard.setData(ClipboardData(text: dir));
// ignore: use_build_context_synchronously
if (!mounted) {
return;
}
showPlatformSnackbar(
context: context,
title: 'extension.import.extension-dir'.i18n,
Expand Down Expand Up @@ -185,7 +186,7 @@ class _ExtensionPageState extends State<ExtensionPage> {
body: TabBarView(children: [
ListView(
children: [
if (c.extensions.isEmpty)
if (c.runtimes.isEmpty)
SizedBox(
height: 300,
child: Column(
Expand All @@ -195,7 +196,7 @@ class _ExtensionPageState extends State<ExtensionPage> {
],
),
),
for (final ext in c.extensions.values)
for (final ext in c.runtimes.values)
ExtensionTile(ext.extension),
],
),
Expand Down Expand Up @@ -240,7 +241,7 @@ class _ExtensionPageState extends State<ExtensionPage> {
],
),
const SizedBox(height: 16),
if (c.extensions.isEmpty)
if (c.runtimes.isEmpty)
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -261,7 +262,7 @@ class _ExtensionPageState extends State<ExtensionPage> {
Expanded(
child: ListView(
children: [
for (final ext in c.extensions.values)
for (final ext in c.runtimes.values)
Container(
margin: const EdgeInsets.only(bottom: 8),
child: ExtensionTile(ext.extension),
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/extension_repo/widgets/extension_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class _ExtensionCardState extends State<ExtensionCard> {
@override
void initState() {
setState(() {
isInstall = ExtensionUtils.extensions.containsKey(widget.package);
isInstall = ExtensionUtils.runtimes.containsKey(widget.package);
hasUpgrade = isInstall &&
ExtensionUtils.extensions[widget.package]!.extension.version !=
ExtensionUtils.runtimes[widget.package]!.extension.version !=
widget.version;
});
super.initState();
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/extension_settings/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ExtensionSettingsPageController extends GetxController {
}

onRefresh() async {
runtime.value = ExtensionUtils.extensions[package];
runtime.value = ExtensionUtils.runtimes[package];
settings.clear();
settings.addAll(await DatabaseUtils.getExtensionSettings(package));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home/widgets/home_resent_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class _HomeRecentCardState extends State<HomeRecentCard> {
}

_getUpdate() async {
_runtime = ExtensionUtils.extensions[widget.history.package];
_runtime = ExtensionUtils.runtimes[widget.history.package];
if (_runtime == null) {
return;
}
Expand Down
Loading

0 comments on commit 9872a3e

Please sign in to comment.