Skip to content

Commit

Permalink
Run Maui update on MainThread
Browse files Browse the repository at this point in the history
  • Loading branch information
ipax77 committed Nov 15, 2024
1 parent 4d32203 commit d2dc387
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void Dispose()
dsstatsService.ScanStateChanged -= DssstatsService_ScanStateChanged;
dsstatsService.DecodeStateChanged -= DssstatsService_DecodeStateChanged;
NavigationManager.LocationChanged -= NavigationManager_LocationChanged;
updateService.UpdateProgress -= UpdateService_UpdateProgress;
}

public async Task CheckForUpdates(bool init = false)
Expand Down
36 changes: 26 additions & 10 deletions src/dsstats.maui/dsstats.maui8/Services/StoreUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,41 @@ public async Task<bool> UpdateApp()
{
try
{
var storeContext = StoreContext.GetDefault();
var result = await MainThread.InvokeOnMainThreadAsync(async () =>
{
var storeContext = StoreContext.GetDefault();
var availableUpdates = await storeContext.GetAppAndOptionalStorePackageUpdatesAsync();

var availableUpdates = await storeContext.GetAppAndOptionalStorePackageUpdatesAsync();
ArgumentNullException.ThrowIfNull(availableUpdates);
if (availableUpdates is null || availableUpdates.Count == 0)
{
logger.LogWarning("No updates available.");
return null;
}

var progress = new Progress<StorePackageUpdateStatus>(progress =>
OnUpdateProgress(new() { Progress = Convert.ToInt32(progress.PackageDownloadProgress * 100.0) }));
var progress = new Progress<StorePackageUpdateStatus>(progress =>
OnUpdateProgress(new() { Progress = Convert.ToInt32(progress.PackageDownloadProgress * 100.0) }));

var result = await storeContext.RequestDownloadStorePackageUpdatesAsync(availableUpdates)
.AsTask(progress);
return await storeContext.RequestDownloadStorePackageUpdatesAsync(availableUpdates)
.AsTask(progress);
});

if (result == null)
return false;

if (result.OverallState != StorePackageUpdateState.Completed)
{
var failedUpdates = result.StorePackageUpdateStatuses.Where(
status => status.PackageUpdateState != StorePackageUpdateState.Completed)
.ToList();
.ToList();

if (failedUpdates.Count > 0)
{
logger.LogError("app update failed: {errorlist}", string.Join(", ", failedUpdates.Select(s => s.PackageFamilyName)));
var errorDetails = failedUpdates.Select(s => new
{
PackageFamilyName = s.PackageFamilyName,
Error = s.PackageUpdateState
});
logger.LogError("App update failed for packages: {errorDetails}", string.Join(", ", errorDetails));
}
}
else
Expand All @@ -83,8 +98,9 @@ public async Task<bool> UpdateApp()
}
catch (Exception ex)
{
logger.LogError("app update failed: {error}", ex.Message);
logger.LogError("App update failed: {error}", ex);
}
return false;
}

}

0 comments on commit d2dc387

Please sign in to comment.