Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions UndoAssessment/UndoAssessment/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<TabBar>
<ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
<ShellContent Title="Browse" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" />
<ShellContent Title="Test" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:TestPage}" />
</TabBar>

<!--
Expand Down
2 changes: 2 additions & 0 deletions UndoAssessment/UndoAssessment/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public AppShell()
InitializeComponent();
Routing.RegisterRoute(nameof(ItemDetailPage), typeof(ItemDetailPage));
Routing.RegisterRoute(nameof(NewItemPage), typeof(NewItemPage));
Routing.RegisterRoute(nameof(TestPage), typeof(TestPage));
Routing.RegisterRoute(nameof(UserFormPage), typeof(UserFormPage));
}

}
Expand Down
21 changes: 21 additions & 0 deletions UndoAssessment/UndoAssessment/Models/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace UndoAssessment.Models
{
public class HttpResponse
{
public string message { get; set; }
public string date { get; set; }
public int errorCode { get; set; }

public bool IsSuccess
{
get
{
return errorCode == 0;
}
}
}
}
12 changes: 12 additions & 0 deletions UndoAssessment/UndoAssessment/Models/UserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace UndoAssessment.Models
{
public class UserModel
{
public string Name { get; set; }
public string Age { get; set; }
}
}
3 changes: 3 additions & 0 deletions UndoAssessment/UndoAssessment/Services/IDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UndoAssessment.Models;

namespace UndoAssessment.Services
{
Expand All @@ -11,6 +12,8 @@ public interface IDataStore<T>
Task<bool> DeleteItemAsync(string id);
Task<T> GetItemAsync(string id);
Task<IEnumerable<T>> GetItemsAsync(bool forceRefresh = false);
void SetUser(UserModel user);
UserModel GetUser();
}
}

11 changes: 11 additions & 0 deletions UndoAssessment/UndoAssessment/Services/MockDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace UndoAssessment.Services
public class MockDataStore : IDataStore<Item>
{
readonly List<Item> items;
private UserModel user;

public MockDataStore()
{
Expand Down Expand Up @@ -56,5 +57,15 @@ public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
{
return await Task.FromResult(items);
}

public void SetUser(UserModel user)
{
this.user = user;
}

public UserModel GetUser()
{
return this.user;
}
}
}
37 changes: 37 additions & 0 deletions UndoAssessment/UndoAssessment/Services/TestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UndoAssessment.Models;

namespace UndoAssessment.Services
{
public class TestService
{
private readonly string _api1Endpoint = "https://malkarakundostagingpublicapi.azurewebsites.net/success";
private readonly string _api2Endpoint = "https://malkarakundostagingpublicapi.azurewebsites.net/fail";

private readonly HttpClient _httpClient = new HttpClient();

public async Task<HttpResponse> GetSuccess()
{
return await this.CommonHttpRequest(_api1Endpoint);
}

public async Task<HttpResponse> GetFailure()
{
return await this.CommonHttpRequest(_api2Endpoint);
}

public async Task<HttpResponse> CommonHttpRequest(string endpoint)
{
var response = await _httpClient.GetAsync(endpoint);
var content = await response.Content.ReadAsStringAsync();

return JsonConvert.DeserializeObject<HttpResponse>(content);
}
}
}
21 changes: 21 additions & 0 deletions UndoAssessment/UndoAssessment/UndoAssessment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2578" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.6" />
</ItemGroup>

<ItemGroup>
<Compile Update="Views\NewItemPage.xaml.cs">
<DependentUpon>NewItemPage.xaml</DependentUpon>
</Compile>
<Compile Update="Views\UserFormPage.xaml.cs">
<DependentUpon>UserFormPage.xaml</DependentUpon>
</Compile>
<Compile Update="Views\TestPage.xaml.cs">
<SubType>Code</SubType>
<DependentUpon>TestPage.xaml</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Views\TestPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
102 changes: 102 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/TestViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Xamarin.Forms;
using UndoAssessment.Models;
using UndoAssessment.Services;
using UndoAssessment.Views;

