Advertisement
Guest User

Login

a guest
Jul 13th, 2018
2,762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.04 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. // MY ViewModel -> LoginModel.cs
  6.  
  7.     public class LoginModel : INotifyPropertyChanged
  8.     {
  9.  
  10.  
  11.  
  12.  
  13.         // navigation from ViewModel Handling with delcaration of INavigation
  14.         public Command OpenPageCommand { get; }
  15.         INavigation Navigation;
  16.  
  17.         // property changed event handler
  18.  
  19.         public event PropertyChangedEventHandler PropertyChanged;
  20.  
  21.         void OnPropertyChanged(string name)
  22.         {
  23.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  24.         }
  25.  
  26.         // Strings declarations
  27.         public string password;
  28.         //SecureString token;
  29.         public string email;
  30.         string _DisplayMessage;
  31.         bool IsBusy = false;
  32.         bool IsEmailValid = false;
  33.         bool IsPasswordValid = false;
  34.  
  35.         public string DisplayMessage => _DisplayMessage;
  36.  
  37.         public string LoginText { get; set; }
  38.         public string SignupText {get; set; }
  39.         public string LearnMore { get; set; }
  40.         public string EmailText { get; set; }
  41.         public string PasswordText { get; set; }
  42.         public ICommand SignupClickedCommand { get; set; }
  43.         public ICommand LoginClickedCommand { get; set; }
  44.         public ICommand LearnMoreClickedCommand { get; set; }
  45.         public int Length { get; }
  46.    
  47.  
  48.         public LoginModel(INavigation LoginView)
  49.         {
  50.             Navigation = LoginView;
  51.             OpenPageCommand = new Command(async () => await OpenPage(), () => !IsBusy);
  52.  
  53.             async Task OpenPage()
  54.             {
  55.                 await Navigation.PushAsync(new LoginView());
  56.             }
  57.  
  58.             LoginText = "Log in";
  59.             SignupText = "Sign up";
  60.             LearnMore = "Learn more";
  61.             EmailText = "Email";
  62.             PasswordText = "Password";
  63.             SignupClickedCommand = new Command(SignupClicked);
  64.             LoginClickedCommand = new Command(LoginClickedAsync);
  65.             LearnMoreClickedCommand = new Command(LearnMoreClicked);
  66.             var emailStr = App.TokenStorage.GetToken("Email");
  67.             var passStr = App.TokenStorage.GetToken("Password");
  68.  
  69.             if (emailStr != null && passStr != null)
  70.             {
  71.                 Email = emailStr;
  72.                 Password = passStr;
  73.             }
  74.             OnPropertyChanged(nameof(email));
  75.             OnPropertyChanged(nameof(password));
  76.             OnPropertyChanged(nameof(DisplayMessage));
  77.         }
  78.  
  79.  
  80.  
  81.  
  82.         public string Email
  83.         {
  84.             get { return email; }
  85.             set
  86.             {
  87.                 email = value;
  88.  
  89.                 if (Regex.Match(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$").Success)
  90.                 {
  91.                    
  92.                     IsEmailValid = true;
  93.                     _DisplayMessage = "";
  94.                 }
  95.                 else
  96.                 {
  97.                     // not to forget to set value to false as the value is dynamic
  98.                     IsEmailValid = false;
  99.                     _DisplayMessage = "Wrong email field!";
  100.                 }
  101.  
  102.                 //OnPropertyChanged(nameof(Email));
  103.                 //OnPropertyChanged(nameof(DisplayMessage));
  104.             }
  105.         }
  106.  
  107.  
  108.         public string Password
  109.         {
  110.             get { return password; }
  111.             set
  112.             {
  113.                 password = value;
  114.  
  115.                 if (Regex.Match(password, @"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$").Success)
  116.                 {
  117.                    
  118.                     IsPasswordValid = true;
  119.                     _DisplayMessage = "";
  120.                 }
  121.                 else
  122.                 {
  123.                     IsPasswordValid = false;
  124.                     _DisplayMessage = "A-z | 0-1 | 8 chars min.";
  125.  
  126.                 }
  127.  
  128.                 //OnPropertyChanged(nameof(Password));
  129.                 //OnPropertyChanged(nameof(DisplayMessage));
  130.             }
  131.         }
  132.  
  133.  
  134.         public void LoginClickedAsync()
  135.         {
  136.             if (IsEmailValid && IsPasswordValid)
  137.             {
  138.                 // delete username & password from SecureStorage
  139.                 App.TokenStorage.DeleteToken("Email");
  140.                 App.TokenStorage.DeleteToken("Password");
  141.  
  142.                 // creating new user object and saving it within local DB
  143.                
  144.                 User user = new User(email, password);
  145.                 //var Login(user);
  146.                 //var result = await App.RestService.Login(user);
  147.  
  148.                 //App.UserDatabase.SaveUser(user);
  149.  
  150.                 RestService restService = new RestService();
  151.  
  152.  
  153.                 // initiate asynchronous call from within syncronous method and getting its value
  154.                 var loginTask = Task.Run(() => restService.LoginAsync(user)).Result;
  155.  
  156.                 if(loginTask)
  157.                 {
  158.  
  159.  
  160.                     Navigation.PushAsync(new WelcomeView());    
  161.                 }
  162.             }
  163.  
  164.  
  165.            
  166.         }
  167.  
  168.         public void SignupClicked()
  169.         {
  170.             Navigation.PushAsync(new SignupView());
  171.         }
  172.  
  173.         public void LearnMoreClicked()
  174.         {
  175.             Navigation.PushAsync(new WelcomeView());
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182.  
  183. // My ModelView LoginView.xaml
  184.  
  185.  
  186.     <ContentPage.Content>
  187.         <StackLayout Padding="10" Spacing="10" >
  188.             <StackLayout VerticalOptions="CenterAndExpand" >
  189.                 <Image Source="icon.png"/>
  190.             </StackLayout>
  191.  
  192.  
  193.             <StackLayout HorizontalOptions="CenterAndExpand"
  194.                          VerticalOptions="FillAndExpand" >
  195.  
  196.                 <StackLayout VerticalOptions="StartAndExpand">
  197.                     <StackLayout>
  198.                         <!--<StackLayout.Padding>
  199.                             <OnIdiom x:TypeArguments="Thickness">
  200.                                 <OnIdiom.Phone>40,0,40,0</OnIdiom.Phone>
  201.                                 <OnIdiom.Tablet>140,0,140,0</OnIdiom.Tablet>
  202.                             </OnIdiom>
  203.                         </StackLayout.Padding>-->
  204.                    
  205.                         <Entry  Text="{Binding Email}"
  206.                                 Placeholder="{Binding EmailText}"
  207.                                 WidthRequest="250"/>
  208.  
  209.                         <Entry  Text="{Binding Password}"
  210.                                 Placeholder="{Binding PasswordText}"
  211.                                 IsPassword="True"
  212.                                 WidthRequest="250"/>
  213.  
  214.                         <Label  Text="{Binding DisplayMessage}"
  215.                                 HorizontalTextAlignment="Center" />
  216.                     </StackLayout>
  217.  
  218.  
  219.                     <StackLayout>
  220.                         <Button Text="{Binding LoginText}"
  221.                                 BackgroundColor="LightGreen"
  222.                                 Command="{Binding LoginClickedCommand}"/>
  223.                     </StackLayout>
  224.  
  225.                     <StackLayout Orientation="Horizontal"  
  226.                                  HorizontalOptions="FillAndExpand">
  227.                        
  228.                         <StackLayout Orientation="Vertical"  
  229.                                      HorizontalOptions="FillAndExpand">
  230.                            
  231.                             <Button Text="{Binding SignupText}"
  232.                                     Command="{Binding SignupClickedCommand}"
  233.                                     BackgroundColor="Transparent"/>
  234.                            
  235.                         </StackLayout>
  236.                        
  237.                         <StackLayout Orientation="Vertical"  
  238.                                      HorizontalOptions="FillAndExpand">
  239.                        
  240.                             <Button Text="{Binding LearnMore}"  
  241.                                     BackgroundColor="Transparent"
  242.                                     Command="{Binding LearnMoreClickedCommand}"/>
  243.                            
  244.                         </StackLayout>
  245.                     </StackLayout>
  246.                 </StackLayout>
  247.             </StackLayout>
  248.         </StackLayout>
  249.     </ContentPage.Content>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement