Skip to content

Commit

Permalink
♻️ Refactor blacklist with new beans/set/methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Feb 16, 2020
1 parent f8fc5ad commit d1955c8
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 45 deletions.
53 changes: 19 additions & 34 deletions lib/api/user_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,61 +172,46 @@ class UserAPI {
///
/// Blacklists.
///
static List<String> blacklist = [];
static final blacklist = <BlacklistUser>{};

static Future getBlacklist({int pos, int size}) {
return NetUtils.getWithCookieSet(
API.blacklist(pos: pos, size: size),
);
}

static void fAddToBlacklist({int uid, String name}) {
NetUtils.postWithCookieSet(
API.addToBlacklist,
data: {'fid': uid},
).then((response) {
addToBlacklist(uid: uid, name: name);
static void fAddToBlacklist(BlacklistUser user) {
NetUtils.postWithCookieSet(API.addToBlacklist, data: {'fid': user.uid}).then((response) {
blacklist.add(user);
showToast('屏蔽成功');
Instances.eventBus.fire(BlacklistUpdateEvent());
UserAPI.unFollow(uid).catchError((e) {
debugPrint('${e.toString()}');
unFollow(user.uid).catchError((e) {
debugPrint('Unfollow user failed: $e');
});
}).catchError((e) {
showToast('屏蔽失败');
debugPrint('Add $name $uid to blacklist failed : $e');
debugPrint('Add $user to blacklist failed : $e');
});
}

static void fRemoveFromBlacklist({int uid, String name}) {
NetUtils.postWithCookieSet(
API.removeFromBlacklist,
data: {'fid': uid},
).then((response) {
removeFromBlackList(uid: uid, name: name);
showToast('取消屏蔽成功');
Instances.eventBus.fire(BlacklistUpdateEvent());
}).catchError((e) {
static void fRemoveFromBlacklist(BlacklistUser user) {
blacklist.remove(user);
showToast('取消屏蔽成功');
Instances.eventBus.fire(BlacklistUpdateEvent());
NetUtils.postWithCookieSet(API.removeFromBlacklist, data: {'fid': user.uid}).catchError((e) {
showToast('取消屏蔽失败');
debugPrint('Remove $name $uid from blacklist failed: $e');
debugPrint('${e.response}');
debugPrint('Remove $user from blacklist failed: $e');
if (blacklist.contains(user)) blacklist.remove(user);
Instances.eventBus.fire(BlacklistUpdateEvent());
});
}

static void setBlacklist(List list) {
if (list.length > 0)
if (list.isNotEmpty) {
list.forEach((person) {
addToBlacklist(
uid: int.parse(person['uid'].toString()),
name: person['username'],
);
final user = BlacklistUser.fromJson(person);
blacklist.add(user);
});
}

static void addToBlacklist({int uid, String name}) {
blacklist.add(jsonEncode({'uid': uid.toString(), 'username': name}));
}

static void removeFromBlackList({int uid, String name}) {
blacklist.remove(jsonEncode({'uid': uid.toString(), 'username': name}));
}
}
}
67 changes: 67 additions & 0 deletions lib/controller/comment_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,73 @@ class CommentListInPostState extends State<CommentListInPost> with AutomaticKeep
_refreshList();
}

void confirmDelete(context, Comment comment) async {
final confirm = await ConfirmationDialog.show(
context,
title: '删除评论',
content: '是否确认删除这条评论?',
showConfirm: true,
);
if (confirm) {
final _loadingDialogController = LoadingDialogController();
LoadingDialog.show(
context,
text: '正在删除评论',
controller: _loadingDialogController,
isGlobal: false,
);
CommentAPI.deleteComment(comment.post.id, comment.id).then((response) {
_loadingDialogController.changeState('success', '评论删除成功');
Instances.eventBus.fire(PostCommentDeletedEvent(comment.post.id));
}).catchError((e) {
debugPrint(e.toString());
_loadingDialogController.changeState('failed', '评论删除失败');
});
}
}

void replyTo(int index) {
if (_comments.length >= index && _comments[index] != null) {
navigatorState.pushNamed(
Routes.OPENJMU_ADD_COMMENT,
arguments: {
'post': widget.post,
'comment': _comments?.elementAt(index) ?? null,
},
);
}
}

void showActions(context, int index) {
ConfirmationBottomSheet.show(
context,
children: <Widget>[
if (widget.post.uid == currentUser.uid)
ConfirmationBottomSheetAction(
icon: Icon(Icons.delete),
text: '删除评论',
onTap: () => confirmDelete(context, _comments[index]),
)
else
ConfirmationBottomSheetAction(
icon: Icon(Icons.reply),
text: '回复评论',
onTap: () => replyTo(index),
),
ConfirmationBottomSheetAction(
icon: Icon(Icons.report),
text: '复制评论',
onTap: () {
Clipboard.setData(ClipboardData(
text: replaceMentionTag(_comments[index].content),
));
showToast('已复制到剪贴板');
},
),
],
);
}

Future<Null> _loadList() async {
isLoading = true;
try {
Expand Down
1 change: 1 addition & 0 deletions lib/model/beans.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export 'package:openjmu/model/special_text.dart';
part 'beans.g.dart';

part 'app_message.dart';
part 'blacklist_user.dart';
part 'changelog.dart';
part 'comment.dart';
part 'course.dart';
Expand Down
34 changes: 34 additions & 0 deletions lib/model/blacklist_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
///
/// [Author] Alex (https://github.com/AlexVincent525)
/// [Date] 2020-02-16 22:02
///
part of 'beans.dart';

class BlacklistUser {
int uid;
String username;

BlacklistUser({this.uid, this.username});

BlacklistUser.fromJson(Map<String, dynamic> json) {
uid = json['uid'];
username = json['username'];
}

Map<String, dynamic> toJson() {
return {'uid': uid, 'username': username};
}

@override
String toString() {
return 'BlacklistUser ${JsonEncoder.withIndent(' ').convert(toJson())}';
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is BlacklistUser && runtimeType == other.runtimeType && uid == other.uid;

@override
int get hashCode => uid.hashCode;
}
2 changes: 1 addition & 1 deletion lib/pages/post/post_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class PostDetailPageState extends State<PostDetailPage> {
showConfirm: true,
);
if (confirm) {
UserAPI.fAddToBlacklist(uid: widget.post.uid, name: widget.post.nickname);
UserAPI.fAddToBlacklist(BlacklistUser(uid: widget.post.uid, username: widget.post.nickname));
}
}

Expand Down
14 changes: 7 additions & 7 deletions lib/pages/user/user_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,20 +426,19 @@ class _UserPageState extends State<UserPage>
),
];

void removeFromBlacklist(context, Map<String, dynamic> user) async {
void removeFromBlacklist(context, BlacklistUser user) async {
final confirm = await ConfirmationDialog.show(
context,
title: '移出黑名单',
content: '确定不再屏蔽此人吗?',
showConfirm: true,
);
if (confirm) {
UserAPI.fRemoveFromBlacklist(uid: int.parse(user['uid']), name: user['username']);
UserAPI.fRemoveFromBlacklist(user);
}
}

Widget blacklistUser(String user) {
final _user = jsonDecode(user);
Widget blacklistUser(BlacklistUser user) {
return Padding(
padding: EdgeInsets.all(suSetWidth(8.0)),
child: Column(
Expand All @@ -458,12 +457,13 @@ class _UserPageState extends State<UserPage>
),
),
Text(
_user['username'],
user.username,
style: TextStyle(fontSize: suSetSp(18.0)),
overflow: TextOverflow.ellipsis,
),
GestureDetector(
onTap: () => removeFromBlacklist(context, _user),
behavior: HitTestBehavior.opaque,
onTap: () => removeFromBlacklist(context, user),
child: Container(
padding: EdgeInsets.symmetric(
horizontal: suSetWidth(10.0),
Expand Down Expand Up @@ -713,7 +713,7 @@ class _UserPageState extends State<UserPage>
crossAxisCount: 3,
children: List<Widget>.generate(
UserAPI.blacklist.length,
(i) => blacklistUser(UserAPI.blacklist[i]),
(i) => blacklistUser(UserAPI.blacklist.elementAt(i)),
),
)
: Center(
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/cards/post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class _PostCardState extends State<PostCard> {
showConfirm: true,
);
if (confirm) {
UserAPI.fAddToBlacklist(uid: widget.post.uid, name: widget.post.nickname);
UserAPI.fAddToBlacklist(BlacklistUser(uid: widget.post.uid, username: widget.post.nickname));
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/widgets/cards/team_post_preview_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ class TeamPostPreviewCard extends StatelessWidget {
}

void delete(context) {
final post = Provider.of<TeamPostProvider>(context).post;
final provider = Provider.of<TeamPostProvider>(context, listen: false);
final post = provider.post;
TeamPostAPI.deletePost(postId: post.tid, postType: 7).then((response) {
showToast('删除成功');
Instances.eventBus.fire(TeamPostDeletedEvent(postId: post.tid));
});
}

void confirmAction(context) {
final provider = Provider.of<TeamPostProvider>(context, listen: false);
final post = provider.post;
ConfirmationBottomSheet.show(
context,
children: <Widget>[
Expand Down Expand Up @@ -72,7 +75,7 @@ class TeamPostPreviewCard extends StatelessWidget {
showConfirm: true,
);
if (confirm) {
UserAPI.fAddToBlacklist(uid: post.uid, name: post.nickname);
UserAPI.fAddToBlacklist(BlacklistUser(uid: post.uid, username: post.nickname));
}
}

Expand Down

0 comments on commit d1955c8

Please sign in to comment.