Skip to content

Commit

Permalink
Fuck you Windows.Data.Json fuck you UWP fuck you WinRT !!!
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun committed Jul 19, 2024
1 parent 7667dfe commit d107881
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 63 deletions.
2 changes: 1 addition & 1 deletion source/iNKORE.UI.WPF.Modern.Gallery/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xmlns:sc="clr-namespace:SamplesCommon;assembly=SamplesCommon"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
StartupUri="MainWindow.xaml">
StartupUri="MainWindow.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
6 changes: 6 additions & 0 deletions source/iNKORE.UI.WPF.Modern.Gallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public static Process BrowseWeb(string path)
return null;
}
}

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
e.Handled = true;
}
}
}
173 changes: 111 additions & 62 deletions source/iNKORE.UI.WPF.Modern.Gallery/DataModel/ControlInfoDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using Windows.Data.Json;
using Windows.Storage;

namespace iNKORE.UI.WPF.Modern.Gallery.DataModel
{
Expand Down Expand Up @@ -187,87 +186,137 @@ private async Task GetControlInfoDataAsync()
await Task.Run(() => jsonText = reader.ReadToEnd());
}

JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonArray = jsonObject["Groups"].GetArray();
JsonDocument jsonDocument = JsonDocument.Parse(jsonText);
JsonElement jsonArray = jsonDocument.RootElement.GetProperty("Groups");

