-
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.
- Loading branch information
Showing
9 changed files
with
116 additions
and
106 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
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
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
76 changes: 76 additions & 0 deletions
76
Microsoft.PlatformChannels/Platforms/Android/PlatformConversionExtensions.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,76 @@ | ||
namespace Microsoft.PlatformChannels; | ||
|
||
public static class PlatformConversionExtensions | ||
{ | ||
public static PlatformObject[] ToPlatformObjects(this object[] objs) | ||
{ | ||
PlatformObject[] r = null; | ||
|
||
if (objs is not null && objs.Length >= 1) | ||
r = objs.ToArray().Select(a => ToPlatformObject(a)).ToArray(); | ||
return r; | ||
} | ||
|
||
public static PlatformObject ToPlatformObject(this object obj) | ||
{ | ||
if (obj is null) | ||
return null; | ||
|
||
var objType = obj.GetType(); | ||
|
||
try { | ||
if (obj is PlatformObject pobj) | ||
return pobj; | ||
} catch { } | ||
|
||
if (objType == typeof(string)) | ||
return new Java.Lang.String((string)obj); | ||
else if (objType == typeof(int)) | ||
return new Java.Lang.Integer((int)obj); | ||
else if (objType == typeof(double)) | ||
return new Java.Lang.Double((double)obj); | ||
else if (objType == typeof(float)) | ||
return new Java.Lang.Float((float)obj); | ||
else if (objType == typeof(bool)) | ||
return new Java.Lang.Boolean((bool)obj); | ||
else if (objType == typeof(long)) | ||
return new Java.Lang.Long((long)obj); | ||
else if (objType == typeof(short)) | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
return new Java.Lang.Short((short)obj); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
throw new NotSupportedException(); | ||
} | ||
|
||
public static object[] ToDotNetObjects(this PlatformObject[] objs) | ||
{ | ||
object[] r = null; | ||
|
||
if (objs is not null && objs.Length >= 1) | ||
r = objs.ToArray().Select(a => ToDotNetObject(a)).ToArray(); | ||
return r; | ||
} | ||
|
||
public static object ToDotNetObject(this PlatformObject obj) | ||
{ | ||
if (obj is Java.Lang.String jstr) | ||
return jstr.ToString(); | ||
else if (obj is Java.Lang.Double jdbl) | ||
return jdbl.DoubleValue(); | ||
else if (obj is Java.Lang.Integer jint) | ||
return jint.IntValue(); | ||
else if (obj is Java.Lang.Float jflt) | ||
return jflt.FloatValue(); | ||
else if (obj is Java.Lang.Boolean jbl) | ||
return jbl.BooleanValue(); | ||
else if (obj is Java.Lang.Long jlng) | ||
return jlng.LongValue(); | ||
else if (obj is Java.Lang.Short jsht) | ||
return jsht.ShortValue(); | ||
else if (obj is Java.Lang.Number jnum) | ||
return jnum.DoubleValue(); | ||
|
||
return obj; | ||
} | ||
} |
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,24 @@ | ||
| ||
namespace Microsoft.PlatformChannels; | ||
|
||
public partial class ViewChannel : PlatformObject, IPlatformChannelMessageHandler | ||
{ | ||
internal ViewChannel(PlatformViewChannel platformChannel) | ||
{ | ||
PlatformViewChannel = platformChannel; | ||
PlatformViewChannel.SetManagedHandler(this); | ||
} | ||
|
||
public event ChannelMessageDelegate OnReceiveFromPlatform; | ||
|
||
internal PlatformViewChannel PlatformViewChannel { get; set; } | ||
|
||
public PlatformObject OnChannelMessage(string messageId, params PlatformObject[] parameters) | ||
=> ReceiveFromPlatform(messageId, parameters.ToDotNetObjects()).ToPlatformObject(); | ||
|
||
public virtual object ReceiveFromPlatform(string messageId, params object[] parameters) | ||
=> OnReceiveFromPlatform?.Invoke(messageId, parameters); | ||
|
||
public object SendToPlatform(string messageId, params object[] parameters) | ||
=> PlatformViewChannel.HandleMessageFromDotNet(messageId, parameters.ToPlatformObjects()).ToDotNetObject(); | ||
} |
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