108 lines
4 KiB
C#
108 lines
4 KiB
C#
using System.Collections.Generic;
|
|
|
|
using Carousel;
|
|
|
|
using LightJSON;
|
|
|
|
using Misc;
|
|
|
|
namespace ConsoleConnection
|
|
{
|
|
public class ConsoleCarouselModelBuilder
|
|
{
|
|
public CarouselModel BuildModel(JSON sourceJSON)
|
|
{
|
|
sourceJSON = InjectActionCommands(sourceJSON);
|
|
|
|
CarouselModel model = new CarouselModel();
|
|
model.HasVisibleActions = JSONParseHelper.HasVisibleActions(sourceJSON);
|
|
model.IsEnabled = sourceJSON.GetFieldAsBool("isEnabled") || !sourceJSON.HasField("isEnabled") /* legacy protocol didn't have the field at all */;
|
|
|
|
List<JSON> rows = sourceJSON.GetFieldAsArray("rows");
|
|
for (int index = 0; index < rows.Count; ++index)
|
|
{
|
|
RowModel currentRow = BuildRowModel(rows[index], index);
|
|
model.Rows.Add(currentRow);
|
|
}
|
|
|
|
var localizedStrings = sourceJSON.GetFieldAsObject("localizedTexts").Children;
|
|
foreach (var localizedString in localizedStrings)
|
|
{
|
|
model.LocalizedStrings.Add(localizedString.Key, localizedString.Value.AsString());
|
|
}
|
|
|
|
return model;
|
|
}
|
|
|
|
private JSON InjectActionCommands(JSON carousel)
|
|
{
|
|
int i = 0; int j = 0;
|
|
JSON jsonRows = carousel.GetField("rows");
|
|
if (jsonRows != null)
|
|
{
|
|
var rows = jsonRows.Elements;
|
|
foreach (var row in rows)
|
|
{
|
|
var items = row.GetField("items").Elements;
|
|
j = 0;
|
|
foreach (var item in items)
|
|
{
|
|
JSONParseHelper.FillActionsCommand(item, i, j);
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
return carousel;
|
|
}
|
|
|
|
private RowModel BuildRowModel(JSON rowSource, int currentRowIndex)
|
|
{
|
|
RowModel currentRow = new RowModel();
|
|
currentRow.Title = StringProcessor.removeMarkers(rowSource.GetFieldAsString("title"));
|
|
|
|
List<JSON> itemsOfRow = rowSource.GetFieldAsArray("items");
|
|
for (int itemIndex = 0; itemIndex < itemsOfRow.Count; ++itemIndex)
|
|
{
|
|
ItemModel currentItem = BuildItemModel(itemsOfRow[itemIndex]);
|
|
currentRow.Items.Add(currentItem);
|
|
}
|
|
|
|
return currentRow;
|
|
}
|
|
|
|
private ItemModel BuildItemModel(JSON itemSource)
|
|
{
|
|
ItemModel itemModel = new ItemModel();
|
|
itemModel.Title = StringProcessor.removeMarkers(itemSource.GetFieldAsString("title"));
|
|
itemModel.ImageURI = itemSource.GetFieldAsString("image");
|
|
itemModel.IsEnabled = itemSource.GetFieldAsBool("isEnabled");
|
|
itemModel.LockStatus = (ItemModel.LockType)itemSource.GetFieldAsNumber("isLocked"); // was a bool, name kept for retrocompatibility
|
|
itemModel.UnlimitedStatus = (ItemModel.UnlimitedType)itemSource.GetFieldAsNumber("unlimited");
|
|
itemModel.New = itemSource.GetFieldAsBool("new");
|
|
itemModel.Stars = (int)itemSource.GetFieldAsNumber("stars");
|
|
itemModel.Favorite = itemSource.GetFieldAsBool("favorite");
|
|
|
|
List<JSON> actionsOfItem = itemSource.GetFieldAsArray("actions");
|
|
for (int actionIndex = 0; actionIndex < actionsOfItem.Count; ++actionIndex)
|
|
{
|
|
ActionModel currentAction = BuildActionModel(actionsOfItem[actionIndex]);
|
|
itemModel.Actions.Add(currentAction);
|
|
}
|
|
|
|
return itemModel;
|
|
}
|
|
|
|
private ActionModel BuildActionModel(JSON actionSource)
|
|
{
|
|
ActionModel actionModel = new ActionModel();
|
|
actionModel.Title = StringProcessor.removeMarkers(actionSource.GetFieldAsString("title"));
|
|
|
|
JSON command = JSONParseHelper.GetActionCommand(actionSource);
|
|
actionModel.UserData = command;
|
|
|
|
return actionModel;
|
|
}
|
|
}
|
|
}
|