Remove dead code from History (#1228)

* Clean history

* clean unit tests

* cleaning

* Remove friend class in HistoryViewModel
This commit is contained in:
Rudy Huyn 2020-05-25 15:02:27 -07:00 committed by GitHub
parent c37f540265
commit 143907f21f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 46 additions and 440 deletions

View file

@ -232,7 +232,6 @@ task<void> ApplicationViewModel::HandleToggleAlwaysOnTop(float width, float heig
localSettings->Values->Insert(HeightLocalSettings, height);
bool success = co_await ApplicationView::GetForCurrentView()->TryEnterViewModeAsync(ApplicationViewMode::Default);
CalculatorViewModel->AreHistoryShortcutsEnabled = success;
CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = success;
CalculatorViewModel->IsAlwaysOnTop = !success;
IsAlwaysOnTop = !success;
@ -261,7 +260,6 @@ task<void> ApplicationViewModel::HandleToggleAlwaysOnTop(float width, float heig
}
bool success = co_await ApplicationView::GetForCurrentView()->TryEnterViewModeAsync(ApplicationViewMode::CompactOverlay, compactOptions);
CalculatorViewModel->AreHistoryShortcutsEnabled = !success;
CalculatorViewModel->HistoryVM->AreHistoryShortcutsEnabled = !success;
CalculatorViewModel->IsAlwaysOnTop = success;
IsAlwaysOnTop = success;

View file

@ -72,67 +72,6 @@ bool Utils::IsLastCharacterTarget(_In_ wstring const& input, _In_ wchar_t target
return !input.empty() && input.back() == target;
}
void Utils::SerializeCommandsAndTokens(
_In_ shared_ptr<vector<pair<wstring, int>>> const& tokens,
_In_ shared_ptr<vector<shared_ptr<IExpressionCommand>>> const& commands,
DataWriter ^ writer)
{
// Save the size of the commands vector
writer->WriteUInt32(static_cast<unsigned int>(commands->size()));
SerializeCommandVisitor cmdVisitor(writer);
for (const auto& exprCmd : *commands)
{
CalculationManager::CommandType commandType = exprCmd->GetCommandType();
writer->WriteInt32(static_cast<int>(commandType));
exprCmd->Accept(cmdVisitor);
}
writer->WriteUInt32(static_cast<unsigned int>(tokens->size()));
for (const auto& eachToken : *tokens)
{
auto stringData = ref new Platform::String(eachToken.first.c_str());
auto intData = eachToken.second;
writer->WriteUInt32(writer->MeasureString(stringData));
writer->WriteString(stringData);
writer->WriteInt32(intData);
}
}
const shared_ptr<vector<shared_ptr<IExpressionCommand>>> Utils::DeserializeCommands(DataReader ^ reader)
{
auto commandVector = make_shared<vector<shared_ptr<IExpressionCommand>>>();
auto commandVectorSize = reader->ReadUInt32();
CommandDeserializer cmdDeserializer(reader);
for (unsigned int i = 0; i < commandVectorSize; ++i)
{
auto commandTypeInt = reader->ReadInt32();
CalculationManager::CommandType commandType = static_cast<CalculationManager::CommandType>(commandTypeInt);
shared_ptr<IExpressionCommand> exprCmd = cmdDeserializer.Deserialize(commandType);
commandVector->push_back(exprCmd);
}
return commandVector;
}
const shared_ptr<vector<pair<wstring, int>>> Utils::DeserializeTokens(DataReader ^ reader)
{
auto tokenVector = make_shared<vector<pair<wstring, int>>>();
auto tokensSize = reader->ReadUInt32();
for (unsigned int i = 0; i < tokensSize; ++i)
{
auto stringDataLen = reader->ReadUInt32();
auto stringData = reader->ReadString(stringDataLen);
auto intData = reader->ReadInt32();
tokenVector->emplace_back(stringData->Data(), intData);
}
return tokenVector;
}
DateTime Utils::GetUniversalSystemTime()
{
SYSTEMTIME sysTime = {};

View file

@ -386,13 +386,6 @@ namespace Utils
double GetDoubleFromWstring(std::wstring input);
int GetWindowId();
void RunOnUIThreadNonblocking(std::function<void()>&& function, _In_ Windows::UI::Core::CoreDispatcher ^ currentDispatcher);
void SerializeCommandsAndTokens(
_In_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
_In_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands,
Windows::Storage::Streams::DataWriter ^ writer);
const std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> DeserializeCommands(Windows::Storage::Streams::DataReader ^ reader);
const std::shared_ptr<std::vector<std::pair<std::wstring, int>>> DeserializeTokens(Windows::Storage::Streams::DataReader ^ reader);
Windows::Foundation::DateTime GetUniversalSystemTime();
bool IsDateTimeOlderThan(Windows::Foundation::DateTime dateTime, const long long duration);

View file

@ -21,6 +21,7 @@ using namespace Windows::Security::Cryptography;
using namespace Windows::Foundation::Collections;
static StringReference HistoryVectorLengthKey{ L"HistoryVectorLength" };
static StringReference ItemsSizeKey{ L"ItemsCount" };
namespace CalculatorApp::ViewModel::HistoryResourceKeys
{
@ -34,7 +35,6 @@ HistoryViewModel::HistoryViewModel(_In_ CalculationManager::CalculatorManager* c
AreHistoryShortcutsEnabled = true;
Items = ref new Platform::Collections::Vector<HistoryItemViewModel ^>();
ItemSize = 0;
}
// this will reload Items with the history list based on current mode
@ -75,7 +75,7 @@ void HistoryViewModel::ReloadHistory(_In_ ViewMode currentMode)
}
Items = historyListVM;
UpdateItemSize();
RaisePropertyChanged(ItemsSizeKey);
}
void HistoryViewModel::OnHistoryItemAdded(_In_ unsigned int addedItemIndex)
@ -101,8 +101,7 @@ void HistoryViewModel::OnHistoryItemAdded(_In_ unsigned int addedItemIndex)
assert(addedItemIndex <= m_calculatorManager->MaxHistorySize() && addedItemIndex >= 0);
Items->InsertAt(0, item);
UpdateItemSize();
SaveHistory();
RaisePropertyChanged(ItemsSizeKey);
}
void HistoryViewModel::SetCalculatorDisplay(CalculatorDisplay& calculatorDisplay)
@ -115,7 +114,7 @@ void HistoryViewModel::ShowItem(_In_ HistoryItemViewModel ^ e)
{
unsigned int index;
Items->IndexOf(e, &index);
TraceLogger::GetInstance()->LogHistoryItemLoad((ViewMode)m_currentMode, ItemSize, (int)(index));
TraceLogger::GetInstance()->LogHistoryItemLoad((ViewMode)m_currentMode, Items->Size, (int)(index));
HistoryItemClicked(e);
}
@ -126,15 +125,8 @@ void HistoryViewModel::DeleteItem(_In_ HistoryItemViewModel ^ e)
{
if (m_calculatorManager->RemoveHistoryItem(itemIndex))
{
// Keys for the history container are index based.
// SaveHistory() re-inserts the items anyway, so it's faster to just clear out the container.
CalculationManager::CalculatorMode currentMode = m_currentMode;
ApplicationDataContainer ^ historyContainer = GetHistoryContainer(currentMode);
historyContainer->Values->Clear();
Items->RemoveAt(itemIndex);
UpdateItemSize();
SaveHistory();
RaisePropertyChanged(ItemsSizeKey);
}
}
}
@ -147,16 +139,14 @@ void HistoryViewModel::OnHideCommand(_In_ Platform::Object ^ e)
void HistoryViewModel::OnClearCommand(_In_ Platform::Object ^ e)
{
if (AreHistoryShortcutsEnabled == true)
if (AreHistoryShortcutsEnabled)
{
m_calculatorManager->ClearHistory();
if (Items->Size > 0)
{
CalculationManager::CalculatorMode currentMode = m_currentMode;
ClearHistoryContainer(currentMode);
Items->Clear();
UpdateItemSize();
RaisePropertyChanged(ItemsSizeKey);
}
if (m_localizedHistoryCleared == nullptr)
@ -167,210 +157,7 @@ void HistoryViewModel::OnClearCommand(_In_ Platform::Object ^ e)
}
}
// this method restores history vector per mode
void HistoryViewModel::RestoreHistory(_In_ CalculationManager::CalculatorMode cMode)
{
ApplicationDataContainer ^ historyContainer = GetHistoryContainer(cMode);
std::shared_ptr<std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>>> historyVector =
std::make_shared<std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>>>();
auto historyVectorLength = static_cast<int>(historyContainer->Values->Lookup(HistoryVectorLengthKey));
bool failure = false;
if (historyVectorLength > 0)
{
for (int i = 0; i < historyVectorLength; ++i)
{
try
{
// deserialize each item
auto item = DeserializeHistoryItem(i.ToString(), historyContainer);
std::shared_ptr<CalculationManager::HISTORYITEM> Item = std::make_shared<CalculationManager::HISTORYITEM>(item);
historyVector->push_back(Item);
}
catch (Platform::Exception ^ e)
{
failure = true;
break;
}
}
if (!failure)
{
// if task has been cancelled set history to 0
m_calculatorManager->SetHistory(cMode, *historyVector);
// update length once again for consistency between stored number of items and length
UpdateHistoryVectorLength(static_cast<int>(historyVector->size()), cMode);
}
else
{
// in case of failure do not show any item
UpdateHistoryVectorLength(0, cMode);
}
}
}
Platform::String ^ HistoryViewModel::GetHistoryContainerKey(_In_ CalculationManager::CalculatorMode cMode)
{
Platform::ValueType ^ modeValue = static_cast<int>(cMode);
return Platform::String::Concat(modeValue->ToString(), L"_History");
}
ApplicationDataContainer ^ HistoryViewModel::GetHistoryContainer(_In_ CalculationManager::CalculatorMode cMode)
{
ApplicationDataContainer ^ localSettings = ApplicationData::Current->LocalSettings;
ApplicationDataContainer ^ historyContainer;
// naming container based on mode
Platform::String ^ historyContainerKey = GetHistoryContainerKey(cMode);
if (localSettings->Containers->HasKey(historyContainerKey))
{
historyContainer = localSettings->Containers->Lookup(historyContainerKey);
}
else
{
// create container for adding data
historyContainer = localSettings->CreateContainer(historyContainerKey, ApplicationDataCreateDisposition::Always);
int initialHistoryVectorLength = 0;
historyContainer->Values->Insert(HistoryVectorLengthKey, initialHistoryVectorLength);
}
return historyContainer;
}
void HistoryViewModel::ClearHistoryContainer(_In_ CalculationManager::CalculatorMode cMode)
{
ApplicationDataContainer ^ localSettings = ApplicationData::Current->LocalSettings;
localSettings->DeleteContainer(GetHistoryContainerKey(cMode));
}
// this method will be used to update the history item length
void HistoryViewModel::UpdateHistoryVectorLength(_In_ int newValue, _In_ CalculationManager::CalculatorMode cMode)
{
ApplicationDataContainer ^ historyContainer = GetHistoryContainer(cMode);
historyContainer->Values->Remove(HistoryVectorLengthKey);
historyContainer->Values->Insert(HistoryVectorLengthKey, newValue);
}
void HistoryViewModel::ClearHistory()
{
ClearHistoryContainer(CalculationManager::CalculatorMode::Standard);
ClearHistoryContainer(CalculationManager::CalculatorMode::Scientific);
}
unsigned long long HistoryViewModel::GetMaxItemSize()
{
return static_cast<unsigned long long>(m_calculatorManager->MaxHistorySize());
}
void HistoryViewModel::SaveHistory()
{
ApplicationDataContainer ^ historyContainer = GetHistoryContainer(m_currentMode);
auto const& currentHistoryVector = m_calculatorManager->GetHistoryItems(m_currentMode);
bool failure = false;
int index = 0;
Platform::String ^ serializedHistoryItem;
for (auto const& item : currentHistoryVector)
{
try
{
serializedHistoryItem = SerializeHistoryItem(item);
historyContainer->Values->Insert(index.ToString(), serializedHistoryItem);
}
catch (Platform::Exception ^)
{
failure = true;
break;
}
++index;
}
if (!failure)
{
// insertion is successful
UpdateHistoryVectorLength(static_cast<int>(currentHistoryVector.size()), m_currentMode);
}
else
{
UpdateHistoryVectorLength(0, m_currentMode);
}
}
// this serializes a history item into a base64 encoded string
Platform::String ^ HistoryViewModel::SerializeHistoryItem(_In_ std::shared_ptr<CalculationManager::HISTORYITEM> const& item)
{
DataWriter ^ writer = ref new DataWriter();
auto expr = item->historyItemVector.expression;
auto result = item->historyItemVector.result;
auto platformExpr = ref new Platform::String(expr.c_str());
writer->WriteUInt32(writer->MeasureString(platformExpr));
writer->WriteString(platformExpr);
auto platformResult = ref new Platform::String(result.c_str());
writer->WriteUInt32(writer->MeasureString(platformResult));
writer->WriteString(platformResult);
Utils::SerializeCommandsAndTokens(item->historyItemVector.spTokens, item->historyItemVector.spCommands, writer);
IBuffer ^ buffer = writer->DetachBuffer();
if (buffer == nullptr)
{
throw ref new Platform::Exception(E_POINTER, ref new Platform::String(L"History Item is NULL"));
}
return CryptographicBuffer::EncodeToBase64String(buffer);
}
CalculationManager::HISTORYITEM
HistoryViewModel::DeserializeHistoryItem(_In_ Platform::String ^ historyItemKey, _In_ ApplicationDataContainer ^ historyContainer)
{
CalculationManager::HISTORYITEM historyItem;
if (historyContainer->Values->HasKey(historyItemKey))
{
Object ^ historyItemValues = historyContainer->Values->Lookup(historyItemKey);
if (historyItemValues == nullptr)
{
throw ref new Platform::Exception(E_POINTER, ref new Platform::String(L"History Item is NULL"));
}
String ^ historyData = safe_cast<Platform::String ^>(historyItemValues);
IBuffer ^ buffer = CryptographicBuffer::DecodeFromBase64String(historyData);
if (buffer == nullptr)
{
throw ref new Platform::Exception(E_POINTER, ref new Platform::String(L"History Item is NULL"));
}
DataReader ^ reader = DataReader::FromBuffer(buffer);
auto exprLen = reader->ReadUInt32();
auto expression = reader->ReadString(exprLen);
historyItem.historyItemVector.expression = expression->Data();
auto resultLen = reader->ReadUInt32();
auto result = reader->ReadString(resultLen);
historyItem.historyItemVector.result = result->Data();
historyItem.historyItemVector.spCommands = Utils::DeserializeCommands(reader);
historyItem.historyItemVector.spTokens = Utils::DeserializeTokens(reader);
}
else
{
throw ref new Platform::Exception(E_ACCESSDENIED, ref new Platform::String(L"History Item not found"));
}
return historyItem;
}
bool HistoryViewModel::IsValid(_In_ CalculationManager::HISTORYITEM item)
{
return (
!item.historyItemVector.expression.empty() && !item.historyItemVector.result.empty() && (bool)item.historyItemVector.spCommands
&& (bool)item.historyItemVector.spTokens);
}
void HistoryViewModel::UpdateItemSize()
{
ItemSize = Items->Size;
}

