88using System ;
99using System . Collections . Generic ;
1010using System . Linq ;
11+ using Telegram . Converters ;
1112using Telegram . Native ;
1213using Telegram . Td . Api ;
1314
@@ -38,6 +39,23 @@ private TextStyleRun(TextStyleRun run)
3839 Type = run . Type ;
3940 }
4041
42+ #region DateTime
43+
44+ public string FormattedText { get ; set ; } = string . Empty ;
45+
46+ public string Update ( StyledParagraph paragraph )
47+ {
48+ if ( string . IsNullOrEmpty ( FormattedText ) || Type is TextEntityTypeDateTime { FormattingType : DateTimeFormattingTypeRelative } )
49+ {
50+ FormattedText = Formatter . Relative ( Type as TextEntityTypeDateTime ) ;
51+ paragraph . IsDirty = true ;
52+ }
53+
54+ return FormattedText ;
55+ }
56+
57+ #endregion
58+
4159 public bool HasFlag ( TextStyle flag )
4260 {
4361 return ( Flags & flag ) != 0 ;
@@ -578,6 +596,9 @@ public StyledText(string text, IList<TextEntity> entities, IList<StyledParagraph
578596
579597 public partial class StyledParagraph
580598 {
599+ private readonly bool _hasDates ;
600+ private readonly bool _hasRelativeDates ;
601+
581602 public StyledParagraph ( string text , IList < TextEntity > entities )
582603 : this ( text , 0 , text . Length , entities )
583604 {
@@ -586,6 +607,7 @@ public StyledParagraph(string text, IList<TextEntity> entities)
586607
587608 public StyledParagraph ( string text , int offset , int length , IList < TextEntity > entities , TextDirectionality ? direction = null , int padding = 0 )
588609 {
610+ Text = text ;
589611 Offset = offset ;
590612 Length = length ;
591613 Entities = entities ?? Array . Empty < TextEntity > ( ) ;
@@ -596,6 +618,9 @@ public StyledParagraph(string text, int offset, int length, IList<TextEntity> en
596618
597619 if ( entities ? . Count > 0 )
598620 {
621+ _hasRelativeDates = entities . Any ( x => x . Type is TextEntityTypeDateTime { FormattingType : DateTimeFormattingTypeRelative } ) ;
622+ _hasDates = _hasRelativeDates || entities . Any ( x => x . Type is TextEntityTypeDateTime { FormattingType : DateTimeFormattingTypeAbsolute } ) ;
623+
599624 Type = entities [ 0 ] . Type switch
600625 {
601626 TextEntityTypePreCode preCode => new TextParagraphTypeMonospace ( preCode . Language ) ,
@@ -607,6 +632,8 @@ public StyledParagraph(string text, int offset, int length, IList<TextEntity> en
607632 }
608633 }
609634
635+ public string Text { get ; }
636+
610637 public int Offset { get ; }
611638
612639 public int Length { get ; }
@@ -622,6 +649,50 @@ public StyledParagraph(string text, int offset, int length, IList<TextEntity> en
622649 public int Padding { get ; }
623650
624651 public TextParagraphType Type { get ; }
652+
653+ public bool IsDirty { get ; set ; } = true ;
654+
655+ private string _dynamicText ;
656+ private IList < TextStylePart > _dynamicParts ;
657+
658+ public IList < TextStylePart > GetParts ( out string text )
659+ {
660+ text = _dynamicText ?? Text ;
661+
662+ if ( _hasDates && IsDirty )
663+ {
664+ text = Text ;
665+
666+ var parts = new List < TextStylePart > ( ) ;
667+ var offset = 0 ;
668+
669+ foreach ( var entity in Runs )
670+ {
671+ if ( entity . Type is TextEntityTypeDateTime && entity . FormattedText != null )
672+ {
673+ text = text . Remove ( entity . Offset + offset , entity . Length ) ;
674+ text = text . Insert ( entity . Offset + offset , entity . FormattedText ) ;
675+
676+ offset += entity . FormattedText . Length - entity . Length ;
677+ }
678+ else if ( entity . Flags != TextStyle . None )
679+ {
680+ parts . Add ( new TextStylePart
681+ {
682+ Offset = entity . Offset + offset ,
683+ Length = entity . Length ,
684+ Type = entity . Flags
685+ } ) ;
686+ }
687+ }
688+
689+ _dynamicText = text ;
690+ _dynamicParts = parts ;
691+ IsDirty = false ;
692+ }
693+
694+ return _dynamicParts ?? Parts ;
695+ }
625696 }
626697
627698 public interface TextParagraphType
0 commit comments