Skip to content

Commit

Permalink
Merge branch 'feature/scheduler-cleanup' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstonis committed Oct 1, 2023
2 parents 1559186 + 9d64ae6 commit 9ef952a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 25 deletions.
78 changes: 74 additions & 4 deletions Stellar.Avalonia/UserControlBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.ComponentModel;
using System.ComponentModel;
using System.Reactive;
using System.Reactive.Disposables;
using Avalonia;
using Avalonia.Controls;
using Avalonia.ReactiveUI;

namespace Stellar.Avalonia;
Expand Down Expand Up @@ -66,4 +64,76 @@ public void Dispose()
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

public abstract class UserControlBase<TViewModel, TDataModel> : ReactiveUserControl<TViewModel>, IStellarView<TViewModel>
where TViewModel : class
{
private bool _isDisposed;

[EditorBrowsable(EditorBrowsableState.Never)]
public ViewManager ViewManager { get; } = new AvaloniaViewManager<TViewModel>();

public IObservable<Unit> UserControlInitialized => ViewManager.Initialized;

public IObservable<Unit> Activated => ViewManager.Activated;

public IObservable<Unit> Deactivated => ViewManager.Deactivated;

public IObservable<Unit> Disposed => ViewManager.Disposed;

public IObservable<LifecycleEvent> Lifecycle => ViewManager.Lifecycle;

public virtual void Initialize()
{
}

public abstract void SetupUserInterface();

public abstract void Bind(CompositeDisposable disposables);

protected abstract void MapDataModelToViewModel(TViewModel viewModel, TDataModel dataModel);

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);

ViewManager.HandleActivated(this);
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
ViewManager.OnLifecycle(LifecycleEvent.IsDisappearing);

ViewManager.HandleDeactivated(this);

base.OnDetachedFromVisualTree(e);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
ViewManager.PropertyChanged<UserControlBase<TViewModel, TDataModel>, TViewModel>(this, change.Property.Name);

base.OnPropertyChanged(change);
}

protected override void OnDataContextEndUpdate()
{
base.OnDataContextEndUpdate();

if (ViewModel is not null && DataContext is TDataModel dataModel)
{
MapDataModelToViewModel(ViewModel, dataModel);
}
}

protected virtual void Dispose(bool disposing) =>
this.ManageDispose(disposing, ref _isDisposed);

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
23 changes: 8 additions & 15 deletions Stellar.Maui/Extensions/MauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,18 @@ public static MauiAppBuilder PreCacheComponents<TStellarAssembly>(this MauiAppBu

public static MauiAppBuilder UseStellarComponents<TStellarAssembly>(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.UseMicrosoftDependencyResolver();

PlatformRegistrationManager.SetRegistrationNamespaces(RegistrationNamespace.Maui);
Locator.CurrentMutable.InitializeSplat();
Locator.CurrentMutable.InitializeReactiveUI();

RxApp.TaskpoolScheduler = Schedulers.ShortTermThreadPoolScheduler;

mauiAppBuilder.Services.AddSingleton(
serviceProvider =>
{
var dispatcher = serviceProvider.GetRequiredService<IDispatcher>();
var mauiScheduler = new MauiScheduler(dispatcher);
RxApp.MainThreadScheduler = mauiScheduler;
return mauiScheduler;
});
mauiAppBuilder.Services.UseMicrosoftDependencyResolver();

mauiAppBuilder.Services.TryAddEnumerable(ServiceDescriptor.Scoped<IMauiInitializeScopedService, MauiSchedulerInitializer>());
mauiAppBuilder
.Services
.AddSingleton(serviceProvider => new MauiScheduler(serviceProvider.GetRequiredService<IDispatcher>()))
.ConfigureStellarComponents(typeof(TStellarAssembly).GetTypeInfo().Assembly);

mauiAppBuilder.Services.ConfigureStellarComponents(typeof(TStellarAssembly).GetTypeInfo().Assembly);
mauiAppBuilder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IMauiInitializeScopedService, MauiSchedulerInitializer>());

return mauiAppBuilder;
}
Expand All @@ -45,7 +37,8 @@ private class MauiSchedulerInitializer : IMauiInitializeScopedService
{
public void Initialize(IServiceProvider services)
{
_ = services.GetRequiredService<MauiScheduler>();
RxApp.MainThreadScheduler = services.GetRequiredService<MauiScheduler>();
RxApp.TaskpoolScheduler = Schedulers.ShortTermThreadPoolScheduler;
}
}

Expand Down
12 changes: 6 additions & 6 deletions Stellar.Maui/MauiScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisp
innerDisp.Disposable = action(this, state);
}
});

return innerDisp;
}
else

if (!innerDisp.IsDisposed)
{
if (!innerDisp.IsDisposed)
{
innerDisp.Disposable = action(this, state);
}
innerDisp.Disposable = action(this, state);
}

return innerDisp;
Expand Down Expand Up @@ -72,4 +72,4 @@ public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<I

return Schedule(state, dueTime - Now, action);
}
}
}
1 change: 1 addition & 0 deletions Stellar.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "[Solution Items]", "[Soluti
.gitignore = .gitignore
Directory.build.props = Directory.build.props
stylecop.json = stylecop.json
global.json = global.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stellar.MauiSample", "Stellar.MauiSample\Stellar.MauiSample.csproj", "{19579D26-E2AD-4B24-BF34-068EEDDB8C92}"
Expand Down
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"rollForward": "latestMajor",
"allowPrerelease": false
}
}

0 comments on commit 9ef952a

Please sign in to comment.