Exemple #1
0
        public async Task InsertComment(FurniturePostItemComment comment)
        {
            var post = await this.GetById(comment.PostItemId);

            // nguoi nay chua co trong danh asch
            if (post.UserComments.Any(x => x == comment.CreatedById) == false)
            {
                post.UserComments.Add(comment.CreatedById);
                _postItems.ReplaceOne(p => p.Id == post.Id, post);
            }
            _postItemComments.InsertOne(comment);
        }
        private async void SendComment_Clicked(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(viewModel.CommentText) && viewModel.CommentPosts.Any() == false)
            {
                await DisplayAlert("", Language.nhap_binh_luan_hoac_chon_tin_dang_de_gui_binh_luan, Language.dong);

                return;
            }

            loadingPopup.IsVisible = true;

            FurniturePostItemComment comment = new FurniturePostItemComment();

            if (!string.IsNullOrWhiteSpace(viewModel.CommentText))
            {
                comment.HasText = true;
                comment.Text    = viewModel.CommentText.Trim();
            }

            comment.PostItemId  = viewModel.PostItem.Id;
            comment.CreatedDate = DateTime.Now;
            comment.CreatedById = UserLogged.Id;
            comment.CreatedBy   = new PostItemUser() // them cho nay de giao dien co. ko luu db.
            {
                UserId   = UserLogged.Id,
                FullName = UserLogged.FullName,
                Avatar   = UserLogged.AvatarUrl // neu la lnk full thi avatar full url tra ve nguyen ven.
            };
            if (viewModel.CommentPosts.Any())
            {
                comment.HasProduct = true;
                comment.Products   = viewModel.CommentPosts.ToList();
            }

            viewModel.InsertComment(comment);
            viewModel.Comments.Insert(0, comment);


            CancelCommnet_Clicked(sender, EventArgs.Empty);
            EditorComment.Unfocus();

            loadingPopup.IsVisible = false;


            try
            {
                INotificationService notificationService = DependencyService.Get <INotificationService>();
                var AllUserReceiveNoti = await viewModel._postItemService.GetReceiveNotificationUser(viewModel.PostItem.Id);

                string[] CommentUserIds = AllUserReceiveNoti.Where(x => x != UserLogged.Id).ToArray();

                string NotificationTitle = UserLogged.FullName + Language.da_binh_luan_trong_bai_viet;
                string NotificationImage = (viewModel.PostItem.Images != null && viewModel.PostItem.Images.Length > 0) ? AvatarHelper.GetPostAvatar(viewModel.PostItem.Images.FirstOrDefault()) : "";
                foreach (var userIdComment in CommentUserIds)
                {
                    Guid ReceiverId = Guid.Parse(userIdComment);
                    NotificationModel notification = new NotificationModel()
                    {
                        UserId            = ReceiverId,
                        CurrentBadgeCount = (int)notificationService.CountNotReadNotificationUser(ReceiverId) + 1,
                        Title             = NotificationTitle,
                        NotificationType  = NotificationType.VIewFurniturePostItem,
                        PostItemId        = viewModel.PostItem.Id,
                        CreatedDate       = DateTime.Now,
                        IsRead            = false,
                        Thumbnail         = NotificationImage
                    };
                    await notificationService.AddNotification(notification, Language.binh_luan);
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("", Language.co_loi_khi_gui_thong_bao, Language.dong);
            }
        }