lock (_lock)
{
string pageRoot = "iNKORE.UI.WPF.Modern.Gallery.ControlPages.";
foreach (JsonValue groupValue in jsonArray)
foreach (JsonElement groupValue in jsonArray.EnumerateArray())
{
if (groupValue.ValueKind != JsonValueKind.Object)
{
continue;
}

JsonObject groupObject = groupValue.GetObject();
JsonElement groupObject;
if (groupValue.TryGetProperty("UniqueId", out JsonElement uniqueIdElement) &&
groupValue.TryGetProperty("Title", out JsonElement titleElement) &&
groupValue.TryGetProperty("Subtitle", out JsonElement subtitleElement) &&
groupValue.TryGetProperty("ImagePath", out JsonElement imagePathElement) &&
groupValue.TryGetProperty("ImageIconPath", out JsonElement imageIconPathElement) &&
groupValue.TryGetProperty("Description", out JsonElement descriptionElement))
{
groupObject = groupValue;
}
else
{
continue;
}

ControlInfoDataGroup group = new ControlInfoDataGroup(groupObject["UniqueId"].GetString(),
groupObject["Title"].GetString(),
groupObject["Subtitle"].GetString(),
groupObject["ImagePath"].GetString(),
groupObject["ImageIconPath"].GetString(),
groupObject["Description"].GetString());
ControlInfoDataGroup group = new ControlInfoDataGroup(uniqueIdElement.GetString(),
titleElement.GetString(),
subtitleElement.GetString(),
imagePathElement.GetString(),
imageIconPathElement.GetString(),
descriptionElement.GetString());

foreach (JsonValue itemValue in groupObject["Items"].GetArray())
if (groupObject.TryGetProperty("Items", out JsonElement itemsElement) && itemsElement.ValueKind == JsonValueKind.Array)
{
JsonObject itemObject = itemValue.GetObject();
foreach (JsonElement itemValue in itemsElement.EnumerateArray())
{
if (itemValue.ValueKind != JsonValueKind.Object)
{
continue;
}

string badgeString = null;
JsonElement itemObject;
if (itemValue.TryGetProperty("UniqueId", out JsonElement itemUniqueIdElement) &&
itemValue.TryGetProperty("Title", out JsonElement itemTitleElement) &&
itemValue.TryGetProperty("Subtitle", out JsonElement itemSubtitleElement) &&
itemValue.TryGetProperty("ImagePath", out JsonElement itemImagePathElement) &&
itemValue.TryGetProperty("ImageIconPath", out JsonElement itemImageIconPathElement) &&
itemValue.TryGetProperty("Description", out JsonElement itemDescriptionElement) &&
itemValue.TryGetProperty("Content", out JsonElement itemContentElement))
{
itemObject = itemValue;
}
else
{
continue;
}

bool isNew = itemObject.ContainsKey("IsNew") ? itemObject["IsNew"].GetBoolean() : false;
bool isUpdated = itemObject.ContainsKey("IsUpdated") ? itemObject["IsUpdated"].GetBoolean() : false;
bool isPreview = itemObject.ContainsKey("IsPreview") ? itemObject["IsPreview"].GetBoolean() : false;
string badgeString = null;

if (isNew)
{
badgeString = "New";
}
else if (isUpdated)
{
badgeString = "Updated";
}
else if (isPreview)
{
badgeString = "Preview";
}
bool isNew = itemObject.TryGetProperty("IsNew", out JsonElement isNewElement) && isNewElement.GetBoolean();
bool isUpdated = itemObject.TryGetProperty("IsUpdated", out JsonElement isUpdatedElement) && isUpdatedElement.GetBoolean();
bool isPreview = itemObject.TryGetProperty("IsPreview", out JsonElement isPreviewElement) && isPreviewElement.GetBoolean();

var item = new ControlInfoDataItem(itemObject["UniqueId"].GetString(),
itemObject["Title"].GetString(),
itemObject["Subtitle"].GetString(),
itemObject["ImagePath"].GetString(),
itemObject["ImageIconPath"].GetString(),
badgeString,
itemObject["Description"].GetString(),
itemObject["Content"].GetString(),
isNew,
isUpdated,
isPreview);
if (isNew)
{
badgeString = "New";
}
else if (isUpdated)
{
badgeString = "Updated";
}
else if (isPreview)
{
badgeString = "Preview";
}

{
string pageString = pageRoot + item.UniqueId + "Page";
Type pageType = Type.GetType(pageString);
item.IncludedInBuild = pageType != null;
}
var item = new ControlInfoDataItem(itemUniqueIdElement.GetString(),
itemTitleElement.GetString(),
itemSubtitleElement.GetString(),
itemImagePathElement.GetString(),
itemImageIconPathElement.GetString(),
badgeString,
itemDescriptionElement.GetString(),
itemContentElement.GetString(),
isNew,
isUpdated,
isPreview);

if (itemObject.ContainsKey("Docs"))
{
foreach (JsonValue docValue in itemObject["Docs"].GetArray())
{
JsonObject docObject = docValue.GetObject();
item.Docs.Add(new ControlInfoDocLink(docObject["Title"].GetString(), docObject["Uri"].GetString()));
string pageString = pageRoot + item.UniqueId + "Page";
Type pageType = Type.GetType(pageString);
item.IncludedInBuild = pageType != null;
}
}

if (itemObject.ContainsKey("RelatedControls"))
{
foreach (JsonValue relatedControlValue in itemObject["RelatedControls"].GetArray())
if (itemObject.TryGetProperty("Docs", out JsonElement docsElement) && docsElement.ValueKind == JsonValueKind.Array)
{
item.RelatedControls.Add(relatedControlValue.GetString());
foreach (JsonElement docValue in docsElement.EnumerateArray())
{
if (docValue.ValueKind != JsonValueKind.Object)
{
continue;
}

if (docValue.TryGetProperty("Title", out JsonElement docTitleElement) &&
docValue.TryGetProperty("Uri", out JsonElement docUriElement))
{
item.Docs.Add(new ControlInfoDocLink(docTitleElement.GetString(), docUriElement.GetString()));
}
}
}
}

group.Items.Add(item);
}
if (!Groups.Any(g => g.Title == group.Title))
{
Groups.Add(group);
if (itemObject.TryGetProperty("RelatedControls", out JsonElement relatedControlsElement) && relatedControlsElement.ValueKind == JsonValueKind.Array)
{
foreach (JsonElement relatedControlValue in relatedControlsElement.EnumerateArray())
{
if (relatedControlValue.ValueKind == JsonValueKind.String)
{
item.RelatedControls.Add(relatedControlValue.GetString());
}
}
}

group.Items.Add(item);
}
if (!Groups.Any(g => g.Title == group.Title))
{
Groups.Add(group);
}
}
}
}
Expand Down

0 comments on commit d107881

Please sign in to comment.