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
5 changes: 5 additions & 0 deletions UndoAssessment/UndoAssessment/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Xamarin.Forms.Xaml;
using UndoAssessment.Services;
using UndoAssessment.Views;
using UndoAssessment.Networking;

namespace UndoAssessment
{
Expand All @@ -14,6 +15,10 @@ public App ()
InitializeComponent();

DependencyService.Register<MockDataStore>();
DependencyService.Register<HttpService>();
DependencyService.Register<TempApiService>();
DependencyService.Register<PopupService>();

MainPage = new AppShell();
}

Expand Down
4 changes: 4 additions & 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="Api Call" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ApiCallPage}" />
</TabBar>

<!--
Expand All @@ -42,6 +43,9 @@
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
</TabBar>

<FlyoutItem>
<ShellContent ContentTemplate="{DataTemplate local:UserInfoPage}"/>

</FlyoutItem>
</Shell>

1 change: 1 addition & 0 deletions UndoAssessment/UndoAssessment/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public AppShell()
InitializeComponent();
Routing.RegisterRoute(nameof(ItemDetailPage), typeof(ItemDetailPage));
Routing.RegisterRoute(nameof(NewItemPage), typeof(NewItemPage));
Routing.RegisterRoute(nameof(UserInfoPage), typeof(UserInfoPage));
}

}
Expand Down
10 changes: 10 additions & 0 deletions UndoAssessment/UndoAssessment/Models/ApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace UndoAssessment.Models
{
public class ApiResponse
{
public string Message { get; set; } = null;
public int ErrorCode { get; set; } = 200;
}
}

10 changes: 10 additions & 0 deletions UndoAssessment/UndoAssessment/Models/UserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace UndoAssessment.Models
{
public class UserModel
{
public string UserName { get; set; }
public int Age { get; set; }
}
}

55 changes: 55 additions & 0 deletions UndoAssessment/UndoAssessment/Networking/HttpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using UndoAssessment.Models;
using Xamarin.Essentials;
using Xamarin.Forms;
using System.Text.Json;

namespace UndoAssessment.Networking
{

public class HttpService : IHttpService
{
private readonly JsonSerializerOptions _options;
public HttpService()
{
_options = new JsonSerializerOptions
{
PropertyNamingPolicy= JsonNamingPolicy.CamelCase
};
}

public async Task<ApiResponse> GetAsync<TResult>(string uri)
{
try
{
if (Connectivity.NetworkAccess != NetworkAccess.Internet)
{
//NO network
}
HttpClient httpClient = CreateHttpClient();
var response = await httpClient.GetAsync(uri);

string serialized = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<ApiResponse>(serialized, _options);

return result;
}
catch (Exception ex)
{
return null;
}
}

private HttpClient CreateHttpClient()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return httpClient;
}
}
}

13 changes: 13 additions & 0 deletions UndoAssessment/UndoAssessment/Networking/IHttpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Threading.Tasks;
using UndoAssessment.Models;

namespace UndoAssessment.Networking
{
public interface IHttpService
{
Task<ApiResponse> GetAsync<TResult>(string uri);
}

}

24 changes: 24 additions & 0 deletions UndoAssessment/UndoAssessment/Services/PopupService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;

namespace UndoAssessment.Services
{
public interface IPopupService
{
Task ShowMessageAsync(string message);
}


public class PopupService : IPopupService
{
public PopupService()
{
}

public async Task ShowMessageAsync(string message)
{
await App.Current.MainPage.DisplayAlert("Message", message, "Ok");
}
}
}

36 changes: 36 additions & 0 deletions UndoAssessment/UndoAssessment/Services/TempApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading.Tasks;
using UndoAssessment.Models;
using UndoAssessment.Networking;
using Xamarin.Forms;

namespace UndoAssessment.Services
{
public interface ITempApiService
{
Task<ApiResponse> SuccessApiCall();
Task<ApiResponse> FailApiCall();
}

public class TempApiService : ITempApiService
{
private readonly IHttpService _httpService;
public TempApiService()
{
_httpService = DependencyService.Resolve<IHttpService>();
}

public Task<ApiResponse> SuccessApiCall()
{
var url = "https://malkarakundostagingpublicapi.azurewebsites.net/success";
return _httpService.GetAsync<ApiResponse>(url);
}

public Task<ApiResponse> FailApiCall()
{
var url = "https://malkarakundostagingpublicapi.azurewebsites.net/fail";
return _httpService.GetAsync<ApiResponse>(url);
}
}
}

