From 32597416d0ce4bfe5fa985f6d161017e2a93788c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tian=20Liao=20=E2=98=95?= Date: Mon, 20 May 2024 02:53:49 +0000 Subject: [PATCH] Merged PR 10778666: Recall | Show error dialog if launching from snapshot has failed ## What Per Figma design, we can show an error dialog with messages for the failures happens during snapshot launch. ## Notes - Fixed a crash about taking a snapshot when Calculator hasn't initialized a standard calculator. - Simplified the restore path. ## Screenshot ![image.png](https://dev.azure.com/microsoft/d1a24475-535d-4c83-988a-9491e6dbc858/_apis/git/repositories/6255259e-4ead-4d8d-b165-55eeacc5ca48/pullRequests/10778666/attachments/image.png) Related work items: #50858262 --- src/CalcViewModel/ApplicationViewModel.cpp | 3 +- .../StandardCalculatorViewModel.cpp | 2 +- src/Calculator/Resources/en-US/Resources.resw | 14 ++++- src/Calculator/Views/MainPage.xaml.cs | 59 ++++++++----------- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/CalcViewModel/ApplicationViewModel.cpp b/src/CalcViewModel/ApplicationViewModel.cpp index 16358f97..3b831645 100644 --- a/src/CalcViewModel/ApplicationViewModel.cpp +++ b/src/CalcViewModel/ApplicationViewModel.cpp @@ -710,8 +710,9 @@ Windows::Data::Json::JsonObject ^ ApplicationViewModel::SaveApplicationSnapshot( ApplicationSnapshot applicationSnapshot; applicationSnapshot.SnapshotVersion = SnapshotHelper::SnapshotVersion; applicationSnapshot.Mode = static_cast(Mode); - if (NavCategory::IsCalculatorViewMode(m_mode) && m_CalculatorViewModel != nullptr) + if (m_CalculatorViewModel != nullptr && m_mode == ViewMode::Standard) { + // Standard calculator is the only supported mode so far. applicationSnapshot.StandardCalc = m_CalculatorViewModel->GetStandardCalculatorSnapshot(); } return SnapshotHelper::SaveSnapshotToJson(applicationSnapshot); diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index e1a0a12c..42546906 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -1786,7 +1786,7 @@ void StandardCalculatorViewModel::SetBitshiftRadioButtonCheckedAnnouncement(Plat StandardCalculatorSnapshot StandardCalculatorViewModel::GetStandardCalculatorSnapshot() const { StandardCalculatorSnapshot snapshot; - auto historyItems = m_standardCalculatorManager.GetHistoryItems(); + auto& historyItems = m_standardCalculatorManager.GetHistoryItems(); if (!historyItems.empty()) { snapshot.CalcManager.HistoryItems = std::move(historyItems); diff --git a/src/Calculator/Resources/en-US/Resources.resw b/src/Calculator/Resources/en-US/Resources.resw index aee01e98..4e374ac5 100644 --- a/src/Calculator/Resources/en-US/Resources.resw +++ b/src/Calculator/Resources/en-US/Resources.resw @@ -1773,7 +1773,7 @@ J An abbreviation for a measurement unit of energy - + kWh An abbreviation for a measurement unit of electricity consumption @@ -2145,7 +2145,7 @@ Joules A measurement unit for energy. (Please choose the most appropriate plural form to fit any number between 0 and 999,999,999,999,999) - + Kilowatt-hours A measurement unit for electricity consumption. (Please choose the most appropriate plural form to fit any number between 0 and 999,999,999,999,999) @@ -4754,4 +4754,12 @@ Open the context menu for available actions Screen reader prompt for the context menu of the expression box - + + OK + The text of OK button to dismiss an error dialog. + + + Couldn't restore this snapshot. + The error message to notify user that restoring from snapshot has failed. + + \ No newline at end of file diff --git a/src/Calculator/Views/MainPage.xaml.cs b/src/Calculator/Views/MainPage.xaml.cs index c5d4eebf..829af697 100644 --- a/src/Calculator/Views/MainPage.xaml.cs +++ b/src/Calculator/Views/MainPage.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Threading.Tasks; using Windows.ApplicationModel.UserActivities; using Windows.Data.Json; @@ -21,9 +22,11 @@ using CalculatorApp.ViewModel; using CalculatorApp.ViewModel.Common; using CalculatorApp.ViewModel.Common.Automation; +using wuxc = Windows.UI.Xaml.Controls; + namespace CalculatorApp { - public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page + public sealed partial class MainPage : wuxc.Page { public static readonly DependencyProperty NavViewCategoriesSourceProperty = DependencyProperty.Register(nameof(NavViewCategoriesSource), typeof(List), typeof(MainPage), new PropertyMetadata(default)); @@ -165,42 +168,19 @@ namespace CalculatorApp { var channel = UserActivityChannel.GetDefault(); var activity = await channel.GetOrCreateUserActivityAsync(snapshotArgs.ActivityId); - if (!snapshotArgs.VerifyIncomingActivity(activity)) + + // Work around for bug https://microsoft.visualstudio.com/DefaultCollection/OS/_workitems/edit/48931227 + // where ContentInfo can't be directly accessed. + if (snapshotArgs.VerifyIncomingActivity(activity) && + JsonObject.TryParse(activity.ToJson(), out var activityJson) && + activityJson.ContainsKey("contentInfo") && + Model.TryRestoreFromSnapshot(activityJson.GetNamedObject("contentInfo"))) { - // something's going wrong with the activity - // TODO: show error dialog - return; + SelectNavigationItemByModel(); } else { - if (JsonObject.TryParse(activity.ToJson(), out var activityJson)) - { - try - { - // Work around for bug https://microsoft.visualstudio.com/DefaultCollection/OS/_workitems/edit/48931227 where ContentInfo can't be directly accessed. - var contentJson = activityJson.GetNamedObject("contentInfo"); - if (Model.TryRestoreFromSnapshot(contentJson)) - { - SelectNavigationItemByModel(); - } - else - { - // TODO: show error dialog - return; - } - } - catch (Exception ex) - { - // TODO: show error dialog - return; - } - } - else - { - // data corrupted - // TODO: show error dialog - return; - } + await ShowSnapshotLaunchErrorAsync(); } }); Model.Initialize(initialMode); @@ -681,6 +661,19 @@ namespace CalculatorApp CloseSettingsPopup(); } + private async Task ShowSnapshotLaunchErrorAsync() + { + var resProvider = AppResourceProvider.GetInstance(); + var dialog = new wuxc.ContentDialog + { + Title = resProvider.GetResourceString("AppName"), + Content = new wuxc.TextBlock { Text = resProvider.GetResourceString("SnapshotRestoreError") }, + CloseButtonText = resProvider.GetResourceString("ErrorButtonOk"), + DefaultButton = wuxc.ContentDialogButton.Close + }; + await dialog.ShowAsync(); + } + private Calculator m_calculator; private GraphingCalculator m_graphingCalculator; private UnitConverter m_converter;