Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: add timed shutdown setting (issue #1586) (#1666)

funnytime78b3c4f6cdc4
diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dart
index 9dfcffb5..7c148fad 100644
--- a/lib/pages/player/player_item.dart
+++ b/lib/pages/player/player_item.dart
@@ -44,6 +44,7 @@ class PlayerItem extends StatefulWidget {
required this.keyboardFocus,
required this.sendDanmaku,
required this.showDanmakuDestinationPickerAndSend,
+ required this.pauseForTimedShutdown,
this.disableAnimations = false,
});
@@ -56,6 +57,7 @@ class PlayerItem extends StatefulWidget {
final FocusNode keyboardFocus;
final bool disableAnimations;
final void Function(String) showDanmakuDestinationPickerAndSend;
+ final VoidCallback pauseForTimedShutdown;
@override
State<PlayerItem> createState() => _PlayerItemState();
@@ -1514,6 +1516,7 @@ class _PlayerItemState extends State<PlayerItem>
showSyncPlayEndPointSwitchDialog:
showSyncPlayEndPointSwitchDialog,
showDanmakuDestinationPickerAndSend: widget.showDanmakuDestinationPickerAndSend,
+ pauseForTimedShutdown: widget.pauseForTimedShutdown,
disableAnimations: widget.disableAnimations,
handleScreenShot: handleScreenshot,
skipOP: skipOP,
@@ -1539,6 +1542,7 @@ class _PlayerItemState extends State<PlayerItem>
showSyncPlayRoomCreateDialog,
showSyncPlayEndPointSwitchDialog:
showSyncPlayEndPointSwitchDialog,
+ pauseForTimedShutdown: widget.pauseForTimedShutdown,
disableAnimations: widget.disableAnimations,
skipOP: skipOP,
),
diff --git a/lib/pages/player/player_item_panel.dart b/lib/pages/player/player_item_panel.dart
index 3a0589e9..f310fe0e 100644
--- a/lib/pages/player/player_item_panel.dart
+++ b/lib/pages/player/player_item_panel.dart
@@ -16,6 +16,7 @@ import 'package:kazumi/utils/constants.dart';
import 'package:hive_ce/hive.dart';
import 'package:kazumi/utils/storage.dart';
import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
+import 'package:kazumi/utils/timed_shutdown_service.dart';
class PlayerItemPanel extends StatefulWidget {
const PlayerItemPanel({
@@ -42,6 +43,7 @@ class PlayerItemPanel extends StatefulWidget {
required this.showSyncPlayRoomCreateDialog,
required this.showSyncPlayEndPointSwitchDialog,
required this.showDanmakuDestinationPickerAndSend,
+ required this.pauseForTimedShutdown,
this.disableAnimations = false,
});
@@ -67,6 +69,7 @@ class PlayerItemPanel extends StatefulWidget {
final void Function() showSyncPlayRoomCreateDialog;
final void Function() showSyncPlayEndPointSwitchDialog;
final void Function(String) showDanmakuDestinationPickerAndSend;
+ final VoidCallback pauseForTimedShutdown;
final bool disableAnimations;
@override
@@ -269,6 +272,8 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
});
}
+
+
@override
void initState() {
super.initState();
@@ -1212,10 +1217,90 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
),
),
),
+ // 定时关闭
+ SubmenuButton(
+ menuChildren: [
+ MenuItemButton(
+ onPressed: () {
+ TimedShutdownService().cancel();
+ },
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: Text(
+ "不开启",
+ style: TextStyle(
+ color: !TimedShutdownService().isActive
+ ? Theme.of(context).colorScheme.primary
+ : null,
+ ),
+ ),
+ ),
+ ),
+ ),
+ for (final int minutes in [15, 30, 60])
+ MenuItemButton(
+ onPressed: () {
+ TimedShutdownService().start(minutes, onExpired: widget.pauseForTimedShutdown);
+ KazumiDialog.showToast(message: '已设置 ${TimedShutdownService().formatMinutesToDisplay(minutes)} 后定时关闭');
+ },
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: Text(
+ "$minutes 分钟",
+ style: TextStyle(
+ color: TimedShutdownService().setMinutes == minutes
+ ? Theme.of(context).colorScheme.primary
+ : null,
+ ),
+ ),
+ ),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () {
+ TimedShutdownService.showCustomTimerDialog(
+ onExpired: widget.pauseForTimedShutdown,
+ );
+ },
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: Text("自定义"),
+ ),
+ ),
+ ),
+ ],
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: ValueListenableBuilder<int>(
+ valueListenable: TimedShutdownService().remainingSecondsNotifier,
+ builder: (context, remainingSeconds, child) {
+ return Text(
+ remainingSeconds > 0
+ ? "定时关闭 (${TimedShutdownService().formatRemainingTime()})"
+ : "定时关闭",
+ );
+ },
+ ),
+ ),
+ ),
+ ),
SubmenuButton(
menuChildren: [
MenuItemButton(
child: Container(
+
height: 48,
constraints: BoxConstraints(minWidth: 112),
child: Align(
diff --git a/lib/pages/player/smallest_player_item_panel.dart b/lib/pages/player/smallest_player_item_panel.dart
index f85d6eb5..7b465ea0 100644
--- a/lib/pages/player/smallest_player_item_panel.dart
+++ b/lib/pages/player/smallest_player_item_panel.dart
@@ -16,6 +16,7 @@ import 'package:kazumi/utils/storage.dart';
import 'package:kazumi/bean/appbar/drag_to_move_bar.dart' as dtb;
import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
import 'package:kazumi/bean/widget/embedded_native_control_area.dart';
+import 'package:kazumi/utils/timed_shutdown_service.dart';
class SmallestPlayerItemPanel extends StatefulWidget {
const SmallestPlayerItemPanel({
@@ -37,6 +38,7 @@ class SmallestPlayerItemPanel extends StatefulWidget {
required this.showVideoInfo,
required this.showSyncPlayRoomCreateDialog,
required this.showSyncPlayEndPointSwitchDialog,
+ required this.pauseForTimedShutdown,
this.disableAnimations = false,
});
@@ -57,6 +59,7 @@ class SmallestPlayerItemPanel extends StatefulWidget {
final void Function() showVideoInfo;
final void Function() showSyncPlayRoomCreateDialog;
final void Function() showSyncPlayEndPointSwitchDialog;
+ final VoidCallback pauseForTimedShutdown;
final bool disableAnimations;
@override
@@ -879,6 +882,85 @@ class _SmallestPlayerItemPanelState extends State<SmallestPlayerItemPanel> {
),
),
),
+ // 定时关闭
+ SubmenuButton(
+ menuChildren: [
+ MenuItemButton(
+ onPressed: () {
+ TimedShutdownService().cancel();
+ },
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: Text(
+ "不开启",
+ style: TextStyle(
+ color: !TimedShutdownService().isActive
+ ? Theme.of(context).colorScheme.primary
+ : null,
+ ),
+ ),
+ ),
+ ),
+ ),
+ for (final int minutes in [15, 30, 60])
+ MenuItemButton(
+ onPressed: () {
+ TimedShutdownService().start(minutes, onExpired: widget.pauseForTimedShutdown);
+ KazumiDialog.showToast(message: '已设置 ${TimedShutdownService().formatMinutesToDisplay(minutes)} 后定时关闭');
+ },
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: Text(
+ "$minutes 分钟",
+ style: TextStyle(
+ color: TimedShutdownService().setMinutes == minutes
+ ? Theme.of(context).colorScheme.primary
+ : null,
+ ),
+ ),
+ ),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () {
+ TimedShutdownService.showCustomTimerDialog(
+ onExpired: widget.pauseForTimedShutdown,
+ );
+ },
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: Text("自定义"),
+ ),
+ ),
+ ),
+ ],
+ child: Container(
+ height: 48,
+ constraints: BoxConstraints(minWidth: 112),
+ child: Align(
+ alignment: Alignment.centerLeft,
+ child: ValueListenableBuilder<int>(
+ valueListenable: TimedShutdownService().remainingSecondsNotifier,
+ builder: (context, remainingSeconds, child) {
+ return Text(
+ remainingSeconds > 0
+ ? "定时关闭 (${TimedShutdownService().formatRemainingTime()})"
+ : "定时关闭",
+ );
+ },
+ ),
+ ),
+ ),
+ ),
],
),
],
diff --git a/lib/pages/video/video_page.dart b/lib/pages/video/video_page.dart
index cd898201..8ec125a5 100644
--- a/lib/pages/video/video_page.dart
+++ b/lib/pages/video/video_page.dart
@@ -21,6 +21,7 @@ import 'package:scrollview_observer/scrollview_observer.dart';
import 'package:kazumi/pages/player/episode_comments_sheet.dart';
import 'package:window_manager/window_manager.dart';
import 'package:kazumi/bean/widget/embedded_native_control_area.dart';
+import 'package:kazumi/utils/timed_shutdown_service.dart';
class VideoPage extends StatefulWidget {
const VideoPage({super.key});
@@ -213,6 +214,8 @@ class _VideoPageState extends State<VideoPage>
videoPageController.episodeCommentsList.clear();
Utils.unlockScreenRotation();
tabController.dispose();
+ // Cancel timed shutdown when leaving anime page
+ TimedShutdownService().cancel();
super.dispose();
}
@@ -317,6 +320,13 @@ class _VideoPageState extends State<VideoPage>
Navigator.of(context).pop();
}
+ /// Callback for timed shutdown - pauses video when timer expires
+ void pauseForTimedShutdown() {
+ if (playerController.playing) {
+ playerController.pause();
+ }
+ }
+
/// 发送弹幕 由于接口限制, 暂时未提交云端
void sendDanmaku(String msg) async {
keyboardFocus.requestFocus();
@@ -759,6 +769,7 @@ class _VideoPageState extends State<VideoPage>
sendDanmaku: sendDanmaku,
disableAnimations: disableAnimations,
showDanmakuDestinationPickerAndSend: showDanmakuDestinationPickerAndSend,
+ pauseForTimedShutdown: pauseForTimedShutdown,
),
),
diff --git a/lib/utils/timed_shutdown_service.dart b/lib/utils/timed_shutdown_service.dart
new file mode 100644
index 00000000..edb12133
--- /dev/null
+++ b/lib/utils/timed_shutdown_service.dart
@@ -0,0 +1,268 @@
+import 'dart:async';
+import 'package:flutter/material.dart';
+import 'package:flutter/cupertino.dart';
+import 'package:kazumi/bean/dialog/dialog_helper.dart';
+import 'package:kazumi/utils/logger.dart';
+
+class TimedShutdownService {
+ static final TimedShutdownService _instance = TimedShutdownService._internal();
+ factory TimedShutdownService() => _instance;
+ TimedShutdownService._internal();
+
+ Timer? _shutdownTimer;
+ int _remainingSeconds = 0;
+ bool _isDialogShowing = false;
+ /// Last set minutes, used for repeat functionality
+ int _lastSetMinutes = 0;
+
+ /// Callback to invoke when timer expires (e.g., pause video)
+ VoidCallback? _onExpiredCallback;
+
+ /// Remaining time in seconds notifier
+ final ValueNotifier<int> remainingSecondsNotifier = ValueNotifier<int>(0);
+
+ /// Currently set minutes notifier (for UI display)
+ final ValueNotifier<int> setMinutesNotifier = ValueNotifier<int>(0);
+
+ /// Whether a shutdown timer is currently active
+ bool get isActive => _shutdownTimer != null && _shutdownTimer!.isActive;
+
+ /// Currently set minutes (0 = disabled)
+ int get setMinutes => setMinutesNotifier.value;
+
+ /// Remaining time in seconds
+ int get remainingSeconds => remainingSecondsNotifier.value;
+
+ /// Start the shutdown timer with the given duration in minutes
+ /// [onExpired] callback is invoked when timer expires (before showing dialog)
+ void start(int minutes, {VoidCallback? onExpired}) {
+ cancel();
+ if (minutes <= 0) return;
+
+ _lastSetMinutes = minutes;
+ _remainingSeconds = minutes * 60;
+ remainingSecondsNotifier.value = _remainingSeconds;
+ setMinutesNotifier.value = minutes;
+ _onExpiredCallback = onExpired;
+
+ // Update remaining time every second (runs globally, not tied to playback)
+ _shutdownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
+ if (_remainingSeconds > 0) {
+ _remainingSeconds--;
+ remainingSecondsNotifier.value = _remainingSeconds;
+ }
+
+ if (_remainingSeconds <= 0) {
+ timer.cancel();
+ _shutdownTimer = null;
+ _onTimerExpired();
+ }
+ });
+ }
+
+ /// Repeat the timer with the last set duration
+ void repeat() {
+ if (_lastSetMinutes > 0) {
+ start(_lastSetMinutes, onExpired: _onExpiredCallback);
+ }
+ }
+
+ /// Cancel the current shutdown timer
+ void cancel() {
+ _shutdownTimer?.cancel();
+ _shutdownTimer = null;
+ _remainingSeconds = 0;
+ _onExpiredCallback = null;
+ if (remainingSecondsNotifier.value != 0) {
+ remainingSecondsNotifier.value = 0;
+ }
+ if (setMinutesNotifier.value != 0) {
+ setMinutesNotifier.value = 0;
+ }
+
+ // If dialog is showing, dismiss it
+ if (_isDialogShowing) {
+ KazumiDialog.dismiss();
+ _isDialogShowing = false;
+ }
+ }
+
+ /// Called when timer expires: invoke callback and show dialog
+ void _onTimerExpired() {
+ // Reset UI state so it doesn't show 00:00
+ setMinutesNotifier.value = 0;
+
+ // Invoke the callback if set (e.g., pause video)
+ try {
+ _onExpiredCallback?.call();
+ } catch (e) {
+ KazumiLogger().e('TimedShutdownService: onExpired callback failed', error: e);
+ }
+
+ _showTimerExpiredDialog();
+ }
+
+ /// Show the timer expired dialog with repeat/close options
+ void _showTimerExpiredDialog() {
+ if (_isDialogShowing) return;
+ _isDialogShowing = true;
+
+ KazumiDialog.show(
+ clickMaskDismiss: false,
+ onDismiss: () {
+ _isDialogShowing = false;
+ },
+ builder: (context) {
+ return AlertDialog(
+ title: const Text('定时关闭'),
+ content: const Text('定时时间已到,视频已暂停'),
+ actions: [
+ TextButton(
+ onPressed: () {
+ _isDialogShowing = false;
+ KazumiDialog.dismiss();
+ repeat();
+ KazumiDialog.showToast(message: '已重新开始 $_lastSetMinutes 分钟定时');
+ },
+ child: const Text('重复'),
+ ),
+ TextButton(
+ onPressed: () {
+ _isDialogShowing = false;
+ KazumiDialog.dismiss();
+ },
+ child: Text(
+ '关闭',
+ style: TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ ],
+ );
+ },
+ );
+ }
+
+ /// Format remaining seconds to a readable string (e.g., "15:30")
+ String formatRemainingTime() {
+ int totalSeconds = remainingSecondsNotifier.value;
+ if (totalSeconds <= 0) return '00:00';
+ final minutes = totalSeconds ~/ 60;
+ final seconds = totalSeconds % 60;
+ return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
+ }
+
+ /// Format minutes to readable display string (e.g., "1 小时 30 分钟")
+ String formatMinutesToDisplay(int totalMinutes) {
+ final hours = totalMinutes ~/ 60;
+ final minutes = totalMinutes % 60;
+ if (hours > 0 && minutes > 0) {
+ return '$hours 小时 $minutes 分钟';
+ } else if (hours > 0) {
+ return '$hours 小时';
+ } else {
+ return '$minutes 分钟';
+ }
+ }
+
+ /// Show custom timer picker dialog and start timer if user confirms
+ /// Uses KazumiDialog to avoid context-related resource leaks
+ /// [onExpired] callback is invoked when timer expires (before showing dialog)
+ static void showCustomTimerDialog({
+ String title = '自定义定时',
+ bool autoStart = true,
+ VoidCallback? onExpired,
+ void Function(int)? onResult,
+ }) {
+ int selectedHours = 0;
+ int selectedMinutes = 0;
+
+ KazumiDialog.show(
+ builder: (context) {
+ return StatefulBuilder(
+ builder: (context, setState) {
+ return AlertDialog(
+ title: Text(title),
+ content: SizedBox(
+ height: 200,
+ child: Row(
+ children: [
+ // Hours picker
+ Expanded(
+ child: Column(
+ children: [
+ const Text('时', style: TextStyle(fontSize: 14)),
+ const SizedBox(height: 8),
+ Expanded(
+ child: CupertinoPicker(
+ scrollController: FixedExtentScrollController(initialItem: selectedHours),
+ itemExtent: 40,
+ onSelectedItemChanged: (index) {
+ setState(() => selectedHours = index);
+ },
+ children: List.generate(25, (index) => Center(
+ child: Text(index.toString().padLeft(2, '0'), style: const TextStyle(fontSize: 20)),
+ )),
+ ),
+ ),
+ ],
+ ),
+ ),
+ const Text(':', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
+ // Minutes picker
+ Expanded(
+ child: Column(
+ children: [
+ const Text('分', style: TextStyle(fontSize: 14)),
+ const SizedBox(height: 8),
+ Expanded(
+ child: CupertinoPicker(
+ scrollController: FixedExtentScrollController(initialItem: selectedMinutes),
+ itemExtent: 40,
+ onSelectedItemChanged: (index) {
+ setState(() => selectedMinutes = index);
+ },
+ children: List.generate(60, (index) => Center(
+ child: Text(index.toString().padLeft(2, '0'), style: const TextStyle(fontSize: 20)),
+ )),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ],
+ ),
+ ),
+ actions: [
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(),
+ child: Text(
+ '取消',
+ style: TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ TextButton(
+ onPressed: () {
+ final totalMinutes = selectedHours * 60 + selectedMinutes;
+ if (totalMinutes <= 0) {
+ KazumiDialog.showToast(message: '请选择有效的时间');
+ return;
+ }
+ KazumiDialog.dismiss();
+ if (autoStart) {
+ TimedShutdownService().start(totalMinutes, onExpired: onExpired);
+ KazumiDialog.showToast(
+ message: '已设置 ${TimedShutdownService().formatMinutesToDisplay(totalMinutes)} 后定时关闭',
+ );
+ }
+ onResult?.call(totalMinutes);
+ },
+ child: const Text('确定'),
+ ),
+ ],
+ );
+ },
+ );
+ },
+ );
+ }
+}