//called by ChatMessageService when receiving a post from server public void AddReceivedPostUI(PostItem item) { InvokeOnMainThread(() => { ChatBubbleViewExtension postBubble = null; //if posted by this account, then find pending post then undo transparency if (string.Equals(item.UserId, Settings.UserId)) { var bubbleArr = PendingPostBubbles.ToArray(); for (int c = 0; c < bubbleArr.Length; c++) { Console.WriteLine("Searching for matching post, iteration {0}", c); if (IsSamePostItem(bubbleArr[c].PostData, item)) { Console.WriteLine("Found possible matching post w same title and body, iteration {0} with body {1} and alpha {2}", c, bubbleArr[c].PostData.Body, bubbleArr[c].Alpha); if (IsPending(bubbleArr[c])) { Console.WriteLine("Found pending post, iteration {0} with body {1} and alpha {2}", c, bubbleArr[c].PostData.Body, bubbleArr[c].Alpha); bubbleArr[c].Alpha = 1.0f; Console.WriteLine("Set alpha to {0}", bubbleArr[c].Alpha); } //else? } else { Console.WriteLine("iteration {0}, not a match", c); } } } else { postBubble = CreateChatBubble(item, PostStatusType.Received2ndPartyPostFromServer); } ChatScrollView.ContentSize = new CGSize(ChatScrollView.ContentSize.Width, ChatBubbleY + TextField.Frame.Height + SmileyButton.Frame.Height + 100); }); }
//called by client app when user composes message public void AddLocalClientPostToUI(PostItem item, PostStatusType postStatus) { InvokeOnMainThread(() => { Console.WriteLine("Adding post, ChatBubbleY is {0}", ChatBubbleY); ChatBubbleViewExtension postBubble = CreateChatBubble(item, postStatus); if (postStatus == PostStatusType.SendingPost) { Console.WriteLine("Adding post w body {0} to pending list", item.Body); PendingPosts.Add(item); postBubble.Alpha = 0.5f; postBubble.PostData = item; PendingPostBubbles.Add(postBubble); } ChatScrollView.ContentSize = new CGSize(ChatScrollView.ContentSize.Width, ChatBubbleY + TextField.Frame.Height + SmileyButton.Frame.Height + 100); Console.WriteLine("Added post, ChatBubbleY is {0}", ChatBubbleY); }); }
ChatBubbleViewExtension CreateChatBubble(PostItem post, PostStatusType postStatus) { bool IsLocal = false; if (postStatus == PostStatusType.SendingPost || postStatus == PostStatusType.PreviousPost) { IsLocal = true; } Console.WriteLine("postStatus is {0}, IsLocal is {1}", postStatus.ToString(), IsLocal); //base measurements var verticalImageAdjustment = 5f; var ChatAreaSpacing = 10f; var ImageDimension = 22f; nfloat labelSpacing = 5; var ChatBubbleWidth = 200; //use DecideChatBubbleWidth( var ChatBubbleHeight = 100; //heightDiff *1.5f; //dependent measurements ChatBubbleY += ChatBubbleHeight + ChatAreaSpacing; labelWidth = ChatBubbleWidth - (ChatBubbleWidth - (labelSpacing * 2)); labelHeight = ChatBubbleHeight - (ChatBubbleHeight - (labelSpacing * 2)); var ImageAndBubbleFrame = new CGRect(0, ChatBubbleY, ChatScrollView.Frame.Width, ChatBubbleHeight); var ImageX = IsLocal ? ImageAndBubbleFrame.Width - ChatAreaSpacing - ImageDimension : ChatAreaSpacing; var ChatBubbleX = IsLocal ? ImageX - ChatBubbleWidth - ChatAreaSpacing : ImageDimension + (ChatAreaSpacing * 2); //profile pic var ProfileImageView = new UIImageView(); ProfileImageView.Image = UIImage.FromFile("profilepics/people.png"); ProfileImageView.ContentMode = UIViewContentMode.ScaleAspectFill; ProfileImageView.Frame = new CGRect(ImageX, verticalImageAdjustment, ImageDimension, ImageDimension); //chat bubble without the profile pic var ChatFrame = new CGRect(ChatBubbleX, 0, ChatBubbleWidth, ChatBubbleHeight); var ChatMessage = new UIView(); ChatMessage.BackgroundColor = UIColor.White; ChatMessage.Layer.CornerRadius = 12; ChatMessage.Frame = ChatFrame; //this is the username var TitleLabel = new UILabel(new CGRect(labelSpacing, labelSpacing, labelWidth, labelHeight)); TitleLabel.TextColor = UIColor.LightGray; TitleLabel.TextAlignment = UITextAlignment.Left; TitleLabel.Font = UIFont.SystemFontOfSize(14); TitleLabel.Text = post.Title; TitleLabel.SizeToFit(); //message content var BodyLabel = new UILabel(new CGRect(labelSpacing, TitleLabel.Frame.Height + (labelSpacing * 2), labelWidth, labelHeight)); BodyLabel.TextColor = UIColor.Black; BodyLabel.Text = post.Body; BodyLabel.Font = UIFont.SystemFontOfSize(15); BodyLabel.SizeToFit(); BodyLabel.Lines = 50; BodyLabel.LineBreakMode = UILineBreakMode.WordWrap; //time message was sent var TimeStampLabel = new UILabel(new CGRect(ChatBubbleWidth - (labelWidth * 4), ChatBubbleHeight - labelHeight - (labelSpacing * 2) , labelWidth, labelHeight)); TimeStampLabel.TextAlignment = UITextAlignment.Left; TimeStampLabel.Font = UIFont.SystemFontOfSize(12); TimeStampLabel.Text = string.Format("{0}:{1}", DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes); TimeStampLabel.TextColor = UIColor.LightGray; TimeStampLabel.SizeToFit(); //add message text into one bubble view ChatMessage.AddSubview(TitleLabel); ChatMessage.BringSubviewToFront(TitleLabel); ChatMessage.AddSubview(BodyLabel); ChatMessage.BringSubviewToFront(BodyLabel); ChatMessage.AddSubview(TimeStampLabel); ChatMessage.BringSubviewToFront(TimeStampLabel); ChatMessage.SizeToFit(); //add image and text bubble together //this is the chat bubble containing text and the profile pic var ImageAndBubbleViewContainer = new ChatBubbleViewExtension(); ImageAndBubbleViewContainer.BackgroundColor = UIColor.Clear; ImageAndBubbleViewContainer.Frame = ImageAndBubbleFrame; ImageAndBubbleViewContainer.AddSubview(ProfileImageView); ImageAndBubbleViewContainer.BringSubviewToFront(ProfileImageView); ImageAndBubbleViewContainer.AddSubview(ChatMessage); ImageAndBubbleViewContainer.BringSubviewToFront(ChatMessage); //add image and text bubble to chat window ChatScrollView.AddSubview(ImageAndBubbleViewContainer); ChatScrollView.BringSubviewToFront(ImageAndBubbleViewContainer); return(ImageAndBubbleViewContainer); }
bool IsPending(ChatBubbleViewExtension postBubble) { return(postBubble.Alpha < 1.0f); }