leecrossley / cordova-plugin-datetime-picker

Cordova / PhoneGap Date Time Picker Plugin for Apache Cordova >= 3.0.0 (Windows Phone 8)
8 stars 3 forks source link

No ResultHandlers #2

Closed leecrossley closed 9 years ago

leecrossley commented 9 years ago

DispatchCommandResult -> ResultHandlers.ContainsKey(result.CallbackId) always fails, as ResultHandlers is an empty array :/

leecrossley commented 9 years ago

The webBrowser_Unloaded method which detaches event handlers of plugin commands (to prevent memory leak on page navigation) clears ALL plugin callbacks (aka ResultHandlers).

This results in the callback being lost and a "Failed to locate callback for id" error.

I'm unable to see a workaround for this without modifying the BaseCommand or NativeExecution.

leecrossley commented 9 years ago

As a temporary workaround, the platforms\wp8\cordovalib\Commands\BaseCommand.cs DetatchHandlers() method could be replaced with:

        public void DetachHandlers()
        {
            this.OnCommandResult = null;
            this.OnCustomScript = null;
            foreach (string callbackId in new List<string>(ResultHandlers.Keys))
            {
                if (!callbackId.ToLower().Contains("datetimepicker"))
                {
                    RemoveResultHandler(callbackId);
                }
            }
        }
Frix33 commented 9 years ago

For avoiding monkey patching you can save the handler in selectDate method of DateTimePicker.cs class an then readd that on ResultHandler dictionary before DispatchCommandResult, its work great for me:

public event EventHandler<PluginResult> mySavedHandler;
    ....
public void selectDate(string options)
{
   ....
    if (ResultHandlers.ContainsKey(CurrentCommandCallbackId))
    {
        mySavedHandler = ResultHandlers[CurrentCommandCallbackId];
    }
    ....
}

private void dateTimePickerTask_Completed(object sender, DateTimePickerTask.DateTimeResult e)
{
......
 try {
      if (!ResultHandlers.ContainsKey(CurrentCommandCallbackId))
      {
          ResultHandlers.Add(CurrentCommandCallbackId, mySavedHandler);
      }
        .....
      DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result + ""));
    }
}
leecrossley commented 9 years ago

Great - thanks! I've not it tested yet it but it looks good so I'll merge into master. Not sure why I didn't do this...