-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add iOS/Catalyst support to Microsoft.Maui.PlatformChannels
- Loading branch information
Showing
6 changed files
with
132 additions
and
12 deletions.
There are no files selected for viewing
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
2 changes: 2 additions & 0 deletions
2
Microsoft.Maui.PlatformChannels/Platforms/MacCatalyst/GlobalUsings.ios.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,2 @@ | ||
global using PlatformView = global::UIKit.UIView; | ||
global using PlatformObject = global::Foundation.NSObject; |
121 changes: 121 additions & 0 deletions
121
Microsoft.Maui.PlatformChannels/Platforms/MacCatalyst/PlatformChannelViewHandler.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,121 @@ | ||
using Microsoft.Maui.Handlers; | ||
using Microsoft.PlatformChannels; | ||
using Foundation; | ||
using UIKit; | ||
|
||
using iOSViewChannel = Microsoft.PlatformChannels.Platform.ViewChannel; | ||
|
||
namespace Microsoft.Maui.PlatformChannels; | ||
|
||
public partial class PlatformChannelViewHandler : ViewHandler<IPlatformChannelView, UIKit.UIView> | ||
{ | ||
iOSViewChannel platformViewChannel; | ||
PlatformManagedHandler managedHandler; | ||
UIView viewGroup; | ||
|
||
protected string ChannelTypeId { get; private set; } | ||
protected string ChannelInstanceId { get; private set; } | ||
|
||
IChannelService channelService; | ||
|
||
protected override UIKit.UIView CreatePlatformView() | ||
{ | ||
if (viewGroup is null) { | ||
viewGroup = new UIView(new CoreGraphics.CGRect(0, 0, 400, 100)); | ||
} | ||
EnsureChannelCreated(); | ||
return viewGroup; | ||
} | ||
|
||
void EnsureChannelCreated() | ||
{ | ||
if (string.IsNullOrEmpty(VirtualView?.ChannelTypeId)) | ||
return; | ||
|
||
var instanceId = VirtualView?.ChannelInstanceId ?? ChannelService.DEFAULT_INSTANCE_ID; | ||
|
||
// Is the new id the same as the old? | ||
if (VirtualView?.ChannelTypeId == ChannelTypeId) | ||
{ | ||
// Is the instance null on both (so both default and the same) | ||
// or is the new instance Id not the same as the old | ||
if (instanceId == ChannelInstanceId) | ||
return; // No change to actual channel, return | ||
} | ||
|
||
channelService ??= MauiContext.Services.GetRequiredService<IChannelService>(); | ||
|
||
if (platformViewChannel is not null) | ||
{ | ||
foreach (var subview in viewGroup.Subviews) { | ||
subview.RemoveFromSuperview (); | ||
} | ||
|
||
if (viewGroup.Superview is not null) | ||
viewGroup.RemoveFromSuperview (); | ||
|
||
channelService.DisposeChannel(ChannelTypeId, ChannelInstanceId); | ||
platformViewChannel = null; | ||
} | ||
|
||
ChannelTypeId = VirtualView.ChannelTypeId; | ||
ChannelInstanceId = instanceId; | ||
|
||
var channel = Microsoft.PlatformChannels.Platform.ChannelService.GetOrCreate(ChannelTypeId, ChannelInstanceId); | ||
|
||
if (channel == null) { | ||
throw new InvalidOperationException($"No registered ViewChannel found for: '{ChannelTypeId}', instance: '{ChannelInstanceId}'"); | ||
} | ||
|
||
if (channel is not iOSViewChannel viewChannel) | ||
throw new InvalidCastException($"Registered channel '{ChannelTypeId}' is not a 'ViewChannel' type for instance: '{ChannelInstanceId}"); | ||
|
||
platformViewChannel = viewChannel; | ||
|
||
managedHandler = new PlatformManagedHandler((id, parameters) => | ||
{ | ||
return OnReceivedFromPlatform?.Invoke(id, parameters); | ||
}); | ||
|
||
platformViewChannel.SetManagedHandler(managedHandler); | ||
|
||
var platformView = platformViewChannel.GetPlatformView(); | ||
viewGroup.AddSubview (platformView); | ||
} | ||
|
||
public static void MapChannelTypeId(IPlatformViewHandler handler, IPlatformChannelView view) | ||
{ | ||
if (handler is PlatformChannelViewHandler h) | ||
h.EnsureChannelCreated(); | ||
} | ||
|
||
public static void MapChannelInstanceId(IPlatformViewHandler handler, IPlatformChannelView view) | ||
{ | ||
if (handler is PlatformChannelViewHandler h) | ||
h.EnsureChannelCreated(); | ||
} | ||
|
||
internal object SendToPlatformImpl(string messageId, object[] args) | ||
{ | ||
var platformObjs = args.ToPlatformObjects(); | ||
|
||
var platformResp = platformViewChannel?.OnChannelMessage(messageId, platformObjs); | ||
|
||
var result = platformResp.ToDotNetObject(); | ||
|
||
return result; | ||
} | ||
|
||
class PlatformManagedHandler : NSObject, Microsoft.PlatformChannels.Platform.IChannelMessageHandler | ||
{ | ||
public PlatformManagedHandler(Func<string, object[], object> callback) | ||
{ | ||
Callback = callback; | ||
} | ||
|
||
protected readonly Func<string, object[], object> Callback; | ||
|
||
public PlatformObject OnChannelMessage(string id, PlatformObject[] parameters) | ||
=> Callback?.Invoke(id, parameters.ToDotNetObjects()).ToPlatformObject(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
Microsoft.Maui.PlatformChannels/Platforms/iOS/GlobalUsings.ios.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 @@ | ||
../MacCatalyst/GlobalUsings.ios.cs |
1 change: 1 addition & 0 deletions
1
Microsoft.Maui.PlatformChannels/Platforms/iOS/PlatformChannelViewHandler.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 @@ | ||
../MacCatalyst/PlatformChannelViewHandler.cs |
11 changes: 0 additions & 11 deletions
11
Microsoft.Maui.PlatformChannels/Platforms/iOS/ViewChannel.ios.cs
This file was deleted.
Oops, something went wrong.