public async void DownloadItem(ResourceItem item) { var client = new HttpClient(); var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; CancellationTokens[item.id] = cancellationTokenSource; try { Progress <double> progress = new Progress <double>(); progress.ProgressChanged += (sender, value) => { ReportProgress(item, value); }; var tempFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder; var tempFile = await tempFolder.CreateFileAsync(GuidHelper.CreateNewGuid().ToString() + ".zip"); var response = await client.GetAsync(item.item, HttpCompletionOption.ResponseHeadersRead, cancellationToken); response.EnsureSuccessStatusCode(); var total = response.Content.Headers.ContentLength.HasValue ? response.Content.Headers.ContentLength.Value : -1L; var canReportProgress = total != -1 && progress != null; using (var stream = await response.Content.ReadAsStreamAsync()) using (var outputStream = await tempFile.OpenStreamForWriteAsync()) { var totalRead = 0L; var buffer = new byte[4096]; var isMoreToRead = true; do { cancellationToken.ThrowIfCancellationRequested(); var read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); if (read == 0) { isMoreToRead = false; } else { var data = new byte[read]; buffer.ToList().CopyTo(0, data, 0, read); totalRead += read; await outputStream.WriteAsync(data, 0, read); if (canReportProgress) { ((IProgress <double>)progress).Report((totalRead * 1d) / (total * 1d)); } } } while (isMoreToRead); } var destinationPath = ItemPath(item); ZipFile.ExtractToDirectory(tempFile.Path, destinationPath); File.WriteAllText(destinationPath + "\\description.json", JsonConvert.SerializeObject(item)); CancellationTokens.Remove(item.id); DownloadSuccess(item); } catch { CancellationTokens.Remove(item.id); DownloadFailure(item); } }
public AddSpare() { this.InitializeComponent(); Guid.Text = GuidHelper.CreateNewGuid().ToString(); }
private void GenerateGuidBtn_OnClick(object sender, RoutedEventArgs e) => Guid.Text = GuidHelper.CreateNewGuid().ToString();
private async void Submit_Click(object sender, RoutedEventArgs e) { // alert user ContentDialogResult result = await new ConfirmationDialog().EnqueueAndShowIfAsync(); if (result == ContentDialogResult.Primary) { if (order == null) { await new MistakeDialog().EnqueueAndShowIfAsync(); } else { try { var completion = true; var isZero = true; using (var context = new Context()) { var dic = (await context.Dic.AddAsync(new Dic() { Id = GuidHelper.CreateNewGuid(), OrderId = order.Id, Status = DicStatusEnum.Dispatching.ToString() })).Entity; var orderProducts = context.OrderProduct.Include(x => x.Product).Where(x => x.OrderId == order.Id); foreach (var orderProduct in orderProducts) { // dispatched items uint dispatched = (uint)context.Did.Include(x => x.Dic).Where(x => x.Dic.OrderId == orderProduct.OrderId && x.ProductId == orderProduct.ProductId).Sum(x => x.Quantity); uint quantity = 0; // if the product of order is not equeueing completely, still missing something to dispatch if ((quantity = orderProduct.Quantity - dispatched) > 0) { var product = orderProduct.Product; // total (sold + existign) spare count uint sprcnt = (uint)context.Spare.Include(x => x.Category).Count(x => x.Category.ProductId == orderProduct.ProductId); // sold spare count // uint didsprcnt = (uint)context.DidSpare.Include(x => x.Did).Count(x => x.Did.ProductId == orderProduct.ProductId); uint didcnt = (uint)context.Did.Where(x => x.ProductId == orderProduct.ProductId).Sum(x => x.Quantity); uint remaining = 0; // only non-danger level product could be added on did if ((remaining = sprcnt - didcnt) > product.DangerLevel) { uint real; // if remaining stock does not able to complete the ordered quantity if (remaining < quantity) { // move all the remaining stock to roder. real = remaining; // the order is not completed. completion = false; } else { real = quantity; } // if there has at least 1 DID isZero = false; // add the did into database await context.Did.AddAsync( new Did() { Id = GuidHelper.CreateNewGuid(), DicId = dic.Id, ProductId = orderProduct.ProductId, Quantity = real, } ); } else { // system ignored order because of insufficient stocking and reached danger-level completion = false; } } } // if there has at least 1 DID and 1 DIC if (!isZero) { await context.SaveChangesAsync(); } } // if the dic exists on database if (!isZero) { if (!completion) { await new ContentDialog() { Title = "ALERT", Content = "Some of ordered items might be missing because of the system has no sufficient stock to supply.", CloseButtonText = "OK", Width = 400 }.EnqueueAndShowIfAsync(); } NotificationManager.CreateNotification(order.DealerId, "An Order Is Being Processing", $"{SignInManager.CurrentUser.FirstName} {SignInManager.CurrentUser.LastName} has decided to process one of your order requests.", NotificationTypeEnum.Dic, order.Id); await new SuccessDialog().EnqueueAndShowIfAsync(); Frame.Navigate(typeof(ViewOrderDispatchStatus), order, new DrillInNavigationTransitionInfo()); } else { await new ContentDialog() { Title = "ALERT", Content = "Because of insufficient stock supplyment, there has no order could be requested to be delivered.", CloseButtonText = "OK", Width = 400 }.EnqueueAndShowIfAsync(); } } catch (Exception) { await new ErrorDialog().EnqueueAndShowIfAsync(); } } } }