Skip to content
This repository was archived by the owner on Nov 14, 2020. It is now read-only.

Commit 64e4c9e

Browse files
Refactored BaseViewModel & UserDataViewModel
- Moved IsBusy to UserDataViewModel because it is the only view model using the property - Updated boolean property to follow Yes/No naming format
1 parent bb548d7 commit 64e4c9e

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

src/XamarinAzureChallenge/XamarinAzureChallenge.Shared/Models/User.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class User
1313
[JsonProperty("phone")]
1414
public string Phone { get; set; } = string.Empty;
1515

16-
[JsonProperty("acceptTermsOfService")]
17-
public bool AcceptTermsOfService { get; set; } = false;
16+
[JsonProperty("isTermsOfServiceAccepted")]
17+
public bool IsTermsOfServiceAccepted { get; set; } = false;
1818

19-
[JsonProperty("allowsComercialUse")]
20-
public bool AllowsComercialUse { get; set; } = false;
19+
[JsonProperty("isComercialCommunicationsAccepted")]
20+
public bool IsComercialCommunicationsAccepted { get; set; } = false;
2121
}
2222
}

src/XamarinAzureChallenge/XamarinAzureChallenge/Pages/UserDataPage.xaml

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@
156156
Text="I accept the terms of service*" />
157157
<Switch Grid.Row="10"
158158
Grid.Column="1"
159-
IsToggled="{Binding User.AcceptTermsOfService}"
159+
IsToggled="{Binding User.IsTermsOfServiceAccepted}"
160160
VerticalOptions="Center" />
161161

162162
<Label Grid.Row="11"
163163
Style="{StaticResource ToggleTitleStyle}"
164164
Text="I give consent for Microsoft to send me personalised commercial communications" />
165165
<Switch Grid.Row="11"
166166
Grid.Column="1"
167-
IsToggled="{Binding User.AllowsComercialUse}"
167+
IsToggled="{Binding User.IsComercialCommunicationsAccepted}"
168168
VerticalOptions="Center" />
169169

170170

src/XamarinAzureChallenge/XamarinAzureChallenge/ViewModels/BaseViewModel.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,14 @@
22
using System.ComponentModel;
33
using System.Runtime.CompilerServices;
44
using System.Threading.Tasks;
5-
using System.Windows.Input;
65
using Xamarin.Forms;
76

87
namespace XamarinAzureChallenge.ViewModels
98
{
109
public abstract class BaseViewModel : INotifyPropertyChanged
1110
{
12-
private bool isBusy;
13-
1411
public event PropertyChangedEventHandler PropertyChanged;
1512

16-
protected BaseViewModel()
17-
{
18-
FeatureNotAvailableCommand = new Command(async () => await ShowFeatureNotAvailable());
19-
}
20-
21-
public ICommand FeatureNotAvailableCommand { get; }
22-
23-
public bool IsBusy
24-
{
25-
get => isBusy;
26-
set => SetAndRaisePropertyChanged(ref isBusy, value);
27-
}
28-
2913
protected void SetAndRaisePropertyChanged<TRef>(
3014
ref TRef field, TRef value, [CallerMemberName] string propertyName = null)
3115
{
@@ -43,15 +27,10 @@ protected void SetAndRaisePropertyChangedIfDifferentValues<TRef>(
4327
SetAndRaisePropertyChanged(ref field, value, propertyName);
4428
}
4529

46-
protected Task NavigateToPage(Page page, bool clearStack = false)
30+
protected Task NavigateToPage(Page page)
4731
{
4832
return RunOnUIThread(async () =>
49-
{
50-
if (clearStack)
51-
Application.Current.MainPage = new NavigationPage(page);
52-
else
53-
await Application.Current.MainPage.Navigation.PushAsync(page);
54-
});
33+
await Application.Current.MainPage.Navigation.PushAsync(page));
5534
}
5635

5736
protected Task NavigateBack()
@@ -66,7 +45,7 @@ protected Task ShowFeatureNotAvailable()
6645
await Application.Current.MainPage.DisplayAlert("Feature not available", "", "Ok"));
6746
}
6847

69-
protected Task DisplayAlert(string message)
48+
protected Task DisplayInvalidFieldAlert(string message)
7049
{
7150
return RunOnUIThread(async () =>
7251
await Application.Current.MainPage.DisplayAlert("Invalid Field", message, "Ok"));

src/XamarinAzureChallenge/XamarinAzureChallenge/ViewModels/UserDataViewModel.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ public class UserDataViewModel : BaseViewModel
1717
private readonly Lazy<HttpClient> clientHolder = new Lazy<HttpClient>();
1818

1919
private User user;
20+
private bool isBusy;
2021

2122
public UserDataViewModel()
2223
{
2324
User = new User();
24-
SubmitCommand = new Command(async () => await SubmitCommmandExecute());
25+
SubmitCommand = new Command(async () => await SubmitCommmandExecute(User));
2526
PrivacyStatementCommand = new Command(async () => await PrivacyStatementCommandExecute());
2627
}
2728

2829
public ICommand SubmitCommand { get; }
2930
public ICommand PrivacyStatementCommand { get; }
3031

32+
public bool IsBusy
33+
{
34+
get => isBusy;
35+
set => SetAndRaisePropertyChanged(ref isBusy, value);
36+
}
37+
3138
public User User
3239
{
3340
get => user;
@@ -36,7 +43,7 @@ public User User
3643

3744
private HttpClient Client => clientHolder.Value;
3845

39-
private async Task SubmitCommmandExecute()
46+
private async Task SubmitCommmandExecute(User submittedUser)
4047
{
4148
if (IsBusy)
4249
return;
@@ -45,7 +52,7 @@ private async Task SubmitCommmandExecute()
4552

4653
try
4754
{
48-
var areFieldsValid = await AreFieldsValid();
55+
var areFieldsValid = await AreFieldsValid(submittedUser.Name, submittedUser.Email, submittedUser.Phone, submittedUser.IsTermsOfServiceAccepted);
4956

5057
if (areFieldsValid)
5158
{
@@ -68,25 +75,25 @@ private async Task SubmitCommmandExecute()
6875
}
6976
}
7077

71-
private async Task<bool> AreFieldsValid()
78+
private async Task<bool> AreFieldsValid(string name, string email, string phone, bool isTermsOfServiceAccepted)
7279
{
7380
var result = false;
7481

75-
if (string.IsNullOrWhiteSpace(User.Name))
82+
if (string.IsNullOrWhiteSpace(name))
7683
{
77-
await DisplayAlert("Name cannot be blank");
84+
await DisplayInvalidFieldAlert("Name cannot be blank");
7885
}
79-
else if (string.IsNullOrWhiteSpace(User.Email))
86+
else if (string.IsNullOrWhiteSpace(email))
8087
{
81-
await DisplayAlert("Email cannot be blank");
88+
await DisplayInvalidFieldAlert("Email cannot be blank");
8289
}
83-
else if (string.IsNullOrWhiteSpace(User.Phone))
90+
else if (string.IsNullOrWhiteSpace(phone))
8491
{
85-
await DisplayAlert("Phone cannot be blank");
92+
await DisplayInvalidFieldAlert("Phone cannot be blank");
8693
}
87-
else if (!User.AcceptTermsOfService)
94+
else if (!isTermsOfServiceAccepted)
8895
{
89-
await DisplayAlert("Terms of Service Not Accepted");
96+
await DisplayInvalidFieldAlert("Terms of Service Not Accepted");
9097
}
9198
else
9299
{

0 commit comments

Comments
 (0)