From 824be1101357397b22143f414eed5303f413cde9 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 16 Apr 2020 14:07:21 +0200 Subject: [PATCH] Cache parsed emojies --- lib/helpers/comments_helper.dart | 3 ++- lib/helpers/posts_helper.dart | 3 ++- lib/models/comment.dart | 8 ++++--- lib/models/displayed_content.dart | 38 +++++++++++++++++++++++++++++++ lib/models/post.dart | 5 ++-- lib/ui/tiles/post_tile.dart | 8 +++---- lib/ui/widgets/text_widget.dart | 30 ++++++++---------------- 7 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 lib/models/displayed_content.dart diff --git a/lib/helpers/comments_helper.dart b/lib/helpers/comments_helper.dart index dd9ba7c..f74bcde 100644 --- a/lib/helpers/comments_helper.dart +++ b/lib/helpers/comments_helper.dart @@ -1,5 +1,6 @@ import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/comment.dart'; +import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/models/new_comment.dart'; /// Comments helper @@ -64,7 +65,7 @@ class CommentsHelper { userID: entry["userID"], postID: entry["postID"], timeSent: entry["time_sent"], - content: entry["content"], + content: DisplayedString(entry["content"]), imageURL: entry["img_url"], likes: entry["likes"], userLike: entry["userlike"], diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index 0f554e2..d49c8d7 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -7,6 +7,7 @@ import 'package:comunic/helpers/survey_helper.dart'; import 'package:comunic/lists/comments_list.dart'; import 'package:comunic/lists/posts_list.dart'; import 'package:comunic/models/api_request.dart'; +import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/models/new_post.dart'; import 'package:comunic/models/post.dart'; @@ -206,7 +207,7 @@ class PostsHelper { userPageID: map["user_page_id"], groupID: map["group_id"], timeSent: map["post_time"], - content: map["content"], + content: DisplayedString(map["content"]), visibilityLevel: _APIPostsVisibilityLevelMap[map["visibility_level"]], kind: postKind, fileSize: map["file_size"], diff --git a/lib/models/comment.dart b/lib/models/comment.dart index a706509..1a0ccf3 100644 --- a/lib/models/comment.dart +++ b/lib/models/comment.dart @@ -1,7 +1,8 @@ import 'package:comunic/enums/likes_type.dart'; +import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/models/like_element.dart'; -import 'package:meta/meta.dart'; import 'package:comunic/utils/account_utils.dart' as account; +import 'package:meta/meta.dart'; /// Comments /// @@ -14,7 +15,7 @@ class Comment implements LikeElement { final int userID; final int postID; final int timeSent; - String content; + DisplayedString content; final String imageURL; int likes; bool userLike; @@ -36,7 +37,8 @@ class Comment implements LikeElement { assert(likes != null), assert(userLike != null); - bool get hasContent => content != null && content.length > 0; + bool get hasContent => + content != null && !content.isNull && content.length > 0; bool get hasImage => imageURL != null; diff --git a/lib/models/displayed_content.dart b/lib/models/displayed_content.dart new file mode 100644 index 0000000..f9d03c1 --- /dev/null +++ b/lib/models/displayed_content.dart @@ -0,0 +1,38 @@ +import 'package:flutter_emoji/flutter_emoji.dart'; + +/// Optimized colons Emoji-parsed string +/// +/// @author Pierre Hubert + +class DisplayedString { + String _string; + String _parseCache; + + DisplayedString(this._string); + + int get length => _string.length; + + bool get isEmpty => _string.isEmpty; + + bool get isNull => _string == null; + + String get content => _string; + + set content(String content) { + _string = content; + _parseCache = null; + } + + @override + String toString() { + return _string; + } + + String get parsedString { + if (_parseCache == null) { + _parseCache = EmojiParser().emojify(this._string); + } + + return _parseCache; + } +} diff --git a/lib/models/post.dart b/lib/models/post.dart index ff0baea..f60fe86 100644 --- a/lib/models/post.dart +++ b/lib/models/post.dart @@ -3,6 +3,7 @@ import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/enums/post_visibility_level.dart'; import 'package:comunic/enums/user_access_levels.dart'; import 'package:comunic/lists/comments_list.dart'; +import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/models/like_element.dart'; import 'package:comunic/models/survey.dart'; import 'package:meta/meta.dart'; @@ -17,7 +18,7 @@ class Post implements LikeElement { final int userPageID; final int groupID; final int timeSent; - String content; + DisplayedString content; PostVisibilityLevel visibilityLevel; final PostKind kind; final int fileSize; @@ -73,7 +74,7 @@ class Post implements LikeElement { bool get isGroupPost => groupID != null && groupID > 0; - bool get hasContent => content != null; + bool get hasContent => content != null && !content.isNull; bool get hasComments => comments != null; diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index a573c11..bdf425d 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -521,7 +521,7 @@ class _PostTileState extends State { context: context, title: tr("Update comment content"), message: tr("New content:"), - defaultValue: comment.content, + defaultValue: comment.content.isNull ? "" : comment.content.content, hint: tr("New content..."), ); @@ -529,7 +529,7 @@ class _PostTileState extends State { return showSimpleSnack(context, tr("Could not update comment content!")); setState(() { - comment.content = newContent; + comment.content.content = newContent; }); } @@ -591,7 +591,7 @@ class _PostTileState extends State { context: context, title: tr("Update post content"), message: tr("Please enter message content: "), - defaultValue: widget.post.content == null ? "" : widget.post.content, + defaultValue: widget.post.content.isNull == null ? "" : widget.post.content.content, hint: tr("Post content"), ); @@ -602,7 +602,7 @@ class _PostTileState extends State { return; } - setState(() => widget.post.content = newContent); + setState(() => widget.post.content.content = newContent); } /// Perform the deletion of the post diff --git a/lib/ui/widgets/text_widget.dart b/lib/ui/widgets/text_widget.dart index e64f2be..8ebddcb 100644 --- a/lib/ui/widgets/text_widget.dart +++ b/lib/ui/widgets/text_widget.dart @@ -1,8 +1,8 @@ +import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/utils/bbcode_parser.dart'; import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/navigation_utils.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_emoji/flutter_emoji.dart'; import 'package:url_launcher/url_launcher.dart'; /// Text widget @@ -11,8 +11,8 @@ import 'package:url_launcher/url_launcher.dart'; /// /// @author Pierre Hubert -class TextWidget extends StatefulWidget { - final String content; +class TextWidget extends StatelessWidget { + final DisplayedString content; final bool parseBBcode; final TextStyle style; @@ -25,38 +25,28 @@ class TextWidget extends StatefulWidget { assert(parseBBcode != null), super(key: key); - @override - _TextWidgetState createState() => _TextWidgetState(); -} - -class _TextWidgetState extends State { - Widget _cache; - @override Widget build(BuildContext context) { - if (_cache == null) _cache = doBuild(); + if (this.content.isNull || this.content.isEmpty) return Text(""); - return _cache; - } - - Widget doBuild() { - var content = EmojiParser().emojify(this.widget.content); + var content = this.content.parsedString; // Parse BBcode - if (this.widget.parseBBcode) + if (parseBBcode) return BBCodeParsedWidget( text: content, - parseCallback: (style, text) => _parseLinks(text, style), + parseCallback: (style, text) => _parseLinks(context, text, style), ); // Just parse link return RichText( - text: TextSpan(children: _parseLinks(content, widget.style)), + text: TextSpan(children: _parseLinks(context, content, style)), ); } /// Sub parse function - List _parseLinks(String text, TextStyle style) { + List _parseLinks( + BuildContext context, String text, TextStyle style) { if (style == null) style = TextStyle(); var buff = StringBuffer();