forked from microsoft/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryItemViewModel.cpp
65 lines (56 loc) · 2.09 KB
/
HistoryItemViewModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "HistoryItemViewModel.h"
#include "Common/LocalizationService.h"
using namespace CalculatorApp::Common;
using namespace CalculatorApp::ViewModel;
using namespace std;
using namespace Platform;
HistoryItemViewModel::HistoryItemViewModel(String^ expression, String^ result,
_In_ const shared_ptr<CalculatorVector <pair<wstring, int>>> &spTokens,
_In_ const shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> &spCommands) :m_expression(expression), m_result(result), m_spTokens(spTokens), m_spCommands(spCommands)
{
// updating accessibility names for expression and result
m_accExpression = HistoryItemViewModel::GetAccessibleExpressionFromTokens(spTokens, m_expression);
m_accResult = LocalizationService::GetNarratorReadableString(m_result);
}
String^ HistoryItemViewModel::GetAccessibleExpressionFromTokens(_In_ shared_ptr< CalculatorVector< pair< wstring, int > > > const &spTokens, _In_ String^ fallbackExpression)
{
// updating accessibility names for expression and result
wstringstream accExpression{};
accExpression << L"";
unsigned int nTokens;
HRESULT hr = spTokens->GetSize(&nTokens);
if (SUCCEEDED(hr))
{
pair<wstring, int> tokenItem;
for (unsigned int i = 0; i < nTokens; i++)
{
hr = spTokens->GetAt(i, &tokenItem);
if (FAILED(hr))
{
break;
}
wstring token = tokenItem.first;
accExpression << LocalizationService::GetNarratorReadableToken(StringReference(token.c_str()))->Data();
}
}
if (SUCCEEDED(hr))
{
wstring expressionSuffix{};
hr = spTokens->GetExpressionSuffix(&expressionSuffix);
if (SUCCEEDED(hr))
{
accExpression << expressionSuffix;
}
}
if (FAILED(hr))
{
return LocalizationService::GetNarratorReadableString(fallbackExpression);
}
else
{
return ref new String(accExpression.str().c_str());
}
}