From 6cb166b8ade729f08f1303434758250d186146f6 Mon Sep 17 00:00:00 2001 From: eric langlois Date: Mon, 1 Aug 2022 17:27:04 -0700 Subject: [PATCH] AppNotificationBuilder - Code Improvements (#2801) * Addressing nits from a previous PR * Uniform naming for tests * Addressing PR feedback * IsWindows... is the standard method for checking Windows version * nit Co-authored-by: Eric Langlois --- .../AppNotificationBuilder.cpp | 67 ++++++++++--------- .../AppNotificationBuilderUtility.h | 25 ++----- .../AppNotificationButton.cpp | 36 +++++----- .../AppNotificationButton.h | 8 +-- .../AppNotificationTextProperties.cpp | 4 +- .../AppNotificationTextProperties.h | 2 +- test/AppNotificationBuilderTests/APITests.cpp | 14 ++-- 7 files changed, 70 insertions(+), 86 deletions(-) diff --git a/dev/AppNotifications/AppNotificationBuilder/AppNotificationBuilder.cpp b/dev/AppNotifications/AppNotificationBuilder/AppNotificationBuilder.cpp index a0d150629c..53933dab18 100644 --- a/dev/AppNotifications/AppNotificationBuilder/AppNotificationBuilder.cpp +++ b/dev/AppNotifications/AppNotificationBuilder/AppNotificationBuilder.cpp @@ -10,6 +10,7 @@ #include #include #include +#include using namespace winrt::Windows::Globalization; using namespace winrt::Windows::Globalization::DateTimeFormatting; @@ -18,12 +19,12 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation { bool AppNotificationBuilder::IsUrgentScenarioSupported() { - return GetBuildNumber() >= 19041; + return WindowsVersion::IsWindows10_20H1OrGreater(); } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::AddArgument(hstring const& key, hstring const& value) { - THROW_HR_IF_MSG(E_INVALIDARG, key.empty(), "You must provide a key when adding an argument."); + THROW_HR_IF_MSG(E_INVALIDARG, key.empty(), "You must provide a key when adding an argument"); m_arguments.Insert(key, value); return *this; @@ -32,13 +33,13 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetTimeStamp(winrt::Windows::Foundation::DateTime const& value) { auto seconds{ winrt::clock::to_time_t(value) }; - struct tm buf; + struct tm buf{}; gmtime_s(&buf, &seconds); std::wstringstream buffer; buffer << std::put_time(&buf, L"%FT%T"); - m_timeStamp = wil::str_printf(L" displayTimestamp='%wsZ'", buffer.str().c_str()); + m_timeStamp = wil::str_printf(L" displayTimestamp='%lsZ'", buffer.str().c_str()); return *this; } @@ -56,18 +57,18 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::AddText(hstring const& text) { - THROW_HR_IF_MSG(E_INVALIDARG, m_textLines.size() >= c_maxTextElements, "Maximum number of text elements added."); + THROW_HR_IF_MSG(E_INVALIDARG, m_textLines.size() >= c_maxTextElements, "Maximum number of text elements added"); - m_textLines.push_back(wil::str_printf(L"%ws", text.c_str()).c_str()); + m_textLines.push_back(wil::str_printf(L"%ls", text.c_str()).c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::AddText(hstring const& text, AppNotificationTextProperties const& properties) { - THROW_HR_IF_MSG(E_INVALIDARG, m_textLines.size() >= c_maxTextElements, "Maximum number of text elements added."); + THROW_HR_IF_MSG(E_INVALIDARG, m_textLines.size() >= c_maxTextElements, "Maximum number of text elements added"); std::wstring props{ properties.as().ToString() }; - m_textLines.push_back(wil::str_printf(L"%ws%ws", props.c_str(), text.c_str()).c_str()); + m_textLines.push_back(wil::str_printf(L"%ls%ls", props.c_str(), text.c_str()).c_str()); if (properties.IncomingCallAlignment()) { @@ -78,21 +79,21 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetAttributionText(hstring const& text) { - m_attributionText = wil::str_printf(L"%ws", text.c_str()); + m_attributionText = wil::str_printf(L"%ls", text.c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetAttributionText(hstring const& text, hstring const& language) { - THROW_HR_IF_MSG(E_INVALIDARG, language.empty(), "You must provide a language calling SetAttributionText."); + THROW_HR_IF_MSG(E_INVALIDARG, language.empty(), "You must provide a language calling SetAttributionText"); - m_attributionText = wil::str_printf(L"%ws", language.c_str(), text.c_str()); + m_attributionText = wil::str_printf(L"%ls", language.c_str(), text.c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetInlineImage(winrt::Windows::Foundation::Uri const& imageUri) { - m_inlineImage = wil::str_printf(L"", imageUri.ToString().c_str()); + m_inlineImage = wil::str_printf(L"", imageUri.ToString().c_str()); return *this; } @@ -100,7 +101,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation { if (imageCrop == AppNotificationImageCrop::Circle) { - m_inlineImage = wil::str_printf(L"", imageUri.ToString().c_str()); + m_inlineImage = wil::str_printf(L"", imageUri.ToString().c_str()); } else { @@ -112,17 +113,17 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetInlineImage(winrt::Windows::Foundation::Uri const& imageUri, AppNotificationImageCrop const& imageCrop, hstring const& alternateText) { - THROW_HR_IF_MSG(E_INVALIDARG, alternateText.empty(), "You must provide an alternate text string calling SetInlineImage."); + THROW_HR_IF_MSG(E_INVALIDARG, alternateText.empty(), "You must provide an alternate text string calling SetInlineImage"); std::wstring hintCrop { imageCrop == AppNotificationImageCrop::Circle ? L" hint-crop='circle'" : L"" }; - m_inlineImage = wil::str_printf(L"%ws", imageUri.ToString().c_str(), alternateText.c_str(), hintCrop.c_str()); + m_inlineImage = wil::str_printf(L"%ls", imageUri.ToString().c_str(), alternateText.c_str(), hintCrop.c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetAppLogoOverride(winrt::Windows::Foundation::Uri const& imageUri) { - m_appLogoOverride = wil::str_printf(L"", imageUri.ToString().c_str()); + m_appLogoOverride = wil::str_printf(L"", imageUri.ToString().c_str()); return *this; } @@ -130,7 +131,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation { if (imageCrop == AppNotificationImageCrop::Circle) { - m_appLogoOverride = wil::str_printf(L"", imageUri.ToString().c_str()); + m_appLogoOverride = wil::str_printf(L"", imageUri.ToString().c_str()); } else { @@ -142,49 +143,49 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetAppLogoOverride(winrt::Windows::Foundation::Uri const& imageUri, AppNotificationImageCrop const& imageCrop, hstring const& alternateText) { - THROW_HR_IF_MSG(E_INVALIDARG, alternateText.empty(), "You must provide an alternate text string calling SetAppLogoOverride."); + THROW_HR_IF_MSG(E_INVALIDARG, alternateText.empty(), "You must provide an alternate text string calling SetAppLogoOverride"); std::wstring hintCrop{ imageCrop == AppNotificationImageCrop::Circle ? L" hint-crop='circle'" : L"" }; - m_appLogoOverride = wil::str_printf(L"%ws", imageUri.ToString().c_str(), alternateText.c_str(), hintCrop.c_str()); + m_appLogoOverride = wil::str_printf(L"%ls", imageUri.ToString().c_str(), alternateText.c_str(), hintCrop.c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetHeroImage(winrt::Windows::Foundation::Uri const& imageUri) { - m_heroImage = wil::str_printf(L"", imageUri.ToString().c_str()); + m_heroImage = wil::str_printf(L"", imageUri.ToString().c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetHeroImage(winrt::Windows::Foundation::Uri const& imageUri, hstring const& alternateText) { - THROW_HR_IF_MSG(E_INVALIDARG, alternateText.empty(), "You must provide an alternate text string calling SetHeroImage."); + THROW_HR_IF_MSG(E_INVALIDARG, alternateText.empty(), "You must provide an alternate text string calling SetHeroImage"); - m_heroImage = wil::str_printf(L"%ws", imageUri.ToString().c_str(), alternateText.c_str()); + m_heroImage = wil::str_printf(L"%ls", imageUri.ToString().c_str(), alternateText.c_str()); return *this; } winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder AppNotificationBuilder::SetAudioUri(winrt::Windows::Foundation::Uri const& audioUri) { - m_audio = wil::str_printf(L"