-
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.
Merge pull request #2 from chamons/swift_smash
Complete basic iOS support and port to be fully Swift powered
- Loading branch information
Showing
47 changed files
with
646 additions
and
795 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
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,13 @@ | ||
TARGET=net6.0-ios | ||
#TARGET=net6.0-maccatalyst | ||
|
||
both:: build run | ||
|
||
build:: | ||
make -C Platforms/Apple/ | ||
dotnet build Microsoft.Maui.PlatformChannels/ -f:$(TARGET) | ||
dotnet build -f:$(TARGET) SamplePlatformChannels | ||
|
||
run:: | ||
# -MONO_TRACE=E:all ./SamplePlatformChannels/bin/Debug/net6.0-maccatalyst/maccatalyst-x64/SamplePlatformChannels.app/Contents/MacOS/SamplePlatformChannels | ||
dotnet build -t:Run -f:$(TARGET) SamplePlatformChannels |
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; |
122 changes: 122 additions & 0 deletions
122
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,122 @@ | ||
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) { | ||
// TODO - Fix this hard coded size | ||
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.
This file was deleted.
Oops, something went wrong.
132 changes: 0 additions & 132 deletions
132
Microsoft.PlatformChannels.Binding.Apple/ApiDefinitions.cs
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
Microsoft.PlatformChannels.Binding.Apple/Microsoft.PlatformChannels.Binding.Apple.csproj
This file was deleted.
Oops, something went wrong.
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.