1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Cache parsed emojies

This commit is contained in:
Pierre HUBERT 2020-04-16 14:07:21 +02:00
parent 6e96a554ff
commit 824be11013
7 changed files with 64 additions and 31 deletions

View File

@ -1,5 +1,6 @@
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/comment.dart'; import 'package:comunic/models/comment.dart';
import 'package:comunic/models/displayed_content.dart';
import 'package:comunic/models/new_comment.dart'; import 'package:comunic/models/new_comment.dart';
/// Comments helper /// Comments helper
@ -64,7 +65,7 @@ class CommentsHelper {
userID: entry["userID"], userID: entry["userID"],
postID: entry["postID"], postID: entry["postID"],
timeSent: entry["time_sent"], timeSent: entry["time_sent"],
content: entry["content"], content: DisplayedString(entry["content"]),
imageURL: entry["img_url"], imageURL: entry["img_url"],
likes: entry["likes"], likes: entry["likes"],
userLike: entry["userlike"], userLike: entry["userlike"],

View File

@ -7,6 +7,7 @@ import 'package:comunic/helpers/survey_helper.dart';
import 'package:comunic/lists/comments_list.dart'; import 'package:comunic/lists/comments_list.dart';
import 'package:comunic/lists/posts_list.dart'; import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/models/api_request.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/new_post.dart';
import 'package:comunic/models/post.dart'; import 'package:comunic/models/post.dart';
@ -206,7 +207,7 @@ class PostsHelper {
userPageID: map["user_page_id"], userPageID: map["user_page_id"],
groupID: map["group_id"], groupID: map["group_id"],
timeSent: map["post_time"], timeSent: map["post_time"],
content: map["content"], content: DisplayedString(map["content"]),
visibilityLevel: _APIPostsVisibilityLevelMap[map["visibility_level"]], visibilityLevel: _APIPostsVisibilityLevelMap[map["visibility_level"]],
kind: postKind, kind: postKind,
fileSize: map["file_size"], fileSize: map["file_size"],

View File

@ -1,7 +1,8 @@
import 'package:comunic/enums/likes_type.dart'; import 'package:comunic/enums/likes_type.dart';
import 'package:comunic/models/displayed_content.dart';
import 'package:comunic/models/like_element.dart'; import 'package:comunic/models/like_element.dart';
import 'package:meta/meta.dart';
import 'package:comunic/utils/account_utils.dart' as account; import 'package:comunic/utils/account_utils.dart' as account;
import 'package:meta/meta.dart';
/// Comments /// Comments
/// ///
@ -14,7 +15,7 @@ class Comment implements LikeElement {
final int userID; final int userID;
final int postID; final int postID;
final int timeSent; final int timeSent;
String content; DisplayedString content;
final String imageURL; final String imageURL;
int likes; int likes;
bool userLike; bool userLike;
@ -36,7 +37,8 @@ class Comment implements LikeElement {
assert(likes != null), assert(likes != null),
assert(userLike != 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; bool get hasImage => imageURL != null;

View File

@ -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;
}
}

View File

@ -3,6 +3,7 @@ import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/enums/post_visibility_level.dart'; import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/enums/user_access_levels.dart'; import 'package:comunic/enums/user_access_levels.dart';
import 'package:comunic/lists/comments_list.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/like_element.dart';
import 'package:comunic/models/survey.dart'; import 'package:comunic/models/survey.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
@ -17,7 +18,7 @@ class Post implements LikeElement {
final int userPageID; final int userPageID;
final int groupID; final int groupID;
final int timeSent; final int timeSent;
String content; DisplayedString content;
PostVisibilityLevel visibilityLevel; PostVisibilityLevel visibilityLevel;
final PostKind kind; final PostKind kind;
final int fileSize; final int fileSize;
@ -73,7 +74,7 @@ class Post implements LikeElement {
bool get isGroupPost => groupID != null && groupID > 0; bool get isGroupPost => groupID != null && groupID > 0;
bool get hasContent => content != null; bool get hasContent => content != null && !content.isNull;
bool get hasComments => comments != null; bool get hasComments => comments != null;

View File

@ -521,7 +521,7 @@ class _PostTileState extends State<PostTile> {
context: context, context: context,
title: tr("Update comment content"), title: tr("Update comment content"),
message: tr("New content:"), message: tr("New content:"),
defaultValue: comment.content, defaultValue: comment.content.isNull ? "" : comment.content.content,
hint: tr("New content..."), hint: tr("New content..."),
); );
@ -529,7 +529,7 @@ class _PostTileState extends State<PostTile> {
return showSimpleSnack(context, tr("Could not update comment content!")); return showSimpleSnack(context, tr("Could not update comment content!"));
setState(() { setState(() {
comment.content = newContent; comment.content.content = newContent;
}); });
} }
@ -591,7 +591,7 @@ class _PostTileState extends State<PostTile> {
context: context, context: context,
title: tr("Update post content"), title: tr("Update post content"),
message: tr("Please enter message 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"), hint: tr("Post content"),
); );
@ -602,7 +602,7 @@ class _PostTileState extends State<PostTile> {
return; return;
} }
setState(() => widget.post.content = newContent); setState(() => widget.post.content.content = newContent);
} }
/// Perform the deletion of the post /// Perform the deletion of the post

View File

@ -1,8 +1,8 @@
import 'package:comunic/models/displayed_content.dart';
import 'package:comunic/utils/bbcode_parser.dart'; import 'package:comunic/utils/bbcode_parser.dart';
import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/input_utils.dart';
import 'package:comunic/utils/navigation_utils.dart'; import 'package:comunic/utils/navigation_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_emoji/flutter_emoji.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
/// Text widget /// Text widget
@ -11,8 +11,8 @@ import 'package:url_launcher/url_launcher.dart';
/// ///
/// @author Pierre Hubert /// @author Pierre Hubert
class TextWidget extends StatefulWidget { class TextWidget extends StatelessWidget {
final String content; final DisplayedString content;
final bool parseBBcode; final bool parseBBcode;
final TextStyle style; final TextStyle style;
@ -25,38 +25,28 @@ class TextWidget extends StatefulWidget {
assert(parseBBcode != null), assert(parseBBcode != null),
super(key: key); super(key: key);
@override
_TextWidgetState createState() => _TextWidgetState();
}
class _TextWidgetState extends State<TextWidget> {
Widget _cache;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_cache == null) _cache = doBuild(); if (this.content.isNull || this.content.isEmpty) return Text("");
return _cache; var content = this.content.parsedString;
}
Widget doBuild() {
var content = EmojiParser().emojify(this.widget.content);
// Parse BBcode // Parse BBcode
if (this.widget.parseBBcode) if (parseBBcode)
return BBCodeParsedWidget( return BBCodeParsedWidget(
text: content, text: content,
parseCallback: (style, text) => _parseLinks(text, style), parseCallback: (style, text) => _parseLinks(context, text, style),
); );
// Just parse link // Just parse link
return RichText( return RichText(
text: TextSpan(children: _parseLinks(content, widget.style)), text: TextSpan(children: _parseLinks(context, content, style)),
); );
} }
/// Sub parse function /// Sub parse function
List<InlineSpan> _parseLinks(String text, TextStyle style) { List<InlineSpan> _parseLinks(
BuildContext context, String text, TextStyle style) {
if (style == null) style = TextStyle(); if (style == null) style = TextStyle();
var buff = StringBuffer(); var buff = StringBuffer();