Skip to content

Commit

Permalink
[Solutions] Async solution name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Apr 30, 2024
1 parent 675b3e5 commit 38d61f7
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using WDE.Common.Database;
using System.Threading.Tasks;
using WDE.Common.Database;
using WDE.Common.Parameters;
using WDE.Common.Solution;
using WDE.DatabaseEditors.Data.Interfaces;
Expand All @@ -7,7 +8,8 @@
namespace WDE.DatabaseEditors.Solution
{
[AutoRegister]
public class DatabaseTableSolutionItemNameProvider : ISolutionNameProvider<DatabaseTableSolutionItem>
public class DatabaseTableSolutionItemNameProvider : ISolutionNameProvider<DatabaseTableSolutionItem>,
ISolutionNameProviderAsync<DatabaseTableSolutionItem>
{
private readonly IParameterFactory parameterFactory;
private readonly ITableDefinitionProvider tableDefinitionProvider;
Expand Down Expand Up @@ -38,5 +40,31 @@ public string GetName(DatabaseTableSolutionItem item)

return definition.MultiSolutionName;
}

public async Task<string> GetNameAsync(DatabaseTableSolutionItem item)
{
var definition = tableDefinitionProvider.GetDefinition(item.TableName);
if (definition == null)
return $"Unknown item (" + item.TableName + ")";

var parameter = parameterFactory.Factory(definition.Picker);

if (item.Entries.Count == 1)
{
string name;
if (parameter is IAsyncParameter<long> asyncParameter)
{
name = await asyncParameter.ToStringAsync(item.Entries[0].Key[0], default);
}
else
{
name = parameter.ToString(item.Entries[0].Key[0]);
}

return definition.SingleSolutionName.Replace("{name}", name).Replace("{key}", item.Entries[0].Key.ToString());
}

return definition.MultiSolutionName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading.Tasks;
using AvaloniaStyles.Controls.FastTableView;
using PropertyChanged.SourceGenerator;
using WDE.Common.Services;
using WDE.Common.Utils;
using WDE.DatabaseEditors.Models;

namespace WDE.DatabaseEditors.ViewModels.MultiRow
Expand All @@ -12,7 +15,7 @@ public partial class DatabaseEntitiesGroupViewModel : CustomObservableCollection
{
private bool isExpanded = true;
public DatabaseKey Key { get; }
public string Name { get; }
[Notify] private string name;

public bool IsExpanded
{
Expand All @@ -25,10 +28,16 @@ public bool IsExpanded
}
}

public DatabaseEntitiesGroupViewModel(DatabaseKey key, string name)
public DatabaseEntitiesGroupViewModel(DatabaseKey key, string name, Func<Task<string>> nameLazyGetter)
{
Key = key;
Name = name;
this.name = name;
async Task SetName()
{
Name = await nameLazyGetter();
}

SetName().ListenErrors();
}

public override void OrderByIndices(List<int> indices)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ private void EnsureKey(DatabaseKey entity)
{
if (keys.Add(entity))
{
byEntryGroups[entity] = new DatabaseEntitiesGroupViewModel(entity, GenerateName(entity[0]));
byEntryGroups[entity] = new DatabaseEntitiesGroupViewModel(entity, GenerateName(entity[0]), () => GenerateNameAsync(entity[0]));
Rows.Add(byEntryGroups[entity]);
entities.Add(new CustomObservableCollection<DatabaseEntity>());
}
Expand Down
1 change: 1 addition & 0 deletions WDE.DatabaseEditors/ViewModels/RowPickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public RowPickerViewModel(ViewModelBase baseViewModel,
this.messageBoxService = messageBoxService;
this.noSaveMode = noSaveMode;
Watch(baseViewModel, o => o.IsModified, nameof(Title));
Watch(baseViewModel, o => o.Title, nameof(Title));
ExecuteChangedCommand = noSaveMode ? AlwaysDisabledAsyncCommand.Command : new AsyncAutoCommand(async () =>
{
await baseViewModel.Save.ExecuteAsync();
Expand Down
18 changes: 16 additions & 2 deletions WDE.DatabaseEditors/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ protected ViewModelBase(IHistoryManager history,
redoCommand = new DelegateCommand(History.Redo, CanRedo);
Save = new AsyncAutoCommand(SaveSolutionItem);
title = solutionItemName.GetName(solutionItem);
async Task GetTitleAsync()
{
Title = await solutionItemName.GetNameAsync(solutionItem);
}
GetTitleAsync().ListenErrors();
Icon = iconRegistry.GetIcon(solutionItem);
nameGeneratorParameter = parameterFactory.Factory("Parameter");

Expand Down Expand Up @@ -210,7 +215,7 @@ await messageBoxService.ShowDialog(new MessageBoxFactory<bool>()
}
progress.ReportFinished();
});
Title = solutionItemName.GetName(SolutionItem);
Title = await solutionItemName.GetNameAsync(SolutionItem);
}

protected virtual Task AfterSave() => Task.CompletedTask;
Expand All @@ -220,7 +225,7 @@ await messageBoxService.ShowDialog(new MessageBoxFactory<bool>()

protected virtual async Task<DatabaseTableData?> LoadData()
{
return await databaseTableDataProvider.Load(solutionItem.TableName, null, null, null, solutionItem.Entries.Select(e => e.Key).ToArray()) as DatabaseTableData; ;
return await databaseTableDataProvider.Load(solutionItem.TableName, null, null, null, solutionItem.Entries.Select(e => e.Key).ToArray()) as DatabaseTableData;
}

protected async Task<bool> InternalLoadData()
Expand Down Expand Up @@ -321,6 +326,15 @@ protected string GenerateName(long entity)
{
return nameGeneratorParameter.ToString(entity);
}

protected async Task<string> GenerateNameAsync(long entity)
{
if (nameGeneratorParameter is IAsyncParameter<long> lp)
{
return await lp.ToStringAsync(entity, default);
}
return nameGeneratorParameter.ToString(entity);
}

protected DatabaseTableDefinitionJson tableDefinition = null!;
public DatabaseTableDefinitionJson TableDefinition => tableDefinition;
Expand Down
29 changes: 28 additions & 1 deletion WDE.Solutions/Manager/SolutionItemNameRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WDE.Common;
using WDE.Common.Solution;
using WDE.Module.Attributes;
Expand All @@ -10,24 +11,39 @@ namespace WDE.Solutions.Manager
public class SolutionItemNameRegistry : ISolutionItemNameRegistry
{
private readonly Dictionary<Type, object> nameProviders = new();
private readonly Dictionary<Type, object> asyncNameProviders = new();

public SolutionItemNameRegistry(IEnumerable<ISolutionNameProvider> providers)
public SolutionItemNameRegistry(IEnumerable<ISolutionNameProvider> providers,
IEnumerable<ISolutionNameProviderAsync> asyncProviders)
{
// handy trick with (dynamic) cast, thanks to this proper Generic method will be called!
foreach (ISolutionNameProvider provider in providers)
Register((dynamic) provider);

foreach (ISolutionNameProviderAsync provider in asyncProviders)
RegisterAsync((dynamic)provider);
}

public string GetName(ISolutionItem item)
{
return GetName((dynamic) item);
}

public Task<string> GetNameAsync(ISolutionItem item)
{
return GetNameAsync((dynamic) item);
}

private void Register<T>(ISolutionNameProvider<T> provider) where T : ISolutionItem
{
nameProviders.Add(typeof(T), provider);
}

private void RegisterAsync<T>(ISolutionNameProviderAsync<T> provider) where T : ISolutionItem
{
asyncNameProviders.Add(typeof(T), provider);
}

private string GetName<T>(T item) where T : ISolutionItem
{
if (nameProviders.TryGetValue(item.GetType(), out var nameProvider))
Expand All @@ -41,5 +57,16 @@ private string GetName<T>(T item) where T : ISolutionItem
return item.GetType().Name;
#endif
}

private async Task<string> GetNameAsync<T>(T item) where T : ISolutionItem
{
if (asyncNameProviders.TryGetValue(item.GetType(), out var nameProvider))
{
var x = (ISolutionNameProviderAsync<T>)nameProvider;
return await x.GetNameAsync(item);
}

return GetName<T>(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using WDE.Module.Attributes;
using System.Threading.Tasks;
using WDE.Module.Attributes;

namespace WDE.Common.Solution
{
[UniqueProvider]
public interface ISolutionItemNameRegistry
{
string GetName(ISolutionItem item);
Task<string> GetNameAsync(ISolutionItem item);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using WDE.Module.Attributes;
using System.Threading.Tasks;
using WDE.Module.Attributes;

namespace WDE.Common.Solution
{
Expand All @@ -12,4 +13,15 @@ public interface ISolutionNameProvider<T> : ISolutionNameProvider where T : ISol
{
string GetName(T item);
}

[NonUniqueProvider]
public interface ISolutionNameProviderAsync
{
}

[NonUniqueProvider]
public interface ISolutionNameProviderAsync<T> : ISolutionNameProviderAsync where T : ISolutionItem
{
Task<string> GetNameAsync(T item);
}
}

0 comments on commit 38d61f7

Please sign in to comment.