Skip to content

Commit

Permalink
Renaming 'Context' to Facade'.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanstardev committed Jan 14, 2025
1 parent 4617f8e commit 6af8f88
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 69 deletions.
8 changes: 4 additions & 4 deletions RMVC/RCommand.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace RMVC {
public abstract class RCommand : RCommandBase {

protected internal RFacade context;
protected internal RFacade? facade;
protected abstract void Run();


internal void RunInternal(RFacade context) {
this.context = context;
internal void RunInternal(RFacade facade) {
this.facade = facade;
Run();
}
internal override void ExecuteCommandInternal(RCommand command) {
context.ExecuteCommand(command);
facade?.ExecuteCommand(command);
}

}
Expand Down
4 changes: 2 additions & 2 deletions RMVC/RCommandAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ protected async Task ExecuteCommandAsync(
command.hasParent = true;
if (rTracker != null) {
// Execute command with a scaled RTracker child using the provided percentCap
await rTracker.context.ExecuteCommandAsync(command, rTracker.CreateChild(command, percentCap), percentCap);
await rTracker.facade.ExecuteCommandAsync(command, rTracker.CreateChild(command, percentCap), percentCap);
}
}
internal override void ExecuteCommandInternal(RCommand command) {
rTracker?.context.ExecuteCommand(command);
rTracker?.facade.ExecuteCommand(command);
}
protected virtual void OnCommandExit(bool success) { }
internal void HandleThreadExit() {
Expand Down
2 changes: 1 addition & 1 deletion RMVC/RCommandExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public abstract class RCommandExecutorBase : RActor {
protected RCommandExecutorBase() {
}
protected void ExecuteCommand(RCommandBase command) {
rCommander?.Context.ExecuteCommand(command);
rCommander?.Facade.ExecuteCommand(command);
}
}
}
8 changes: 4 additions & 4 deletions RMVC/RCommander.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace RMVC {
internal class RCommander {
internal RFacade Context;
internal RCommander(RFacade context) {
Context = context;
internal RFacade? Facade;
internal RCommander(RFacade facade) {
Facade = facade;
}
public void ExecuteCommand(RCommandBase command) {
Context.ExecuteCommand(command);
Facade?.ExecuteCommand(command);
}
}
}
98 changes: 49 additions & 49 deletions RMVC/RFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class RFacade {

private DateTime lastProgressUpdateTime = DateTime.MinValue;

private static RFacade? promoterContext = null;
private static RFacade? promoterFacade = null;
private static Type? promoterType = null;
internal static IRAppShell? AppShell { get; private set; } = null;

Expand All @@ -29,67 +29,67 @@ public abstract class RFacade {
private readonly Dictionary<Type, RMediator> mediatorsDictionary = new Dictionary<Type, RMediator>();
private readonly Dictionary<Type, RModel> modelsDictionary = new Dictionary<Type, RModel>();

private static readonly Dictionary<Type, RFacade> activeContexts = new Dictionary<Type, RFacade>();
private static readonly Dictionary<Type, RFacade> activeFacades = new Dictionary<Type, RFacade>();

private static readonly HashSet<Type> pendingContexts = new HashSet<Type>();
private static readonly HashSet<Type> pendingFacades = new HashSet<Type>();

public static void Create(Type contextConcreteType, IRAppShell? appShell = null)
public static void Create(Type facadeConcreteType, IRAppShell? appShell = null)
{
if (contextConcreteType == null) {
throw new ArgumentNullException(nameof(contextConcreteType), "The contextConcreteType cannot be null.");
if (facadeConcreteType == null) {
throw new ArgumentNullException(nameof(facadeConcreteType), "The facadeConcreteType cannot be null.");
}

if (activeContexts.ContainsKey(contextConcreteType)) {
throw new InvalidOperationException($"Could not instantiate: {contextConcreteType}. It may already have been created.");
if (activeFacades.ContainsKey(facadeConcreteType)) {
throw new InvalidOperationException($"Could not instantiate: {facadeConcreteType}. It may already have been created.");
}

try {
pendingContexts.Add(contextConcreteType);
var context = Activator.CreateInstance(contextConcreteType) as RFacade;
pendingFacades.Add(facadeConcreteType);
var facade = Activator.CreateInstance(facadeConcreteType) as RFacade;

if (context == null) {
throw new InvalidOperationException("Failed to create an instance of the context.");
if (facade == null) {
throw new InvalidOperationException("Failed to create an instance of the facade.");
}

activeContexts.Add(contextConcreteType, context);
activeFacades.Add(facadeConcreteType, facade);

if (promoterType != null && contextConcreteType.Equals(promoterType)) {
promoterContext = context;
if (promoterType != null && facadeConcreteType.Equals(promoterType)) {
promoterFacade = facade;
}
else if (promoterContext == null) {
promoterContext = context;
else if (promoterFacade == null) {
promoterFacade = facade;
}

if (AppShell == null) {
AppShell = appShell;
}

context.rCommander = new RCommander(context);
facade.rCommander = new RCommander(facade);

var models = context.RegisterModels();
var models = facade.RegisterModels();
foreach (RModel model in models) {
context.modelsDictionary.Add(model.GetType(), model);
model.rCommander = context.rCommander;
facade.modelsDictionary.Add(model.GetType(), model);
model.rCommander = facade.rCommander;
model.Initialise();
}

var mediators = context.RegisterMediators();
var mediators = facade.RegisterMediators();
foreach (RMediator mediator in mediators) {
context.mediatorsDictionary.Add(mediator.GetType(), mediator);
mediator.rCommander = context.rCommander;
facade.mediatorsDictionary.Add(mediator.GetType(), mediator);
mediator.rCommander = facade.rCommander;
}

RCommandBase startupCommand = context.RegisterStartupCommand();
RCommandBase startupCommand = facade.RegisterStartupCommand();

if (startupCommand != null) {
context.ExecuteCommand(startupCommand);
facade.ExecuteCommand(startupCommand);
}

Log($"Context execution completed: {contextConcreteType?.Name}.");
Log($"Facade execution completed: {facadeConcreteType?.Name}.");
}
catch (Exception ex) {
pendingContexts.Remove(contextConcreteType);
Log($"Cannot instantiate Context instance. Error: {ex.Message}");
pendingFacades.Remove(facadeConcreteType);
Log($"Cannot instantiate Facade instance. Error: {ex.Message}");
throw;
}
}
Expand All @@ -98,11 +98,11 @@ public static void RegisterView(IRViewContract view) {

RMediator? foundMediator = null;

foreach (var context in activeContexts.Values) {
if (context is null || context.mediatorsDictionary is null)
foreach (var facade in activeFacades.Values) {
if (facade is null || facade.mediatorsDictionary is null)
continue;

foreach (var mediator in context.mediatorsDictionary.Values) {
foreach (var mediator in facade.mediatorsDictionary.Values) {
if (mediator.viewBaseType.IsAssignableFrom(view.GetType())) {
foundMediator = mediator;

Expand All @@ -129,11 +129,11 @@ public static void UnregisterView(IRViewContract view) {

RMediator? foundMediator = null;

foreach (var context in activeContexts.Values) {
if (context is null || context.mediatorsDictionary is null)
foreach (var facade in activeFacades.Values) {
if (facade is null || facade.mediatorsDictionary is null)
continue;

foreach (var mediator in context.mediatorsDictionary.Values) {
foreach (var mediator in facade.mediatorsDictionary.Values) {
if (mediator.viewBaseType.IsAssignableFrom(view.GetType())) {
foundMediator = mediator;
if (foundMediator.viewBase == null) {
Expand All @@ -158,7 +158,7 @@ public static void UnregisterView(IRViewContract view) {
public static void Configure(Type? promoterType = null) {
if (hasBeenConfigured)
{
Error("RContext.Configure(...) should only be called once at the start of the application's life.");
Error("RFacade.Configure(...) should only be called once at the start of the application's life.");
}
RFacade.promoterType = promoterType;
hasBeenConfigured = true;
Expand Down Expand Up @@ -220,7 +220,7 @@ double percentCap
}
catch (Exception ex) {
Log("ERROR CAUGHT in async command: " + ex.ToString());
asyncCommand.SetErrorInternal("RContext Async Execution Error.");
asyncCommand.SetErrorInternal("RFacade Async Execution Error.");
}
}, cancellationTokenSource.Token);

Expand All @@ -246,9 +246,9 @@ double percentCap
protected abstract RMediator[] RegisterMediators();
protected abstract RModel[] RegisterModels();

protected static TContext? ContextInstance<TContext>() where TContext : RFacade {
if (activeContexts.TryGetValue(typeof(TContext), out RFacade? context)) {
return context as TContext;
protected static TFacade? FacadeInstance<TFacade>() where TFacade : RFacade {
if (activeFacades.TryGetValue(typeof(TFacade), out RFacade? facade)) {
return facade as TFacade;
}
return null;
}
Expand All @@ -268,30 +268,30 @@ double percentCap
}

protected static RFacade? GetPromoter() {
return promoterContext;
return promoterFacade;
}
private void HandleTaskComplete(RCommandAsync command) {

command.HandleThreadExit();

if (!command.hasParent && command.EnableAutoUpdate) {
command.rTracker?.context.HandleProgressComplete();
command.rTracker?.facade.HandleProgressComplete();
}
}


protected RFacade() {
if (!pendingContexts.Contains(GetType())){
Fatal("Do not instantiate a Context directly. Use the static Initialise() method.");
if (!pendingFacades.Contains(GetType())){
Fatal("Do not instantiate a Facade directly. Use the static Initialise() method.");
}
pendingContexts.Remove(GetType());
pendingFacades.Remove(GetType());
}


private void HandleProgressComplete() {
if (promoterContext == null) return;
if (promoterFacade == null) return;

RCommandBase? cmd = promoterContext.RegisterProgressCompleteCommand();
RCommandBase? cmd = promoterFacade.RegisterProgressCompleteCommand();
if (cmd != null) {
ExecuteCommand(cmd);
}
Expand All @@ -303,9 +303,9 @@ internal void HandleProgressChange(RProgress[] progress) {
else
lastProgressUpdateTime = DateTime.Now;

if (promoterContext == null) return;
if (promoterFacade == null) return;

RCommandBase? cmd = promoterContext.RegisterProgressUpdateCommand(progress);
RCommandBase? cmd = promoterFacade.RegisterProgressUpdateCommand(progress);
if (cmd != null) {
ExecuteCommand(cmd);
}
Expand Down
10 changes: 5 additions & 5 deletions RMVC/RTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class RTracker {
internal bool ErrorOrAbort { get { return _error || _abort; } }

internal RCommandAsync _command;
internal RFacade context;
internal RFacade facade;
private readonly double _cap;
private readonly bool _allowAutoUpdate;

Expand All @@ -27,7 +27,7 @@ internal class RTracker {
private bool _abort;
private bool _error;

internal RTracker(RCommandAsync command, RFacade context, double cap, CancellationToken cancellationToken) {
internal RTracker(RCommandAsync command, RFacade facade, double cap, CancellationToken cancellationToken) {
Id = Guid.NewGuid().ToString();
_cap = cap;
_parent = null;
Expand All @@ -36,7 +36,7 @@ internal RTracker(RCommandAsync command, RFacade context, double cap, Cancellati
_command = command;
_allowAutoUpdate = command.EnableAutoUpdate;

this.context = context;
this.facade = facade;
_text = null;
_title = null;
_abort = false;
Expand Down Expand Up @@ -125,7 +125,7 @@ internal RTracker CreateChild(RCommandAsync command, double percentCap) {
percentCap = RHelper.ClampPercent(percentCap); // Ensure cap is within bounds
Log($"Creating child tracker for {command.GetType().Name} with cap: {percentCap}");

_child = new RTracker(command, context, percentCap * (_cap / 100d), Token);
_child = new RTracker(command, facade, percentCap * (_cap / 100d), Token);
_child._parent = this;

return _child;
Expand Down Expand Up @@ -161,7 +161,7 @@ private void SendProgress() {
var progressSet = GetProgressReport();

if (progressSet.Length > 0) {
context.HandleProgressChange(progressSet);
facade.HandleProgressChange(progressSet);
}
}

Expand Down
1 change: 0 additions & 1 deletion Sample/RMVCApp.Sample.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public override void OnFrameworkInitializationCompleted() {
DataContext = new MainWindowViewModel(),
};

//RMVCApp.RMVC.Context.Configure((MainWindowViewModel)mainWindow.DataContext);
RMVCAppFacade.Create(typeof(RMVCAppFacade), this);

desktop.MainWindow = mainWindow;
Expand Down
2 changes: 1 addition & 1 deletion Sample/RMVCApp.Sample.Core/RMVCAppFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace RMVCApp.Sample.Core {

public class RMVCAppFacade : RFacade {
internal static RMVCAppFacade? Instance => RFacade.ContextInstance<RMVCAppFacade>();
internal static RMVCAppFacade? Instance => RFacade.FacadeInstance<RMVCAppFacade>();

// Models
internal CounterModel? CounterModel => base.Model<CounterModel>();
Expand Down
Binary file modified Sample/RMVCApp.Sample.Unity/Assets/Plugins/RMVC.dll
Binary file not shown.
Binary file not shown.
2 changes: 0 additions & 2 deletions Sample/RMVCApp.Sample.WPF/ViewModels/WeatherViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ public void SetView(WeatherForecastDTO[]? forecasts) {
}

public void OnInitialised() {
// Register the view with the context
RMVCAppFacade.RegisterView(this);
}

public void OnDisposed() {
// Unregister the view from the context
RMVCAppFacade.UnregisterView(this);
}

Expand Down

0 comments on commit 6af8f88

Please sign in to comment.