Skip to content

Commit

Permalink
feat: 镜像、弹幕过滤修复
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Dec 30, 2024
1 parent 3eaf5fc commit b56e03c
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 86 deletions.
33 changes: 17 additions & 16 deletions lib/pages/danmaku/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import 'package:PiliPalaX/http/danmaku.dart';
import 'package:PiliPalaX/models/danmaku/dm.pb.dart';
import 'package:flutter/cupertino.dart';

import '../../utils/storage.dart';

class PlDanmakuController {
PlDanmakuController(this.cid, this.danmakuWeightNotifier, this.danmakuFilterNotifier);
final int cid;
final ValueNotifier<int> danmakuWeightNotifier;
final ValueNotifier<List<Map<String, dynamic>>> danmakuFilterNotifier;
int danmakuWeight = 0;
List<Map<String, dynamic>> danmakuFilter = [];
static int danmakuWeight = 0;
static List<Map<String, dynamic>> danmakuFilter = [];
PlDanmakuController(this.cid){
refresh();
}

Map<int, List<DanmakuElem>> dmSegMap = {};
// 已请求的段落标记
List<bool> requestedSeg = [];
Expand All @@ -17,19 +20,19 @@ class PlDanmakuController {

static int segmentLength = 60 * 6 * 1000;

static void refresh() {
danmakuWeight =
GStorage.setting.get(SettingBoxKey.danmakuWeight, defaultValue: 0);
danmakuFilter = GStorage.onlineCache.get(OnlineCacheKey.danmakuFilterRule,
defaultValue: []).map<Map<String, dynamic>>((e) {
return Map<String, dynamic>.from(e);
}).toList();
}

void initiate(int videoDuration, int progress) {
if (videoDuration <= 0) {
return;
}
danmakuWeightNotifier.addListener(() {
print(
"danmakuWeight changed from $danmakuWeight to ${danmakuWeightNotifier.value}");
danmakuWeight = danmakuWeightNotifier.value;
});
danmakuFilterNotifier.addListener(() {
print("danmakuFilter changed from $danmakuFilter to ${danmakuFilterNotifier.value}");
danmakuFilter = danmakuFilterNotifier.value;
});
if (requestedSeg.isEmpty) {
int segCount = (videoDuration / segmentLength).ceil();
requestedSeg = List<bool>.generate(segCount, (index) => false);
Expand All @@ -38,8 +41,6 @@ class PlDanmakuController {
}

void dispose() {
danmakuWeightNotifier.removeListener(() {});
danmakuFilterNotifier.removeListener(() {});
danmakuFilter.clear();
dmSegMap.clear();
requestedSeg.clear();
Expand Down
7 changes: 2 additions & 5 deletions lib/pages/danmaku/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ class _PlDanmakuState extends State<PlDanmaku> {
super.initState();
enableShowDanmaku =
setting.get(SettingBoxKey.enableShowDanmaku, defaultValue: true);
_plDanmakuController = PlDanmakuController(
widget.cid,
widget.playerController.danmakuWeight,
widget.playerController.danmakuFilterRule);
_plDanmakuController = PlDanmakuController(widget.cid);
if (mounted) {
playerController = widget.playerController;
if (enableShowDanmaku || playerController.isOpenDanmu.value) {
Expand Down Expand Up @@ -103,7 +100,7 @@ class _PlDanmakuState extends State<PlDanmaku> {

if (currentDanmakuList != null && _controller != null) {
Color? defaultColor = playerController.blockTypes.contains(6)
? Colors.white//DmUtils.decimalToColor(16777215)
? Colors.white //DmUtils.decimalToColor(16777215)
: null;
currentDanmakuList
.map((e) => _controller!.addDanmaku(DanmakuContentItem(
Expand Down
39 changes: 24 additions & 15 deletions lib/pages/danmaku_block/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import '../../models/user/danmaku_block.dart';
import '../../plugin/pl_player/controller.dart';
import 'package:PiliPalaX/common/widgets/spring_physics.dart';

import '../danmaku/controller.dart';

class DanmakuBlockPage extends StatefulWidget {
const DanmakuBlockPage({super.key});

Expand All @@ -21,7 +23,6 @@ class _DanmakuBlockPageState extends State<DanmakuBlockPage> {
Get.put(DanmakuBlockController());
final ScrollController scrollController = ScrollController();
Box onlineCache = GStorage.onlineCache;
late PlPlayerController plPlayerController;

static const Map<int, String> ruleLabels = {
0: '关键词',
Expand All @@ -35,24 +36,11 @@ class _DanmakuBlockPageState extends State<DanmakuBlockPage> {
WidgetsBinding.instance.addPostFrameCallback((_) {
_danmakuBlockController.queryDanmakuFilter();
});
plPlayerController = Get.arguments as PlPlayerController;
}


@override
void dispose() {
List<Map<String, dynamic>> simpleRuleList = _danmakuBlockController
.ruleTypes.values
.expand((element) => element)
.map<Map<String, dynamic>>((e) {
//当正则表达式前后都有"/"时,去掉,避免RegExp解析错误
if (e.type == 1 && e.filter.startsWith('/') && e.filter.endsWith('/')) {
e.filter = e.filter.substring(1, e.filter.length - 1);
}
return e.toMap();
}).toList();
print("simpleRuleList:$simpleRuleList");
onlineCache.put(OnlineCacheKey.danmakuFilterRule, simpleRuleList);
plPlayerController.danmakuFilterRule.value = simpleRuleList;
scrollController.removeListener(() {});
scrollController.dispose();
super.dispose();
Expand Down Expand Up @@ -181,6 +169,21 @@ class DanmakuBlockController extends GetxController
tabController = TabController(length: 3, vsync: this);
}

void danmakuFilterRefresh() {
List<Map<String, dynamic>> simpleRuleList = ruleTypes.values
.expand((element) => element)
.map<Map<String, dynamic>>((e) {
//当正则表达式前后都有"/"时,去掉,避免RegExp解析错误
if (e.type == 1 && e.filter.startsWith('/') && e.filter.endsWith('/')) {
e.filter = e.filter.substring(1, e.filter.length - 1);
}
return e.toMap();
}).toList();
print("simpleRuleList:$simpleRuleList");
onlineCache.put(OnlineCacheKey.danmakuFilterRule, simpleRuleList);
PlDanmakuController.refresh();
}

@override
void onClose() {
tabController.dispose();
Expand All @@ -193,11 +196,15 @@ class DanmakuBlockController extends GetxController
SmartDialog.dismiss();
if (result['status']) {
danmakuRules.value = result['data'].rule;
for (var element in ruleTypes.values) {
element.clear();
}
danmakuRules.map((e) {
SimpleRule simpleRule = SimpleRule(e.id!, e.type!, e.filter!);
ruleTypes[e.type!]!.add(simpleRule);
}).toList();
ruleTypes.refresh();
danmakuFilterRefresh();
SmartDialog.showToast(result['data'].toast);
} else {
SmartDialog.showToast(result['msg']);
Expand All @@ -213,6 +220,7 @@ class DanmakuBlockController extends GetxController
danmakuRules.removeWhere((e) => e.id == id);
ruleTypes[type]!.removeWhere((e) => e.id == id);
ruleTypes.refresh();
danmakuFilterRefresh();
SmartDialog.showToast(result['msg']);
} else {
SmartDialog.showToast(result['msg']);
Expand All @@ -230,6 +238,7 @@ class DanmakuBlockController extends GetxController
SimpleRule simpleRule = SimpleRule(data.id!, data.type!, data.filter!);
ruleTypes[type]!.add(simpleRule);
ruleTypes.refresh();
danmakuFilterRefresh();
SmartDialog.showToast('添加成功');
} else {
SmartDialog.showToast(result['msg']);
Expand Down
10 changes: 10 additions & 0 deletions lib/pages/video/introduction/widgets/menu_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ class ActionRowLineItem extends StatelessWidget {
this.onTap,
this.text,
this.loadingStatus = false,
this.icon,
});
final bool? selectStatus;
final Function? onTap;
final bool? loadingStatus;
final String? text;
final IconData? icon;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -135,6 +137,14 @@ class ActionRowLineItem extends StatelessWidget {
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (icon != null)
Icon(
icon,
size: 13,
color: selectStatus!
? Theme.of(context).colorScheme.onSecondaryContainer
: Theme.of(context).colorScheme.outline,
),
AnimatedOpacity(
opacity: loadingStatus! ? 0 : 1,
duration: const Duration(milliseconds: 200),
Expand Down
66 changes: 41 additions & 25 deletions lib/pages/video/widgets/header_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import 'package:PiliPalaX/services/shutdown_timer_service.dart';
import '../../../../models/video/play/CDN.dart';
import '../../../../models/video_detail_res.dart';
import '../../../services/service_locator.dart';
import '../../danmaku/controller.dart';
import '../../danmaku_block/index.dart';
import '../../setting/widgets/select_dialog.dart';
import 'package:PiliPalaX/pages/video/introduction/detail/index.dart';
import 'package:marquee/marquee.dart';
Expand Down Expand Up @@ -229,19 +231,27 @@ class _HeaderControlState extends State<HeaderControl> {
},
),
ListTile(
onTap: () {
Get.back();
},
dense: true,
leading:
const Icon(Icons.switch_video_outlined, size: 20),
title: const Text('功能', style: titleStyle),
trailing: Row(
title: Row(
mainAxisSize: MainAxisSize.min,
children: [
Obx(
() => ActionRowLineItem(
key: const Key('flipX'),
icon: Icons.flip,
onTap: () {
widget.controller!.flipX.value =
!widget.controller!.flipX.value;
},
text: " 镜像翻转 ",
selectStatus: widget.controller!.flipX.value,
),
),
const SizedBox(width: 10),
Obx(
() => ActionRowLineItem(
key: const Key('onlyPlayAudio'),
icon: Icons.headphones,
onTap: () {
widget.controller!.setOnlyPlayAudio(null);
},
Expand All @@ -255,6 +265,7 @@ class _HeaderControlState extends State<HeaderControl> {
Obx(
() => ActionRowLineItem(
key: const Key('continuePlayInBackground'),
icon: Icons.play_circle_outline,
onTap: () {
widget.controller!
.setContinuePlayInBackground(null);
Expand Down Expand Up @@ -978,7 +989,7 @@ class _HeaderControlState extends State<HeaderControl> {
{'value': 1.0, 'label': '满屏'},
];
// 智能云屏蔽
int danmakuWeight = widget.controller!.danmakuWeight.value;
int danmakuWeight = PlDanmakuController.danmakuWeight;
// 显示区域
double showArea = widget.controller!.showArea;
// 不透明度
Expand Down Expand Up @@ -1032,13 +1043,20 @@ class _HeaderControlState extends State<HeaderControl> {
minimumSize: Size.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
onPressed: () => {
Get.back(),
Get.toNamed('/danmakuBlock',
arguments: widget.controller)
},
onPressed: () {
// 弹出对话框
Get.back();
showDialog(
context: context,
useSafeArea: true,
builder: (_) => const Dialog(
insetPadding: EdgeInsets.zero,
child: DanmakuBlockPage(),
),
);
},
child: Text(
"屏蔽管理(${widget.controller!.danmakuFilterRule.value.length})")),
"屏蔽管理(${PlDanmakuController.danmakuFilter.length})")),
],
),
Padding(
Expand All @@ -1065,8 +1083,7 @@ class _HeaderControlState extends State<HeaderControl> {
label: '$danmakuWeight',
onChanged: (double val) {
danmakuWeight = val.toInt();
widget.controller!.danmakuWeight.value =
danmakuWeight;
PlDanmakuController.danmakuWeight = danmakuWeight;
widget.controller!.putDanmakuSettings();
setState(() {});
// try {
Expand Down Expand Up @@ -1439,6 +1456,7 @@ class _HeaderControlState extends State<HeaderControl> {
nowSemanticsLabel = '当前时间:$hour点$minute分';
now.value = '$hour:$minute';
}

startClock() {
getNow();
clock = Timer.periodic(const Duration(seconds: 1), (Timer t) {
Expand Down Expand Up @@ -1742,15 +1760,13 @@ class _HeaderControlState extends State<HeaderControl> {
if (widget.controller!.isFullScreen.value || equivalentFullScreen())
Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
Obx(
() => Text(
" ${now.value}",
style: const TextStyle(
color: Colors.white,
fontSize: 11,
fontFeatures: [FontFeature.tabularFigures()],
),
semanticsLabel: nowSemanticsLabel
),
() => Text(" ${now.value}",
style: const TextStyle(
color: Colors.white,
fontSize: 11,
fontFeatures: [FontFeature.tabularFigures()],
),
semanticsLabel: nowSemanticsLabel),
),
const SizedBox(width: 1.5),
if (widget.controller!.isFullScreen.value)
Expand Down
19 changes: 7 additions & 12 deletions lib/plugin/pl_player/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'package:PiliPalaX/utils/storage.dart';
// import 'package:screen_brightness/screen_brightness.dart';
import 'package:universal_platform/universal_platform.dart';
import '../../models/video/play/subtitle.dart';
import '../../pages/danmaku/controller.dart';
import '../../pages/video/controller.dart';
import '../../pages/video/introduction/bangumi/controller.dart';
import '../../pages/video/introduction/detail/controller.dart';
Expand Down Expand Up @@ -106,6 +107,8 @@ class PlPlayerController {

Rx<bool> _onlyPlayAudio = false.obs;

Rx<bool> _flipX = false.obs;

///
// ignore: prefer_final_fields
Rx<bool> _isSliderMoving = false.obs;
Expand Down Expand Up @@ -236,6 +239,9 @@ class PlPlayerController {
/// 听视频
Rx<bool> get onlyPlayAudio => _onlyPlayAudio;

/// 镜像
Rx<bool> get flipX => _flipX;

/// 是否长按倍速
Rx<bool> get doubleSpeedStatus => _doubleSpeedStatus;

Expand All @@ -257,11 +263,6 @@ class PlPlayerController {

/// 弹幕开关
Rx<bool> isOpenDanmu = false.obs;

/// 弹幕权重
ValueNotifier<int> danmakuWeight = ValueNotifier(0);
ValueNotifier<List<Map<String, dynamic>>> danmakuFilterRule =
ValueNotifier([]);
// 关联弹幕控制器
DanmakuController? danmakuController;
// 弹幕相关配置
Expand Down Expand Up @@ -358,12 +359,6 @@ class PlPlayerController {
_videoType = videoType;
isOpenDanmu.value =
setting.get(SettingBoxKey.enableShowDanmaku, defaultValue: true);
danmakuWeight.value =
setting.get(SettingBoxKey.danmakuWeight, defaultValue: 0);
danmakuFilterRule.value = onlineCache.get(OnlineCacheKey.danmakuFilterRule,
defaultValue: []).map<Map<String, dynamic>>((e) {
return Map<String, dynamic>.from(e);
}).toList();
blockTypes = setting.get(SettingBoxKey.danmakuBlockType, defaultValue: []);
showArea = setting.get(SettingBoxKey.danmakuShowArea, defaultValue: 0.5);
// 不透明度
Expand Down Expand Up @@ -1555,7 +1550,7 @@ class PlPlayerController {
}

void putDanmakuSettings() {
setting.put(SettingBoxKey.danmakuWeight, danmakuWeight.value);
setting.put(SettingBoxKey.danmakuWeight, PlDanmakuController.danmakuWeight);
setting.put(SettingBoxKey.danmakuBlockType, blockTypes);
setting.put(SettingBoxKey.danmakuShowArea, showArea);
setting.put(SettingBoxKey.danmakuOpacity, opacityVal);
Expand Down
Loading

0 comments on commit b56e03c

Please sign in to comment.