style(app): 格式化并拆分长函数

This commit is contained in:
Zhou Weidong committed 2026-10-20 04:38:36 +08:00
1 parent 2b2b7e9913
commit 7850dfc746
31 files changed
+196 -126

No files matched your search

+2 -1
View File
@@ -146,7 +146,8 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
);
late final CommerceRepository _commerceRepository =
widget.commerceRepository ?? HttpCommerceRepository(CommerceApi(_apiClient));
widget.commerceRepository ??
HttpCommerceRepository(CommerceApi(_apiClient));
late final WalletController _walletController = WalletController(
_commerceRepository,
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'json_numbers.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'json_numbers.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'drama.dart';
import 'json_numbers.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import '../models/order.dart';
import '../models/product.dart';
@@ -59,7 +59,8 @@ class _CachedTicket {
final PlayTicket ticket;
final DateTime fetchedAt;
bool isExpired(DateTime now, Duration ttl) => now.difference(fetchedAt) >= ttl;
bool isExpired(DateTime now, Duration ttl) =>
now.difference(fetchedAt) >= ttl;
}
/// 测试与离线场景用:按集返回固定票据,并记录被请求过几次。
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import '../models/search_result.dart';
import '../remote/catalog_api.dart';
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import '../models/order.dart';
import '../models/product.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/foundation.dart';
+2 -6
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import '../../core/data/models/drama.dart';
import '../../core/data/repositories/search_repository.dart';
@@ -10,9 +10,5 @@ class DiscoverBinding {
required Drama drama,
required WatchHistoryRepository history,
SearchRepository? search,
}) => DiscoverLogic(
drama: drama,
history: history,
searchRepository: search,
);
}) => DiscoverLogic(drama: drama, history: history, searchRepository: search);
}
+2 -1
View File
@@ -134,7 +134,8 @@ class DiscoverLogic extends ChangeNotifier {
void _scheduleSearch(String query) {
final alreadyRequested = query == _lastRequestedQuery;
if (alreadyRequested && (searchLoading || searchResults.isNotEmpty || searchError != null)) {
if (alreadyRequested &&
(searchLoading || searchResults.isNotEmpty || searchError != null)) {
return;
}
_lastRequestedQuery = query;
+3 -1
View File
@@ -181,7 +181,8 @@ class _DiscoverPageState extends State<DiscoverPage> {
padding: const EdgeInsets.fromLTRB(0, 0, 0, 12),
child: _languages(l10n, allDramas),
),
), if (logic.resumeRecord != null && logic.resumeDrama(allDramas) != null)
),
if (logic.resumeRecord != null && logic.resumeDrama(allDramas) != null)
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 14),
@@ -495,6 +496,7 @@ class _DiscoverPageState extends State<DiscoverPage> {
if (code == 'en') return l10n.languageEnglish;
return code;
}
Widget _continueCard(
AppLocalizations l10n,
Drama drama,
+20 -10
View File
@@ -25,13 +25,27 @@ class _HomeDramaGrid extends StatelessWidget {
@override
Widget build(BuildContext context) {
final loading = _loadingState();
if (loading != null) return loading;
final search = _searchState();
if (search != null) return search;
if (dramas.isEmpty) return _emptyState();
return SliverMainAxisGroup(
slivers: [
SliverToBoxAdapter(child: _masonry(dramas)),
const SliverToBoxAdapter(child: SizedBox(height: 24)),
],
);
}
Widget? _loadingState() {
if (categories.loading && categories.feed.sections.isEmpty) {
return const SliverFillRemaining(
hasScrollBody: false,
child: Center(child: CircularProgressIndicator(strokeWidth: 2)),
);
}
if (categories.error != null) {
if (categories.error == null) return null;
return _fill(
EmptyState(
key: const Key('home-categories-error'),
@@ -43,6 +57,8 @@ class _HomeDramaGrid extends StatelessWidget {
),
);
}
Widget? _searchState() {
if (searching && searchLoading) {
return const SliverFillRemaining(
hasScrollBody: false,
@@ -54,7 +70,7 @@ class _HomeDramaGrid extends StatelessWidget {
),
);
}
if (searching && searchError != null) {
if (!searching || searchError == null) return null;
return _fill(
EmptyState(
key: const Key('discover-search-error'),
@@ -66,7 +82,8 @@ class _HomeDramaGrid extends StatelessWidget {
),
);
}
if (dramas.isEmpty) {
Widget _emptyState() {
return _fill(
EmptyState(
key: const Key('home-all-dramas-empty'),
@@ -82,13 +99,6 @@ class _HomeDramaGrid extends StatelessWidget {
),
);
}
return SliverMainAxisGroup(
slivers: [
SliverToBoxAdapter(child: _masonry(dramas)),
const SliverToBoxAdapter(child: SizedBox(height: 24)),
],
);
}
Widget _masonry(List<Drama> items) {
return Padding(
+1 -4
View File
@@ -283,10 +283,7 @@ class PlayerLogic extends ChangeNotifier {
}
final ready = await ensureTicket(index, forceRefresh: true);
if (ready) {
await session.select(
index,
preloadIndex: nextVisibleIndex(index),
);
await session.select(index, preloadIndex: nextVisibleIndex(index));
}
return ready;
} finally {
+2 -1
View File
@@ -991,7 +991,8 @@ class _PlaybackOverlayState extends State<_PlaybackOverlay> {
Navigator.pop(sheetContext);
widget.onQualityTap();
},
), ListTile(
),
ListTile(
key: const Key('player-more-download'),
leading: const Icon(
Icons.download_outlined,
+26 -20
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/material.dart';
@@ -26,7 +26,16 @@ class QualitySheet extends StatelessWidget {
key: const Key('player-quality-sheet'),
mainAxisSize: MainAxisSize.min,
children: [
Padding(
_header(l10n),
for (final quality in qualities) _qualityTile(l10n, quality),
_hint(l10n),
],
),
);
}
Widget _header(AppLocalizations l10n) {
return Padding(
padding: const EdgeInsets.fromLTRB(20, 2, 20, 8),
child: Align(
alignment: Alignment.centerLeft,
@@ -39,38 +48,35 @@ class QualitySheet extends StatelessWidget {
),
),
),
),
for (final quality in qualities)
ListTile(
);
}
Widget _qualityTile(AppLocalizations l10n, String quality) {
final active = quality == selected;
return ListTile(
key: Key('player-quality-$quality'),
dense: true,
title: Text(
_label(l10n, quality),
style: TextStyle(
color: quality == selected ? AppColors.blue : AppColors.cream,
color: active ? AppColors.blue : AppColors.cream,
fontSize: 13,
fontWeight: quality == selected
? FontWeight.w700
: FontWeight.w500,
fontWeight: active ? FontWeight.w700 : FontWeight.w500,
),
),
trailing: quality == selected
? const Icon(
Icons.check_rounded,
color: AppColors.blue,
size: 18,
)
trailing: active
? const Icon(Icons.check_rounded, color: AppColors.blue, size: 18)
: null,
),
Padding(
);
}
Widget _hint(AppLocalizations l10n) {
return Padding(
padding: const EdgeInsets.fromLTRB(20, 6, 20, 14),
child: Text(
l10n.qualityAutoHint,
style: const TextStyle(color: AppColors.muted, fontSize: 11),
),
),
],
),
);
}
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/material.dart';
+29 -22
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'dart:async';
@@ -122,7 +122,9 @@ class _StorePageState extends State<StorePage> {
controller.purchaseError,
l10n.orderCreateFailed,
);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(message)));
return;
}
widget.onPurchased?.call();
@@ -162,8 +164,16 @@ class _ProductTile extends StatelessWidget {
children: [
_ProductBadge(vip: product.isVip),
const SizedBox(width: 12),
Expanded(
child: Column(
Expanded(child: _info(l10n)),
const SizedBox(width: 10),
_price(l10n),
],
),
);
}
Widget _info(AppLocalizations l10n) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
@@ -180,16 +190,14 @@ class _ProductTile extends StatelessWidget {
const SizedBox(height: 4),
Text(
_detail(l10n),
style: const TextStyle(
color: AppColors.muted,
fontSize: 11,
),
style: const TextStyle(color: AppColors.muted, fontSize: 11),
),
],
),
),
const SizedBox(width: 10),
Column(
);
}
Widget _price(AppLocalizations l10n) {
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
@@ -217,22 +225,21 @@ class _ProductTile extends StatelessWidget {
fontWeight: FontWeight.w700,
),
),
child: purchasing
? const SizedBox(
child: purchasing ? _buyProgress() : Text(l10n.buy),
),
),
],
);
}
Widget _buyProgress() {
return const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(
strokeWidth: 2,
color: AppColors.onPrimary,
),
)
: Text(l10n.buy),
),
),
],
),
],
),
);
}
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/material.dart';
+5 -4
View File
@@ -90,9 +90,8 @@ class _WalletPageState extends State<WalletPage> {
void _openRecharge(WalletController wallet) {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => RechargePage(
onPurchased: () => unawaited(wallet.load()),
),
builder: (_) =>
RechargePage(onPurchased: () => unawaited(wallet.load())),
),
);
}
@@ -320,7 +319,9 @@ class _LedgerTile extends StatelessWidget {
Widget build(BuildContext context) {
final positive = entry.changeAmount >= 0;
final color = positive ? AppColors.success : AppColors.accent;
final amount = positive ? '+${entry.changeAmount}' : '${entry.changeAmount}';
final amount = positive
? '+${entry.changeAmount}'
: '${entry.changeAmount}';
return Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
+6 -3
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'dart:convert';
@@ -9,7 +9,9 @@ import 'package:fruitshorts/core/data/remote/catalog_api.dart';
import '../support/fake_http_adapter.dart';
void main() {
test('should_request_search_with_keyword_and_paging_when_searching', () async {
test(
'should_request_search_with_keyword_and_paging_when_searching',
() async {
final adapter = FakeHttpAdapter(
(options) async => FakeHttpResponse(
jsonEncode({
@@ -47,5 +49,6 @@ void main() {
expect(page.total, 1);
expect(page.list.single.title, 'Velvet Nights');
expect(page.list.single.language, 'id');
});
},
);
}
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'dart:convert';
+2 -6
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/order.dart';
@@ -81,11 +81,7 @@ AppProduct _product(String name, {int coinAmount = 550, int bonusCoin = 0}) =>
);
class _FakeStoreRepository implements StoreRepository {
_FakeStoreRepository({
this.products = const [],
this.order,
this.orderError,
});
_FakeStoreRepository({this.products = const [], this.order, this.orderError});
final List<AppProduct> products;
final AppOrder? order;
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+69 -21
View File
@@ -8,7 +8,11 @@ import 'package:fruitshorts/pages/discover/discover_logic.dart';
import '../support/fake_watch_history.dart';
Drama _drama(String id, {List<int> numbers = const [1, 2, 3], String language = ''}) => Drama(
Drama _drama(
String id, {
List<int> numbers = const [1, 2, 3],
String language = '',
}) => Drama(
language: language,
id: id,
title: id,
@@ -29,7 +33,9 @@ Drama _drama(String id, {List<int> numbers = const [1, 2, 3], String language =
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('should_resolve_resume_position_of_episode_one_when_constructed', () async {
test(
'should_resolve_resume_position_of_episode_one_when_constructed',
() async {
final history = FakeWatchHistory()..saved = const Duration(seconds: 4);
final logic = DiscoverLogic(history: history, drama: _drama('signal'));
@@ -37,7 +43,8 @@ void main() {
expect(history.reads, ['signal']);
expect(logic.searchOpen, isFalse);
logic.dispose();
});
},
);
test('should_reread_resume_position_when_drama_is_swapped', () async {
final history = FakeWatchHistory();
@@ -51,7 +58,9 @@ void main() {
logic.dispose();
});
test('should_resume_from_last_watch_when_list_card_has_no_episodes', () async {
test(
'should_resume_from_last_watch_when_list_card_has_no_episodes',
() async {
final history = FakeWatchHistory()
..saved = const Duration(seconds: 9)
..lastEpisode = 'signal-2';
@@ -63,10 +72,14 @@ void main() {
expect(await logic.position, const Duration(seconds: 9));
expect(history.reads, ['signal']);
logic.dispose();
});
},
);
test('should_filter_visible_dramas_when_language_is_selected', () {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
final dramas = [
_drama('zh', language: 'zh-CN'),
_drama('id', language: 'id'),
@@ -84,7 +97,10 @@ void main() {
});
test('should_list_distinct_languages_when_feed_has_cards', () {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
final dramas = [
_drama('zh', language: 'zh-CN'),
_drama('id', language: 'id'),
@@ -109,7 +125,10 @@ void main() {
});
test('should_show_search_when_flag_is_on_or_text_is_typed', () {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
expect(logic.showSearch, isFalse);
@@ -126,7 +145,10 @@ void main() {
});
test('should_match_by_id_and_tolerate_missing_id_when_looking_up_drama', () {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
final catalog = [_drama('signal'), _drama('city')];
expect(logic.dramaFor('city', catalog)?.id, 'city');
@@ -167,7 +189,12 @@ void main() {
// Staying active with a changed drama re-reads history.
history.reads.clear();
sync(_drama('vengeance'), active: true, wasActive: true, dramaChanged: true);
sync(
_drama('vengeance'),
active: true,
wasActive: true,
dramaChanged: true,
);
expect(logic.drama.id, 'vengeance');
expect(history.reads, ['vengeance']);
@@ -179,10 +206,13 @@ void main() {
logic.dispose();
});
testWidgets('should_open_and_focus_field_when_search_is_requested_and_page_is_active', (
tester,
) async {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
testWidgets(
'should_open_and_focus_field_when_search_is_requested_and_page_is_active',
(tester) async {
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
@@ -210,12 +240,16 @@ void main() {
await tester.pumpWidget(const SizedBox.shrink());
logic.dispose();
});
},
);
testWidgets('should_keep_search_field_closed_when_page_is_hidden', (
tester,
) async {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
@@ -250,7 +284,10 @@ void main() {
testWidgets('should_return_choice_and_forward_it_when_act_runs_embedded', (
tester,
) async {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
final forwarded = <DiscoverPageAction>[];
final popped = Completer<DiscoverPageAction?>();
late BuildContext pageContext;
@@ -297,7 +334,10 @@ void main() {
testWidgets('should_show_artwork_notice_when_card_has_no_playable_media', (
tester,
) async {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
late BuildContext context;
await tester.pumpWidget(
@@ -322,8 +362,13 @@ void main() {
logic.dispose();
});
testWidgets('should_forward_chosen_drama_when_card_is_embedded', (tester) async {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
testWidgets('should_forward_chosen_drama_when_card_is_embedded', (
tester,
) async {
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
late BuildContext context;
Drama? selected;
@@ -350,7 +395,10 @@ void main() {
});
test('should_do_nothing_when_notifying_after_dispose', () {
final logic = DiscoverLogic(history: FakeWatchHistory(), drama: _drama('s'));
final logic = DiscoverLogic(
history: FakeWatchHistory(),
drama: _drama('s'),
);
var notifications = 0;
logic.addListener(() => notifications++);
+9 -4
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/drama.dart';
@@ -21,7 +21,9 @@ Drama _drama(String id, {String language = 'id'}) => Drama(
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('should_query_backend_and_render_results_when_search_text_changes', () async {
test(
'should_query_backend_and_render_results_when_search_text_changes',
() async {
final repository = _FakeSearchRepository(results: [_drama('velvet')]);
final logic = DiscoverLogic(
history: FakeWatchHistory(),
@@ -33,11 +35,14 @@ void main() {
logic.onSearchChanged();
await pumpEventQueue();
expect(repository.calls, [(keyword: 'velvet', pageIndex: 1, pageSize: 20)]);
expect(repository.calls, [
(keyword: 'velvet', pageIndex: 1, pageSize: 20),
]);
expect(logic.searchLoading, isFalse);
expect(logic.visibleDramas(const []).single.id, 'velvet');
logic.dispose();
});
},
);
test('should_keep_server_results_when_text_only_loses_whitespace', () async {
final repository = _FakeSearchRepository(results: [_drama('velvet')]);
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/play_ticket.dart';
+1 -6
View File
@@ -28,12 +28,7 @@ void main() {
),
);
logic.sessionEpisodes = [
Episode(
id: '12',
number: 1,
title: 'Episode 1',
isFree: true,
),
Episode(id: '12', number: 1, title: 'Episode 1', isFree: true),
];
final ready = await logic.ensureTicket(0);
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
// Author: fruit.com.cn
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+1 -1
View File
@@ -1,4 +1,4 @@
import 'package:fruitshorts/core/data/models/drama.dart';
import 'package:fruitshorts/core/data/models/drama.dart';
import 'package:fruitshorts/core/data/models/home_feed.dart';
import 'package:fruitshorts/core/data/models/search_result.dart';
import 'package:fruitshorts/core/data/repositories/catalog_repository.dart';