Merge pull request #649 from venera-app/feat/comment-blocking

feat: add comment keyword blocking functionality
This commit is contained in:
nyne
2025-11-29 15:04:51 +08:00
committed by GitHub
parent 0ce18cd738
commit 781ff2553d
6 changed files with 131 additions and 5 deletions

View File

@@ -180,6 +180,7 @@ class Settings with ChangeNotifier {
'showFavoriteStatusOnTile': true,
'showHistoryStatusOnTile': false,
'blockedWords': [],
'blockedCommentWords': [],
'defaultSearchTarget': null,
'autoPageTurningInterval': 5, // in seconds
'readerMode': 'galleryLeftToRight', // values of [ReaderMode]

View File

@@ -1,5 +1,18 @@
part of 'comic_page.dart';
bool _shouldBlockComment(Comment comment) {
var blockedWords = appdata.settings["blockedCommentWords"] as List;
if (blockedWords.isEmpty) return false;
var content = comment.content.toLowerCase();
for (var word in blockedWords) {
if (content.contains(word.toString().toLowerCase())) {
return true;
}
}
return false;
}
class CommentsPage extends StatefulWidget {
const CommentsPage({
super.key,
@@ -36,8 +49,9 @@ class _CommentsPageState extends State<CommentsPage> {
_loading = false;
});
} else if (mounted) {
var filteredComments = res.data.where((c) => !_shouldBlockComment(c)).toList();
setState(() {
_comments = res.data;
_comments = filteredComments;
_loading = false;
maxPage = res.subData;
});
@@ -54,8 +68,9 @@ class _CommentsPageState extends State<CommentsPage> {
if (res.error) {
context.showMessage(message: res.errorMessage ?? "Unknown Error");
} else {
var filteredComments = res.data.where((c) => !_shouldBlockComment(c)).toList();
setState(() {
_comments!.addAll(res.data);
_comments!.addAll(filteredComments);
_page++;
if (maxPage == null && res.data.isEmpty) {
maxPage = _page;

View File

@@ -21,7 +21,7 @@ class _CommentsPartState extends State<_CommentsPart> {
@override
void initState() {
comments = widget.comments;
comments = widget.comments.where((c) => !_shouldBlockComment(c)).toList();
super.initState();
}

View File

@@ -1,5 +1,18 @@
part of 'reader.dart';
bool _shouldBlockComment(Comment comment) {
var blockedWords = appdata.settings["blockedCommentWords"] as List;
if (blockedWords.isEmpty) return false;
var content = comment.content.toLowerCase();
for (var word in blockedWords) {
if (content.contains(word.toString().toLowerCase())) {
return true;
}
}
return false;
}
class ChapterCommentsPage extends StatefulWidget {
const ChapterCommentsPage({
super.key,
@@ -44,8 +57,9 @@ class _ChapterCommentsPageState extends State<ChapterCommentsPage> {
_loading = false;
});
} else if (mounted) {
var filteredComments = res.data.where((c) => !_shouldBlockComment(c)).toList();
setState(() {
_comments = res.data;
_comments = filteredComments;
_loading = false;
maxPage = res.subData;
});
@@ -62,8 +76,9 @@ class _ChapterCommentsPageState extends State<ChapterCommentsPage> {
if (res.error) {
context.showMessage(message: res.errorMessage ?? "Unknown Error");
} else {
var filteredComments = res.data.where((c) => !_shouldBlockComment(c)).toList();
setState(() {
_comments!.addAll(res.data);
_comments!.addAll(filteredComments);
_page++;
if (maxPage == null && res.data.isEmpty) {
maxPage = _page;

View File

@@ -60,6 +60,10 @@ class _ExploreSettingsState extends State<ExploreSettings> {
title: "Keyword blocking".tl,
builder: () => const _ManageBlockingWordView(),
).toSliver(),
_PopupWindowSetting(
title: "Comment keyword blocking".tl,
builder: () => const _ManageBlockingCommentWordView(),
).toSliver(),
SelectSetting(
title: "Default Search Target".tl,
settingKey: "defaultSearchTarget",
@@ -250,4 +254,93 @@ Widget setSearchSourcesWidget() {
settingsIndex: "searchSources",
pages: pages,
);
}
class _ManageBlockingCommentWordView extends StatefulWidget {
const _ManageBlockingCommentWordView();
@override
State<_ManageBlockingCommentWordView> createState() =>
_ManageBlockingCommentWordViewState();
}
class _ManageBlockingCommentWordViewState extends State<_ManageBlockingCommentWordView> {
@override
Widget build(BuildContext context) {
assert(appdata.settings["blockedCommentWords"] is List);
return PopUpWidgetScaffold(
title: "Comment keyword blocking".tl,
tailing: [
TextButton.icon(
icon: const Icon(Icons.add),
label: Text("Add".tl),
onPressed: add,
),
],
body: ListView.builder(
itemCount: appdata.settings["blockedCommentWords"].length,
itemBuilder: (context, index) {
return ListTile(
title: Text(appdata.settings["blockedCommentWords"][index]),
trailing: IconButton(
icon: const Icon(Icons.close),
onPressed: () {
appdata.settings["blockedCommentWords"].removeAt(index);
appdata.saveData();
setState(() {});
},
),
);
},
),
);
}
void add() {
showDialog(
context: App.rootContext,
builder: (context) {
var controller = TextEditingController();
String? error;
return StatefulBuilder(builder: (context, setState) {
return ContentDialog(
title: "Add keyword".tl,
content: TextField(
controller: controller,
decoration: InputDecoration(
border: const OutlineInputBorder(),
label: Text("Keyword".tl),
errorText: error,
),
onChanged: (s) {
if (error != null) {
setState(() {
error = null;
});
}
},
).paddingHorizontal(12),
actions: [
Button.filled(
onPressed: () {
if (appdata.settings["blockedCommentWords"]
.contains(controller.text)) {
setState(() {
error = "Keyword already exists".tl;
});
return;
}
appdata.settings["blockedCommentWords"].add(controller.text);
appdata.saveData();
this.setState(() {});
context.pop();
},
child: Text("Add".tl),
),
],
);
});
},
);
}
}