Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.72 KB | None | 0 0
  1. using System;
  2. using MvvmCross.Binding.iOS.Views;
  3. using ILive.UIControls.iOS.Extensions.Anchors;
  4. using UIKit;
  5. using ILive.UIControls.iOS.Extensions;
  6. using ILive.Core;
  7. using ILive.UIControls.iOS;
  8. using Foundation;
  9. using CoreGraphics;
  10. using ILive.Core.Primitives.Chat;
  11.  
  12. namespace ILive.iOS.ViewControllers.Chat
  13. {
  14.     public class MessageBubble : MvxTableViewCell
  15.     {
  16.         public static readonly nfloat TextLeftPadding = 15;
  17.         public static readonly nfloat TextTopPadding = 10;
  18.         public static readonly nfloat SidePadding = 20;
  19.         public static readonly nfloat AvatarSize = 30;
  20.         public static readonly nfloat ElementPadding = 10;
  21.         public static readonly nfloat BubbleRadius = 5;
  22.         public static readonly nfloat StampWidth = 50;
  23.  
  24.         public bool IsInitialized = false;
  25.  
  26.         public ChatMessage Source { get; set; }
  27.  
  28.         public string Message
  29.         {
  30.             get => TextLabel?.Text ?? string.Empty;
  31.             set => TextLabel.Text = value;
  32.         }
  33.  
  34.         public UIView Bubble { get; private set; }
  35.         new public UITextView TextLabel { get; private set; }
  36.  
  37.         public UIView StampView { get; private set; }
  38.         public UILabel DateStampView { get; private set; }
  39.         public UILabel TimeStampView { get; private set; }
  40.         public UIActivityIndicatorView Indicator { get; private set; }
  41.  
  42.         public UIImageView AvatarView { get; set; }
  43.  
  44.         public MessageBubble(ChatMessage item, bool isSelfBubble = false)
  45.         {
  46.             Source = item;
  47.  
  48.             CreateView();
  49.  
  50.             IsInitialized = isSelfBubble ? CompleteSelfUI() : CompleteOtherUI();
  51.         }
  52.  
  53.         void CreateView()
  54.         {
  55.             Bubble = new UIView().AddTo(ContentView);
  56.             TextLabel = new UITextView().AddTo(Bubble);
  57.  
  58.             Bubble.SetCornerRadius(BubbleRadius)
  59.                   .JoinCenterY(ContentView);
  60.  
  61.             TextLabel.JoinTopOf(Bubble, TextTopPadding)
  62.                      .JoinBottomOf(Bubble, TextTopPadding)
  63.                      .JoinRightOf(Bubble, TextLeftPadding)
  64.                      .JoinLeftOf(Bubble, TextLeftPadding)
  65.                      .JoinHeight(0, AnchorType.GreaterThan);
  66.  
  67.             TextLabel.Set(v => v.TextContainerInset = UIEdgeInsets.Zero)
  68.                      .Set(v => v.TextContainer.LineFragmentPadding = 0)
  69.                      .Set(v => v.Font = UIFont.SystemFontOfSize(15, UIFontWeight.Regular))
  70.                      .SetBackgroundColor(UIColor.Clear)
  71.                      .Set(v => v.Text = Source.GetText());
  72.  
  73.             TextLabel.ScrollEnabled = false;
  74.             TextLabel.Editable = false;
  75.  
  76.             StampView = new UIView().AddTo(ContentView);
  77.            
  78.             DateStampView = new UILabel().AddTo(StampView)
  79.                                          .Set(v => v.Text = Source.TimeStamp.ToString(@"dd/MM/yy"))
  80.                                          .Set(v => v.Font = UIFont.SystemFontOfSize(10, UIFontWeight.Light))
  81.                                          .JoinTopOf(StampView)
  82.                                          .JoinRightOf(StampView)
  83.                                          .JoinLeftOf(StampView)
  84.                                          .JoinWidth(StampWidth, AnchorType.GreaterThan)
  85.                                          .JoinHeight(0, AnchorType.GreaterThan)
  86.                                          .SetTextColor(Palette.TextLightGray);
  87.  
  88.             TimeStampView = new UILabel().AddTo(StampView)
  89.                                          .Set(v => v.Text = Source.TimeStamp.ToString("HH:mm"))
  90.                                          .Set(v => v.Font = UIFont.SystemFontOfSize(10, UIFontWeight.Light))
  91.                                          .JoinBottomOf(DateStampView)
  92.                                          .JoinBottomOf(StampView)
  93.                                          .JoinRightOf(StampView)
  94.                                          .JoinLeftOf(StampView)
  95.                                          .JoinBottomOf(StampView)
  96.                                          .JoinWidth(StampWidth, AnchorType.GreaterThan)
  97.                                          .JoinHeight(0, AnchorType.GreaterThan)
  98.                                          .SetTextColor(Palette.TextLightGray);
  99.  
  100.             if (Source.TimeStamp.Equals(default(DateTime)))
  101.             {
  102.                 Indicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray).AddTo(StampView)
  103.                                                                                           .JoinCenterX(StampView)
  104.                                                                                           .JoinCenterY(StampView)
  105.                                                                                           .JoinHeight(StampView)
  106.                                                                                           .JoinWidthToHeight(StampView)
  107.                                                                                           .Set(v => v.StartAnimating())
  108.                                                                                           .Set(v => v.Layer.ZPosition = 1);
  109.                 TimeStampView.Hidden = true;
  110.                 DateStampView.Hidden = true;
  111.             }
  112.         }
  113.  
  114.         bool CompleteSelfUI()
  115.         {
  116.             Bubble.JoinRightOf(ContentView, SidePadding)
  117.                   .SetBackgroundColor(Palette.LightGray);
  118.  
  119.             StampView.JoinLeftOf(ContentView, SidePadding, AnchorType.GreaterThan)
  120.                      .JoinCenterY(Bubble)
  121.                      .JoinLeftOf(Bubble, ElementPadding);
  122.  
  123.             DateStampView.SetAligment(UITextAlignment.Right);
  124.             TimeStampView.SetAligment(UITextAlignment.Right);
  125.  
  126.             return true;
  127.         }
  128.  
  129.         bool CompleteOtherUI()
  130.         {
  131.             AvatarView = new UIImageView().AddTo(ContentView)
  132.                                           .JoinWidth(AvatarSize)
  133.                                           .JoinHeight(AvatarSize)
  134.                                           .JoinLeftOf(ContentView, SidePadding)
  135.                                           .JoinAnchors(v => v.BottomAnchor, Bubble.BottomAnchor)
  136.                                           .SetCornerRadius(AvatarSize / 2f)
  137.                                           .SetBackgroundColor(UIColor.Brown)
  138.                                           .Set(v => v.ClipsToBounds = true);
  139.  
  140.             Bubble.JoinRightOf(AvatarView, ElementPadding)
  141.                   .SetBorder(1, Palette.LightGray.ToColor());
  142.  
  143.             StampView.JoinRightOf(ContentView, SidePadding, AnchorType.LessThan)
  144.                      .JoinCenterY(Bubble)
  145.                      .JoinRightOf(Bubble, ElementPadding);
  146.  
  147.             return true;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement