-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73047d4
commit 7aacbe8
Showing
12 changed files
with
282 additions
and
43 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Maui.ServerDrivenUI/Abstractions/IServerDrivenVisualElement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Maui.ServerDrivenUI; | ||
|
||
public interface IServerDrivenVisualElement | ||
{ | ||
public string? ServerKey | ||
{ | ||
get; set; | ||
} | ||
|
||
public UIElementState State | ||
{ | ||
get; set; | ||
} | ||
public Action OnLoaded | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
void OnStateChanged(UIElementState newState); | ||
void OnError(Exception ex); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Maui.ServerDrivenUI; | ||
|
||
public enum UIElementState | ||
{ | ||
None = 0, | ||
Loading = 1, | ||
Loaded = 2, | ||
Error = 3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Maui.ServerDrivenUI.Services; | ||
|
||
internal static class ServiceProviderHelper | ||
{ | ||
public static IServiceProvider? ServiceProvider => | ||
#if WINDOWS | ||
MauiWinUIApplication.Current?.Services; | ||
#else | ||
IPlatformApplication.Current?.Services; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Maui.ServerDrivenUI.Views; | ||
|
||
namespace Maui.ServerDrivenUI; | ||
|
||
public abstract class ServerDrivenContentPage : ContentPage, IServerDrivenVisualElement | ||
{ | ||
#region BindableProperties | ||
|
||
public static readonly BindableProperty ServerKeyProperty = BindableProperty.Create( | ||
nameof(ServerKey), | ||
typeof(string), | ||
typeof(ServerDrivenContentPage), | ||
null); | ||
|
||
public static readonly BindableProperty StateProperty = BindableProperty.Create( | ||
nameof(State), | ||
typeof(UIElementState), | ||
typeof(ServerDrivenContentPage), | ||
UIElementState.None, | ||
propertyChanged: ServerDrivenVisualElement.OnStatePropertyChanged); | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public string? ServerKey | ||
{ | ||
get => (string?)GetValue(ServerKeyProperty); | ||
set => SetValue(ServerKeyProperty, value); | ||
} | ||
|
||
public UIElementState State | ||
{ | ||
get => (UIElementState)GetValue(StateProperty); | ||
set => SetValue(StateProperty, value); | ||
} | ||
|
||
public Action OnLoaded | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
public ServerDrivenContentPage() => | ||
_ = Task.Run(InitializeComponentAsync); | ||
|
||
#endregion | ||
|
||
#region Protected Methods | ||
|
||
protected virtual Task InitializeComponentAsync() => | ||
ServerDrivenVisualElement.InitializeComponentAsync(this); | ||
|
||
public virtual void OnStateChanged(UIElementState newState) | ||
{ | ||
} | ||
|
||
public virtual void OnError(Exception ex) | ||
{ | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Maui.ServerDrivenUI.Views; | ||
|
||
namespace Maui.ServerDrivenUI; | ||
|
||
public class ServerDrivenView : ContentView, IServerDrivenVisualElement | ||
{ | ||
#region BindableProperties | ||
|
||
public static readonly BindableProperty ServerKeyProperty = BindableProperty.Create( | ||
nameof(ServerKey), | ||
typeof(string), | ||
typeof(ServerDrivenView), | ||
null); | ||
|
||
public static readonly BindableProperty StateProperty = BindableProperty.Create( | ||
nameof(State), | ||
typeof(UIElementState), | ||
typeof(ServerDrivenView), | ||
UIElementState.None, | ||
propertyChanged: ServerDrivenVisualElement.OnStatePropertyChanged); | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public string? ServerKey | ||
{ | ||
get => (string?)GetValue(ServerKeyProperty); | ||
set => SetValue(ServerKeyProperty, value); | ||
} | ||
|
||
public UIElementState State | ||
{ | ||
get => (UIElementState)GetValue(StateProperty); | ||
set => SetValue(StateProperty, value); | ||
} | ||
|
||
public Action OnLoaded | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
public ServerDrivenView() => | ||
Check warning on line 48 in Maui.ServerDrivenUI/Views/ServerDrivenView.cs GitHub Actions / Analyze (csharp)
Check warning on line 48 in Maui.ServerDrivenUI/Views/ServerDrivenView.cs GitHub Actions / Build with dotnet
|
||
_ = Task.Run(InitializeComponentAsync); | ||
|
||
#endregion | ||
|
||
#region IServerDrivenVisualElement | ||
|
||
protected virtual Task InitializeComponentAsync() => | ||
ServerDrivenVisualElement.InitializeComponentAsync(this); | ||
|
||
public virtual void OnStateChanged(UIElementState newState) | ||
{ | ||
} | ||
|
||
public virtual void OnError(Exception ex) | ||
{ | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Maui.ServerDrivenUI.Services; | ||
|
||
namespace Maui.ServerDrivenUI.Views; | ||
|
||
internal class ServerDrivenVisualElement | ||
{ | ||
internal static async Task InitializeComponentAsync(IServerDrivenVisualElement element) | ||
{ | ||
try | ||
{ | ||
MainThread.BeginInvokeOnMainThread(() => element.State = UIElementState.Loading); | ||
|
||
var serverDrivenUiService = ServiceProviderHelper.ServiceProvider?.GetService<IServerDrivenUIService>(); | ||
if (serverDrivenUiService != null) | ||
{ | ||
var xaml = await serverDrivenUiService.GetXamlAsync(element.ServerKey ?? element.GetType().Name).ConfigureAwait(false); | ||
MainThread.BeginInvokeOnMainThread(() => { | ||
var onLoaded = element.OnLoaded; | ||
(element as VisualElement)?.LoadFromXaml(xaml); | ||
|
||
if (element is ServerDrivenContentPage page) | ||
{ | ||
if (page.Content is null) | ||
{ | ||
_ = Task.Run(() => InitializeComponentAsync(element)); | ||
return; | ||
} | ||
} | ||
else if (element is ServerDrivenView view) | ||
{ | ||
if (view.Content is null) | ||
{ | ||
_ = Task.Run(() => InitializeComponentAsync(element)); | ||
return; | ||
} | ||
} | ||
|
||
element.OnLoaded = onLoaded; | ||
element.State = UIElementState.Loaded; | ||
}); | ||
} | ||
else | ||
{ | ||
MainThread.BeginInvokeOnMainThread(() => { | ||
element.State = UIElementState.Error; | ||
element.OnError(new DependencyRegistrationException("IServerDrivenUIService not found, make sure you are calling 'ConfigureServerDrivenUI(s=> s.RegisterElementGetter((k)=> yourApiCall(k)))'")); | ||
}); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
MainThread.BeginInvokeOnMainThread(() => { | ||
element.State = UIElementState.Error; | ||
element.OnError(ex); | ||
}); | ||
} | ||
} | ||
|
||
internal static void OnStatePropertyChanged(BindableObject bindable, object oldValue, object newValue) | ||
{ | ||
if (bindable is not IServerDrivenVisualElement view | ||
|| newValue is not UIElementState newState) | ||
return; | ||
|
||
view.OnStateChanged(newState); | ||
if(newState is UIElementState.Loaded) | ||
view.OnLoaded?.Invoke(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.