Ejemplo n.º 1
0
 public async void Create()
 {
     await Task.Run(async() =>
     {
         // Create sample file; replace if exists.
         Windows.Storage.StorageFolder storageFolder =
             Windows.Storage.ApplicationData.Current.LocalFolder;
         Windows.Storage.StorageFile sampleFile =
             await storageFolder.CreateFileAsync(FileName + ".mport",
                                                 Windows.Storage.CreationCollisionOption.ReplaceExisting);
         string fileserial             = Serialize.Xml_Serialize_Object <Experiment>((Experiment)this);
         Dictionary <string, object> o = new Dictionary <string, object>();
         o.Add("usertoken", Windows.Storage.ApplicationData.Current.LocalSettings.Values["usertoken"]);
         o.Add("experiment", this);
         try
         {
             try
             {
                 await Windows.Storage.FileIO.WriteBytesAsync(sampleFile, Encoding.UTF8.GetBytes(fileserial));
                 Dictionary <string, object> d = await HttpHelpers.Post <Dictionary <string, object> >(o, "experiment");
                 if ((bool)d["success"] == true)
                 {
                     Debug.WriteLine("Yay!");
                 }
             }
             catch
             {
                 Debug.WriteLine("Failed to Save Experiment");
             }
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.Message);
         }
     });
 }
Ejemplo n.º 2
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Button       b    = sender as Button;
            ProgressRing ring = new ProgressRing()
            {
                Width      = 25,
                Height     = 25,
                Foreground = new SolidColorBrush(Colors.Black),
                IsActive   = true
            };

            b.Content = ring;
            Dictionary <String, String> dict     = new Dictionary <string, string>();
            Dictionary <String, object> response = new Dictionary <string, object>();
            Dictionary <String, object> user     = new Dictionary <string, object>();
            string        dialogText             = "";
            var           settings = ApplicationData.Current.LocalSettings;
            ContentDialog d        = new ContentDialog()
            {
                PrimaryButtonText = "Ok"
            };

            if (_username == "" && _password == "")
            {
                dialogText = "Please fill out all of the required fields";
            }
            else
            {
                dict.Add("Username", _username);
                dict.Add("Password", _password);

                response = await HttpHelpers.Post <Dictionary <string, object> >(dict, "auth/login");

                if (response == null)
                {
                    dialogText = "Sorry, something went wrong";
                }
                else if (response.ContainsKey("success") && (bool)response["success"] == true)
                {
                    dialogText            = "Welcome, " + Username + "!";
                    d.PrimaryButtonClick += delegate
                    {
                        this.Frame.Navigate(typeof(UserPage));
                    };

                    user = await HttpHelpers.Get <Dictionary <string, object> >(dict, "user/username/" + Username);

                    if ((bool)user["success"] == true)
                    {
                        User userdata = ((JObject)user["data"]).ToObject <User>();
                        userdata.Save();
                        settings.Values["usertoken"] = response["token"];
                        settings.Values["username"]  = Username;
                    }
                    else
                    {
                        dialogText = "What??";
                    }
                }
                else
                {
                    Dictionary <string, object> error = ((JObject)response["error"]).ToObject <Dictionary <string, object> >();
                    if ((string)error["type"] == "Password")
                    {
                        dialogText = "Incorrect Password";
                    }
                    else if ((string)error["type"] == "Database")
                    {
                        dialogText = "Please include all of the required information";
                    }
                    else if ((string)error["type"] == "NoUser")
                    {
                        dialogText = "Sorry, that user doesn't exist.";
                    }
                    else
                    {
                        dialogText = "Oops, something went wrong";
                    }
                }
            }
            d.Title = dialogText;

            await d.ShowAsync();
        }