namespace UndoAssessment.ViewModels
{
public class TestViewModel : BaseViewModel
{
#region Variables

private TestService _testService = new TestService();

private UserModel _user => DataStore.GetUser();

public string User
{
get
{
if (_user == null)
return "";
else
return $"Name: {_user.Name}\n" +
$"Age: {_user.Age}";
}
}

public bool UserVisibility
{
get
{
return _user != null;
}
}

#endregion

#region Commands

public Command GotoUserFormCommand { get; }

public Command Api1Command { get; }
public Command Api2Command { get; }

private void OnGotoUserFormClicked()
{
Shell.Current.GoToAsync(nameof(UserFormPage));
}

private async void OnApi1Clicked()
{
var response = await _testService.GetSuccess();

if (response.IsSuccess)
await Application.Current.MainPage.DisplayAlert("Success", response.message, "Close");
else
await Application.Current.MainPage.DisplayAlert("Failure", response.message, "Close");
}

private async void OnApi2Clicked()
{
var response = await _testService.GetFailure();

if (response.IsSuccess)
await Application.Current.MainPage.DisplayAlert("Success", response.message, "Close");
else
await Application.Current.MainPage.DisplayAlert("Failure", response.message, "Close");
}

#endregion

#region Contructor

public TestViewModel()
{
Title = "Test Page";

GotoUserFormCommand = new Command(OnGotoUserFormClicked);
Api1Command = new Command(OnApi1Clicked);
Api2Command = new Command(OnApi2Clicked);
}

#endregion

#region Member functions

public void OnAppearing()
{
OnPropertyChanged(nameof(User));
OnPropertyChanged(nameof(UserVisibility));
}

#endregion
}
}
63 changes: 63 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/UserFormViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using UndoAssessment.Models;
using Xamarin.Forms;

namespace UndoAssessment.ViewModels
{
public class UserFormViewModel : BaseViewModel
{
private string name;
private string age;

public UserFormViewModel()
{
SaveCommand = new Command(OnSave, ValidateSave);
CancelCommand = new Command(OnCancel);
this.PropertyChanged +=
(_, __) => SaveCommand.ChangeCanExecute();
}

private bool ValidateSave()
{
return !String.IsNullOrWhiteSpace(name)
&& !String.IsNullOrWhiteSpace(age);
}

public string Name
{
get => name;
set => SetProperty(ref name, value);
}

public string Age
{
get => age;
set => SetProperty(ref age, value);
}

public Command SaveCommand { get; }
public Command CancelCommand { get; }

private async void OnCancel()
{
// This will pop the current page off the navigation stack
await Shell.Current.GoToAsync("..");
}

private async void OnSave()
{
DataStore.SetUser(new UserModel
{
Name = Name,
Age = Age
});

// This will pop the current page off the navigation stack
await Shell.Current.GoToAsync("..");
}
}
}

29 changes: 29 additions & 0 deletions UndoAssessment/UndoAssessment/Views/TestPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="UndoAssessment.Views.TestPage"
Title="{Binding Title}"
xmlns:local="clr-namespace:UndoAssessment.ViewModels"
x:DataType="local:TestViewModel"
x:Name="TestAssessmentPage">

<ContentPage.ToolbarItems>
<ToolbarItem Text="Set User" Command="{Binding GotoUserFormCommand}" />
</ContentPage.ToolbarItems>


<StackLayout Spacing="15" Padding="25">

<!-- User block -->
<Label Text="{Binding User}"
IsVisible="{Binding UserVisibility}"
HorizontalTextAlignment="Center">
</Label>

<StackLayout>
<Button Text="API1" Command="{Binding Api1Command}"></Button>
<Button Text="API2" Command="{Binding Api2Command}"></Button>
</StackLayout>
</StackLayout>
</ContentPage>

Loading