Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ riderModule.iml
.idea/
.idea/*

exports/
imports/
*.smo

# If we set this up
!.idea/codeStyles/
!.idea/inspectionProfiles/
Expand Down
3 changes: 3 additions & 0 deletions SubathonManager.Core/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IniParser;
using IniParser.Model;
using System.Diagnostics.CodeAnalysis;
using SubathonManager.Core.Events;
using SubathonManager.Core.Interfaces;

namespace SubathonManager.Core
Expand Down Expand Up @@ -115,6 +116,7 @@ public virtual void Save()
{
Parser.WriteFile(ConfigPath, Data);
PendingChanges = false;
SettingsEvents.RaiseSettingsUnsavedChanges(PendingChanges);
}

public virtual string GetDatabasePath()
Expand Down Expand Up @@ -146,6 +148,7 @@ public virtual bool Set(string section, string key, string? value)
{
Data[section][key] = value ?? string.Empty;
PendingChanges = true;
SettingsEvents.RaiseSettingsUnsavedChanges(PendingChanges);
return true;
}
return false;
Expand Down
14 changes: 14 additions & 0 deletions SubathonManager.Core/Events/SettingsEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics.CodeAnalysis;

namespace SubathonManager.Core.Events;

[ExcludeFromCodeCoverage]
public static class SettingsEvents
{
public static event Action<bool>? SettingsUnsavedChanges;

public static void RaiseSettingsUnsavedChanges(bool hasPendingChanges)
{
SettingsUnsavedChanges?.Invoke(hasPendingChanges);
}
}
49 changes: 49 additions & 0 deletions SubathonManager.Core/Models/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SubathonManager.Core.Models;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

[ExcludeFromCodeCoverage]
public class Route
Expand All @@ -24,4 +25,52 @@ public string GetRouteUrl(IConfig config, bool editMode = false)
string qString = editMode ? "?edit=true" : "";
return $"http://localhost:{config.Get("Server", "Port", "14040")}/route/{Id}{qString}";
}

/*
public JsonElement ToJson()
{
var obj = new
{
name = Name,

resolution = new
{
width = Width,
height = Height
},

widgets = Widgets.Select(w => w.ToJson("")).ToArray(),

meta = new
{
created = CreatedTimestamp,
updated = UpdatedTimestamp
}
};

return JsonSerializer.SerializeToElement(obj);
}

public static Route? FromJson(JsonElement json)
{
var name = json.GetProperty("name").GetString() ?? string.Empty;
if (string.IsNullOrWhiteSpace(name)) return null;

var route = new Route
{
Name = name
};

var res = json.GetProperty("resolution");
route.Width = res.GetProperty("width").GetInt32();
route.Height = res.GetProperty("height").GetInt32();

foreach (var widget in json.GetProperty("widgets").EnumerateArray().Select(widgetJson => Widget.FromJson(widgetJson, "", route.Id)).OfType<Widget>())
{
route.Widgets.Add(widget);
}

return route;
}
*/
}
151 changes: 148 additions & 3 deletions SubathonManager.Core/Models/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
// ReSharper disable NullableWarningSuppressionIsUsed

namespace SubathonManager.Core.Models;

Expand Down Expand Up @@ -35,6 +36,30 @@ public CssVariable Clone(Guid newWidgetId)
Description = Description
};
}

public static CssVariable? FromJson(JsonElement json, Guid widgetId)
{
var name = json.GetProperty("name").GetString() ?? string.Empty;
var value = json.GetProperty("value").GetString() ?? string.Empty;
if (string.IsNullOrEmpty(name)) return null;
return new CssVariable
{
Name = name,
Value = value,
WidgetId = widgetId
};
}

public JsonElement ToJson()
{
var obj = new
{
name = Name,
value = Value
};

return JsonSerializer.SerializeToElement(obj);
}
}

