Exemplo n.º 1
0
        private async void OnAddNewZone(object obj)
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                // Thuc hien cong viec tai day
                var zoneToCreate = new ZoneDto {
                    Name = "New Zone"
                };
                var json    = JsonConvert.SerializeObject(zoneToCreate);
                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 + "zones/", content);

                    if (response.IsSuccessStatusCode)
                    {
                        var zone = JsonConvert.DeserializeObject <ZoneDto>(await response.Content.ReadAsStringAsync());
                        ListZoneBindProp.Add(zone);
                    }
                };
            }
            catch (Exception e)
            {
                await ShowErrorAsync(e);
            }
            finally
            {
                IsBusy = false;
            }
        }
Exemplo n.º 2
0
        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;
            }
        }