Пример #1
0
        private async void OnAddNewCategory(object obj)
        {
            IsBusy = true;

            try
            {
                // Thuc hien cong viec tai day
                var newCate = new VisualCategoryModel
                {
                    Id           = Guid.NewGuid().ToString(),
                    CategoryName = NewCategoryNameBindProp,
                    Status       = Status.New
                };
                ListCategoryBindProp.Add(newCate);
                NewCategoryNameBindProp = string.Empty;
            }
            catch (Exception e)
            {
                await ShowError(e);
            }
            finally
            {
                IsBusy = false;
            }
        }
Пример #2
0
        private async void OnSave(object obj)
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                // Thuc hien cong viec tai day
                if (IsEditing) //neu dang o che do chinh sua
                {
                    var categoryLogic = new CategoryLogic(_dbContext);
                    // Kiem tra them xoa sua
                    foreach (var category in ListCategoryBindProp)
                    {
                        switch (category.Status)
                        {
                        case Status.New:
                            var newCategory = new Category
                            {
                                Id           = category.Id,
                                CategoryName = category.CategoryName
                            };
                            await categoryLogic.CreateAsync(newCategory, false);

                            break;

                        case Status.Normal:
                            break;

                        case Status.Modified:
                            var modCategory = new Category
                            {
                                Id           = category.Id,
                                CategoryName = category.CategoryName
                            };
                            await categoryLogic.UpdateAsync(modCategory, false);

                            break;

                        case Status.Deleted:
                            break;

                        default:
                            break;
                        }
                        MessagingCenter.Send(category, Messages.CATEGORY_MESSAGE);
                    }

                    foreach (var cate in _listDeleted)
                    {
                        cate.Status = Status.Deleted;
                        await categoryLogic.DeleteAsync(cate.Id, false);

                        MessagingCenter.Send(cate, Messages.CATEGORY_MESSAGE);
                    }
                    //neu co nhap category moi thi tao roi quay ve
                    if (!string.IsNullOrWhiteSpace(NewCategoryNameBindProp))
                    {
                        var newCate = new Category
                        {
                            Id           = Guid.NewGuid().ToString(),
                            CategoryName = NewCategoryNameBindProp,
                        };
                        var newVsCate = new VisualCategoryModel
                        {
                            Id           = newCate.Id,
                            CategoryName = newCate.CategoryName,
                            Status       = Status.New
                        };
                        await categoryLogic.CreateAsync(newCate, false);

                        ListCategoryBindProp.Add(newVsCate);
                        MessagingCenter.Send(newCate, Messages.CATEGORY_MESSAGE);
                    }

                    await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                    await NavigationService.GoBackAsync();
                }
                else // neu dang o che do binh thuong
                {
                    var categoryLogic = new CategoryLogic(_dbContext);
                    // Kiem tra them moi
                    foreach (var category in ListCategoryBindProp)
                    {
                        switch (category.Status)
                        {
                        case Status.New:
                            var newCategory = new Category
                            {
                                Id           = category.Id,
                                CategoryName = category.CategoryName,
                            };
                            await categoryLogic.CreateAsync(newCategory, false);

                            MessagingCenter.Send(category, Messages.CATEGORY_MESSAGE);
                            category.Status = Status.Normal;
                            break;

                        case Status.Normal:
                            break;

                        case Status.Modified:
                            break;

                        case Status.Deleted:
                            break;
                        }
                    }

                    // neu khong nhap gi
                    if (string.IsNullOrWhiteSpace(NewCategoryNameBindProp))
                    {
                        //neu co chon thi tra ve ten category
                        if (SelectedCategoryBindProp != null)
                        {
                            //truyen ten category ve trang tao item
                            var param = new NavigationParameters();
                            param.Add(Keys.CATEGORY, SelectedCategoryBindProp);

                            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                            await NavigationService.GoBackAsync(param);
                        }
                        else
                        {
                            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                            return;
                        }
                    }
                    else // tao moi
                    {
                        var newCate = new Category
                        {
                            Id           = Guid.NewGuid().ToString(),
                            CategoryName = NewCategoryNameBindProp,
                        };
                        var newVsCate = new VisualCategoryModel
                        {
                            Id           = newCate.Id,
                            CategoryName = newCate.CategoryName,
                            Status       = Status.New
                        };
                        await categoryLogic.CreateAsync(newCate, false);

                        ListCategoryBindProp.Add(newVsCate);
                        MessagingCenter.Send(newCate, Messages.CATEGORY_MESSAGE);

                        await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                        //truyen ten category ve trang tao item
                        var param = new NavigationParameters();
                        param.Add(Keys.CATEGORY, newCate);

                        await NavigationService.GoBackAsync(param);
                    }
                }
            }
            catch (Exception e)
            {
                await ShowError(e);
            }
            finally
            {
                IsBusy = false;
            }
        }