[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -69,9 +94,9 @@ public string GetInjectLine()
string val = Value.Split(',')[0].Trim();
sb.Append($"\"{val}\"");
}
else if (Type == WidgetVariableType.EventTypeList ||
Type == WidgetVariableType.StringList ||
Type == WidgetVariableType.EventSubTypeList)
else if (Type is WidgetVariableType.EventTypeList or
WidgetVariableType.StringList or
WidgetVariableType.EventSubTypeList)
{
string val = string.Join(",", Value.Split(',').Select(s =>
{
Expand Down Expand Up @@ -110,6 +135,44 @@ public JsVariable Clone(Guid newWidgetId)
WidgetId = newWidgetId
};
}

public static JsVariable? FromJson(JsonElement json, Guid widgetId)
{
var name = json.GetProperty("name").GetString() ?? string.Empty;
if (string.IsNullOrEmpty(name)) return null;

var typeEl = json.GetProperty("type");
WidgetVariableType type;
if (typeEl.ValueKind == JsonValueKind.Number
&& typeEl.TryGetInt32(out var typeInt)
&& Enum.IsDefined(typeof(WidgetVariableType), typeInt))
{
type = (WidgetVariableType)typeInt;
}
else if (!Enum.TryParse(typeEl.GetString() ?? string.Empty, out type))
{
return null;
}
return new JsVariable
{
Name = name,
Value = json.GetProperty("value").GetString() ?? string.Empty,
Type = type,
WidgetId = widgetId
};
}

public JsonElement ToJson()
{
var obj = new
{
name = Name,
value = Value,
type = Type
};

return JsonSerializer.SerializeToElement(obj);
}
}

[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -168,6 +231,8 @@ public Widget Clone(Guid? routeId, string? newName, int? newZ)

return widget;
}

public string GetPath() => Path.GetDirectoryName(HtmlPath) ?? HtmlPath;

public void ScanCssVariables()
{
Expand Down Expand Up @@ -251,5 +316,85 @@ public override string ToString()
private static partial Regex CssLinkRegex();

[GeneratedRegex(@"--([a-zA-Z0-9-_]+)\s*:\s*([^;]+);")]

private static partial Regex CssVarRegex();

public JsonElement ToJson(string htmlRelPath)
{
var obj = new
{
name = Name,
htmlPath = htmlRelPath,

position = new
{
x = X,
y = Y,
z = Z
},

size = new
{
width = Width,
height = Height
},

scale = new
{
x = ScaleX,
y = ScaleY
},

visibility = Visibility,
docsUrl = DocsUrl,

cssVariables = CssVariables.Select(v => v.ToJson()).ToArray(),
jsVariables = JsVariables.Select(v => v.ToJson()).ToArray()
};

return JsonSerializer.SerializeToElement(obj);
}

/*
public static Widget? FromJson(JsonElement json, string rootPath, Guid routeId)
{
var htmlPath = Path.Join(rootPath, json.GetProperty("htmlPath").GetString());
var name = json.GetProperty("name").GetString() ?? string.Empty;
if (string.IsNullOrEmpty(name)) return null;

var widget = new Widget(name, htmlPath)
{
RouteId = routeId,
Visibility = json.GetProperty("visibility").GetBoolean(),
DocsUrl = json.TryGetProperty("docsUrl", out var d) ? d.GetString() : null
};

var pos = json.GetProperty("position");
widget.X = pos.GetProperty("x").GetSingle();
widget.Y = pos.GetProperty("y").GetSingle();
widget.Z = pos.GetProperty("z").GetInt32();

var size = json.GetProperty("size");
widget.Width = size.GetProperty("width").GetInt32();
widget.Height = size.GetProperty("height").GetInt32();

var scale = json.GetProperty("scale");
widget.ScaleX = scale.GetProperty("x").GetSingle();
widget.ScaleY = scale.GetProperty("y").GetSingle();

foreach (var cssVar in json.GetProperty("cssVariables").EnumerateArray().Select(css =>
CssVariable.FromJson(css, widget.Id)).OfType<CssVariable>())
{
widget.CssVariables.Add(cssVar);
}

foreach (var jsVar in json.GetProperty("jsVariables").EnumerateArray().Select(js =>
JsVariable.FromJson(js, widget.Id)).OfType<JsVariable>())
{
widget.JsVariables.Add(jsVar);
}

return widget;
}
*/
}
Loading