Skip to content

Commit

Permalink
Sort complex forms alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Jan 16, 2025
1 parent e322bd2 commit a299fa5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,9 @@ public override List<ComplexFormComponent> Components
set => throw new NotImplementedException();
}

public override IList<ComplexFormComponent> ComplexForms
public override List<ComplexFormComponent> ComplexForms
{
get =>
new UpdateListProxy<ComplexFormComponent>(
component => _lexboxLcmApi.AddComplexFormComponent(_lexboxLcmApi.EntriesRepository.GetObject(component.ComplexFormEntryId), component),
component => _lexboxLcmApi.RemoveComplexFormComponent(_lexboxLcmApi.EntriesRepository.GetObject(component.ComplexFormEntryId), component),
//todo this does not handle complex forms which reference a sense
i => _lazyComplexForms.Value[i],
_lazyComplexForms.Value.Length
);
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
Relational:ColumnType: jsonb
SnapshotId (no field, Guid?) Shadow FK Index
Navigations:
ComplexForms (IList<ComplexFormComponent>) Collection ToDependent ComplexFormComponent
ComplexForms (List<ComplexFormComponent>) Collection ToDependent ComplexFormComponent
Components (List<ComplexFormComponent>) Collection ToDependent ComplexFormComponent
Senses (List<Sense>) Collection ToDependent Sense
Keys:
Expand Down
8 changes: 6 additions & 2 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private async IAsyncEnumerable<Entry> GetEntries(
var entries = queryable.AsAsyncEnumerable();
await foreach (var entry in entries)
{
entry.ApplySortOrder();
entry.ApplySortOrder(sortWs);
yield return entry;
}
}
Expand All @@ -320,7 +320,11 @@ private async IAsyncEnumerable<Entry> GetEntries(
.LoadWith(e => e.Components)
.AsQueryable()
.SingleOrDefaultAsync(e => e.Id == id);
entry?.ApplySortOrder();
if (entry is not null)
{
var sortWs = await GetWritingSystem(WritingSystemId.Default, WritingSystemType.Vernacular);
entry.ApplySortOrder(sortWs);
}
return entry;
}

Expand Down
42 changes: 34 additions & 8 deletions backend/FwLite/LcmCrdt/QueryHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Globalization;

namespace LcmCrdt;

public static class QueryHelpers
{
public static void ApplySortOrder(this Entry entry)
public static void ApplySortOrder(this Entry entry, WritingSystem? sortWs)
{
var sortCompareInfo = GetCompareInfo(sortWs);
entry.Senses.ApplySortOrder();
entry.Components.ApplySortOrder();
entry.ComplexForms.Sort((a, b) => ComplexFormsComparison(a, b, sortCompareInfo));
foreach (var sense in entry.Senses)
{
sense.ApplySortOrder();
Expand All @@ -19,14 +23,36 @@ public static void ApplySortOrder(this Sense sense)

private static void ApplySortOrder<T>(this List<T> items) where T : IOrderable
{
items.Sort((x, y) =>
items.Sort((a, b) =>
{
var result = x.Order.CompareTo(y.Order);
if (result == 0)
{
result = x.Id.CompareTo(y.Id);
}
return result;
var result = a.Order.CompareTo(b.Order);
if (result != 0) return result;
return a.Id.CompareTo(b.Id);
});
}

private static int ComplexFormsComparison(ComplexFormComponent a, ComplexFormComponent b, CompareInfo compareInfo)
{
var result = compareInfo.Compare(a.ComplexFormHeadword, b.ComplexFormHeadword, CompareOptions.IgnoreCase);
if (result != 0) return result;
return a.ComplexFormEntryId.CompareTo(b.ComplexFormEntryId);
}

private static CompareInfo GetCompareInfo(WritingSystem? writingSystem)
{
if (writingSystem == null || writingSystem.WsId == WritingSystemId.Default)
{
return CultureInfo.InvariantCulture.CompareInfo;
}

try
{
//todo use ICU/SLDR instead
return CultureInfo.CreateSpecificCulture(writingSystem.WsId.Code).CompareInfo;
}
catch
{
return CultureInfo.InvariantCulture.CompareInfo;
}
}
}
2 changes: 1 addition & 1 deletion backend/FwLite/MiniLcm/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public record Entry : IObjectWithId<Entry>
/// <summary>
/// This entry is a part of these complex forms
/// </summary>
public virtual IList<ComplexFormComponent> ComplexForms { get; set; } = [];
public virtual List<ComplexFormComponent> ComplexForms { get; set; } = [];

public virtual IList<ComplexFormType> ComplexFormTypes { get; set; } = [];

Expand Down
2 changes: 2 additions & 0 deletions backend/FwLite/MiniLcm/Models/WritingSystemId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public override void WriteAsPropertyName(Utf8JsonWriter writer, WritingSystemId
{
public string Code { get; init; }

public static readonly WritingSystemId Default = "default";

public WritingSystemId(string code)
{
//__key is used by the LfClassicMiniLcmApi to smuggle non guid ids with possibilitie lists
Expand Down

0 comments on commit a299fa5

Please sign in to comment.