Skip to content

Commit

Permalink
TF-2122 Fix highlight and font search result incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 authored and hoangdat committed May 2, 2024
1 parent 2a6af59 commit 22804ac
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 109 deletions.
8 changes: 0 additions & 8 deletions core/lib/presentation/extensions/string_extension.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:core/utils/app_logger.dart';
import 'package:flutter/material.dart';

extension StringExtension on String {

Expand Down Expand Up @@ -39,11 +38,4 @@ extension StringExtension on String {
return '';
}
}

String get overflow {
return characters
.replaceAll(Characters(''), Characters('\u{200B}'))
.replaceAll(Characters('-'), Characters('\u{2011}'))
.toString();
}
}
82 changes: 39 additions & 43 deletions core/lib/presentation/views/text/rich_text_builder.dart
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
import 'package:core/presentation/utils/style_utils.dart';
import 'package:flutter/material.dart';

class RichTextBuilder {
class RichTextBuilder extends StatelessWidget {

final String _textOrigin;
final String _wordToStyle;
final TextStyle _styleOrigin;
final TextStyle _styleWord;
final String textOrigin;
final String wordToStyle;
final TextStyle styleOrigin;
final TextStyle styleWord;

Key? _key;
int? _maxLines;
TextOverflow? _overflow;
const RichTextBuilder({
super.key,
required this.textOrigin,
required this.wordToStyle,
required this.styleOrigin,
required this.styleWord,
});

RichTextBuilder(
this._textOrigin,
this._wordToStyle,
this._styleOrigin,
this._styleWord,
);

void key(Key key) {
_key = key;
}

void maxLines(int maxLines) {
_maxLines = maxLines;
}

void setOverflow(TextOverflow textOverflow) {
_overflow = textOverflow;
}

RichText build() {
return RichText(
key: _key,
maxLines: _maxLines ?? 1,
softWrap: CommonTextStyle.defaultSoftWrap,
overflow: _overflow ?? CommonTextStyle.defaultTextOverFlow,
text: TextSpan(
style: _styleOrigin,
children: _getSpans(_textOrigin, _wordToStyle, _styleWord)));
@override
Widget build(BuildContext context) {
return Text.rich(
TextSpan(
style: styleOrigin,
children: _getSpans(
text: textOrigin,
word: wordToStyle,
styleOrigin: styleOrigin,
styleWord: styleWord,
)
),
style: styleOrigin,
maxLines: 1,
overflow: TextOverflow.ellipsis
);
}

List<TextSpan> _getSpans(String text, String matchWord, TextStyle style) {
List<TextSpan> _getSpans({
required String text,
required String word,
required TextStyle styleOrigin,
required TextStyle styleWord,
}) {
List<TextSpan> spans = [];
int spanBoundary = 0;
do {
// look for the next match
final startIndex = text.toLowerCase().indexOf(matchWord.toLowerCase(), spanBoundary);
final startIndex = text.toLowerCase().indexOf(word.toLowerCase(), spanBoundary);
// if no more matches then add the rest of the string without style
if (startIndex == -1) {
spans.add(TextSpan(text: text.substring(spanBoundary)));
spans.add(TextSpan(text: text.substring(spanBoundary), style: styleOrigin));
return spans;
}
// add any unStyled text before the next match
if (startIndex > spanBoundary) {
spans.add(TextSpan(text: text.substring(spanBoundary, startIndex)));
spans.add(TextSpan(text: text.substring(spanBoundary, startIndex), style: styleOrigin));
}
// style the matched text
final endIndex = startIndex + matchWord.length;
final endIndex = startIndex + word.length;
final spanText = text.substring(startIndex, endIndex);
spans.add(TextSpan(text: spanText, style: style));
spans.add(TextSpan(text: spanText, style: styleWord));
// mark the boundary to start the next search from
spanBoundary = endIndex;
// continue until there are no more matches
Expand Down
28 changes: 13 additions & 15 deletions core/lib/presentation/views/text/text_overflow_builder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

import 'package:core/presentation/extensions/string_extension.dart';
import 'package:core/presentation/utils/style_utils.dart';
import 'package:core/utils/direction_utils.dart';
import 'package:flutter/material.dart';

class TextOverflowBuilder extends StatelessWidget {
Expand All @@ -14,22 +11,23 @@ class TextOverflowBuilder extends StatelessWidget {
final TextDirection? textDirection;
final TextOverflow? overflow;

const TextOverflowBuilder(this.data, {
super.key,
this.style,
this.textAlign,
this.softWrap = CommonTextStyle.defaultSoftWrap,
this.maxLines = 1,
this.textDirection,
this.overflow = CommonTextStyle.defaultTextOverFlow,
});
const TextOverflowBuilder(
this.data,
{
super.key,
this.style,
this.textAlign,
this.softWrap = true,
this.maxLines = 1,
this.textDirection,
this.overflow = TextOverflow.ellipsis,
}
);

@override
Widget build(BuildContext context) {
return Text(
DirectionUtils.isDirectionRTLByLanguage(context)
? data
: data.overflow,
data,
style: style,
textAlign: textAlign,
softWrap: softWrap,
Expand Down
84 changes: 41 additions & 43 deletions lib/features/thread/presentation/mixin/base_email_item_tile.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

import 'package:core/presentation/extensions/color_extension.dart';
import 'package:core/presentation/extensions/string_extension.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:core/presentation/utils/style_utils.dart';
import 'package:core/presentation/views/text/rich_text_builder.dart';
import 'package:core/presentation/views/text/text_overflow_builder.dart';
import 'package:core/utils/direction_utils.dart';
import 'package:core/utils/platform_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
Expand Down Expand Up @@ -88,20 +86,20 @@ mixin BaseEmailItemTile {
) {
if (isSearchEnabled(isSearchEmailRunning, query)) {
return RichTextBuilder(
DirectionUtils.isDirectionRTLByLanguage(context)
? informationSender(email, mailbox)
: informationSender(email, mailbox).overflow,
query?.value ?? '',
TextStyle(
fontSize: 15,
color: buildTextColorForReadEmail(email),
fontWeight: buildFontForReadEmail(email)),
TextStyle(
fontSize: 15,
color: buildTextColorForReadEmail(email),
backgroundColor: AppColor.bgWordSearch,
fontWeight: buildFontForReadEmail(email))
).build();
textOrigin: informationSender(email, mailbox),
wordToStyle: query?.value ?? '',
styleOrigin: TextStyle(
fontSize: 15,
color: buildTextColorForReadEmail(email),
fontWeight: buildFontForReadEmail(email)
),
styleWord: TextStyle(
fontSize: 15,
color: buildTextColorForReadEmail(email),
backgroundColor: AppColor.bgWordSearch,
fontWeight: buildFontForReadEmail(email)
)
);
} else {
return TextOverflowBuilder(
informationSender(email, mailbox),
Expand All @@ -122,20 +120,20 @@ mixin BaseEmailItemTile {
) {
if (isSearchEnabled(isSearchEmailRunning, query)) {
return RichTextBuilder(
DirectionUtils.isDirectionRTLByLanguage(context)
? email.getEmailTitle()
: email.getEmailTitle().overflow,
query?.value ?? '',
TextStyle(
fontSize: 13,
color: buildTextColorForReadEmail(email),
fontWeight: buildFontForReadEmail(email)),
TextStyle(
fontSize: 13,
backgroundColor: AppColor.bgWordSearch,
color: buildTextColorForReadEmail(email),
fontWeight: buildFontForReadEmail(email))
).build();
textOrigin: email.getEmailTitle(),
wordToStyle: query?.value ?? '',
styleOrigin: TextStyle(
fontSize: 13,
color: buildTextColorForReadEmail(email),
fontWeight: buildFontForReadEmail(email)
),
styleWord: TextStyle(
fontSize: 13,
backgroundColor: AppColor.bgWordSearch,
color: buildTextColorForReadEmail(email),
fontWeight: buildFontForReadEmail(email)
)
);
} else {
return TextOverflowBuilder(
email.getEmailTitle(),
Expand All @@ -155,19 +153,19 @@ mixin BaseEmailItemTile {
) {
if (isSearchEnabled(isSearchEmailRunning, query)) {
return RichTextBuilder(
DirectionUtils.isDirectionRTLByLanguage(context)
? email.getPartialContent()
: email.getPartialContent().overflow,
query?.value ?? '',
const TextStyle(
fontSize: 13,
color: AppColor.colorContentEmail,
fontWeight: FontWeight.normal),
const TextStyle(
fontSize: 13,
color: AppColor.colorContentEmail,
backgroundColor: AppColor.bgWordSearch)
).build();
textOrigin: email.getPartialContent(),
wordToStyle: query?.value ?? '',
styleOrigin: const TextStyle(
fontSize: 13,
color: AppColor.colorContentEmail,
fontWeight: FontWeight.normal
),
styleWord: const TextStyle(
fontSize: 13,
color: AppColor.colorContentEmail,
backgroundColor: AppColor.bgWordSearch
)
);
} else {
return TextOverflowBuilder(
email.getPartialContent(),
Expand Down

0 comments on commit 22804ac

Please sign in to comment.