diff --git a/go/go.mod b/go/go.mod index af9c561..1b63fd2 100644 --- a/go/go.mod +++ b/go/go.mod @@ -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-20211105060849-4f6ae99a942e + github.com/niuhuan/pica-go v0.0.0-20211109050119-cf22461c638f 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 diff --git a/go/go.sum b/go/go.sum index 48d5642..1aa07f5 100644 --- a/go/go.sum +++ b/go/go.sum @@ -41,6 +41,12 @@ github.com/niuhuan/pica-go v0.0.0-20211102035530-65029bd4ee97 h1:wlsmZVsLrZ/dB1K github.com/niuhuan/pica-go v0.0.0-20211102035530-65029bd4ee97/go.mod h1:fx2m+OgMeEZf6/TrfblV9i85SjPsOGbnjIL2gohxP4M= github.com/niuhuan/pica-go v0.0.0-20211105060849-4f6ae99a942e h1:NR/RCPlbx7cMjk7p8GqfkhOqWTH6vyfgYq7Vubjn9mY= github.com/niuhuan/pica-go v0.0.0-20211105060849-4f6ae99a942e/go.mod h1:fx2m+OgMeEZf6/TrfblV9i85SjPsOGbnjIL2gohxP4M= +github.com/niuhuan/pica-go v0.0.0-20211109012708-2cef9976dbaa h1:PnIE6Ltz/dy9K1jZCGfTvPDHGitSwXVvQfzwgVIJSWE= +github.com/niuhuan/pica-go v0.0.0-20211109012708-2cef9976dbaa/go.mod h1:fx2m+OgMeEZf6/TrfblV9i85SjPsOGbnjIL2gohxP4M= +github.com/niuhuan/pica-go v0.0.0-20211109043215-974462cb2b0d h1:jwZdWp1PkEbWAi/w3cI+4kBvIyxHtZgVmFFOu7XnABw= +github.com/niuhuan/pica-go v0.0.0-20211109043215-974462cb2b0d/go.mod h1:fx2m+OgMeEZf6/TrfblV9i85SjPsOGbnjIL2gohxP4M= +github.com/niuhuan/pica-go v0.0.0-20211109050119-cf22461c638f h1:SQ6vAF7nyIDjhUj/SsEHRlxae4XLtygSuxWrJJazN1g= +github.com/niuhuan/pica-go v0.0.0-20211109050119-cf22461c638f/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= diff --git a/go/main/controller/client.go b/go/main/controller/client.go index 65b28d7..fc432f0 100644 --- a/go/main/controller/client.go +++ b/go/main/controller/client.go @@ -425,6 +425,23 @@ func postChildComment(params string) (string, error) { return "", nil } +func postGameChildComment(params string) (string, error) { + var paramsStruct struct { + GameId string `json:"gameId"` + CommentId string `json:"commentId"` + Content string `json:"content"` + } + json.Unmarshal([]byte(params), ¶msStruct) + err := client.PostChildComment(paramsStruct.CommentId, paramsStruct.Content) + if err != nil { + return "", err + } + network_cache.RemoveCaches(fmt.Sprintf("GAME_COMMENT_CHILDREN$%s$%%", paramsStruct.CommentId)) + network_cache.RemoveCaches("MY_COMMENTS$%") + network_cache.RemoveCaches(fmt.Sprintf("GAME_COMMENTS$%s$%%", paramsStruct.GameId)) + return "", nil +} + func switchLikeComment(params string) (string, error) { var paramsStruct struct { CommentId string `json:"commentId"` @@ -478,3 +495,68 @@ func game(gameId string) (string, error) { }, ) } + +func gameComments(params string) (string, error) { + var paramsStruct struct { + GameId string `json:"gameId"` + Page int `json:"page"` + } + json.Unmarshal([]byte(params), ¶msStruct) + gameId := paramsStruct.GameId + page := paramsStruct.Page + return cacheable( + fmt.Sprintf("GAME_COMMENTS$%s$%d", gameId, page), + time.Hour*2, + func() (interface{}, error) { + return client.GameCommentsPage(gameId, page) + }, + ) +} + +func postGameComment(params string) (string, error) { + var paramsStruct struct { + GameId string `json:"gameId"` + Content string `json:"content"` + } + json.Unmarshal([]byte(params), ¶msStruct) + err := client.PostGameComment(paramsStruct.GameId, paramsStruct.Content) + if err != nil { + return "", err + } + network_cache.RemoveCaches("MY_COMMENTS$%") + network_cache.RemoveCaches(fmt.Sprintf("GAME_COMMENTS$%s$%%", paramsStruct.GameId)) + return "", nil +} + +func gameCommentChildren(params string) (string, error) { + var paramsStruct struct { + CommentId string `json:"commentId"` + Page int `json:"page"` + } + json.Unmarshal([]byte(params), ¶msStruct) + commentId := paramsStruct.CommentId + page := paramsStruct.Page + return cacheable( + fmt.Sprintf("GAME_COMMENT_CHILDREN$%s$%d", commentId, page), + time.Hour*2, + func() (interface{}, error) { + return client.GameCommentChildren(commentId, page) + }, + ) +} + +func switchLikeGameComment(params string) (string, error) { + var paramsStruct struct { + CommentId string `json:"commentId"` + GameId string `json:"gameId"` + } + json.Unmarshal([]byte(params), ¶msStruct) + rsp, err := client.SwitchLikeComment(paramsStruct.CommentId) + if err != nil { + return "", err + } + network_cache.RemoveCaches(fmt.Sprintf("GAME_COMMENT_CHILDREN$%s$%%", paramsStruct.CommentId)) + network_cache.RemoveCaches("MY_COMMENTS$%") + network_cache.RemoveCaches(fmt.Sprintf("GAME_COMMENTS$%s$%%", paramsStruct.GameId)) + return *rsp, nil +} diff --git a/go/main/controller/pikapi.go b/go/main/controller/pikapi.go index ae9ec0c..7681033 100644 --- a/go/main/controller/pikapi.go +++ b/go/main/controller/pikapi.go @@ -585,6 +585,16 @@ func FlatInvoke(method string, params string) (string, error) { return game(params) case "games": return games(params) + case "gameComments": + return gameComments(params) + case "postGameComment": + return postGameComment(params) + case "gameCommentChildren": + return gameCommentChildren(params) + case "switchLikeGameComment": + return switchLikeGameComment(params) + case "postGameChildComment": + return postGameChildComment(params) case "viewLogPage": return viewLogPage(params) case "clearAllViewLog": diff --git a/lib/basic/Entities.dart b/lib/basic/Entities.dart index a72daf2..7a275f4 100644 --- a/lib/basic/Entities.dart +++ b/lib/basic/Entities.dart @@ -66,6 +66,8 @@ class Page { this.page = json["page"]; this.pages = json["pages"]; } + + Page.of(this.total, this.limit, this.page, this.pages); } /// 分类 @@ -255,12 +257,10 @@ class CommentPage extends Page { } } -/// 漫画评论详情 -class Comment { +class CommentBase { late String id; late String content; late CommentUser user; - late String comic; late bool isTop; late bool hide; late String createdAt; @@ -268,11 +268,10 @@ class Comment { late int commentsCount; late bool isLiked; - Comment.fromJson(Map json) { + CommentBase.fromJson(Map json) { this.id = json["_id"]; this.content = json["content"]; this.user = CommentUser.fromJson(Map.of(json["_user"])); - this.comic = json["_comic"]; this.isTop = json["isTop"]; this.hide = json["hide"]; this.createdAt = json["created_at"]; @@ -282,6 +281,24 @@ class Comment { } } +/// 子评论 +class ChildOfComment extends Comment { + late String parent; + + ChildOfComment.fromJson(Map json) : super.fromJson(json) { + this.parent = json["_parent"]; + } +} + +/// 漫画评论详情 +class Comment extends CommentBase { + late String comic; + + Comment.fromJson(Map json) : super.fromJson(json) { + this.comic = json["_comic"]; + } +} + /// 评论的用户信息 class CommentUser extends BasicUser { late String role; @@ -604,10 +621,54 @@ class CommentChildrenPage extends Page { } /// 子评论 -class CommentChild extends Comment { - late String parent; +class CommentChild extends ChildOfComment { + late String comic; CommentChild.fromJson(Map json) : super.fromJson(json) { - this.parent = json["_parent"]; + this.comic = json["_comic"]; + } +} + +/// 漫画评论分页 +class GameCommentPage extends Page { + late List docs; + + GameCommentPage.fromJson(Map json) : super.fromJson(json) { + this.docs = List.from(json["docs"]) + .map((e) => Map.from(e)) + .map((e) => GameComment.fromJson(e)) + .toList(); + } +} + +/// 游戏评论 +class GameComment extends CommentBase { + late String game; + + GameComment.fromJson(Map json) : super.fromJson(json) { + this.game = json["_game"]; + } +} + +/// 子评论分页 +class GameCommentChildrenPage extends Page { + late List docs; + + GameCommentChildrenPage.fromJson(Map json) + : super.fromJson(json) { + this.docs = []; + if (json["docs"] != null) { + docs.addAll( + List.of(json["docs"]).map((e) => GameCommentChild.fromJson(e)).toList()); + } + } +} + +/// 子评论 +class GameCommentChild extends ChildOfComment { + late String game; + + GameCommentChild.fromJson(Map json) : super.fromJson(json) { + this.game = json["_game"]; } } diff --git a/lib/basic/Method.dart b/lib/basic/Method.dart index 24bc9ee..909d726 100644 --- a/lib/basic/Method.dart +++ b/lib/basic/Method.dart @@ -322,6 +322,14 @@ class Method { return CommentChildrenPage.fromJson(json.decode(rsp)); } + /// 喜欢/取消喜欢 一条评论 + Future switchLikeComment(String commentId, String comicId) { + return _flatInvoke("switchLikeComment", { + "commentId": commentId, + "comicId": comicId, + }); + } + /// 我的评论列表 Future myComments(int page) async { String response = await _flatInvoke("myComments", "$page"); @@ -360,6 +368,53 @@ class Method { return GameInfo.fromJson(json.decode(data)); } + /// 游戏的评论列表 + Future gameComments(String gameId, int page) async { + var rsp = await _flatInvoke("gameComments", { + "gameId": gameId, + "page": page, + }); + return GameCommentPage.fromJson(json.decode(rsp)); + } + + /// 对游戏发送评论 + Future postGameComment(String gameId, String content) { + return _flatInvoke("postGameComment", { + "gameId": gameId, + "content": content, + }); + } + + /// 拉取游戏子评论 + Future gameCommentChildren( + String gameId, + String commentId, + int page, + ) async { + var rsp = await _flatInvoke("gameCommentChildren", { + "gameId": gameId, + "commentId": commentId, + "page": page, + }); + return GameCommentChildrenPage.fromJson(json.decode(rsp)); + } + + /// 喜欢/取消喜欢 一条游戏评论 + Future switchLikeGameComment(String commentId, String gameId) { + return _flatInvoke("switchLikeGameComment", { + "commentId": commentId, + "gameId": gameId, + }); + } + + /// 发送游戏子评论 + Future postGameChildComment(String commentId, String content) { + return _flatInvoke("postGameChildComment", { + "commentId": commentId, + "content": content, + }); + } + /// 清理网络缓存 Future cleanNetworkCache() { return _flatInvoke("cleanNetworkCache", ""); @@ -611,14 +666,6 @@ class Method { return _flatInvoke("saveDownloadThreadCount", "$value"); } - /// 喜欢/取消喜欢 一条评论 - Future switchLikeComment(String commentId, String comicId) { - return _flatInvoke("switchLikeComment", { - "commentId": commentId, - "comicId": comicId, - }); - } - /// 转化为PNG Future convertToPNG(String path) async { return await _channel.invokeMethod("convertToPNG", {"path": path}); diff --git a/lib/screens/ComicInfoScreen.dart b/lib/screens/ComicInfoScreen.dart index a6a5648..1c45044 100644 --- a/lib/screens/ComicInfoScreen.dart +++ b/lib/screens/ComicInfoScreen.dart @@ -5,11 +5,12 @@ import 'package:pikapi/basic/Cross.dart'; import 'package:pikapi/basic/Entities.dart'; import 'package:pikapi/screens/ComicsScreen.dart'; import 'package:pikapi/basic/Navigatior.dart'; +import 'package:pikapi/screens/components/CommentMainType.dart'; import 'package:pikapi/screens/components/ItemBuilder.dart'; import 'package:pikapi/basic/Method.dart'; import 'ComicReaderScreen.dart'; import 'DownloadConfirmScreen.dart'; -import 'components/ComicCommentList.dart'; +import 'components/CommentList.dart'; import 'components/ComicDescriptionCard.dart'; import 'components/ComicInfoCard.dart'; import 'components/ComicTagsCard.dart'; @@ -104,7 +105,7 @@ class _ComicInfoScreenState extends State with RouteAware { ]; var _views = [ _buildEpWrap(_epListFuture, _comicInfo), - ComicCommentList(_comicInfo.id), + CommentList(CommentMainType.COMIC, _comicInfo.id), ]; return DefaultTabController( length: _tabs.length, diff --git a/lib/screens/CommentScreen.dart b/lib/screens/CommentScreen.dart index d92e222..7480d69 100644 --- a/lib/screens/CommentScreen.dart +++ b/lib/screens/CommentScreen.dart @@ -1,15 +1,34 @@ import 'package:flutter/material.dart'; import 'package:pikapi/basic/Common.dart'; import 'package:pikapi/basic/Entities.dart'; +import 'package:pikapi/basic/Entities.dart' as e; import 'package:pikapi/basic/Method.dart'; -import 'package:pikapi/screens/components/ComicCommentItem.dart'; +import 'package:pikapi/screens/components/CommentItem.dart'; +import 'package:pikapi/screens/components/CommentMainType.dart'; import 'package:pikapi/screens/components/ContentBuilder.dart'; -class CommentScreen extends StatefulWidget { - final String comicId; - final Comment comment; +class _CommentChildPage extends e.Page { + late List docs; - const CommentScreen(this.comicId, this.comment); + _CommentChildPage.ofComic(CommentChildrenPage commentPage) + : super.of(commentPage.total, commentPage.limit, commentPage.page, + commentPage.pages) { + this.docs = commentPage.docs; + } + + _CommentChildPage.ofGame(GameCommentChildrenPage commentPage) + : super.of(commentPage.total, commentPage.limit, commentPage.page, + commentPage.pages) { + this.docs = commentPage.docs; + } +} + +class CommentScreen extends StatefulWidget { + final CommentMainType mainType; + final String mainId; + final CommentBase comment; + + const CommentScreen(this.mainType, this.mainId, this.comment); @override State createState() => _CommentScreenState(); @@ -17,14 +36,23 @@ class CommentScreen extends StatefulWidget { class _CommentScreenState extends State { late int _currentPage = 1; - late Future _future = _loadPage(); + late Future<_CommentChildPage> _future = _loadPage(); - Future _loadPage() { - return method.commentChildren( - widget.comicId, - widget.comment.id, - _currentPage, - ); + Future<_CommentChildPage> _loadPage() async { + switch (widget.mainType) { + case CommentMainType.COMIC: + return _CommentChildPage.ofComic(await method.commentChildren( + widget.mainId, + widget.comment.id, + _currentPage, + )); + case CommentMainType.GAME: + return _CommentChildPage.ofGame(await method.gameCommentChildren( + widget.mainId, + widget.comment.id, + _currentPage, + )); + } } Widget _buildChildrenPager() { @@ -32,7 +60,7 @@ class _CommentScreenState extends State { future: _future, onRefresh: _loadPage, successBuilder: - (BuildContext context, AsyncSnapshot snapshot) { + (BuildContext context, AsyncSnapshot<_CommentChildPage> snapshot) { var page = snapshot.data!; return ListView( children: [ @@ -54,7 +82,7 @@ class _CommentScreenState extends State { ), body: Column( children: [ - ComicCommentItem(widget.comment, widget.comicId), + ComicCommentItem(widget.mainType, widget.mainId, widget.comment), Container( height: 3, color: @@ -67,8 +95,8 @@ class _CommentScreenState extends State { ); } - Widget _buildComment(CommentChild e) { - return ComicCommentItem(e, widget.comicId); + Widget _buildComment(CommentBase e) { + return ComicCommentItem(widget.mainType, widget.mainId, e); } Widget _buildPostComment() { @@ -77,7 +105,14 @@ class _CommentScreenState extends State { String? text = await inputString(context, '请输入评论内容'); if (text != null && text.isNotEmpty) { try { - await method.postChildComment(widget.comment.id, text); + switch (widget.mainType) { + case CommentMainType.COMIC: + await method.postChildComment(widget.comment.id, text); + break; + case CommentMainType.GAME: + await method.postGameChildComment(widget.comment.id, text); + break; + } setState(() { _future = _loadPage(); widget.comment.commentsCount++; @@ -110,7 +145,7 @@ class _CommentScreenState extends State { ); } - Widget _buildPrePage(CommentChildrenPage page) { + Widget _buildPrePage(_CommentChildPage page) { if (page.page > 1) { return InkWell( onTap: () { @@ -130,7 +165,7 @@ class _CommentScreenState extends State { return Container(); } - Widget _buildNextPage(CommentChildrenPage page) { + Widget _buildNextPage(_CommentChildPage page) { if (page.page < page.pages) { return InkWell( onTap: () { diff --git a/lib/screens/GameInfoScreen.dart b/lib/screens/GameInfoScreen.dart index 0335ddd..9529ebc 100644 --- a/lib/screens/GameInfoScreen.dart +++ b/lib/screens/GameInfoScreen.dart @@ -2,11 +2,13 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:pikapi/basic/Entities.dart'; import 'package:pikapi/basic/Method.dart'; +import 'package:pikapi/screens/components/CommentMainType.dart'; import 'package:pikapi/screens/components/ContentError.dart'; import 'package:pikapi/screens/components/ContentLoading.dart'; import 'package:pikapi/screens/components/Images.dart'; import 'GameDownloadScreen.dart'; +import 'components/CommentList.dart'; import 'components/GameTitleCard.dart'; // 游戏详情 @@ -59,71 +61,110 @@ class _GameInfoScreenState extends State { return LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { var info = snapshot.data!; - return Scaffold( - appBar: AppBar( - title: Text(info.title), - ), - body: ListView( - children: [ - GameTitleCard(info), - Container( - padding: EdgeInsets.only( - left: 20, right: 20, top: 5, bottom: 10, - ), - child: ClipRRect( - borderRadius: BorderRadius.all(Radius.circular(5)), - child: MaterialButton( - color: Theme.of(context).colorScheme.secondary, - textColor: Colors.white, - onPressed: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => GameDownloadScreen(info)), - ); - }, - child: Container( - padding: EdgeInsets.all(5), - child: Text('下载'), + return DefaultTabController( + length: 2, + child: Scaffold( + appBar: AppBar( + title: Text(info.title), + ), + body: ListView( + children: [ + GameTitleCard(info), + Container( + padding: EdgeInsets.only( + left: 20, + right: 20, + top: 5, + bottom: 10, + ), + child: ClipRRect( + borderRadius: BorderRadius.all(Radius.circular(5)), + child: MaterialButton( + color: Theme.of(context).colorScheme.secondary, + textColor: Colors.white, + onPressed: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => + GameDownloadScreen(info)), + ); + }, + child: Container( + padding: EdgeInsets.all(5), + child: Text('下载'), + ), ), ), ), - ), - Container( - margin: EdgeInsets.only( - top: screenShootMargin, - bottom: screenShootMargin, - ), - height: screenShootHeight, - child: ListView( - padding: EdgeInsets.only( - left: screenShootMargin, - right: screenShootMargin, + Container( + margin: EdgeInsets.only( + top: screenShootMargin, + bottom: screenShootMargin, ), - scrollDirection: Axis.horizontal, - children: info.screenshots - .map((e) => Container( - margin: EdgeInsets.only( - left: screenShootMargin, - right: screenShootMargin, - ), - child: ClipRRect( - borderRadius: iconRadius, - child: RemoteImage( - height: screenShootHeight, - fileServer: e.fileServer, - path: e.path, + height: screenShootHeight, + child: ListView( + padding: EdgeInsets.only( + left: screenShootMargin, + right: screenShootMargin, + ), + scrollDirection: Axis.horizontal, + children: info.screenshots + .map((e) => Container( + margin: EdgeInsets.only( + left: screenShootMargin, + right: screenShootMargin, ), - ), - )) - .toList(), + child: ClipRRect( + borderRadius: iconRadius, + child: RemoteImage( + height: screenShootHeight, + fileServer: e.fileServer, + path: e.path, + ), + ), + )) + .toList(), + ), ), - ), - Container( - padding: EdgeInsets.all(20), - child: Text(info.description, style: descriptionStyle), - ), - ], + Container(height: 20), + Container( + child: Column( + children: [ + Container( + height: 40, + color: Theme.of(context) + .colorScheme + .secondary + .withOpacity(.025), + child: TabBar( + tabs: [ + Tab(text: '详情 '), + Tab(text: '评论 (${info.commentsCount})'), + ], + indicatorColor: + Theme.of(context).colorScheme.secondary, + labelColor: + Theme.of(context).colorScheme.secondary, + onTap: (val) async { + setState(() { + _tabIndex = val; + }); + }, + ), + ), + ], + ), + ), + _tabIndex == 0 + ? Container( + padding: EdgeInsets.all(20), + child: + Text(info.description, style: descriptionStyle), + ) + : CommentList(CommentMainType.GAME, info.id), + ], + ), ), ); }, @@ -131,4 +172,6 @@ class _GameInfoScreenState extends State { }, ); } + + var _tabIndex = 0; } diff --git a/lib/screens/components/ComicCommentItem.dart b/lib/screens/components/CommentItem.dart similarity index 87% rename from lib/screens/components/ComicCommentItem.dart rename to lib/screens/components/CommentItem.dart index 1530ca1..d2c3dc6 100644 --- a/lib/screens/components/ComicCommentItem.dart +++ b/lib/screens/components/CommentItem.dart @@ -4,12 +4,14 @@ import 'package:pikapi/basic/Entities.dart'; import 'package:pikapi/basic/Method.dart'; import 'Avatar.dart'; +import 'CommentMainType.dart'; class ComicCommentItem extends StatefulWidget { - final String comicId; - final Comment comment; + final CommentMainType mainType; + final String mainId; + final CommentBase comment; - const ComicCommentItem(this.comment, this.comicId); + const ComicCommentItem(this.mainType, this.mainId, this.comment); @override State createState() => _ComicCommentItem(); @@ -21,7 +23,6 @@ class _ComicCommentItem extends State { @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( @@ -111,10 +112,20 @@ class _ComicCommentItem extends State { likeLoading = true; }); try { - await method.switchLikeComment( - comment.id, - comicId, - ); + switch (widget.mainType) { + case CommentMainType.COMIC: + await method.switchLikeComment( + comment.id, + widget.mainId, + ); + break; + case CommentMainType.GAME: + await method.switchLikeGameComment( + comment.id, + widget.mainId, + ); + break; + } setState(() { if (comment.isLiked) { comment.isLiked = false; diff --git a/lib/screens/components/ComicCommentList.dart b/lib/screens/components/CommentList.dart similarity index 59% rename from lib/screens/components/ComicCommentList.dart rename to lib/screens/components/CommentList.dart index 16f4256..c570876 100644 --- a/lib/screens/components/ComicCommentList.dart +++ b/lib/screens/components/CommentList.dart @@ -1,27 +1,55 @@ import 'package:flutter/material.dart'; import 'package:pikapi/basic/Common.dart'; import 'package:pikapi/basic/Entities.dart'; +import 'package:pikapi/basic/Entities.dart' as e; import 'package:pikapi/screens/CommentScreen.dart'; import 'package:pikapi/screens/components/ItemBuilder.dart'; import 'package:pikapi/basic/Method.dart'; -import 'ComicCommentItem.dart'; +import 'CommentItem.dart'; +import 'CommentMainType.dart'; -// 漫画的评论列表 -class ComicCommentList extends StatefulWidget { - final String comicId; +class _CommentBasePage extends e.Page { + late List docs; - ComicCommentList(this.comicId); + _CommentBasePage.ofComic(CommentPage commentPage) + : super.of(commentPage.total, commentPage.limit, commentPage.page, + commentPage.pages) { + this.docs = commentPage.docs; + } - @override - State createState() => _ComicCommentListState(); + _CommentBasePage.ofGame(GameCommentPage commentPage) + : super.of(commentPage.total, commentPage.limit, commentPage.page, + commentPage.pages) { + this.docs = commentPage.docs; + } } -class _ComicCommentListState extends State { - late int _currentPage = 1; - late Future _future = _loadPage(); +// 漫画的评论列表 +class CommentList extends StatefulWidget { + final CommentMainType mainType; + final String mainId; - Future _loadPage() { - return method.comments(widget.comicId, _currentPage); + CommentList(this.mainType, this.mainId); + + @override + State createState() => _CommentListState(); +} + +class _CommentListState extends State { + late int _currentPage = 1; + late Future<_CommentBasePage> _future = _loadPage(); + + Future<_CommentBasePage> _loadPage() async { + switch (widget.mainType) { + case CommentMainType.COMIC: + return _CommentBasePage.ofComic( + await method.comments(widget.mainId, _currentPage), + ); + case CommentMainType.GAME: + return _CommentBasePage.ofGame( + await method.gameComments(widget.mainId, _currentPage), + ); + } } @override @@ -29,7 +57,7 @@ class _ComicCommentListState extends State { return ItemBuilder( future: _future, successBuilder: - (BuildContext context, AsyncSnapshot snapshot) { + (BuildContext context, AsyncSnapshot<_CommentBasePage> snapshot) { var page = snapshot.data!; return Column( mainAxisAlignment: MainAxisAlignment.start, @@ -50,16 +78,17 @@ class _ComicCommentListState extends State { ); } - Widget _buildComment(Comment comment) { + Widget _buildComment(CommentBase comment) { return InkWell( onTap: () { Navigator.of(context).push( MaterialPageRoute( - builder: (context) => CommentScreen(widget.comicId, comment), + builder: (context) => + CommentScreen(widget.mainType, widget.mainId, comment), ), ); }, - child: ComicCommentItem(comment, widget.comicId), + child: ComicCommentItem(widget.mainType, widget.mainId, comment), ); } @@ -69,7 +98,14 @@ class _ComicCommentListState extends State { String? text = await inputString(context, '请输入评论内容'); if (text != null && text.isNotEmpty) { try { - await method.postComment(widget.comicId, text); + switch (widget.mainType) { + case CommentMainType.COMIC: + await method.postComment(widget.mainId, text); + break; + case CommentMainType.GAME: + await method.postGameComment(widget.mainId, text); + break; + } setState(() { _future = _loadPage(); }); @@ -101,7 +137,7 @@ class _ComicCommentListState extends State { ); } - Widget _buildPrePage(CommentPage page) { + Widget _buildPrePage(_CommentBasePage page) { if (page.page > 1) { return InkWell( onTap: () { @@ -121,7 +157,7 @@ class _ComicCommentListState extends State { return Container(); } - Widget _buildNextPage(CommentPage page) { + Widget _buildNextPage(_CommentBasePage page) { if (page.page < page.pages) { return InkWell( onTap: () { diff --git a/lib/screens/components/CommentMainType.dart b/lib/screens/components/CommentMainType.dart new file mode 100644 index 0000000..1764238 --- /dev/null +++ b/lib/screens/components/CommentMainType.dart @@ -0,0 +1,4 @@ +enum CommentMainType { + COMIC, + GAME, +}