Skip to content

Commit

Permalink
Merge pull request #223 from Zagrthos/fix/issue92
Browse files Browse the repository at this point in the history
Fix app crash when no internet connection is available
  • Loading branch information
beeradmoore authored Aug 25, 2024
2 parents fce8144 + 4bb6b7f commit 32dba7b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
54 changes: 28 additions & 26 deletions src/Data/DLSSRecord.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using DLSS_Swapper.Extensions;
using System;
using System.Collections.Generic;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using DLSS_Swapper.Extensions;

namespace DLSS_Swapper.Data
{
Expand Down Expand Up @@ -114,7 +109,6 @@ public string DisplayName
}
}


[JsonIgnore]
public LocalRecord LocalRecord { get; set; }

Expand All @@ -128,13 +122,14 @@ public int CompareTo(DLSSRecord other)
return other.VersionNumber.CompareTo(VersionNumber);
}


#region INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

#endregion

private CancellationTokenSource _cancellationTokenSource;
Expand All @@ -149,25 +144,39 @@ internal void CancelDownload()
{
var dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();

if (String.IsNullOrEmpty(DownloadUrl))
if (string.IsNullOrEmpty(DownloadUrl))
{
return (false, "Invalid download URL.", false);
}

_cancellationTokenSource?.Cancel();

LocalRecord.IsDownloading = true;
LocalRecord.DownloadProgress = 0;
LocalRecord.HasDownloadError = false;
LocalRecord.DownloadErrorMessage = String.Empty;
NotifyPropertyChanged("LocalRecord");

_cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _cancellationTokenSource.Token;
var response = await App.CurrentApp.HttpClient.GetAsync(DownloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
if (response.StatusCode != System.Net.HttpStatusCode.OK)

HttpResponseMessage response;
try
{
response = await App.CurrentApp.HttpClient.GetAsync(DownloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

LocalRecord.IsDownloading = true;
LocalRecord.DownloadProgress = 0;
NotifyPropertyChanged("LocalRecord");
}
catch (HttpRequestException ex)
{
Logger.Error(ex.Message);

LocalRecord.IsDownloading = false;
LocalRecord.HasDownloadError = true;
LocalRecord.DownloadErrorMessage = "Could not download DLSS. Please check your internet connection!";
NotifyPropertyChanged("LocalRecord");

return (false, "Could not download DLSS. Please check your internet connection!", false);
}

if (response.StatusCode is not System.Net.HttpStatusCode.OK)
{
dispatcherQueue.TryEnqueue(() =>
{
LocalRecord.IsDownloading = false;
Expand All @@ -185,8 +194,6 @@ internal void CancelDownload()
var buffer = new byte[1024 * 8];
var isMoreToRead = true;



var guid = Guid.NewGuid().ToString().ToUpper();

var tempPath = Storage.GetTemp();
Expand All @@ -206,7 +213,7 @@ internal void CancelDownload()
{
var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

if (bytesRead == 0)
if (bytesRead is 0)
{
isMoreToRead = false;
continue;
Expand All @@ -216,7 +223,6 @@ internal void CancelDownload()

totalBytesRead += bytesRead;


if ((DateTimeOffset.Now - lastUpdated).TotalMilliseconds > 100)
{
lastUpdated = DateTimeOffset.Now;
Expand Down Expand Up @@ -247,11 +253,8 @@ internal void CancelDownload()
NotifyPropertyChanged("LocalRecord");
});



File.Move(tempZipFile, Path.Combine(targetZipDirectory, $"{Version}_{MD5Hash}.zip"), true);


dispatcherQueue.TryEnqueue(() =>
{
LocalRecord.IsDownloaded = true;
Expand All @@ -272,7 +275,6 @@ internal void CancelDownload()
NotifyPropertyChanged("LocalRecord");
});


return (false, String.Empty, true);
}
catch (Exception err)
Expand Down
3 changes: 2 additions & 1 deletion src/Pages/LibraryPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ async Task DeleteRecordAsync(DLSSRecord record)
async Task DownloadRecordAsync(DLSSRecord record)
{
var result = await record?.DownloadAsync();
if (result.Success == false && result.Cancelled == false)
if (result.Success is false && result.Cancelled is false)
{
var dialog = new EasyContentDialog(XamlRoot)
{
Expand All @@ -666,6 +666,7 @@ async Task DownloadRecordAsync(DLSSRecord record)
DefaultButton = ContentDialogButton.Close,
Content = result.Message,
};

await dialog.ShowAsync();
}
}
Expand Down

0 comments on commit 32dba7b

Please sign in to comment.