public override void OnBindViewHolder(RecyclerView.ViewHolder vh, int position)
        {
            try
            {
                int type = GetItemViewType(position);
                var item = MessageList[type];
                if (item == null)
                {
                    return;
                }
                switch (item.MessageType)
                {
                case "text":
                {
                    TextViewHolder holder = vh as TextViewHolder;
                    LoadTextOfChatItem(holder, position, item);
                    break;
                }

                case "media":
                {
                    ImageViewHolder holder = vh as ImageViewHolder;
                    LoadImageOfChatItem(holder, position, item);
                    break;
                }

                case "sticker" when item.Sticker.Contains(".gif"):
                {
                    GifViewHolder holder = vh as GifViewHolder;
                    LoadGifOfChatItem(holder, position, item);
                    break;
                }

                case "sticker":
                {
                    StickerViewHolder holder = vh as StickerViewHolder;
                    LoadStickerOfChatItem(holder, position, item);
                    break;
                }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
Ejemplo n.º 2
0
        private void Initialize(LastChatAdapterViewHolder holder, GetConversationListObject.DataConversation item)
        {
            try
            {
                GlideImageLoader.LoadImage(ActivityContext, item.User.Avater, holder.ImageAvatar, ImageStyle.CircleCrop, ImagePlaceholders.Drawable);

                string name = QuickDateTools.GetNameFinal(item.User);
                if (holder.TxtUsername.Text != name)
                {
                    holder.TxtUsername.Text = name;
                }

                //If message contains Media files
                switch (item.MessageType)
                {
                case "text":
                {
                    holder.LastMessagesIcon.Visibility = ViewStates.Gone;
                    holder.TxtLastMessages.Text        = item.Text.Contains("http")
                            ? Methods.FunString.SubStringCutOf(item.Text, 30)
                            : Methods.FunString.DecodeString(Methods.FunString.SubStringCutOf(item.Text, 30))
                                                         ?? ActivityContext.GetText(Resource.String.Lbl_SendMessage);
                    break;
                }

                case "media":
                {
                    holder.LastMessagesIcon.Visibility = ViewStates.Visible;
                    FontUtils.SetTextViewIcon(FontsIconFrameWork.IonIcons, holder.LastMessagesIcon, IonIconsFonts.Images);
                    holder.TxtLastMessages.Text = Application.Context.GetText(Resource.String.Lbl_SendImageFile);
                    break;
                }

                case "sticker" when item.Sticker.Contains(".gif"):
                {
                    holder.LastMessagesIcon.Visibility = ViewStates.Visible;
                    FontUtils.SetTextViewIcon(FontsIconFrameWork.FontAwesomeLight, holder.LastMessagesIcon, FontAwesomeIcon.Gift);
                    holder.TxtLastMessages.Text = Application.Context.GetText(Resource.String.Lbl_SendGifFile);
                    break;
                }

                case "sticker":
                {
                    holder.LastMessagesIcon.Visibility = ViewStates.Visible;
                    FontUtils.SetTextViewIcon(FontsIconFrameWork.IonIcons, holder.LastMessagesIcon, IonIconsFonts.Happy);
                    holder.TxtLastMessages.Text = Application.Context.GetText(Resource.String.Lbl_SendStickerFile);
                    break;
                }
                }

                //last seen time
                holder.TxtTimestamp.Text = Methods.Time.ReplaceTime(item.Time);

                //Check read message
                if (item.FromId != null && item.ToId != null && item.ToId != UserDetails.UserId && item.FromId == UserDetails.UserId)
                {
                    if (item.Seen == 0)
                    {
                        holder.ImageColor.Visibility = ViewStates.Invisible;
                        holder.TxtUsername.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                        holder.TxtLastMessages.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                    }
                    else
                    {
                        holder.ImageColor.Visibility = ViewStates.Invisible;
                        holder.TxtUsername.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                        holder.TxtLastMessages.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                    }
                }
                else if (item.FromId != null && item.ToId != null && item.ToId == UserDetails.UserId && item.FromId != UserDetails.UserId)
                {
                    if (item.Seen == 0)
                    {
                        holder.TxtUsername.SetTypeface(Typeface.Default, TypefaceStyle.Bold);
                        holder.TxtLastMessages.SetTypeface(Typeface.Default, TypefaceStyle.Bold);

                        if (item.NewMessages != 0)
                        {
                            var drawable = TextDrawable.InvokeBuilder().BeginConfig().FontSize(25).EndConfig().BuildRound(item.NewMessages.ToString(), Color.ParseColor(AppSettings.MainColor));
                            holder.ImageColor.SetImageDrawable(drawable);
                            holder.ImageColor.Visibility = ViewStates.Visible;
                        }
                        else
                        {
                            holder.ImageColor.Visibility = ViewStates.Invisible;
                        }
                    }
                    else
                    {
                        holder.ImageColor.Visibility = ViewStates.Invisible;
                        holder.TxtUsername.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                        holder.TxtLastMessages.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 3
0
        private static void UpdateLastIdMessage(SendMessageObject messages, UserInfoObject userData, string hashId)
        {
            try
            {
                var checker = MessagesBoxActivity.MAdapter?.MessageList?.FirstOrDefault(a => a.Id == Convert.ToInt32(hashId));
                if (checker != null)
                {
                    checker.Id          = messages.Data.Id;
                    checker.FromName    = UserDetails.FullName;
                    checker.FromAvater  = UserDetails.Avatar;
                    checker.ToName      = userData?.Fullname ?? "";
                    checker.ToAvater    = userData?.Avater ?? "";
                    checker.From        = messages.Data.From;
                    checker.To          = messages.Data.To;
                    checker.Text        = Methods.FunString.DecodeString(messages.Data.Text);
                    checker.Media       = checker.Media.Contains("upload/chat/") && !messages.Data.Media.Contains(Client.WebsiteUrl)? Client.WebsiteUrl + "/" + messages.Data.Media: messages.Data.Media;
                    checker.FromDelete  = messages.Data.FromDelete;
                    checker.ToDelete    = messages.Data.ToDelete;
                    checker.Sticker     = messages.Data.Sticker;
                    checker.CreatedAt   = messages.Data.CreatedAt;
                    checker.Seen        = messages.Data.Seen;
                    checker.Type        = "Sent";
                    checker.MessageType = messages.Data.MessageType;

                    string text = Methods.FunString.DecodeString(messages.Data.Text);

                    switch (checker.MessageType)
                    {
                    case "text":
                    {
                        text = string.IsNullOrEmpty(text) ? Application.Context.GetText(Resource.String.Lbl_SendMessage) : Methods.FunString.DecodeString(messages.Data.Text);
                        break;
                    }

                    case "media":
                    {
                        text = Application.Context.GetText(Resource.String.Lbl_SendImageFile);
                        break;
                    }

                    case "sticker" when checker.Sticker.Contains(".gif"):
                    {
                        text = Application.Context.GetText(Resource.String.Lbl_SendGifFile);
                        break;
                    }

                    case "sticker":
                    {
                        text = Application.Context.GetText(Resource.String.Lbl_SendStickerFile);
                        break;
                    }
                    }

                    var dataUser = LastChatActivity.MAdapter?.UserList?.FirstOrDefault(a => a.User.Id == messages.Data.To);
                    if (dataUser != null)
                    {
                        var index = LastChatActivity.MAdapter?.UserList?.IndexOf(LastChatActivity.MAdapter.UserList?.FirstOrDefault(x => x.User.Id == messages.Data.To));
                        if (index > -1)
                        {
                            dataUser.Text = text;

                            LastChatActivity.MAdapter?.UserList?.Move(Convert.ToInt32(index), 0);
                            LastChatActivity.MAdapter?.NotifyItemMoved(Convert.ToInt32(index), 0);

                            var data = LastChatActivity.MAdapter?.UserList?.FirstOrDefault(a => a.User.Id == dataUser.User.Id);
                            if (data != null)
                            {
                                data.Id          = dataUser.Id;
                                data.Owner       = dataUser.Owner;
                                data.User        = dataUser.User;
                                data.Seen        = dataUser.Seen;
                                data.Text        = dataUser.Text;
                                data.Media       = dataUser.Media;
                                data.Sticker     = dataUser.Sticker;
                                data.Time        = dataUser.Time;
                                data.CreatedAt   = dataUser.CreatedAt;
                                data.NewMessages = dataUser.NewMessages;
                                data.MessageType = dataUser.MessageType;

                                LastChatActivity.MAdapter.NotifyItemChanged(LastChatActivity.MAdapter.UserList.IndexOf(data));
                            }
                        }
                    }
                    else
                    {
                        if (userData != null)
                        {
                            LastChatActivity.MAdapter?.UserList?.Insert(0, new GetConversationListObject.DataConversation()
                            {
                                Id          = userData.Id,
                                Owner       = 0,
                                User        = userData,
                                Seen        = 1,
                                Text        = text,
                                Media       = checker.Media,
                                Sticker     = messages.Data.Sticker,
                                Time        = messages.Data.CreatedAt,
                                CreatedAt   = messages.Data.CreatedAt,
                                NewMessages = 0
                            });

                            LastChatActivity.MAdapter?.NotifyItemInserted(0);
                        }
                    }

                    SqLiteDatabase dbDatabase = new SqLiteDatabase();
                    GetChatConversationsObject.Messages message = new GetChatConversationsObject.Messages
                    {
                        Id          = messages.Data.Id,
                        FromName    = UserDetails.FullName,
                        FromAvater  = UserDetails.Avatar,
                        ToName      = userData?.Fullname ?? "",
                        ToAvater    = userData?.Avater ?? "",
                        From        = messages.Data.From,
                        To          = messages.Data.To,
                        Text        = Methods.FunString.DecodeString(messages.Data.Text),
                        Media       = checker.Media,
                        FromDelete  = messages.Data.FromDelete,
                        ToDelete    = messages.Data.ToDelete,
                        Sticker     = messages.Data.Sticker,
                        CreatedAt   = messages.Data.CreatedAt,
                        Seen        = messages.Data.Seen,
                        Type        = "Sent",
                        MessageType = messages.Data.MessageType,
                    };
                    //Update All data users to database
                    dbDatabase.InsertOrUpdateToOneMessages(message);
                    dbDatabase.Dispose();

                    MessagesBoxActivity.UpdateOneMessage(message);
                    MessagesBoxActivity.GetInstance()?.ChatBoxRecyclerView.ScrollToPosition(MessagesBoxActivity.MAdapter.MessageList.IndexOf(MessagesBoxActivity.MAdapter.MessageList.Last()));

                    if (AppSettings.RunSoundControl)
                    {
                        Methods.AudioRecorderAndPlayer.PlayAudioFromAsset("Popup_SendMesseges.mp3");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }