Daddoon / BlazorMobile

Create full C# driven hybrid-apps for iOS, Android, UWP & Desktop with Blazor!
MIT License
413 stars 55 forks source link

Does [ProxyInterface] supports properties? or only async methods? #224

Closed arivera12 closed 4 years ago

arivera12 commented 4 years ago

I play around with [ProxyInterface] I found it doesn't support sync methods, only async. I think this should be specified in the main docs. But what about properties and event?

Daddoon commented 4 years ago

Sync methods were supported but have been deprecated by Design.

Maybe the doc does not explain this clearly, but it is written:

API calls from Blazor to native are awaitable from a Task object

I think that if you have to write this "sync" you are maybe missing something. Async is for security reason, as it tell from one side or an other that the method called can be waited or not, giving the opportunity to your Web UI to not freeze during the operation as the execution is normally waiting in the background for the result thanks to the await keyword.

From a Design point a view, as it's a kind of remote communication, this is "not reliable" by essence, like if you were talking to a server, a SQL server or else, it implie doing work on wire.

Also keep in mind that if you are writing some codes on native that don't need async logic at all and you don't want to mess a lot with the method signature, you know that you can put the keyword async in the method declaration, right ?

Like public async bool MyMethod in the implementation instead of the interface declaration that would be something like Task MyMethod .

What are you talking about for properties or events ? You mean transferring something serialized, or how to fire events from native to web transparently without a ProxyInterface intervention ?

arivera12 commented 4 years ago

Also keep in mind that if you are writing some codes on native that don't need async logic at all and you don't want to mess a lot with the method signature, you know that you can put the keyword async in the method declaration, right ?

Yes, I have few methods non async, I just added the async keyword and they worked without any issues.

What are you talking about for properties or events ? You mean transferring something serialized, or how to fire events from native to web transparently without a ProxyInterface intervention ?

Let's say I want to wrap this Xamarin.Essential api

https://docs.microsoft.com/en-us/xamarin/essentials/connectivity?context=xamarin%2Fxamarin-forms&tabs=android#using-connectivity

The interface would be something like this regarding on the api

    public interface IConnectivity
    {
        NetworkAccess NetworkAccess { get; }
        IEnumerable<ConnectionProfile> ConnectionProfiles { get; }
        event EventHandler<ConnectivityChangedEventArgs> ConnectivityChanged;
    }

NetworkAccess and ConnectionProfiles are properties.

ConnectivityChanged is an event.

Does this will work?

Or I will need to do some refactors like this:

Interface

async Task<NetworkAccess> GetNetworkAccess()

Implementation

async Task<NetworkAccess> GetNetworkAccess() => Xamarin.Essentials.Connectivity.NetworkAccess

Daddoon commented 4 years ago

BlazorMobile does not support Events forwarding with the ProxyInterface API. It just receive/send data, but is not aware on how to fire back an event, i mean with how you would do it with a regular C# EventHandler.

However, you have 2 possible solutions. The most portable, take care that the Browser (so Blazor) is already aware of the network connectivity. I don't know if there is caveat, but there is navigator.online in Javascript. You may just code some event handler in Javascript that forward it to Blazor C# or use a plugin for Blazor that forward it for you.

If you really want to go the native route with BlazorMobile, you can write on the native side the event logic handling when the network state change, and forward the event to Blazor by using the Message API of BlazorMobile, see the PostMessage method part, that is i think the most convenient for you, as you would be able to register one or multiple code path (in Blazor) to a specific event you are awaiting from Native.

arivera12 commented 4 years ago

The Message API answers my question regarding on Events forwarding.

What about properties are they supported by [ProxyInterface]?

arivera12 commented 4 years ago

What about properties are they supported by [ProxyInterface]?

Ok, I answered my own question doing a quick test, properties are not supported, only async Task.