Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MY ViewModel -> LoginModel.cs
- public class LoginModel : INotifyPropertyChanged
- {
- // navigation from ViewModel Handling with delcaration of INavigation
- public Command OpenPageCommand { get; }
- INavigation Navigation;
- // property changed event handler
- public event PropertyChangedEventHandler PropertyChanged;
- void OnPropertyChanged(string name)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- // Strings declarations
- public string password;
- //SecureString token;
- public string email;
- string _DisplayMessage;
- bool IsBusy = false;
- bool IsEmailValid = false;
- bool IsPasswordValid = false;
- public string DisplayMessage => _DisplayMessage;
- public string LoginText { get; set; }
- public string SignupText {get; set; }
- public string LearnMore { get; set; }
- public string EmailText { get; set; }
- public string PasswordText { get; set; }
- public ICommand SignupClickedCommand { get; set; }
- public ICommand LoginClickedCommand { get; set; }
- public ICommand LearnMoreClickedCommand { get; set; }
- public int Length { get; }
- public LoginModel(INavigation LoginView)
- {
- Navigation = LoginView;
- OpenPageCommand = new Command(async () => await OpenPage(), () => !IsBusy);
- async Task OpenPage()
- {
- await Navigation.PushAsync(new LoginView());
- }
- LoginText = "Log in";
- SignupText = "Sign up";
- LearnMore = "Learn more";
- EmailText = "Email";
- PasswordText = "Password";
- SignupClickedCommand = new Command(SignupClicked);
- LoginClickedCommand = new Command(LoginClickedAsync);
- LearnMoreClickedCommand = new Command(LearnMoreClicked);
- var emailStr = App.TokenStorage.GetToken("Email");
- var passStr = App.TokenStorage.GetToken("Password");
- if (emailStr != null && passStr != null)
- {
- Email = emailStr;
- Password = passStr;
- }
- OnPropertyChanged(nameof(email));
- OnPropertyChanged(nameof(password));
- OnPropertyChanged(nameof(DisplayMessage));
- }
- public string Email
- {
- get { return email; }
- set
- {
- email = value;
- if (Regex.Match(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$").Success)
- {
- IsEmailValid = true;
- _DisplayMessage = "";
- }
- else
- {
- // not to forget to set value to false as the value is dynamic
- IsEmailValid = false;
- _DisplayMessage = "Wrong email field!";
- }
- //OnPropertyChanged(nameof(Email));
- //OnPropertyChanged(nameof(DisplayMessage));
- }
- }
- public string Password
- {
- get { return password; }
- set
- {
- password = value;
- if (Regex.Match(password, @"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$").Success)
- {
- IsPasswordValid = true;
- _DisplayMessage = "";
- }
- else
- {
- IsPasswordValid = false;
- _DisplayMessage = "A-z | 0-1 | 8 chars min.";
- }
- //OnPropertyChanged(nameof(Password));
- //OnPropertyChanged(nameof(DisplayMessage));
- }
- }
- public void LoginClickedAsync()
- {
- if (IsEmailValid && IsPasswordValid)
- {
- // delete username & password from SecureStorage
- App.TokenStorage.DeleteToken("Email");
- App.TokenStorage.DeleteToken("Password");
- // creating new user object and saving it within local DB
- User user = new User(email, password);
- //var Login(user);
- //var result = await App.RestService.Login(user);
- //App.UserDatabase.SaveUser(user);
- RestService restService = new RestService();
- // initiate asynchronous call from within syncronous method and getting its value
- var loginTask = Task.Run(() => restService.LoginAsync(user)).Result;
- if(loginTask)
- {
- Navigation.PushAsync(new WelcomeView());
- }
- }
- }
- public void SignupClicked()
- {
- Navigation.PushAsync(new SignupView());
- }
- public void LearnMoreClicked()
- {
- Navigation.PushAsync(new WelcomeView());
- }
- }
- }
- // My ModelView LoginView.xaml
- <ContentPage.Content>
- <StackLayout Padding="10" Spacing="10" >
- <StackLayout VerticalOptions="CenterAndExpand" >
- <Image Source="icon.png"/>
- </StackLayout>
- <StackLayout HorizontalOptions="CenterAndExpand"
- VerticalOptions="FillAndExpand" >
- <StackLayout VerticalOptions="StartAndExpand">
- <StackLayout>
- <!--<StackLayout.Padding>
- <OnIdiom x:TypeArguments="Thickness">
- <OnIdiom.Phone>40,0,40,0</OnIdiom.Phone>
- <OnIdiom.Tablet>140,0,140,0</OnIdiom.Tablet>
- </OnIdiom>
- </StackLayout.Padding>-->
- <Entry Text="{Binding Email}"
- Placeholder="{Binding EmailText}"
- WidthRequest="250"/>
- <Entry Text="{Binding Password}"
- Placeholder="{Binding PasswordText}"
- IsPassword="True"
- WidthRequest="250"/>
- <Label Text="{Binding DisplayMessage}"
- HorizontalTextAlignment="Center" />
- </StackLayout>
- <StackLayout>
- <Button Text="{Binding LoginText}"
- BackgroundColor="LightGreen"
- Command="{Binding LoginClickedCommand}"/>
- </StackLayout>
- <StackLayout Orientation="Horizontal"
- HorizontalOptions="FillAndExpand">
- <StackLayout Orientation="Vertical"
- HorizontalOptions="FillAndExpand">
- <Button Text="{Binding SignupText}"
- Command="{Binding SignupClickedCommand}"
- BackgroundColor="Transparent"/>
- </StackLayout>
- <StackLayout Orientation="Vertical"
- HorizontalOptions="FillAndExpand">
- <Button Text="{Binding LearnMore}"
- BackgroundColor="Transparent"
- Command="{Binding LearnMoreClickedCommand}"/>
- </StackLayout>
- </StackLayout>
- </StackLayout>
- </StackLayout>
- </StackLayout>
- </ContentPage.Content>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement