Skip to content

Commit

Permalink
Run scrutinio 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 7802e0f commit 52ac445
Showing 1 changed file with 56 additions and 44 deletions.
100 changes: 56 additions & 44 deletions ClassevivaPCTO/Controls/ScrutiniListView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed partial class ScrutiniListView : UserControl

public ScrutiniDocumentsResult ItemsSource
{
get { return (ScrutiniDocumentsResult)GetValue(ItemsSourceProperty); }
get { return (ScrutiniDocumentsResult) GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}

Expand All @@ -37,7 +37,7 @@ public ScrutiniDocumentsResult ItemsSource
DependencyPropertyChangedEventArgs e
)
{
ScrutiniListView currentInstance = (ScrutiniListView)d;
ScrutiniListView currentInstance = (ScrutiniListView) d;

var newValue = e.NewValue as ScrutiniDocumentsResult;

Expand All @@ -63,7 +63,7 @@ public ScrutiniListView()
{
this.InitializeComponent();

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

apiWrapper = PoliciesDispatchProxy<IClassevivaAPI>.CreateProxy(apiClient);
Expand All @@ -74,60 +74,72 @@ private async void ButtonOpen_Click(object sender, RoutedEventArgs e)
var senderbutton = sender as Button;
var currentScrutinio = (senderbutton.DataContext as ScrutinioAdapter).CurrentObject;

await Task.Run(async () =>
{
var getFileResult = await GetScrutinioFileAsBytes(currentScrutinio);
var getFileResult = await GetScrutinioFileAsBytes(currentScrutinio);

byte[] bytes = getFileResult.Item1;
byte[] bytes = getFileResult.Item1;
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
getFileResult.Item2,
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(
getFileResult.Item2,
Windows.Storage.CreationCollisionOption.ReplaceExisting
);
await Windows.Storage.FileIO.WriteBytesAsync(file, bytes);
await Windows.System.Launcher.LaunchFileAsync(file);
});
});
}

private async void ButtonSave_Click(object sender, RoutedEventArgs e)
{
var senderbutton = sender as Button;
var currentScrutinio = (senderbutton.DataContext as ScrutinioAdapter).CurrentObject;


var getFileResult = await GetScrutinioFileAsBytes(currentScrutinio);

byte[] bytes = getFileResult.Item1;

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

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

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);
var getFileResult = await GetScrutinioFileAsBytes(currentScrutinio);
//scrivo il file
await Windows.Storage.FileIO.WriteBytesAsync(file, bytes);
byte[] bytes = getFileResult.Item1;
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 = getFileResult.Item2;
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 Down

0 comments on commit 52ac445

Please sign in to comment.