stash changes

This commit is contained in:
Tian Liao 2024-10-25 13:38:40 +08:00
commit bfb6682360
5 changed files with 291 additions and 81 deletions

View file

@ -11,12 +11,13 @@
namespace CalculatorApp::ViewModel::Snapshot namespace CalculatorApp::ViewModel::Snapshot
{ {
UnaryCommand::UnaryCommand(Windows::Foundation::Collections::IVectorView<int> ^ cmds) UnaryCommand::UnaryCommand()
{ {
for (auto cmd : cmds)
{
m_cmds.push_back(cmd);
} }
UnaryCommand::UnaryCommand(std::vector<int> cmds)
: m_cmds(std::move(cmds))
{
} }
Windows::Foundation::Collections::IVectorView<int> ^ UnaryCommand::Commands::get() Windows::Foundation::Collections::IVectorView<int> ^ UnaryCommand::Commands::get()
@ -24,22 +25,64 @@ namespace CalculatorApp::ViewModel::Snapshot
return ref new Platform::Collections::VectorView<int>(m_cmds); return ref new Platform::Collections::VectorView<int>(m_cmds);
} }
OperandCommand::OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, Windows::Foundation::Collections::IVectorView<int> ^ cmds) void UnaryCommand::Commands::set(Windows::Foundation::Collections::IVectorView<int> ^ commands)
{ {
IsNegative = isNegative; m_cmds.clear();
IsDecimalPresent = isDecimal; for (auto cmd : commands)
IsSciFmt = isSciFmt;
for (auto cmd : cmds)
{ {
m_cmds.push_back(cmd); m_cmds.push_back(cmd);
} }
} }
BinaryCommand::BinaryCommand()
{
Command = 0;
}
BinaryCommand::BinaryCommand(int cmd)
{
Command = cmd;
}
OperandCommand::OperandCommand()
{
IsNegative = false;
IsDecimalPresent = false;
IsSciFmt = false;
}
OperandCommand::OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, std::vector<int> cmds)
{
IsNegative = isNegative;
IsDecimalPresent = isDecimal;
IsSciFmt = isSciFmt;
m_cmds = std::move(cmds);
}
Windows::Foundation::Collections::IVectorView<int> ^ OperandCommand::Commands::get() Windows::Foundation::Collections::IVectorView<int> ^ OperandCommand::Commands::get()
{ {
return ref new Platform::Collections::VectorView<int>(m_cmds); return ref new Platform::Collections::VectorView<int>(m_cmds);
} }
void OperandCommand::Commands::set(Windows::Foundation::Collections::IVectorView<int> ^ commands)
{
m_cmds.clear();
for (auto cmd : commands)
{
m_cmds.push_back(cmd);
}
}
Parentheses::Parentheses()
{
Command = 0;
}
Parentheses::Parentheses(int cmd)
{
Command = cmd;
}
ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd) { ICalcManagerIExprCommand ^ CreateExprCommand(const IExpressionCommand* exprCmd) {
switch (exprCmd->GetCommandType()) switch (exprCmd->GetCommandType())
{ {
@ -78,6 +121,12 @@ namespace CalculatorApp::ViewModel::Snapshot
} }
} }
CalcManagerHistoryToken::CalcManagerHistoryToken()
{
OpCodeName = nullptr;
CommandIndex = 0;
}
CalcManagerHistoryToken::CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex) CalcManagerHistoryToken::CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex)
{ {
assert(opCodeName != nullptr && "opCodeName is mandatory."); assert(opCodeName != nullptr && "opCodeName is mandatory.");
@ -85,6 +134,14 @@ namespace CalculatorApp::ViewModel::Snapshot
CommandIndex = cmdIndex; CommandIndex = cmdIndex;
} }
CalcManagerHistoryItem::CalcManagerHistoryItem()
{
Tokens = ref new Platform::Collections::Vector<CalcManagerHistoryToken ^>();
Commands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
Expression = ref new Platform::String();
Result = ref new Platform::String();
}
CalcManagerHistoryItem::CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item) CalcManagerHistoryItem::CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item)
{ {
Tokens = ref new Platform::Collections::Vector<CalcManagerHistoryToken ^>(); Tokens = ref new Platform::Collections::Vector<CalcManagerHistoryToken ^>();
@ -103,6 +160,11 @@ namespace CalculatorApp::ViewModel::Snapshot
Result = ref new Platform::String(item.historyItemVector.result.c_str()); Result = ref new Platform::String(item.historyItemVector.result.c_str());
} }
CalcManagerSnapshot::CalcManagerSnapshot()
{
HistoryItems = nullptr;
}
CalcManagerSnapshot::CalcManagerSnapshot(const CalculationManager::CalculatorManager& calcMgr) CalcManagerSnapshot::CalcManagerSnapshot(const CalculationManager::CalculatorManager& calcMgr)
{ {
auto& items = calcMgr.GetHistoryItems(); auto& items = calcMgr.GetHistoryItems();
@ -116,6 +178,12 @@ namespace CalculatorApp::ViewModel::Snapshot
} }
} }
PrimaryDisplaySnapshot::PrimaryDisplaySnapshot()
{
DisplayValue = ref new Platform::String();
IsError = false;
}
PrimaryDisplaySnapshot::PrimaryDisplaySnapshot(Platform::String ^ display, bool isError) PrimaryDisplaySnapshot::PrimaryDisplaySnapshot(Platform::String ^ display, bool isError)
{ {
assert(display != nullptr && "display is mandatory"); assert(display != nullptr && "display is mandatory");
@ -123,6 +191,12 @@ namespace CalculatorApp::ViewModel::Snapshot
IsError = isError; IsError = isError;
} }
ExpressionDisplaySnapshot::ExpressionDisplaySnapshot()
{
Tokens = ref new Platform::Collections::Vector<CalcManagerHistoryToken ^>();
Commands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
}
ExpressionDisplaySnapshot::ExpressionDisplaySnapshot( ExpressionDisplaySnapshot::ExpressionDisplaySnapshot(
const std::vector<CalcHistoryToken>& tokens, const std::vector<CalcHistoryToken>& tokens,
const std::vector<std::shared_ptr<IExpressionCommand>>& commands) const std::vector<std::shared_ptr<IExpressionCommand>>& commands)
@ -140,6 +214,14 @@ namespace CalculatorApp::ViewModel::Snapshot
} }
} }
StandardCalculatorSnapshot::StandardCalculatorSnapshot()
{
CalcManager = ref new CalcManagerSnapshot();
PrimaryDisplay = ref new PrimaryDisplaySnapshot();
ExpressionDisplay = nullptr;
DisplayCommands = ref new Platform::Collections::Vector<ICalcManagerIExprCommand ^>();
}
std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>> ToUnderlying(Windows::Foundation::Collections::IVector<CalcManagerHistoryItem ^> ^ items) std::vector<std::shared_ptr<CalculationManager::HISTORYITEM>> ToUnderlying(Windows::Foundation::Collections::IVector<CalcManagerHistoryItem ^> ^ items)
{ {
return {}; // TODO return {}; // TODO

View file

@ -17,15 +17,15 @@ public
public public
ref struct UnaryCommand sealed : public ICalcManagerIExprCommand ref struct UnaryCommand sealed : public ICalcManagerIExprCommand
{ {
property Windows::Foundation::Collections::IVectorView<int> ^ Commands { Windows::Foundation::Collections::IVectorView<int> ^ get(); }; property Windows::Foundation::Collections::IVectorView<int> ^ Commands {
Windows::Foundation::Collections::IVectorView<int> ^ get();
void set(Windows::Foundation::Collections::IVectorView<int> ^ commands);
};
explicit UnaryCommand(Windows::Foundation::Collections::IVectorView<int> ^ cmds); UnaryCommand();
internal :; internal :;
explicit UnaryCommand(std::vector<int> cmds) explicit UnaryCommand(std::vector<int> cmds);
: m_cmds(std::move(cmds))
{
}
std::vector<int> m_cmds; std::vector<int> m_cmds;
}; };
@ -33,10 +33,11 @@ public
ref struct BinaryCommand sealed : public ICalcManagerIExprCommand ref struct BinaryCommand sealed : public ICalcManagerIExprCommand
{ {
property int Command; property int Command;
explicit BinaryCommand(int cmd)
{ BinaryCommand();
Command = cmd;
} internal :;
explicit BinaryCommand(int cmd);
}; };
public public
@ -45,17 +46,15 @@ public
property bool IsNegative; property bool IsNegative;
property bool IsDecimalPresent; property bool IsDecimalPresent;
property bool IsSciFmt; property bool IsSciFmt;
property Windows::Foundation::Collections::IVectorView<int> ^ Commands { Windows::Foundation::Collections::IVectorView<int> ^ get(); }; property Windows::Foundation::Collections::IVectorView<int> ^ Commands {
Windows::Foundation::Collections::IVectorView<int> ^ get();
void set(Windows::Foundation::Collections::IVectorView<int> ^ commands);
};
OperandCommand();
explicit OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, Windows::Foundation::Collections::IVectorView<int> ^ cmds);
internal :; internal :;
explicit OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, std::vector<int> cmds) explicit OperandCommand(bool isNegative, bool isDecimal, bool isSciFmt, std::vector<int> cmds);
{
IsNegative = isNegative;
IsDecimalPresent = isDecimal;
IsSciFmt = isSciFmt;
m_cmds = std::move(cmds);
}
std::vector<int> m_cmds; std::vector<int> m_cmds;
}; };
@ -63,10 +62,11 @@ public
ref struct Parentheses sealed : public ICalcManagerIExprCommand ref struct Parentheses sealed : public ICalcManagerIExprCommand
{ {
property int Command; property int Command;
explicit Parentheses(int cmd)
{ Parentheses();
Command = cmd;
} internal :;
explicit Parentheses(int cmd);
}; };
public public
@ -75,6 +75,9 @@ public
property Platform::String ^ OpCodeName; // mandatory property Platform::String ^ OpCodeName; // mandatory
property int CommandIndex; property int CommandIndex;
CalcManagerHistoryToken();
internal :;
explicit CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex); explicit CalcManagerHistoryToken(Platform::String ^ opCodeName, int cmdIndex);
}; };
@ -86,11 +89,7 @@ public
property Platform::String ^ Expression; // mandatory property Platform::String ^ Expression; // mandatory
property Platform::String ^ Result; // mandatory property Platform::String ^ Result; // mandatory
// explicit CalcManagerHistoryItem( CalcManagerHistoryItem();
// Windows::Foundation::Collections::IVector<CalcManagerHistoryToken ^> ^ tokens,
// Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ commands,
// Platform::String ^ expression,
// Platform::String ^ result);
internal :; internal :;
explicit CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item); explicit CalcManagerHistoryItem(const CalculationManager::HISTORYITEM& item);
@ -101,6 +100,8 @@ public
{ {
property Windows::Foundation::Collections::IVector<CalcManagerHistoryItem ^> ^ HistoryItems; // optional property Windows::Foundation::Collections::IVector<CalcManagerHistoryItem ^> ^ HistoryItems; // optional
CalcManagerSnapshot();
internal :; internal :;
explicit CalcManagerSnapshot(const CalculationManager::CalculatorManager& calcMgr); explicit CalcManagerSnapshot(const CalculationManager::CalculatorManager& calcMgr);
}; };
@ -111,6 +112,8 @@ public
property Platform::String ^ DisplayValue; // mandatory property Platform::String ^ DisplayValue; // mandatory
property bool IsError; property bool IsError;
PrimaryDisplaySnapshot();
internal :; internal :;
explicit PrimaryDisplaySnapshot(Platform::String ^ display, bool isError); explicit PrimaryDisplaySnapshot(Platform::String ^ display, bool isError);
}; };
@ -121,6 +124,8 @@ public
property Windows::Foundation::Collections::IVector<CalcManagerHistoryToken ^> ^ Tokens; property Windows::Foundation::Collections::IVector<CalcManagerHistoryToken ^> ^ Tokens;
property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ Commands; property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ Commands;
ExpressionDisplaySnapshot();
internal :; internal :;
using CalcHistoryToken = std::pair<std::wstring, int>; using CalcHistoryToken = std::pair<std::wstring, int>;
explicit ExpressionDisplaySnapshot(const std::vector<CalcHistoryToken>& tokens, const std::vector<std::shared_ptr<IExpressionCommand>>& commands); explicit ExpressionDisplaySnapshot(const std::vector<CalcHistoryToken>& tokens, const std::vector<std::shared_ptr<IExpressionCommand>>& commands);
@ -133,6 +138,8 @@ public
property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory property PrimaryDisplaySnapshot ^ PrimaryDisplay; // mandatory
property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional property ExpressionDisplaySnapshot ^ ExpressionDisplay; // optional
property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ DisplayCommands; // mandatory property Windows::Foundation::Collections::IVector<ICalcManagerIExprCommand ^> ^ DisplayCommands; // mandatory
StandardCalculatorSnapshot();
}; };
public public

