0

today i was looking for some projects with ilspy i did not understand how Actions can be used like this, in one class this method is called

    public void Login(Action success, Action<bool> failure)
    {
        if (!FB.IsInitialized)
        {
            Debug.Log("[FB] Not yet initialized. Will init again!");
            FB.Init(new InitDelegate(this.OnInitComplete), null, null);
            return;
        }
        new LoginWithReadPermissions(this.READ_PERMISSIONS, delegate
        {
            ServiceLocator.GetDB().SetBool("facebookBound", true, false);
            this.OnLoginCompleted(success, failure);
        }, delegate
        {
            failure(false);
        });
    }

and this is the other class that is called by above method

    public class LoginWithReadPermissions
{
    private readonly Action _failureCallback;

    private readonly Action _successCallback;

    public LoginWithReadPermissions(string[] scope, Action success, Action failure)
    {
        this._successCallback = success;
        this._failureCallback = failure;
        FB.LogInWithReadPermissions(scope, new FacebookDelegate<ILoginResult>(this.AuthCallback));
    }

    private void AuthCallback(ILoginResult result)
    {
        if (result.Error == null && FB.IsLoggedIn)
        {
            this._successCallback();
        }
        else
        {
            this._failureCallback();
        }
    }
}

Can someone please explain what is going on there I have never faced with this kind of Action usage. Thanks for replies.

1 Answers1

0
public void Login(Action success, Action<bool> failure)
{
    if (!FB.IsInitialized)
    {
        Debug.Log("[FB] Not yet initialized. Will init again!");
        FB.Init(new InitDelegate(this.OnInitComplete), null, null);
        return;
    }
    new LoginWithReadPermissions(this.READ_PERMISSIONS, delegate
    {
        ServiceLocator.GetDB().SetBool("facebookBound", true, false);
        this.OnLoginCompleted(success, failure);
    }, delegate
    {
        failure(false);
    });
}

The code above is just shorthand for this:

public void Login(Action success, Action<bool> failure)
    {
        Action successAction = () =>
        {
            ServiceLocator.GetDB().SetBool("facebookBound", true, false);
            this.OnLoginCompleted(success, failure);
        };

        Action failureAction = () =>
        {
            failure(false);
        };

        if (!FB.IsInitialized)
        {
            Debug.Log("[FB] Not yet initialized. Will init again!");
            FB.Init(new InitDelegate(this.OnInitComplete), null, null);
            return;
        }
        new LoginWithReadPermissions(this.READ_PERMISSIONS, successAction, failureAction);
    }
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61