10 changes: 10 additions & 0 deletions UndoAssessment/UndoAssessment/UndoAssessment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<BuildWithMSBuildOnMono>true</BuildWithMSBuildOnMono>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2578" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.6" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<None Remove="Networking\" />
<None Remove="Extensions\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Networking\" />
<Folder Include="Extensions\" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/ApiCallViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Web;
using UndoAssessment.Services;
using UndoAssessment.Views;
using Xamarin.Forms;

namespace UndoAssessment.ViewModels
{
public class ApiCallViewModel : BaseViewModel, IQueryAttributable
{
private readonly ITempApiService _tempApiService;
private readonly IPopupService _popupService;

public ApiCallViewModel()
{
_tempApiService = DependencyService.Resolve<ITempApiService>();
_popupService = DependencyService.Resolve<IPopupService>();
SuccessApiCallCommand = new Command(OnSuccessApiCall);
FailApiCallCommand = new Command(OnFailApiCall);
GetUserInfoCommand = new Command(OnGetUserInfo);
}

public Command SuccessApiCallCommand { get; }

public Command FailApiCallCommand { get; }

public Command GetUserInfoCommand { get; }

private async void OnSuccessApiCall(object obj)
{
var response = await _tempApiService.SuccessApiCall();
if(response != null)
{
await _popupService.ShowMessageAsync(response.Message ?? "");
}
}

private async void OnFailApiCall(object obj)
{
var response = await _tempApiService.FailApiCall();
if (response != null)
{
await _popupService.ShowMessageAsync(response.Message ?? "");
}
}

private async void OnGetUserInfo(object obj)
{
await Shell.Current.GoToAsync($"{nameof(UserInfoPage)}");

}

public async void ApplyQueryAttributes(IDictionary<string, string> query)
{
string name = HttpUtility.UrlDecode(query["name"]);
string age = HttpUtility.UrlDecode(query["age"]);

string message = "";
if (!string.IsNullOrWhiteSpace(name))
{
message += $"Name: {name} ";
}
if (!string.IsNullOrWhiteSpace(age))
{
message += $"Age: {age}";
}
if (!string.IsNullOrWhiteSpace(message))
{
await _popupService.ShowMessageAsync(message);
}
}
}
}

37 changes: 37 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/UserInfoViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using UndoAssessment.Models;
using UndoAssessment.Views;
using Xamarin.Forms;

namespace UndoAssessment.ViewModels
{
public class UserInfoViewModel : BaseViewModel
{
public UserInfoViewModel()
{
SubmitCommand = new Command(OnSubmit);
}

private string _userName;
public string UserName
{
get => _userName;
set => SetProperty(ref _userName, value);
}

private int _age;
public int Age
{
get => _age;
set => SetProperty(ref _age, value);
}

public Command SubmitCommand { get; }

private async void OnSubmit(object obj)
{
await Shell.Current.GoToAsync($"..?name={UserName}&age={Age}");
}
}
}

50 changes: 50 additions & 0 deletions UndoAssessment/UndoAssessment/Views/ApiCallPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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:vm="clr-namespace:UndoAssessment.ViewModels"
x:Class="UndoAssessment.Views.ApiCallPage">
<ContentPage.BindingContext>
<vm:ApiCallViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="150"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
Padding="20,0"
VerticalOptions="Start"
HorizontalOptions="End"
Margin="5"
Text="User Info"
Command="{Binding GetUserInfoCommand}"
BackgroundColor="{StaticResource Primary}"
TextColor="White" />
<StackLayout
Grid.Row="1"
HorizontalOptions="CenterAndExpand"
Orientation="Horizontal">
<Button
Padding="20,0"
VerticalOptions="Center"
Margin="0,10,0,0"
Text="Success Call"
Command="{Binding SuccessApiCallCommand}"
BackgroundColor="{StaticResource Primary}"
TextColor="White" />
<Button
Margin="0,10,0,0"
Padding="20,0"
VerticalOptions="Center"
Text="Fail Call"
Command="{Binding FailApiCallCommand}"
BackgroundColor="{StaticResource Primary}"
TextColor="White" />
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>

Loading