View file

@ -1,5 +1,10 @@
using System;
using System.Linq;
using System.Text.Json;
using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Activation;
using CalculatorApp.ViewModel.Snapshot; using CalculatorApp.ViewModel.Snapshot;
using CalculatorApp.JsonUtils;
namespace CalculatorApp namespace CalculatorApp
{ {
@ -29,7 +34,18 @@ namespace CalculatorApp
public static SnapshotLaunchArguments GetSnapshotLaunchArgs(this IProtocolActivatedEventArgs args) public static SnapshotLaunchArguments GetSnapshotLaunchArgs(this IProtocolActivatedEventArgs args)
{ {
return null; try
{
var rawbase64 = args.Uri.Segments.Skip(1).Aggregate((folded, x) => folded += x);
var compressed = Convert.FromBase64String(rawbase64);
var jsonStr = DeflateUtils.Decompress(compressed);
var snapshot = JsonSerializer.Deserialize<ApplicationSnapshotAlias>(jsonStr);
return new SnapshotLaunchArguments { HasError = false, Snapshot = snapshot.Value };
}
catch (Exception)
{
return new SnapshotLaunchArguments { HasError = true };
}
} }
} }
} }

View file

@ -20,10 +20,7 @@ namespace CalculatorApp
} }
} }
public static bool TryDecompress(byte[] data, out string text) public static string Decompress(byte[] data)
{
text = null;
try
{ {
using (var srcStream = new MemoryStream(data)) using (var srcStream = new MemoryStream(data))
using (var deflateStream = new DeflateStream(srcStream, CompressionMode.Decompress)) using (var deflateStream = new DeflateStream(srcStream, CompressionMode.Decompress))
@ -31,13 +28,7 @@ namespace CalculatorApp
{ {
deflateStream.CopyTo(resultStream); deflateStream.CopyTo(resultStream);
byte[] decompressed = resultStream.ToArray(); byte[] decompressed = resultStream.ToArray();
text = Encoding.UTF8.GetString(decompressed); return Encoding.UTF8.GetString(decompressed);
return true;
}
}
catch (Exception)
{
return false;
} }
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using CalculatorApp.ViewModel.Snapshot; using CalculatorApp.ViewModel.Snapshot;
using Windows.ApplicationModel;
namespace CalculatorApp.JsonUtils namespace CalculatorApp.JsonUtils
{ {
@ -18,7 +19,11 @@ namespace CalculatorApp.JsonUtils
set => Value.OpCodeName = value; set => Value.OpCodeName = value;
} }
[JsonPropertyName("c")] [JsonPropertyName("c")]
public int CommandIndex { get => Value.CommandIndex; } public int CommandIndex
{
get => Value.CommandIndex;
set => Value.CommandIndex = value;
}
} }
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$t")] [JsonPolymorphic(TypeDiscriminatorPropertyName = "$t")]
@ -36,7 +41,11 @@ namespace CalculatorApp.JsonUtils
public UnaryCommand Value; public UnaryCommand Value;
[JsonPropertyName("c")] [JsonPropertyName("c")]
public IList<int> Commands { get => Value.Commands.ToList(); } public IReadOnlyList<int> Commands
{
get => Value.Commands;
set => Value.Commands = value;
}
} }
internal class BinaryCommandAlias : ICalcManagerIExprCommandAlias internal class BinaryCommandAlias : ICalcManagerIExprCommandAlias
@ -45,7 +54,11 @@ namespace CalculatorApp.JsonUtils
public BinaryCommand Value; public BinaryCommand Value;
[JsonPropertyName("c")] [JsonPropertyName("c")]
public int Command { get => Value.Command; } public int Command
{
get => Value.Command;
set => Value.Command = value;
}
} }
internal class OperandCommandAlias : ICalcManagerIExprCommandAlias internal class OperandCommandAlias : ICalcManagerIExprCommandAlias
@ -54,13 +67,29 @@ namespace CalculatorApp.JsonUtils
public OperandCommand Value; public OperandCommand Value;
[JsonPropertyName("n")] [JsonPropertyName("n")]
public bool IsNegative { get => Value.IsNegative; } public bool IsNegative
{
get => Value.IsNegative;
set => Value.IsNegative = value;
}
[JsonPropertyName("d")] [JsonPropertyName("d")]
public bool IsDecimalPresent { get => Value.IsDecimalPresent; } public bool IsDecimalPresent
{
get => Value.IsDecimalPresent;
set => Value.IsDecimalPresent = value;
}
[JsonPropertyName("s")] [JsonPropertyName("s")]
public bool IsSciFmt { get => Value.IsSciFmt; } public bool IsSciFmt
{
get => Value.IsSciFmt;
set => Value.IsSciFmt = value;
}
[JsonPropertyName("c")] [JsonPropertyName("c")]
public IList<int> Commands { get => Value.Commands.ToList(); } public IReadOnlyList<int> Commands
{
get => Value.Commands;
set => Value.Commands = value;
}
} }
internal class ParenthesesAlias : ICalcManagerIExprCommandAlias internal class ParenthesesAlias : ICalcManagerIExprCommandAlias
@ -69,7 +98,11 @@ namespace CalculatorApp.JsonUtils
public Parentheses Value; public Parentheses Value;
[JsonPropertyName("c")] [JsonPropertyName("c")]
public int Command { get => Value.Command; } public int Command
{
get => Value.Command;
set => Value.Command = value;
}
} }
internal class CalcManagerHistoryItemAlias internal class CalcManagerHistoryItemAlias
@ -78,23 +111,32 @@ namespace CalculatorApp.JsonUtils
public CalcManagerHistoryItem Value; public CalcManagerHistoryItem Value;
[JsonPropertyName("t")] [JsonPropertyName("t")]
public IList<CalcManagerHistoryTokenAlias> Tokens public IEnumerable<CalcManagerHistoryTokenAlias> Tokens
{ {
get => Value.Tokens.Select(x => new CalcManagerHistoryTokenAlias { Value = x }).ToList(); get => Value.Tokens.Select(x => new CalcManagerHistoryTokenAlias { Value = x });
set => Value.Tokens = value.Select(Helpers.MapHistoryToken).ToList();
} }
[JsonPropertyName("c")] [JsonPropertyName("c")]
public IList<ICalcManagerIExprCommandAlias> Commands public IEnumerable<ICalcManagerIExprCommandAlias> Commands
{ {
get => Value.Commands.Select(Helpers.MapCommandAlias).ToList(); get => Value.Commands.Select(Helpers.MapCommandAlias);
set => Value.Commands = value.Select(Helpers.MapCommandAlias).ToList();
} }
[JsonPropertyName("e")] [JsonPropertyName("e")]
public string Expression { get => Value.Expression; } public string Expression
{
get => Value.Expression;
set => Value.Expression = value;
}
[JsonPropertyName("r")] [JsonPropertyName("r")]
public string Result { get => Value.Result; } public string Result
{
get => Value.Result;
set => Value.Result = value;
}
} }
internal class CalcManagerSnapshotAlias internal class CalcManagerSnapshotAlias
@ -103,7 +145,11 @@ namespace CalculatorApp.JsonUtils
public CalcManagerSnapshot Value; public CalcManagerSnapshot Value;
[JsonPropertyName("h")] [JsonPropertyName("h")]
public IList<CalcManagerHistoryItemAlias> HistoryItems { get => Value.HistoryItems.Select(x => new CalcManagerHistoryItemAlias { Value = x }).ToList(); } public IEnumerable<CalcManagerHistoryItemAlias> HistoryItems
{
get => Value.HistoryItems.Select(x => new CalcManagerHistoryItemAlias { Value = x });
set => Value.HistoryItems = value.Select(x => new CalcManagerHistoryItem { Tokens = x.Tokens.Select(Helpers.MapHistoryToken).ToList() }).ToList();
}
} }
internal class PrimaryDisplaySnapshotAlias internal class PrimaryDisplaySnapshotAlias
@ -112,9 +158,17 @@ namespace CalculatorApp.JsonUtils
public PrimaryDisplaySnapshot Value; public PrimaryDisplaySnapshot Value;
[JsonPropertyName("d")] [JsonPropertyName("d")]
public string DisplayValue { get => Value.DisplayValue; } public string DisplayValue
{
get => Value.DisplayValue;
set => Value.DisplayValue = value;
}
[JsonPropertyName("e")] [JsonPropertyName("e")]
public bool IsError { get => Value.IsError; } public bool IsError
{
get => Value.IsError;
set => IsError = value;
}
} }
internal class ExpressionDisplaySnapshotAlias internal class ExpressionDisplaySnapshotAlias
@ -123,9 +177,17 @@ namespace CalculatorApp.JsonUtils
public ExpressionDisplaySnapshot Value; public ExpressionDisplaySnapshot Value;
[JsonPropertyName("t")] [JsonPropertyName("t")]
public IList<CalcManagerHistoryTokenAlias> Tokens { get => Value.Tokens.Select(x => new CalcManagerHistoryTokenAlias { Value = x }).ToList(); } public IEnumerable<CalcManagerHistoryTokenAlias> Tokens
{
get => Value.Tokens.Select(x => new CalcManagerHistoryTokenAlias { Value = x });
set => Value.Tokens = value.Select(Helpers.MapHistoryToken).ToList();
}
[JsonPropertyName("c")] [JsonPropertyName("c")]
public IList<ICalcManagerIExprCommandAlias> Commands { get => Value.Commands.Select(Helpers.MapCommandAlias).ToList(); } public IEnumerable<ICalcManagerIExprCommandAlias> Commands
{
get => Value.Commands.Select(Helpers.MapCommandAlias);
set => Value.Commands = value.Select(Helpers.MapCommandAlias).ToList();
}
} }
internal class StandardCalculatorSnapshotAlias internal class StandardCalculatorSnapshotAlias
@ -134,13 +196,29 @@ namespace CalculatorApp.JsonUtils
public StandardCalculatorSnapshot Value; public StandardCalculatorSnapshot Value;
[JsonPropertyName("m")] [JsonPropertyName("m")]
public CalcManagerSnapshotAlias CalcManager { get => new CalcManagerSnapshotAlias { Value = Value.CalcManager }; } public CalcManagerSnapshotAlias CalcManager
{
get => new CalcManagerSnapshotAlias { Value = Value.CalcManager };
set => Value.CalcManager = value.Value;
}
[JsonPropertyName("p")] [JsonPropertyName("p")]
public PrimaryDisplaySnapshotAlias PrimaryDisplay { get => new PrimaryDisplaySnapshotAlias { Value = Value.PrimaryDisplay }; } public PrimaryDisplaySnapshotAlias PrimaryDisplay
{
get => new PrimaryDisplaySnapshotAlias { Value = Value.PrimaryDisplay };
set => Value.PrimaryDisplay = value.Value;
}
[JsonPropertyName("e")] [JsonPropertyName("e")]
public ExpressionDisplaySnapshotAlias ExpressionDisplay { get => new ExpressionDisplaySnapshotAlias { Value = Value.ExpressionDisplay }; } public ExpressionDisplaySnapshotAlias ExpressionDisplay
{
get => new ExpressionDisplaySnapshotAlias { Value = Value.ExpressionDisplay };
set => Value.ExpressionDisplay = value.Value;
}
[JsonPropertyName("c")] [JsonPropertyName("c")]
public IList<ICalcManagerIExprCommandAlias> Commands { get => Value.DisplayCommands.Select(Helpers.MapCommandAlias).ToList(); } public IEnumerable<ICalcManagerIExprCommandAlias> Commands
{
get => Value.DisplayCommands.Select(Helpers.MapCommandAlias);
set => Value.DisplayCommands = value.Select(Helpers.MapCommandAlias).ToList();
}
} }
internal class ApplicationSnapshotAlias internal class ApplicationSnapshotAlias
@ -149,13 +227,22 @@ namespace CalculatorApp.JsonUtils
public ApplicationSnapshot Value; public ApplicationSnapshot Value;
[JsonPropertyName("m")] [JsonPropertyName("m")]
public int Mode { get => Value.Mode; } public int Mode { get => Value.Mode; set => Value.Mode = value; }
[JsonPropertyName("s")] [JsonPropertyName("s")]
public StandardCalculatorSnapshotAlias StandardCalculatorSnapshot { get => new StandardCalculatorSnapshotAlias { Value = Value.StandardCalculator }; } public StandardCalculatorSnapshotAlias StandardCalculatorSnapshot
{
get => new StandardCalculatorSnapshotAlias { Value = Value.StandardCalculator };
set => Value.StandardCalculator = value.Value;
}
} }
internal static class Helpers internal static class Helpers
{ {
public static CalcManagerHistoryToken MapHistoryToken(CalcManagerHistoryTokenAlias token)
{
return new CalcManagerHistoryToken { OpCodeName = token.OpCodeName, CommandIndex = token.CommandIndex };
}
public static ICalcManagerIExprCommandAlias MapCommandAlias(ICalcManagerIExprCommand exprCmd) public static ICalcManagerIExprCommandAlias MapCommandAlias(ICalcManagerIExprCommand exprCmd)
{ {
if (exprCmd is UnaryCommand unary) if (exprCmd is UnaryCommand unary)
@ -176,5 +263,32 @@ namespace CalculatorApp.JsonUtils
} }
throw new NotImplementedException("unhandled command type."); throw new NotImplementedException("unhandled command type.");
} }
public static ICalcManagerIExprCommand MapCommandAlias(ICalcManagerIExprCommandAlias exprCmd)
{
if (exprCmd is UnaryCommandAlias unary)
{
return new UnaryCommand { Commands = unary.Commands };
}
else if (exprCmd is BinaryCommandAlias binary)
{
return new BinaryCommand { Command = binary.Command };
}
else if (exprCmd is OperandCommandAlias operand)
{
return new OperandCommand
{
IsNegative = operand.IsNegative,
IsDecimalPresent = operand.IsDecimalPresent,
IsSciFmt = operand.IsSciFmt,
Commands = operand.Commands
};
}
else if (exprCmd is ParenthesesAlias paren)
{
return new Parentheses { Command = paren.Command };
}
throw new NotImplementedException("unhandled command type.");
}
} }
} }