Ejemplo n.º 1
0
        private static async Task <bool> ApplyCustomHandlers(SpannableStringBuilder builder, TagNode node, Func <TagNode, SpannableStringBuilder, Task <bool> > customHandler)
        {
            if (customHandler != null && await customHandler(node, builder))
            {
                return(true);
            }
            var start     = builder.Length();
            var parameter = node.AttributeValues.Count > 0 ? node.AttributeValues.Values.First() : null;

            switch (node.Tag.Name)
            {
            case "session":
                builder.Append(" " + parameter);
                builder.SetSpan(new ImageSpan(sessionIcon.Value), start, start + 1, SpanTypes.MarkMark);
                builder.SetSpan(new ClickableSpan(view => {
                    var channel        = parameter;
                    var channelManager = Mvx.GetSingleton <IChannelManager>();
                    if (channelManager.JoinedChannels.ContainsKey(channel))
                    {
                        ViewChannel(channel);
                        return;
                    }
                    NotifyCollectionChangedEventHandler listener = null;
                    listener = (sender, args) => {
                        if (args.Action != NotifyCollectionChangedAction.Add || args.NewItems.Cast <Channel>().All(x => x.Name != channel))
                        {
                            return;
                        }
                        ViewChannel(channel);
                        channelManager.JoinedChannels.CollectionChanged -= listener;
                    };
                    channelManager.JoinedChannels.CollectionChanged += listener;
                    channelManager.JoinChannel(channel);
                }), start + 1, builder.Length(), 0);
                return(true);

            case "icon":
            case "eicon":
                builder.Append(' ');
                var icon  = node.Tag.Name == "icon" ? Helpers.GetAvatar(node.ToText()) : $"https://static.f-list.net/images/eicon/{node.ToText()}.gif";
                var image = new BitmapDrawable(await Mvx.GetSingleton <IMvxImageCache <Bitmap> >().RequestImage(icon));
                var size  = MLHelpers.DpToPixelsInt(40);
                image.SetBounds(0, 0, size, size);
                builder.SetSpan(new ImageSpan(image), start, start + 1, 0);
                return(true);

            case "user":
                var name = node.ToText();
                builder.Append(" " + name);
                var character = Mvx.GetSingleton <ICharacterManager>().GetCharacter(name);
                builder.SetSpan(new ImageSpan(StatusIconConverter.GetDrawable(character.Status)), start, start + 1, SpanTypes.MarkMark);
                builder.SetSpan(new PureClickableSpan(view => {
                    Mvx.GetSingleton <IViewModelNavigator>().Show <CharacterViewModel, string>(character.Name);
                }), start, builder.Length(), 0);
                builder.SetSpan(new ForegroundColorSpan(new Color(CharacterViewModel.GetColor(character).ARGB)), start + 1, builder.Length(), 0);
                if (character.IsBookmarked || character.IsFriend)
                {
                    builder.SetSpan(new ColoredUnderlineSpan(unchecked ((int)0xff00cc00)), start + 1, builder.Length(), 0);
                }
                return(true);

            default:
                return(false);
            }
        }