Example #1
0
 public async Task<bool> LoginAccount(string mail,string password)
 {            
     byte[] pwd = Functions.GetBytes(password);
     Account account = new Account(mail, pwd);
     JToken body = JToken.FromObject(account);
     _userInfo = new UserInfo();
     IDictionary<string, string> argument = new Dictionary<string, string>
     {
         {"type" , "Test"}
     };
     try
     {
         if (Utilities.Helpers.NetworkHelper.Instance.HasInternetConnection)
         {
             var result = await App.MobileService.InvokeApiAsync("Users", body, HttpMethod.Post, argument);
             JObject user = JObject.Parse(result.ToString());
             UserInfo = user.ToObject<UserInfo>();             
             PaybayPushClient.UploadChannel(UserInfo.UserId);                    
             MessageInboxViewModel.registerClient();
             //MediateClass.MessageVM = new MessageInboxViewModel();
             //MediateClass.MessageVM.registerClient();      
         }
         else
         {
             await new MessageDialog("You have not internet connection!Can not login!","Login").ShowAsync();
             return false;
         }
     }
     catch (Exception ex)
     {                
         return false;
     }
     return true;               
 }
        private async void alterOrCreateUser()
        {
            UserInfo user = new UserInfo();

            if (FullNameTextBox.Text != "" && AddressTextBox.Text != "" && PhoneTextBox.Text != "" && EmailTextBox.Text != ""
                && PasswordTextBox.Password != "")
            {
                if (Functions.EmailIsValid(EmailTextBox.Text))
                {                    
                    user.Username = FullNameTextBox.Text;
                    user.Address = AddressTextBox.Text;
                    user.Phone = PhoneTextBox.Text;
                    user.Email = EmailTextBox.Text;
                    user.Gender = ((bool)MaleRadioButton.IsChecked) ? true : false;
                    user.Pass = Functions.GetBytes(PasswordTextBox.Password);
                    user.Birthday = BirthdayDatePicker.Date.DateTime;
                    ComboBoxItem ComboItem = (ComboBoxItem)TypeCommboBox.SelectedItem;
                    int typeid = int.Parse(ComboItem.Tag.ToString());                    
                    
                    if (!UserInfoViewModel.isUpdate)
                    {
                        user.TypeId = typeid;
                        bool result = await MediateClass.UserVM.AlterOrCreateUser(user, mediaFile, HttpMethod.Post);
                        if (result)
                        {
                            await new MessageDialog("Create Account is successful!", "Notification!").ShowAsync();
                            Frame.GoBack();
                        }
                        else
                        {
                            await new MessageDialog("Upload avatar is not successful!", "Notification!").ShowAsync();
                            EmailTextBox.Focus(FocusState.Pointer);
                        }
                    }
                    else
                    {
                        user.Avatar = MediateClass.UserVM.UserInfo.Avatar;
                        user.SasQuery = MediateClass.UserVM.UserInfo.SasQuery;
                        user.UserId = MediateClass.UserVM.UserInfo.UserId;
                        user.TypeId = (MediateClass.UserVM.UserInfo.TypeId == 1) ? MediateClass.UserVM.UserInfo.TypeId : typeid;
                        bool result = await MediateClass.UserVM.AlterOrCreateUser(user, mediaFile, HttpMethod.Put);
                        if (result)
                        {
                            await new MessageDialog("Update Account is successful!", "Notification!").ShowAsync();
                            ((Popup)Frame.Parent).IsOpen = false;
                            UserInfoViewModel.isUpdate = false;
                        }
                        else
                        {
                            await new MessageDialog("Update account is not successful!", "Notification!").ShowAsync();
                            EmailTextBox.Focus(FocusState.Pointer);
                        }
                    }
                }
                else
                {
                    await new MessageDialog("Your email is NOT valid!Please try again!", "Notification").ShowAsync();
                    EmailTextBox.Focus(FocusState.Pointer);
                }
            }
            else
            {
                if(!UserInfoViewModel.isUpdate)
                    await new MessageDialog("Please fill all the infomation!Thank you!", "Notification").ShowAsync();
                else
                    await new MessageDialog("Please confirm your password!", "Notification").ShowAsync();
            }
            ToggleProgressRing();
        }
Example #3
0
        public async Task<bool> AlterOrCreateUser(UserInfo user, StorageFile mediaFile, HttpMethod method)
        {            
            try
            {
                if (NetworkHelper.Instance.HasInternetConnection)
                {
                    if (user.Avatar == null || user.Avatar == "/Assets/lol.jpg")
                    {
                        if (mediaFile == null)
                            user.Avatar = "/Assets/lol.jpg";
                        else
                            user.Avatar = null;
                    }
                    JToken data = JToken.FromObject(user);
                    JToken result = await App.MobileService.InvokeApiAsync("Users", data, method, null);
                    JObject response = JObject.Parse(result.ToString());
                    if (method == HttpMethod.Put)
                    {
                        UserInfo = response.ToObject<UserInfo>();
                        if (mediaFile != null)
                        {
                            uploadAvatar(UserInfo.Username, UserInfo.Avatar, UserInfo.SasQuery, mediaFile);
                        }
                        return true;
                    }
                    if (mediaFile != null)
                    {
                        uploadAvatar(response["Username"].ToString(), response["Avatar"].ToString(), response["SasQuery"].ToString(), mediaFile);
                    }
                }
                else
                {
                    await new MessageDialog("You have not internet connection!", "Create Account").ShowAsync();
                }
            }
            catch (Exception ex)
            {
                await new MessageDialog("Email had already exists!", "Create Account!").ShowAsync();
                return false;
            }

            return true;
        }