Commit f2b813bb authored by Nadya Atanasova's avatar Nadya Atanasova Committed by EddyVerbruggen
Browse files

Take deserialization outside the methods as it can be reused.

Adding TryCast method improves readability and decreases the code nesting.
parent ce95a56a
Loading
Loading
Loading
Loading
+33 −21
Changes for src/wp8/PushPlugin.cs: 33 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -3,9 +3,9 @@ using Microsoft.Phone.Notification;
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Runtime.Serialization;
using System.Windows;
using WPCordovaClassLib.Cordova.JSON;

namespace WPCordovaClassLib.Cordova.Commands
{
@@ -19,19 +19,14 @@ namespace WPCordovaClassLib.Cordova.Commands
        {
            Options pushOptions;

            try
            if (!TryDeserializeOptions(options, out pushOptions))
            {
                string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
                pushOptions = JSON.JsonHelper.Deserialize<Options>(args[0]);
                this.channelName = pushOptions.ChannelName;
                this.toastCallback = pushOptions.NotificationCallback;
            }
            catch (Exception)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
                return;
            }

            this.toastCallback = pushOptions.NotificationCallback;
            channelName = pushOptions.ChannelName;
            pushChannel = HttpNotificationChannel.Find(channelName);
            if (pushChannel == null)
            {
@@ -96,16 +91,15 @@ namespace WPCordovaClassLib.Cordova.Commands

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
                if (frame != null)
                {
                    PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
                    if (page != null)
                    {
                        CordovaView cView = page.FindName("CordovaView") as CordovaView; // was: PGView
                        if (cView != null)
                PhoneApplicationFrame frame;
                PhoneApplicationPage page;
                CordovaView cView;

                if (TryCast(Application.Current.RootVisual, out frame) &&
                    TryCast(frame.Content, out page) &&
                    TryCast(page.FindName("CordovaView"), out cView))
                {
                            cView.Browser.Dispatcher.BeginInvoke((ThreadStart)delegate()
                    cView.Browser.Dispatcher.BeginInvoke(() =>
                    {
                        try
                        {
@@ -115,12 +109,30 @@ namespace WPCordovaClassLib.Cordova.Commands
                        {
                            Debug.WriteLine("ERROR: Exception in InvokeScriptCallback :: " + ex.Message);
                        }

                    });
                }
            });
        }

         private static bool TryDeserializeOptions<T>(string options, out T result) where T : class
        {
            result = null;
            try
            {
                var args = JsonHelper.Deserialize<string[]>(options);
                result = JsonHelper.Deserialize<T>(args[0]);
                return true;
            }
            catch
            {
                return false;
            }
        }
            });

        private static bool TryCast<T>(object obj, out T result) where T : class
        {
            result = obj as T;
            return result != null;
        }

        [DataContract]