Skip to content

Commit

Permalink
🐛 修复进度记录错位,隐藏资源相关
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed May 20, 2024
1 parent a8342e6 commit b7f6e2a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 56 deletions.
8 changes: 7 additions & 1 deletion lib/pages/play/play_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package imports:
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';
Expand Down Expand Up @@ -91,8 +92,13 @@ class PlayController extends StateNotifier<PlayControllerState> {

/// 保存播放进度
Future<void> saveProgress() async {
if (state.player.state.playlist.medias.isEmpty) return;
var progress = state.player.state.position.inMilliseconds;
await state.hive.updateProgress(progress);
var media =
state.player.state.playlist.medias[state.player.state.playlist.index];
var episode = media.extras?['episode'];
debugPrint('progress: $progress, episode: $episode');
await state.hive.updateProgress(progress, episode);
}

/// 跳转
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/play/play_history_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Flutter imports:
import 'package:flutter/foundation.dart';

// Package imports:
import 'package:fluent_ui/fluent_ui.dart';

Expand Down Expand Up @@ -268,7 +271,7 @@ class _PlayHistoryPageState extends State<PlayHistoryPage> {
children: [
buildDelHistoryButton(item),
buildEditButton(item),
buildSearchButton(item),
if (kDebugMode) buildSearchButton(item),
],
),
content: Column(
Expand Down
6 changes: 4 additions & 2 deletions lib/pages/play/play_list_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Flutter imports:
import 'package:flutter/foundation.dart';

// Package imports:
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -135,8 +138,7 @@ class _PlayListPageState extends ConsumerState<PlayListPage>
onPressed: () async => await fileTool.openScreenshotDir(),
),
),
const SizedBox(width: 8),
buildSourceBox(),
if (kDebugMode) ...[const SizedBox(width: 8), buildSourceBox()],
const Spacer(),
const SizedBox(width: 8),
]);
Expand Down
14 changes: 11 additions & 3 deletions lib/pages/play/play_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ class _PlayVideoWidgetState extends ConsumerState<PlayVideoWidget> {
await player.play();
}

/// 保存进度
Future<void> saveProgress() async {
if (player.state.playlist.medias.isEmpty) return;
var progress = player.state.position.inMilliseconds;
var media = player.state.playlist.medias[player.state.playlist.index];
var episode = media.extras?['episode'];
await hivePlay.updateProgress(progress, episode);
}

