Skip to content

Commit

Permalink
add Blocker
Browse files Browse the repository at this point in the history
  • Loading branch information
3003h committed Nov 6, 2023
1 parent 53577f7 commit 688c39d
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 32 deletions.
97 changes: 81 additions & 16 deletions lib/common/controller/block_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,49 @@ class BlockController extends GetxController {
final ruleForCommentator = <BlockRule>[].obs;
final ruleForComment = <BlockRule>[].obs;

bool matchRule({String? text, required BlockType blockType}) {
if (text == null || text.isEmpty) {
return false;
}
final List<BlockRule> _ruleList = getRuleListByType(blockType);
for (final BlockRule _rule in _ruleList) {
final _ruleText = _rule.ruleText?.trim();

// default enabled
if (!(_rule.enabled ?? true) || _ruleText == null || _ruleText.isEmpty) {
continue;
}
if (_rule.enableRegex ?? false) {
final RegExp _regExp = RegExp(_ruleText);
if (_regExp.hasMatch(text)) {
logger.d('matchRule:${_rule.toJson()}\ntext: $text');
return true;
}
} else {
if (text.contains(_ruleText)) {
logger.d('matchRule: ${_rule.toJson()}\ntext: $text');
return true;
}
}
}
return false;
}

List<BlockRule> getRuleListByType(BlockType? blockType) {
switch (blockType) {
case BlockType.title:
return ruleForTitle;
case BlockType.uploader:
return ruleForUploader;
case BlockType.commentator:
return ruleForCommentator;
case BlockType.comment:
return ruleForComment;
default:
return [];
}
}

@override
void onInit() {
super.onInit();
Expand All @@ -24,13 +67,14 @@ class BlockController extends GetxController {
ruleForComment.value = ehSettingService.blockConfig.ruleForComment ?? [];
}

void updateBlockRule() {
void saveBlockRule() {
ehSettingService.blockConfig = ehSettingService.blockConfig.copyWith(
ruleForTitle: ruleForTitle,
ruleForUploader: ruleForUploader,
ruleForCommentator: ruleForCommentator,
ruleForComment: ruleForComment,
);
Global.saveProfile();
}

void addBlockRule(BlockRule result) {
Expand All @@ -40,21 +84,42 @@ class BlockController extends GetxController {
}
final BlockType? _blockType =
BlockType.values.asNameMap()[result.blockType];
switch (_blockType) {
case BlockType.title:
ruleForTitle.add(result);
break;
case BlockType.uploader:
ruleForUploader.add(result);
break;
case BlockType.commentator:
ruleForCommentator.add(result);
break;
case BlockType.comment:
ruleForComment.add(result);
break;
default:
break;
getRuleListByType(_blockType).add(result);
saveBlockRule();
}

void updateBlockRule(BlockRule oldBlockRule, BlockRule newBlockRule) {
logger.d('oldBlockRule ${oldBlockRule.toJson()}');
logger.d('newBlockRule ${newBlockRule.toJson()}');

// if type is different, remove old rule and add new rule
if (oldBlockRule.blockType != newBlockRule.blockType) {
final _oldBlockType =
BlockType.values.asNameMap()[oldBlockRule.blockType];
removeRule(
oldBlockRule.blockType,
getRuleListByType(_oldBlockType).indexOf(oldBlockRule),
);
addBlockRule(newBlockRule);
return;
}

if (newBlockRule.ruleText?.trim().isEmpty ?? true) {
return;
}
final BlockType? _blockType =
BlockType.values.asNameMap()[oldBlockRule.blockType];

final int _index = getRuleListByType(_blockType).indexOf(oldBlockRule);
getRuleListByType(_blockType)[_index] = newBlockRule;

saveBlockRule();
}

void removeRule(String? blockType, int index) {
logger.d('removeRule $blockType $index');
final BlockType? _blockType = BlockType.values.asNameMap()[blockType];
getRuleListByType(_blockType).removeAt(index);
saveBlockRule();
}
}
17 changes: 15 additions & 2 deletions lib/pages/gallery/view/comment_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fehviewer/common/controller/block_controller.dart';
import 'package:fehviewer/common/service/controller_tag_service.dart';
import 'package:fehviewer/common/service/ehsetting_service.dart';
import 'package:fehviewer/common/service/theme_service.dart';
Expand Down Expand Up @@ -34,6 +35,7 @@ class _CommentPageState extends State<CommentPage>
late CommentController controller;

EhSettingService get _ehSettingService => Get.find();
BlockController get _blockController => Get.find();

@override
void initState() {
Expand Down Expand Up @@ -76,7 +78,7 @@ class _CommentPageState extends State<CommentPage>
return Obx(() {
logger.t('build commentListView');

late final List<GalleryComment>? _comments;
List<GalleryComment>? _comments;

if (_ehSettingService.showOnlyUploaderComment) {
final _uploaderId = controller.comments
Expand All @@ -95,7 +97,18 @@ class _CommentPageState extends State<CommentPage>
} else {
_comments = controller.comments;
}
// _comments = controller.comments;

// 根据屏蔽规则过滤评论
_comments = _comments?.where((element) {
return !_blockController.matchRule(
text: element.text,
blockType: BlockType.comment,
) &&
!_blockController.matchRule(
text: element.name,
blockType: BlockType.commentator,
);
}).toList();

return Container(
padding: const EdgeInsets.symmetric(horizontal: kPadding),
Expand Down
14 changes: 14 additions & 0 deletions lib/pages/gallery/view/gallery_widget.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fehviewer/common/controller/block_controller.dart';
import 'package:fehviewer/common/service/controller_tag_service.dart';
import 'package:fehviewer/common/service/ehsetting_service.dart';
import 'package:fehviewer/common/service/layout_service.dart';
Expand Down Expand Up @@ -349,6 +350,7 @@ class TopCommentEx extends StatelessWidget {
final String? uploader;

EhSettingService get _ehSettingService => Get.find();
BlockController get _blockController => Get.find();

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -377,6 +379,18 @@ class TopCommentEx extends StatelessWidget {
_ehSettingService.scoreFilteringThreshold);
}

// 根据屏蔽规则过滤评论
_comments = _comments.where((element) {
return !_blockController.matchRule(
text: element.text,
blockType: BlockType.comment,
) &&
!_blockController.matchRule(
text: element.name,
blockType: BlockType.commentator,
);
});

_comments = _comments.take(max);

return _comments
Expand Down
15 changes: 13 additions & 2 deletions lib/pages/setting/block/block_rule_edit_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ class BlockRuleEditPage extends GetView<BlockController> {
@override
Widget build(BuildContext context) {
final String _title = L10n.of(context).edit_block_rule;
final blockRuleTextEditingController = TextEditingController();

final BlockRule? _blockRuleFromArg =
Get.arguments is BlockRule ? Get.arguments as BlockRule : null;

BlockRule _blockRule = blockRule ??
_blockRuleFromArg ??
BlockRule(
ruleText: '',
blockType: controller.latestBlockType?.name ?? BlockType.title.name,
enabled: true,
enableRegex: controller.latestEnableRegex ?? false,
);
final blockRuleTextEditingController = TextEditingController(
text: _blockRule.ruleText,
);

final List<Widget> _list = <Widget>[
TextSwitchItem(
Expand Down Expand Up @@ -70,9 +76,14 @@ class BlockRuleEditPage extends GetView<BlockController> {
color: CupertinoColors.placeholderText,
height: 1.25,
),
textInputAction: TextInputAction.done,
style: const TextStyle(height: 1.2),
onEditingComplete: () {
FocusScope.of(context).requestFocus(FocusNode());
},
keyboardType: TextInputType.text,
onChanged: (value) {
logger.d('value $value');
logger.t('value $value');
_blockRule = _blockRule.copyWith(
ruleText: value.replaceAll('\n', ' '));
},
Expand Down
94 changes: 89 additions & 5 deletions lib/pages/setting/block/block_rules_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:fehviewer/common/controller/block_controller.dart';
import 'package:fehviewer/common/service/layout_service.dart';
import 'package:fehviewer/common/service/theme_service.dart';
import 'package:fehviewer/component/setting_base.dart';
import 'package:fehviewer/fehviewer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';
import 'package:sliver_tools/sliver_tools.dart';

class BlockRulesPage extends GetView<BlockController> {
const BlockRulesPage({super.key});
Expand All @@ -12,6 +16,16 @@ class BlockRulesPage extends GetView<BlockController> {
Widget build(BuildContext context) {
final String _title = L10n.of(context).block_rules;

final List<({String groupName, List<BlockRule> rules})> ruleGroups = [
(groupName: L10n.of(context).title, rules: controller.ruleForTitle),
(groupName: L10n.of(context).uploader, rules: controller.ruleForUploader),
(
groupName: L10n.of(context).commentator,
rules: controller.ruleForCommentator
),
(groupName: L10n.of(context).comment, rules: controller.ruleForComment),
];

return Obx(() {
return CupertinoPageScaffold(
backgroundColor: !ehTheme.isDarkMode
Expand All @@ -24,7 +38,6 @@ class BlockRulesPage extends GetView<BlockController> {
padding: const EdgeInsets.all(0),
minSize: 40,
child: const Icon(
// FontAwesomeIcons.plus,
CupertinoIcons.plus_circle,
size: 28,
),
Expand All @@ -39,10 +52,81 @@ class BlockRulesPage extends GetView<BlockController> {
},
),
),
child: SafeArea(
bottom: false,
top: false,
child: ListView(),
child: CustomScrollView(
slivers: [
SliverSafeArea(
left: false,
right: false,
sliver: MultiSliver(children: [
...ruleGroups.map(
(e) => MultiSliver(
pushPinnedChildren: true,
children: [
if (e.rules.isNotEmpty)
Container(
padding: const EdgeInsets.only(
left: 20,
bottom: 4,
top: 20,
),
width: double.infinity,
child: Text(
e.groupName ?? '',
style: const TextStyle(fontSize: 14),
textAlign: TextAlign.start,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
final BlockRule _blockRule = e.rules[index];
return Slidable(
key: Key(
'_blockRule.blockType${_blockRule.hashCode}'),
endActionPane: ActionPane(
extentRatio: 0.25,
motion: const ScrollMotion(),
children: [
SlidableAction(
onPressed: (_) => controller.removeRule(
_blockRule.blockType, index),
backgroundColor:
CupertinoDynamicColor.resolve(
CupertinoColors.systemRed, context),
foregroundColor: Colors.white,
icon: CupertinoIcons.delete,
// label: L10n.of(context).delete,
),
],
),
child: SelectorSettingItem(
title: _blockRule.ruleText ?? '',
hideDivider: _blockRule == e.rules.last,
onTap: () async {
final result = await Get.toNamed<dynamic>(
EHRoutes.blockRuleEdit,
arguments: _blockRule,
id: isLayoutLarge ? 2 : null,
);
if (result != null && result is BlockRule) {
controller.updateBlockRule(
_blockRule,
result,
);
}
},
),
);
},
childCount: e.rules.length,
),
),
],
),
),
]),
),
],
),
);
});
Expand Down
Loading

0 comments on commit 688c39d

Please sign in to comment.