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; } }
private async void OnAddNewCategory(object obj) { if (IsBusy) { return; } IsBusy = true; try { // Thuc hien cong viec tai day var categoryToCreate = new CategoryDto { Name = "New Category", Icon = FontAwesomeIcon.Coffee }; var json = JsonConvert.SerializeObject(categoryToCreate); var content = new StringContent(json, Encoding.UTF8, "application/json"); // Thuc hien cong viec tai day using (var client = new HttpClient()) { HttpResponseMessage response = new HttpResponseMessage(); response = await client.PostAsync(Properties.Resources.BaseUrl + "categories/", content); if (response.IsSuccessStatusCode) { var category = JsonConvert.DeserializeObject <CategoryDto>(await response.Content.ReadAsStringAsync()); ListCategoryBindProp.Add(category); } }; } catch (Exception e) { await ShowErrorAsync(e); } finally { IsBusy = false; } }
public async override void OnNavigatedTo(INavigationParameters parameters) { switch (parameters.GetNavigationMode()) { case NavigationMode.Back: if (parameters.ContainsKey("item")) { var item = parameters["item"] as ItemForInvoiceDto; if (InvoiceBindProp == null) { InvoiceBindProp = new InvoiceDto(); } InvoiceBindProp.Items.Add(item); InvoiceBindProp.TotalPrice += item.Value; } if (parameters.ContainsKey(nameof(InvoiceBindProp))) { var invoice = parameters[nameof(InvoiceBindProp)] as InvoiceDto; await _connection.InvokeAsync("DeleteInvoice", InvoiceBindProp.Id); InvoiceBindProp = null; TempInvoiceBindProp = null; } break; case NavigationMode.New: using (var client = new HttpClient()) { var categoryTask = client.GetAsync(Properties.Resources.BaseUrl + "categories/"); var discountTask = client.GetAsync(Properties.Resources.BaseUrl + "discounts/"); var invoiceTask = client.GetAsync(Properties.Resources.BaseUrl + "invoices/"); var zoneTask = client.GetAsync(Properties.Resources.BaseUrl + "zones/"); var allTasks = new List <Task> { categoryTask, discountTask, invoiceTask, zoneTask }; while (allTasks.Any()) { Task finished = await Task.WhenAny(allTasks); if (finished == categoryTask) { if (categoryTask.Result.IsSuccessStatusCode) { var categories = JsonConvert.DeserializeObject <IEnumerable <CategoryDto> >(await categoryTask.Result.Content.ReadAsStringAsync()); foreach (var category in categories) { ListCategoryBindProp.Add(category); } } else { await PageDialogService.DisplayAlertAsync("Lỗi", $"{await categoryTask.Result.Content.ReadAsStringAsync()}", "Đóng"); } } else if (finished == discountTask) { if (discountTask.Result.IsSuccessStatusCode) { var discounts = JsonConvert.DeserializeObject <IEnumerable <DiscountDto> >(await discountTask.Result.Content.ReadAsStringAsync()); foreach (var discount in discounts) { ListDiscountBindProp.Add(discount); } } else { await PageDialogService.DisplayAlertAsync("Lỗi", $"{await discountTask.Result.Content.ReadAsStringAsync()}", "Đóng"); } } else if (finished == invoiceTask) { if (invoiceTask.Result.IsSuccessStatusCode) { var invoices = JsonConvert.DeserializeObject <IEnumerable <InvoiceDto> >(await invoiceTask.Result.Content.ReadAsStringAsync()); foreach (var invoice in invoices) { ListInvoiceBindProp.Add(invoice); } } else { await PageDialogService.DisplayAlertAsync("Lỗi", $"{await invoiceTask.Result.Content.ReadAsStringAsync()}", "Đóng"); } } else if (finished == zoneTask) { if (zoneTask.Result.IsSuccessStatusCode) { var zones = JsonConvert.DeserializeObject <IEnumerable <ZoneDto> >(await zoneTask.Result.Content.ReadAsStringAsync()); foreach (var zone in zones) { ListZoneBindProp.Add(zone); } } else { await PageDialogService.DisplayAlertAsync("Lỗi", $"{await zoneTask.Result.Content.ReadAsStringAsync()}", "Đóng"); } } allTasks.Remove(finished); } var id = Xamarin.Essentials.Preferences.Get("token", "token"); var response = await client.GetAsync(Properties.Resources.BaseUrl + "users/" + id); if (response.IsSuccessStatusCode) { var user = JsonConvert.DeserializeObject <UserDto>(await response.Content.ReadAsStringAsync()); CurrentUserBindProp = user; } } break; case NavigationMode.Forward: break; case NavigationMode.Refresh: break; default: break; } }
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; } }