View file

@ -26,10 +26,16 @@ namespace CalculatorApp
{
public:
OBSERVABLE_OBJECT();
OBSERVABLE_PROPERTY_RW(int, ItemSize);
OBSERVABLE_PROPERTY_RW(Windows::UI::Xaml::Interop::IBindableObservableVector ^, Items);
OBSERVABLE_PROPERTY_R(Windows::Foundation::Collections::IObservableVector<HistoryItemViewModel ^> ^, Items);
OBSERVABLE_PROPERTY_RW(bool, AreHistoryShortcutsEnabled);
OBSERVABLE_PROPERTY_RW(CalculatorApp::Common::Automation::NarratorAnnouncement ^, HistoryAnnouncement);
OBSERVABLE_PROPERTY_R(CalculatorApp::Common::Automation::NarratorAnnouncement ^, HistoryAnnouncement);
property int ItemsCount
{
int get()
{
return Items->Size;
}
}
void OnHistoryItemAdded(_In_ unsigned int addedItemIndex);
@ -42,7 +48,6 @@ namespace CalculatorApp
event HideHistoryClickedHandler ^ HideHistoryClicked;
event HistoryItemClickedHandler ^ HistoryItemClicked;
void ShowItem(_In_ CalculatorApp::ViewModel::HistoryItemViewModel ^ e);
void ClearHistory();
internal : HistoryViewModel(_In_ CalculationManager::CalculatorManager* calculatorManager);
void SetCalculatorDisplay(CalculatorDisplay& calculatorDisplay);
@ -51,27 +56,11 @@ namespace CalculatorApp
void DeleteItem(_In_ CalculatorApp::ViewModel::HistoryItemViewModel ^ e);
// store history in app data functions
Platform::String ^ SerializeHistoryItem(_In_ std::shared_ptr<CalculationManager::HISTORYITEM> const& item);
void SaveHistory();
private:
CalculationManager::CalculatorManager* const m_calculatorManager;
CalculatorDisplay m_calculatorDisplay;
CalculationManager::CalculatorMode m_currentMode;
Platform::String ^ m_localizedHistoryCleared;
void RestoreHistory(_In_ CalculationManager::CalculatorMode cMode);
CalculationManager::HISTORYITEM
DeserializeHistoryItem(_In_ Platform::String ^ historyItemKey, _In_ Windows::Storage::ApplicationDataContainer ^ historyContainer);
Windows::Storage::ApplicationDataContainer ^ GetHistoryContainer(_In_ CalculationManager::CalculatorMode cMode);
Platform::String ^ GetHistoryContainerKey(_In_ CalculationManager::CalculatorMode cMode);
void ClearHistoryContainer(_In_ CalculationManager::CalculatorMode cMode);
void UpdateHistoryVectorLength(_In_ int newValue, _In_ CalculationManager::CalculatorMode cMode);
bool IsValid(_In_ CalculationManager::HISTORYITEM item);
friend class CalculatorDisplay;
void UpdateItemSize();
};
}
}

View file

@ -130,7 +130,6 @@ StandardCalculatorViewModel::StandardCalculatorViewModel()
IsOperandEnabled = true;
IsNegateEnabled = true;
IsDecimalEnabled = true;
AreHistoryShortcutsEnabled = true;
AreProgrammerRadixOperatorsEnabled = false;
}

View file

@ -76,7 +76,6 @@ namespace CalculatorApp
OBSERVABLE_PROPERTY_R(CalculatorApp::Common::NumberBase, CurrentRadixType);
OBSERVABLE_PROPERTY_R(bool, AreTokensUpdated);
OBSERVABLE_PROPERTY_R(bool, AreAlwaysOnTopResultsUpdated);
OBSERVABLE_PROPERTY_RW(bool, AreHistoryShortcutsEnabled);
OBSERVABLE_PROPERTY_R(bool, AreProgrammerRadixOperatorsEnabled);
OBSERVABLE_PROPERTY_R(bool, IsInputEmpty);
OBSERVABLE_PROPERTY_R(CalculatorApp::Common::Automation::NarratorAnnouncement ^, Announcement);