Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a PositiveAction Action to the MessagingServiceQuestion type. #1

Merged
merged 2 commits into from Jan 18, 2016

Conversation

jsauvexamarin
Copy link
Contributor

Can be used like this:
var task = Application.Current?.MainPage?.DisplayAlert(info.Title, info.Question, info.Positive, info.Negative);
if (task != null && await task)
info?.PositiveAction?.Invoke();

Can be used like this:
var task = Application.Current?.MainPage?.DisplayAlert(info.Title, info.Question, info.Positive, info.Negative);
if (task != null && await task)
	info.PositiveAction.Invoke();
@jamesmontemagno
Copy link
Owner

So I am thinking that actually it should have a OnResult, which returns the Positive or Negative probably a boolean is fine here and let's you make a decision based on it.

Also, wondering if it should be an Action or a Fun

@jsauvexamarin
Copy link
Contributor Author

You mean instead of only taking action on Positive? Instead, allowing dev to specify the action taken, whether Positive or Negative?

@jsauvexamarin
Copy link
Contributor Author

I'd be cool with Action or Func. My thought for Action is: at the point that I'm calling SendMessage, I already have access to all the values I'll need in order to take action when the user makes a decision.

@jamesmontemagno
Copy link
Owner

Yeah, that sounds good, i think just it should be Action that returns if it was positive or negative so you can respond. If you implement that I can go ahead and merge in.

@jsauvexamarin
Copy link
Contributor Author

I guess I'm not understanding. Since the DisplayAlert gets fired off by using the MessageService, then we essentially have no control over what happens with the result of the user decision. Wouldn't you have to setup a second pub/sub item with the MessagingService to communicate the result back to the original caller? If you just tuck an Action or a Func into the TArgs type, then it can be fired off in the subscriber, like this:

MessagingService.Current.Subscribe<MessagingServiceQuestion>(MessageKeys.DisplayQuestion, async (service, info) =>
{
    var task = Application.Current?.MainPage?.DisplayAlert(info.Title, info.Question, info.Positive, info.Negative);
    if (task != null && await task)
        info?.SuccessAction?.Invoke();
});

I dunno, maybe I'm missing something. Thoughts?

@jamesmontemagno
Copy link
Owner

So,

I would do this:

public Action<bool> OnCompleted { get; set; }


MessagingService.Current.Subscribe<MessagingServiceQuestion>(MessageKeys.DisplayQuestion, async (service, info) =>
{
    var task = Application.Current?.MainPage?.DisplayAlert(info.Title, info.Question, info.Positive, info.Negative);
    if (task == null)
      return;
   var result = await task;
    info?.OnCompleted?.Invoke(result);
});

I think this should also be added to the alert, but with just an public Action OnCompleted;

@jsauvexamarin
Copy link
Contributor Author

Ahhh!! I dig. I'll wire it up and update the PR.

@jsauvexamarin
Copy link
Contributor Author

And the usage can even be simplified to:

MessagingService.Current.Subscribe<MessagingServiceQuestion>(MessageKeys.DisplayQuestion, async (service, info) =>
{
    var task = Application.Current?.MainPage?.DisplayAlert(info.Title, info.Question, info.Positive, info.Negative);
    if (task == null)
      return;
    info?.OnCompleted?.Invoke(await task);
});

...placing the await task directly in the Invoke call, avoiding the var altogether. Pretty slick!

...or maybe even this:

MessagingService.Current.Subscribe<MessagingServiceQuestion>(MessageKeys.DisplayQuestion, async (service, info) =>
{
    var task = Application.Current?.MainPage?.DisplayAlert(info.Title, info.Question, info.Positive, info.Negative);
    if (task != null)
        info?.OnCompleted?.Invoke(await task);
});

@jamesmontemagno
Copy link
Owner

In your code make sure you actually do this:

if (task != null)
{
   var result = await task;
        info?.OnCompleted?.Invoke(result);
}

Because if any of those were null then your task would never get awaited!

@jsauvexamarin
Copy link
Contributor Author

Oh, true!

jamesmontemagno added a commit that referenced this pull request Jan 18, 2016
Added a PositiveAction Action to the MessagingServiceQuestion type.
@jamesmontemagno jamesmontemagno merged commit 89674ef into jamesmontemagno:master Jan 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants