Skip to content

Commit

Permalink
Run notice attachment download on background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabboxl committed Jul 30, 2023
1 parent b2348e5 commit 7802e0f
Showing 1 changed file with 56 additions and 39 deletions.
95 changes: 56 additions & 39 deletions ClassevivaPCTO/Dialogs/NoticeDialogContent.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public NoticeDialogContent(Notice notice, NoticeReadResult noticeReadResult)

AttachmentsListView.ItemsSource = notice.attachments;

App app = (App)App.Current;
App app = (App) App.Current;
var apiClient = app.Container.GetService<IClassevivaAPI>();

apiWrapper = PoliciesDispatchProxy<IClassevivaAPI>.CreateProxy(apiClient);
Expand All @@ -50,16 +50,25 @@ public NoticeDialogContent(Notice notice, NoticeReadResult noticeReadResult)
private async void ButtonOpen_Click(object sender, RoutedEventArgs e)
{
var senderbutton = sender as AppBarButton;
var currentAttachment = (NoticeAttachment)senderbutton.DataContext;
var currentAttachment = (NoticeAttachment) senderbutton.DataContext;

byte[] bytes = await GetAttachmentAsBytes(currentAttachment);
await Task.Run(async () =>
{
byte[] bytes = await GetAttachmentAsBytes(currentAttachment);
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
currentAttachment.fileName,
Windows.Storage.CreationCollisionOption.ReplaceExisting
);
await Windows.Storage.FileIO.WriteBytesAsync(file, bytes);
await Windows.System.Launcher.LaunchFileAsync(file);
//run on ui thread
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
currentAttachment.fileName,
Windows.Storage.CreationCollisionOption.ReplaceExisting
);
await Windows.Storage.FileIO.WriteBytesAsync(file, bytes);
var success = await Windows.System.Launcher.LaunchFileAsync(file);
});
});
}

private async void ButtonSave_Click(object sender, RoutedEventArgs e)
Expand All @@ -68,39 +77,46 @@ private async void ButtonSave_Click(object sender, RoutedEventArgs e)
var currentAttachment = senderbutton.DataContext as NoticeAttachment;


byte[] bytes = await GetAttachmentAsBytes(currentAttachment);

var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;

savePicker.FileTypeChoices.Add("Allegato", new List<string>() { "." });
savePicker.SuggestedFileName = currentAttachment.fileName;

Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
await Task.Run(async () =>
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.DeferUpdates(file);

//scrivo il file
await Windows.Storage.FileIO.WriteBytesAsync(file, bytes);
byte[] bytes = await GetAttachmentAsBytes(currentAttachment);
Windows.Storage.Provider.FileUpdateStatus status =
await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
//run on ui thread
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
//completato
}
else
{
//trakkar errore?
}
}
else
{
//we need to track the error
}
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("Allegato", new List<string>() {"."});
savePicker.SuggestedFileName = currentAttachment.fileName;
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.DeferUpdates(file);
//scrivo il file
await Windows.Storage.FileIO.WriteBytesAsync(file, bytes);
Windows.Storage.Provider.FileUpdateStatus status =
await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
{
//completato
}
else
{
//trakkar errore?
}
}
else
{
//we need to track the error
}
});
});
}


Expand All @@ -114,6 +130,7 @@ private async Task<byte[]> GetAttachmentAsBytes(NoticeAttachment attachment)
CurrentNotice.evtCode,
attachment.attachNum.ToString()
);

byte[] bytes = await attachmentBinary.Content.ReadAsByteArrayAsync();

return bytes;
Expand Down

0 comments on commit 7802e0f

Please sign in to comment.