/// 控制栏的数据构建
/// todo,这边的 widget 似乎不会改变,详见 https://github.com/media-kit/media-kit/issues/808
MaterialDesktopVideoControlsThemeData buildControls() {
Expand All @@ -162,13 +171,12 @@ class _PlayVideoWidgetState extends ConsumerState<PlayVideoWidget> {
seekBarPositionColor: base.darker,
bottomButtonBar: [
IconButton(
icon: const Icon(FluentIcons.play),
icon: const Icon(FluentIcons.play, color: Colors.white),
onPressed: () async {
var isPlaying = player.state.playing;
if (isPlaying) {
await player.pause();
await hivePlay
.updateProgress(player.state.position.inMilliseconds);
await saveProgress();
} else {
await player.play();
}
Expand Down
3 changes: 1 addition & 2 deletions lib/pages/play/play_vod_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class _PlayVodPageState extends ConsumerState<PlayVodPage>
super.didUpdateWidget(oldWidget);
if (oldWidget.subject == widget.subject) return;
Future.microtask(() async {
await saveProgress();
await freshList();
});
}
Expand Down Expand Up @@ -115,7 +114,7 @@ class _PlayVodPageState extends ConsumerState<PlayVodPage>
var progress = player.state.position.inMilliseconds;
var media = playList[index];
var episode = media.extras?['episode'];
await hive.updateProgress(progress, index: episode);
await hive.updateProgress(progress, episode);
}

/// 处理非本地源的播放
Expand Down
62 changes: 39 additions & 23 deletions lib/source/utils/source_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ Future<void> showSourceSearchDialog(
content: ListView.separated(
padding: const EdgeInsets.only(right: 12),
itemCount: find.length,
separatorBuilder: (context, index) =>
const SizedBox(height: 12, child: Center(child: Divider())),
separatorBuilder: (context, index) => const SizedBox(height: 16),
itemBuilder: (context, index) {
return SourceSearchItem(
item: item, data: find[index], callback: callback);
Expand Down Expand Up @@ -83,21 +82,29 @@ class _SourceSearchItemState extends State<SourceSearchItem> {

/// 构建无封面的卡片
Widget buildEmptyCover({String? err}) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 12.h),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
BaseThemeIcon.darkest(FluentIcons.photo_error, size: 28.sp),
Text(
err ?? '无封面',
style: TextStyle(
color: FluentTheme.of(context).accentColor.darkest,
return DecoratedBox(
decoration: BoxDecoration(color: FluentTheme.of(context).cardColor),
child: Padding(
padding: const EdgeInsets.all(8),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
BaseThemeIcon.darkest(FluentIcons.photo_error, size: 28.sp),
Expanded(
child: Tooltip(
message: err ?? '无封面',
child: Text(
err ?? '无封面',
style: TextStyle(
color: FluentTheme.of(context).accentColor.darkest,
),
),
),
),
),
],
],
),
),
),
);
Expand Down Expand Up @@ -135,7 +142,7 @@ class _SourceSearchItemState extends State<SourceSearchItem> {
),
),
if (find.desc != null && find.desc!.isNotEmpty)
Text(find.desc!, maxLines: 2, overflow: TextOverflow.ellipsis),
Text(find.desc!, maxLines: 3, overflow: TextOverflow.ellipsis),
const Spacer(),
IconButton(
icon: const BaseThemeIcon(FluentIcons.accept),
Expand Down Expand Up @@ -180,18 +187,16 @@ class _SourceSearchItemState extends State<SourceSearchItem> {
/// buildCard
Widget buildCard(BtSourceFind find) {
return SizedBox(
height: 150.h,
height: 200.h,
child: Card(
padding: EdgeInsets.zero,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
ConstrainedBox(
constraints: BoxConstraints(maxHeight: 150.h, maxWidth: 200.w),
child: buildCover(find),
),
Flexible(child: buildCover(find)),
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(4.sp),
child: buildInfo(find),
Expand All @@ -203,6 +208,17 @@ class _SourceSearchItemState extends State<SourceSearchItem> {
);
}

/// buildList
List<Widget> buildList() {
var res = <Widget>[];
for (var find in widget.data.find) {
res.add(buildCard(find));
if (find == widget.data.find.last) continue;
res.add(const SizedBox(height: 12, child: Center(child: Divider())));
}
return res;
}

@override
Widget build(BuildContext context) {
return Column(
Expand All @@ -211,7 +227,7 @@ class _SourceSearchItemState extends State<SourceSearchItem> {
source.name,
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
for (var find in widget.data.find) buildCard(find),
...buildList(),
],
);
}
Expand Down
28 changes: 5 additions & 23 deletions lib/store/play_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,10 @@ class PlayHive extends ChangeNotifier {
}

/// 更新当前播放进度
Future<void> updateProgress(int progress, {int? index}) async {
Future<void> updateProgress(int progress, int index) async {
var model = curModel;
if (model == null) return;
int find;
if (index != null) {
find = model.items.indexWhere((e) => e.episode == index);
} else {
find = model.items.indexWhere((e) => e.episode == curEp);
}
var find = model.items.indexWhere((e) => e.episode == curEp);
if (find == -1) return;
model.items[find].progress = progress;
BTLogTool.info('更新进度: $progress');
Expand Down Expand Up @@ -278,18 +273,6 @@ class PlayHive extends ChangeNotifier {
return model.sources[sourceIndex].items[find].index;
}

int getNextBmfEpisode(int subject) {
var model = box.get(subject);
if (model == null) return 0;
var sourceIndex = model.sources.indexWhere((e) => e.source == "BMF");
if (sourceIndex == -1) return 0;
var max = 0;
for (var item in model.sources[sourceIndex].items) {
if (item.index > max) max = item.index;
}
return max + 1;
}

/// 删除播放进度
Future<void> deleteProgress(int subjectId, {int? episode}) async {
var model = box.get(subjectId);
Expand Down Expand Up @@ -327,10 +310,9 @@ class PlayHive extends ChangeNotifier {
void switchSubject(int value) {
curModel = box.get(value);
curSource = curModel!.sources[0].source;
if (curModel!.items.isNotEmpty) {
curEp = curModel!.items[0].episode;
} else {
curEp = 0;
var list = getPlayList(subject: value);
if (list.isNotEmpty) {
curEp = list[0].extras?['episode'];
}
notifyListeners();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/log_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BTLogTool {
printTime: true,
);
} else {
printer = PrettyPrinter(printTime: true);
printer = PrettyPrinter(printTime: true, methodCount: 5);
}
logger = Logger(
filter: BTLogFilter(),
Expand Down

0 comments on commit b7f6e2a

Please sign in to comment.