Skip to content

Instantly share code, notes, and snippets.

@kevinmutlow
Last active February 25, 2020 20:45
Show Gist options
  • Save kevinmutlow/fb763cbf3c803d73f94ea220bafcfe0d to your computer and use it in GitHub Desktop.
Save kevinmutlow/fb763cbf3c803d73f94ea220bafcfe0d to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:App.Core.Converters"
x:Class="App.Core.Pages.Account.RegisterPage"
Title="{ Binding PageTitle }">
<ContentPage.Resources>
<ResourceDictionary>
<converters:InverseBoolConverter x:Key="notConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
</OnPlatform>
</ContentPage.Padding>
<Grid
RowSpacing="0"
ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="6*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<BoxView
Grid.Row="0"
BackgroundColor="#00CCD6" />
<Image
Grid.Row="0"
Source="brand_logo.png"
VerticalOptions="Center"
HorizontalOptions="Start"
HeightRequest="75"
WidthRequest="75"
Margin="20,0,0,0"/>
<Label
Grid.Row="0"
Text="REGISTER"
FontSize="Large"
TextColor="White"
HorizontalOptions="End"
VerticalOptions="Center"
Margin="0,0,20,0"/>
<ScrollView
Grid.Row="1">
<StackLayout
VerticalOptions="Fill"
HorizontalOptions="Center"
Spacing="15"
Padding="0,15,0,0">
<StackLayout HorizontalOptions="Center" Spacing="5">
<Label
Text="Full Name:" />
<Entry
Text="{ Binding FullName }"
WidthRequest="300" />
</StackLayout>
<StackLayout HorizontalOptions="Center" Spacing="5">
<Label
Text="Email:" />
<Entry
Text="{ Binding Email }"
Keyboard="Email"
WidthRequest="300" />
</StackLayout>
<StackLayout HorizontalOptions="Center" Spacing="5">
<Label
Text="Confirm Email:" />
<Entry
Text="{ Binding ConfirmEmail }"
Keyboard="Email"
WidthRequest="300" />
<Label
Text="Email addresses do not match"
IsVisible="{ Binding IsEmailMatching, Converter={StaticResource notConverter} }"
FontSize="Small"
TextColor="Red" />
</StackLayout>
<StackLayout HorizontalOptions="Center" Spacing="5">
<Label
Text="Password:" />
<Entry
Text="{ Binding Password }"
WidthRequest="300"
IsPassword="True" />
</StackLayout>
</StackLayout>
</ScrollView>
<!-- FOOTER row -->
<StackLayout
Grid.Row="2"
Orientation="Horizontal"
BackgroundColor="#07616c">
<Label
Text="Already have an account?"
TextColor="White"
HorizontalOptions="Start"
VerticalOptions="Center"
Margin="10,0,0,0">
<Label.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Command="{ Binding BackToLoginCommand }" />
</Label.GestureRecognizers>
</Label>
<Button
Text="REGISTER"
TextColor="White"
HorizontalOptions="EndAndExpand"
VerticalOptions="Center"
Margin="0,0,10,0"
Command="{ Binding RegisterCommand }" />
</StackLayout>
<!-- MODAL BACKGROUND LAYER -->
<BoxView
Grid.Row="0"
Grid.RowSpan="3"
BackgroundColor="Black"
Opacity="0.8"
IsVisible="{ Binding IsBusy }" />
<ActivityIndicator
Grid.Row="0"
Grid.RowSpan="3"
VerticalOptions="Center"
HorizontalOptions="Center"
Color="White"
IsRunning="{ Binding IsBusy }"
IsVisible="{ Binding IsBusy }" />
<!-- CANCEL BUTTON (appears after X seconds after register) -->
<StackLayout
Grid.Row="0"
Grid.RowSpan="3"
VerticalOptions="Center"
TranslationY="100"
Margin="50,0"
IsVisible="{ Binding IsShowCancel }">
<Label
Text="Still working...please wait!"
FontSize="Small"
TextColor="White"
LineBreakMode="WordWrap"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"/>
<Button
Text="Cancel"
Command="{ Binding CancelRegisterCommand }"
HorizontalOptions="Center" />
</StackLayout>
</Grid>
</ContentPage>
using App.Core.ViewModels.Account;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace App.Core.Pages.Account
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class RegisterPage : ContentPage
{
public RegisterPage ()
{
InitializeComponent ();
BindingContext = new RegisterPageViewModel(Navigation);
}
}
}
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace App.Core.ViewModels.Account
{
public class RegisterPageViewModel : BasePageViewModel
{
private readonly INavigation Navigation;
public RegisterPageViewModel(INavigation _navigation)
{
Navigation = _navigation;
PageTitle = "Register";
CheckEmailAddressesMatch();
}
#region Properties
private string _fullName;
public string FullName
{
get { return _fullName; }
set { SetPropertyValue(ref _fullName, value); }
}
private string _email;
public string Email
{
get { return _email; }
set
{
if (SetPropertyValue(ref _email, value))
{
this.CheckEmailAddressesMatch();
((Command)RegisterCommand).ChangeCanExecute();
}
}
}
private string _confirmEmail;
public string ConfirmEmail
{
get { return _confirmEmail; }
set
{
if (SetPropertyValue(ref _confirmEmail, value))
{
this.CheckEmailAddressesMatch();
((Command)RegisterCommand).ChangeCanExecute();
}
}
}
private string _password;
public string Password
{
get { return _password; }
set
{
if (SetPropertyValue(ref _password, value))
{
((Command)RegisterCommand).ChangeCanExecute();
}
}
}
private bool _isEmailMatching;
public bool IsEmailMatching
{
get { return _isEmailMatching; }
set { SetPropertyValue(ref _isEmailMatching, value); }
}
private bool _isShowCancel;
public bool IsShowCancel
{
get { return _isShowCancel; }
set { SetPropertyValue(ref _isShowCancel, value); }
}
#endregion
#region Commands
private ICommand _registerCommand;
public ICommand RegisterCommand
{
get { return _registerCommand = _registerCommand ?? new Command(RegisterAction, CanRegisterAction); }
}
private ICommand _cancelRegisterCommand;
public ICommand CancelRegisterCommand
{
get { return _cancelRegisterCommand = _cancelRegisterCommand ?? new Command(CancelRegisterAction); }
}
private ICommand _backToLoginCommand;
public ICommand BackToLoginCommand
{
get { return _backToLoginCommand = _backToLoginCommand ?? new Command(BackToLoginAction); }
}
#endregion
#region Methods
void CheckEmailAddressesMatch()
{
if (string.IsNullOrWhiteSpace(this.Email)
|| string.IsNullOrWhiteSpace(this.ConfirmEmail))
{
IsEmailMatching = true;
return;
}
if (this.Email == this.ConfirmEmail)
{
IsEmailMatching = true;
return;
}
IsEmailMatching = false;
}
bool CanRegisterAction()
{
//Perform your "Can Register?" logic here...
if (string.IsNullOrWhiteSpace(this.Email)
|| string.IsNullOrWhiteSpace(this.ConfirmEmail)
|| string.IsNullOrWhiteSpace(this.Password))
return false;
if (!this.IsEmailMatching)
return false;
return true;
}
async void RegisterAction()
{
IsBusy = true;
//TODO - perform your registration logic + navigate to the next page as applicable
//Simulate an API call to show busy/progress indicator
Task.Delay(20000).ContinueWith((t) => { IsBusy = false; IsShowCancel = false; });
//Show the Cancel button after X seconds
Task.Delay(5000).ContinueWith((t) => IsShowCancel = true);
}
void CancelRegisterAction()
{
//TODO - perform cancellation logic
IsBusy = false;
IsShowCancel = false;
}
void BackToLoginAction()
{
//TODO - navigate to the login page
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment