Skip to content

Commit

Permalink
fix update writing system api in fwdata bridge so that it works, writ…
Browse files Browse the repository at this point in the history
…e a test for updating the abbreviation
  • Loading branch information
hahn-kev committed Jan 27, 2025
1 parent daaac0c commit 486e7d9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
13 changes: 9 additions & 4 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,15 @@ private WritingSystem FromLcmWritingSystem(CoreWritingSystemDefinition ws, int i
};
}

public Task<WritingSystem> GetWritingSystem(WritingSystemId id, WritingSystemType type)
public async Task<WritingSystem> GetWritingSystem(WritingSystemId id, WritingSystemType type)
{
throw new NotImplementedException();
var writingSystems = await GetWritingSystems();
return type switch
{
WritingSystemType.Vernacular => writingSystems.Vernacular.FirstOrDefault(ws => ws.WsId == id),
WritingSystemType.Analysis => writingSystems.Analysis.FirstOrDefault(ws => ws.WsId == id),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
} ?? throw new NullReferenceException($"unable to find writing system with id {id}");
}

internal void CompleteExemplars(WritingSystems writingSystems)
Expand Down Expand Up @@ -208,13 +214,12 @@ await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem",
"Revert WritingSystem",
async () =>

Check warning on line 215 in backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FwHeadless / publish-fw-headless

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 215 in backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 215 in backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Linux

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 215 in backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 215 in backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 215 in backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Windows

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var updateProxy = new UpdateWritingSystemProxy(lcmWritingSystem, this)
var updateProxy = new UpdateWritingSystemProxy(lcmWritingSystem)
{
Id = Guid.Empty,
Type = type,
};
update.Apply(updateProxy);
updateProxy.CommitUpdate(Cache);
});
return await GetWritingSystem(id, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,43 @@ namespace FwDataMiniLcmBridge.Api.UpdateProxy;

public record UpdateWritingSystemProxy : WritingSystem
{
private readonly CoreWritingSystemDefinition _origLcmWritingSystem;
private readonly CoreWritingSystemDefinition _workingLcmWritingSystem;
private readonly FwDataMiniLcmApi _lexboxLcmApi;
private readonly CoreWritingSystemDefinition _lcmWritingSystem;

[SetsRequiredMembers]
public UpdateWritingSystemProxy(CoreWritingSystemDefinition lcmWritingSystem, FwDataMiniLcmApi lexboxLcmApi)
public UpdateWritingSystemProxy(CoreWritingSystemDefinition lcmWritingSystem)
{
_origLcmWritingSystem = lcmWritingSystem;
_workingLcmWritingSystem = new CoreWritingSystemDefinition(lcmWritingSystem, cloneId: true);
base.Abbreviation = Abbreviation = _origLcmWritingSystem.Abbreviation ?? "";
base.Name = Name = _origLcmWritingSystem.LanguageName ?? "";
base.Font = Font = _origLcmWritingSystem.DefaultFontName ?? "";
_lexboxLcmApi = lexboxLcmApi;
}

public void CommitUpdate(LcmCache cache)
{
if (_workingLcmWritingSystem.Id == _origLcmWritingSystem.Id)
{
cache.ServiceLocator.WritingSystemManager.Set(_workingLcmWritingSystem);
}
else
{
// Changing the ID of a writing system requires LCM to do a lot of work, so only go through that process if absolutely required
WritingSystemServices.MergeWritingSystems(cache, _workingLcmWritingSystem, _origLcmWritingSystem);
}
_lcmWritingSystem = lcmWritingSystem;
base.Abbreviation = Abbreviation = _lcmWritingSystem.Abbreviation ?? "";
base.Name = Name = _lcmWritingSystem.LanguageName ?? "";
base.Font = Font = _lcmWritingSystem.DefaultFontName ?? "";
}

public override required WritingSystemId WsId
{
get => _workingLcmWritingSystem.Id;
set => _workingLcmWritingSystem.Id = value;
get => _lcmWritingSystem.Id;
set => throw new NotSupportedException("Changing the ID of a writing system is not supported");
}

public override required string Name
{
get => _workingLcmWritingSystem.LanguageName;
get => _lcmWritingSystem.LanguageName;
set { } // Silently do nothing; name should be derived from WsId at all times, so if the name should change then so should the WsId
}

public override required string Abbreviation
{
get => _workingLcmWritingSystem.Abbreviation;
set => _workingLcmWritingSystem.Abbreviation = value;
get => _lcmWritingSystem.Abbreviation;
set => _lcmWritingSystem.Abbreviation = value;
}

public override required string Font
{
get => _workingLcmWritingSystem.DefaultFontName;
get => _lcmWritingSystem.DefaultFontName;
set
{
if (value != _workingLcmWritingSystem.DefaultFontName)
if (value != _lcmWritingSystem.DefaultFontName)
{
_workingLcmWritingSystem.DefaultFont = new FontDefinition(value);
_lcmWritingSystem.DefaultFont = new FontDefinition(value);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions backend/FwLite/MiniLcm.Tests/WritingSystemTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ await Api.CreateWritingSystem(WritingSystemType.Vernacular,
});
await action.Should().ThrowAsync<DuplicateObjectException>();
}

[Fact]
public async Task UpdateExistingWritingSystem_Works()
{
var writingSystems = await Api.GetWritingSystems();
var writingSystem = writingSystems.Vernacular.First();
var original = writingSystem.Copy();
writingSystem.Abbreviation = "New Abbreviation";
var updatedWritingSystem = await Api.UpdateWritingSystem(original, writingSystem);
updatedWritingSystem.Abbreviation.Should().Be("New Abbreviation");
}
}
4 changes: 2 additions & 2 deletions backend/FwLite/MiniLcm/Models/WritingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MiniLcm.Models;

public record WritingSystem: IObjectWithId
public record WritingSystem: IObjectWithId<WritingSystem>
{
public required Guid Id { get; set; }
public virtual required WritingSystemId WsId { get; set; }
Expand All @@ -28,7 +28,7 @@ public void RemoveReference(Guid id, DateTimeOffset time)
{
}

public IObjectWithId Copy()
public WritingSystem Copy()
{
return new WritingSystem
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
</script>
<form class="flex flex-col gap-2 p-2">
<CrdtTextField label="Language Code" readonly={!newWs} on:change={() => onChange()} bind:value={writingSystem.wsId}/>
<CrdtTextField label="Name" on:change={() => onChange()} bind:value={writingSystem.name}/>
<!-- todo changing the name for FieldWorks writing systems is not yet supported-->
<CrdtTextField label="Name" readonly={!newWs} on:change={() => onChange()} bind:value={writingSystem.name}/>
<CrdtTextField label="Abbreviation" on:change={() => onChange()} bind:value={writingSystem.abbreviation}/>
<CrdtTextField label="Font" on:change={() => onChange()} bind:value={writingSystem.font}/>
{#if newWs}
Expand Down

0 comments on commit 486e7d9

Please sign in to comment.