Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using MvvmCross.Binding.iOS.Views;
- using ILive.UIControls.iOS.Extensions.Anchors;
- using UIKit;
- using ILive.UIControls.iOS.Extensions;
- using ILive.Core;
- using ILive.UIControls.iOS;
- using Foundation;
- using CoreGraphics;
- using ILive.Core.Primitives.Chat;
- namespace ILive.iOS.ViewControllers.Chat
- {
- public class MessageBubble : MvxTableViewCell
- {
- public static readonly nfloat TextLeftPadding = 15;
- public static readonly nfloat TextTopPadding = 10;
- public static readonly nfloat SidePadding = 20;
- public static readonly nfloat AvatarSize = 30;
- public static readonly nfloat ElementPadding = 10;
- public static readonly nfloat BubbleRadius = 5;
- public static readonly nfloat StampWidth = 50;
- public bool IsInitialized = false;
- public ChatMessage Source { get; set; }
- public string Message
- {
- get => TextLabel?.Text ?? string.Empty;
- set => TextLabel.Text = value;
- }
- public UIView Bubble { get; private set; }
- new public UITextView TextLabel { get; private set; }
- public UIView StampView { get; private set; }
- public UILabel DateStampView { get; private set; }
- public UILabel TimeStampView { get; private set; }
- public UIActivityIndicatorView Indicator { get; private set; }
- public UIImageView AvatarView { get; set; }
- public MessageBubble(ChatMessage item, bool isSelfBubble = false)
- {
- Source = item;
- CreateView();
- IsInitialized = isSelfBubble ? CompleteSelfUI() : CompleteOtherUI();
- }
- void CreateView()
- {
- Bubble = new UIView().AddTo(ContentView);
- TextLabel = new UITextView().AddTo(Bubble);
- Bubble.SetCornerRadius(BubbleRadius)
- .JoinCenterY(ContentView);
- TextLabel.JoinTopOf(Bubble, TextTopPadding)
- .JoinBottomOf(Bubble, TextTopPadding)
- .JoinRightOf(Bubble, TextLeftPadding)
- .JoinLeftOf(Bubble, TextLeftPadding)
- .JoinHeight(0, AnchorType.GreaterThan);
- TextLabel.Set(v => v.TextContainerInset = UIEdgeInsets.Zero)
- .Set(v => v.TextContainer.LineFragmentPadding = 0)
- .Set(v => v.Font = UIFont.SystemFontOfSize(15, UIFontWeight.Regular))
- .SetBackgroundColor(UIColor.Clear)
- .Set(v => v.Text = Source.GetText());
- TextLabel.ScrollEnabled = false;
- TextLabel.Editable = false;
- StampView = new UIView().AddTo(ContentView);
- DateStampView = new UILabel().AddTo(StampView)
- .Set(v => v.Text = Source.TimeStamp.ToString(@"dd/MM/yy"))
- .Set(v => v.Font = UIFont.SystemFontOfSize(10, UIFontWeight.Light))
- .JoinTopOf(StampView)
- .JoinRightOf(StampView)
- .JoinLeftOf(StampView)
- .JoinWidth(StampWidth, AnchorType.GreaterThan)
- .JoinHeight(0, AnchorType.GreaterThan)
- .SetTextColor(Palette.TextLightGray);
- TimeStampView = new UILabel().AddTo(StampView)
- .Set(v => v.Text = Source.TimeStamp.ToString("HH:mm"))
- .Set(v => v.Font = UIFont.SystemFontOfSize(10, UIFontWeight.Light))
- .JoinBottomOf(DateStampView)
- .JoinBottomOf(StampView)
- .JoinRightOf(StampView)
- .JoinLeftOf(StampView)
- .JoinBottomOf(StampView)
- .JoinWidth(StampWidth, AnchorType.GreaterThan)
- .JoinHeight(0, AnchorType.GreaterThan)
- .SetTextColor(Palette.TextLightGray);
- if (Source.TimeStamp.Equals(default(DateTime)))
- {
- Indicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray).AddTo(StampView)
- .JoinCenterX(StampView)
- .JoinCenterY(StampView)
- .JoinHeight(StampView)
- .JoinWidthToHeight(StampView)
- .Set(v => v.StartAnimating())
- .Set(v => v.Layer.ZPosition = 1);
- TimeStampView.Hidden = true;
- DateStampView.Hidden = true;
- }
- }
- bool CompleteSelfUI()
- {
- Bubble.JoinRightOf(ContentView, SidePadding)
- .SetBackgroundColor(Palette.LightGray);
- StampView.JoinLeftOf(ContentView, SidePadding, AnchorType.GreaterThan)
- .JoinCenterY(Bubble)
- .JoinLeftOf(Bubble, ElementPadding);
- DateStampView.SetAligment(UITextAlignment.Right);
- TimeStampView.SetAligment(UITextAlignment.Right);
- return true;
- }
- bool CompleteOtherUI()
- {
- AvatarView = new UIImageView().AddTo(ContentView)
- .JoinWidth(AvatarSize)
- .JoinHeight(AvatarSize)
- .JoinLeftOf(ContentView, SidePadding)
- .JoinAnchors(v => v.BottomAnchor, Bubble.BottomAnchor)
- .SetCornerRadius(AvatarSize / 2f)
- .SetBackgroundColor(UIColor.Brown)
- .Set(v => v.ClipsToBounds = true);
- Bubble.JoinRightOf(AvatarView, ElementPadding)
- .SetBorder(1, Palette.LightGray.ToColor());
- StampView.JoinRightOf(ContentView, SidePadding, AnchorType.LessThan)
- .JoinCenterY(Bubble)
- .JoinRightOf(Bubble, ElementPadding);
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement