Skip to content

Commit

Permalink
switch comment like
Browse files Browse the repository at this point in the history
  • Loading branch information
niuhuan committed Nov 2, 2021
1 parent cf14565 commit b32cc56
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-flutter-desktop/plugins/url_launcher v0.1.2
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20201108214237-06ea97f0c265
github.com/miguelpruivo/flutter_file_picker/go v0.0.0-20210622152105-9f0a811028a0
github.com/niuhuan/pica-go v0.0.0-20211008032829-544a58c56bee
github.com/niuhuan/pica-go v0.0.0-20211102035530-65029bd4ee97
github.com/pkg/errors v0.9.1
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
golang.org/x/mobile v0.0.0-20210924032853-1c027f395ef7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/miguelpruivo/flutter_file_picker/go v0.0.0-20210622152105-9f0a811028a
github.com/miguelpruivo/flutter_file_picker/go v0.0.0-20210622152105-9f0a811028a0/go.mod h1:csuW+TFyYKtiUwNvcvhcpyX4quPI7Pvv0SUogdqCW4I=
github.com/niuhuan/pica-go v0.0.0-20211008032829-544a58c56bee h1:SewxTGOkoJ3JUmLLQ1RXUcqLdifT5502QZQl09eCGL8=
github.com/niuhuan/pica-go v0.0.0-20211008032829-544a58c56bee/go.mod h1:fx2m+OgMeEZf6/TrfblV9i85SjPsOGbnjIL2gohxP4M=
github.com/niuhuan/pica-go v0.0.0-20211102035530-65029bd4ee97 h1:wlsmZVsLrZ/dB1KzyRl3TR6vRa6eefVE/16LnSHrQs8=
github.com/niuhuan/pica-go v0.0.0-20211102035530-65029bd4ee97/go.mod h1:fx2m+OgMeEZf6/TrfblV9i85SjPsOGbnjIL2gohxP4M=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
16 changes: 16 additions & 0 deletions go/main/controller/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,22 @@ func postChildComment(params string) (string, error) {
return "", nil
}

func switchLikeComment(params string) (string, error) {
var paramsStruct struct {
CommentId string `json:"commentId"`
ComicId string `json:"comicId"`
}
json.Unmarshal([]byte(params), &paramsStruct)
rsp, err := client.SwitchLikeComment(paramsStruct.CommentId)
if err != nil {
return "", err
}
network_cache.RemoveCaches(fmt.Sprintf("COMMENT_CHILDREN$%s$%%", paramsStruct.CommentId))
network_cache.RemoveCaches("MY_COMMENTS$%")
network_cache.RemoveCaches(fmt.Sprintf("COMMENTS$%s$%%", paramsStruct.ComicId))
return *rsp, nil
}

func myComments(pageStr string) (string, error) {
page, err := strconv.Atoi(pageStr)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions go/main/controller/pikapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ func FlatInvoke(method string, params string) (string, error) {
return "", nil
case "loadDownloadThreadCount":
return strconv.Itoa(loadDownloadThreadCount()), nil
case "switchLikeComment":
return switchLikeComment(params)
}
return "", errors.New("method not found : " + method)
}
7 changes: 7 additions & 0 deletions lib/basic/Method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,11 @@ class Method {
return _flatInvoke("saveDownloadThreadCount", "$value");
}

/// 喜欢/取消喜欢 一条评论
Future switchLikeComment(String commentId, String comicId) {
return _flatInvoke("switchLikeComment", {
"commentId": commentId,
"comicId": comicId,
});
}
}
4 changes: 2 additions & 2 deletions lib/screens/CommentScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class _CommentScreenState extends State<CommentScreen> {
),
body: Column(
children: [
ComicCommentItem(widget.comment),
ComicCommentItem(widget.comment, widget.comicId),
Container(
height: 3,
color:
Expand All @@ -68,7 +68,7 @@ class _CommentScreenState extends State<CommentScreen> {
}

Widget _buildComment(CommentChild e) {
return ComicCommentItem(e);
return ComicCommentItem(e, widget.comicId);
}

Widget _buildPostComment() {
Expand Down
78 changes: 61 additions & 17 deletions lib/screens/components/ComicCommentItem.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import 'package:flutter/material.dart';
import 'package:pikapi/basic/Common.dart';
import 'package:pikapi/basic/Entities.dart';
import 'package:pikapi/basic/Method.dart';

import 'PicaAvatar.dart';

class ComicCommentItem extends StatelessWidget {
class ComicCommentItem extends StatefulWidget {
final String comicId;
final Comment comment;

const ComicCommentItem(this.comment);
const ComicCommentItem(this.comment, this.comicId);

@override
State<StatefulWidget> createState() => _ComicCommentItem();
}

class _ComicCommentItem extends State<ComicCommentItem> {
var likeLoading = false;

@override
Widget build(BuildContext context) {
var comment = widget.comment;
var comicId = widget.comicId;
var theme = Theme.of(context);
var nameStyle = TextStyle(fontWeight: FontWeight.bold);
var levelStyle = TextStyle(
Expand Down Expand Up @@ -93,22 +104,55 @@ class ComicCommentItem extends StatelessWidget {
])
: TextSpan(),
WidgetSpan(child: Container(width: 12)),
TextSpan(children: [
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Icon(
comment.isLiked
? Icons.favorite
: Icons.favorite_border,
size: 13,
color: theme.colorScheme.secondary
.withOpacity(.7)),
),
WidgetSpan(child: Container(width: 5)),
TextSpan(
text: '${comment.likesCount}',
WidgetSpan(
child: GestureDetector(
onTap: () async {
setState(() {
likeLoading = true;
});
try {
await method.switchLikeComment(
comment.id,
comicId,
);
setState(() {
if (comment.isLiked) {
comment.isLiked = false;
comment.likesCount--;
} else {
comment.isLiked = true;
comment.likesCount++;
}
});
} catch (e, s) {
defaultToast(context, "点赞失败");
} finally {
setState(() {
likeLoading = false;
});
}
},
child: Text.rich(
TextSpan(style: levelStyle, children: [
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Icon(
likeLoading
? Icons.refresh
: comment.isLiked
? Icons.favorite
: Icons.favorite_border,
size: 13,
color: theme.colorScheme.secondary
.withOpacity(.7)),
),
WidgetSpan(child: Container(width: 5)),
TextSpan(
text: '${comment.likesCount}',
),
]),
),
]),
)),
],
)),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/components/ComicCommentList.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class _ComicCommentListState extends State<ComicCommentList> {
),
);
},
child: ComicCommentItem(comment),
child: ComicCommentItem(comment, widget.comicId),
);
}

Expand Down

0 comments on commit b32cc56

Please sign in to comment.