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

Expand All @@ -12,8 +10,9 @@ public partial class App : Application
public App ()
{
InitializeComponent();

DependencyService.Register<MockDataStore>();
DependencyService.Register<AssessmentService>();
DependencyService.Register<RequestProvider.RequestProvider>();
MainPage = new AppShell();
}

Expand Down
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="API" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:AssessmentPage}" />
</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(AssessmentPage), typeof(AssessmentPage));
Routing.RegisterRoute(nameof(AddUserInfoPage), typeof(AddUserInfoPage));
}

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

namespace UndoAssessment.Models
{
public class ResponseModel
{
[JsonProperty("errorcode")]
public int ErrorCode { get; set; }

[JsonProperty("message")]
public string Message { get; set; }

[JsonProperty("date")]
public string Date { get; set; }
}
}
19 changes: 19 additions & 0 deletions UndoAssessment/UndoAssessment/RequestProvider/IRequestProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace UndoAssessment.RequestProvider
{
public interface IRequestProvider
{
Task<TResult> GetAsync<TResult>(string uri, string token = "", Dictionary<string, string> headers = null);

Task<TResult> PostAsync<TResult>(string uri, TResult data, string token = "", Dictionary<string, string> headers = null);

Task<TResult> PutAsync<TResult, TInput>(string uri, TInput data, string token = "", Dictionary<string, string> headers = null);

Task<string> PutAsync<TInput>(string uri, TInput data, string token = "", Dictionary<string, string> headers = null);

}
}
96 changes: 96 additions & 0 deletions UndoAssessment/UndoAssessment/RequestProvider/RequestProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Newtonsoft.Json;

namespace UndoAssessment.RequestProvider
{
public class RequestProvider : IRequestProvider
{
private HttpClient httpClient;
public RequestProvider()
{
httpClient = CreateHttpClient();
}

public async Task<TResult> GetAsync<TResult>(string uri, string token = "",
Dictionary<string, string> headers = null)
{
HttpResponseMessage response = null;
try
{
if (!string.IsNullOrEmpty(token))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}

if (headers == null)
{
headers = new Dictionary<string, string>();
}

SetHeaders(httpClient, headers);

response = await httpClient.GetAsync(uri);

string serialized = await response.Content.ReadAsStringAsync();

TResult result = await Task.Run(() =>
JsonConvert.DeserializeObject<TResult>(serialized));

return result;

}
catch (Exception)
{
return await Task.Run(() => JsonConvert.DeserializeObject<TResult>(""));
}

}

public Task<TResult> PostAsync<TResult>(string uri, TResult data, string token = "", Dictionary<string, string> headers = null)
{
throw new NotImplementedException();
}

public Task<TResult> PutAsync<TResult, TInput>(string uri, TInput data, string token = "", Dictionary<string, string> headers = null)
{
throw new NotImplementedException();
}

public Task<string> PutAsync<TInput>(string uri, TInput data, string token = "", Dictionary<string, string> headers = null)
{
throw new NotImplementedException();
}

#region private methods
private HttpClient CreateHttpClient(string token = "")
{
httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(5);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

if (!string.IsNullOrEmpty(token))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
return httpClient;
}

private void SetHeaders(HttpClient httpClient, Dictionary<string, string> headers)
{
httpClient.DefaultRequestHeaders.Clear();
foreach (KeyValuePair<string, string> header in headers)
{
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
#endregion
}
}
33 changes: 33 additions & 0 deletions UndoAssessment/UndoAssessment/Services/AssessmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using UndoAssessment.Models;
using UndoAssessment.RequestProvider;
using Xamarin.Forms;

namespace UndoAssessment.Services
{
public class AssessmentService : IAssessmentService
{
private readonly IRequestProvider _requestProvider;
private string baseURL = "https://malkarakundostagingpublicapi.azurewebsites.net";
public AssessmentService()
{
_requestProvider = DependencyService.Get<IRequestProvider>();
}
public async Task<ResponseModel> ExecuteFailureAPI()
{
var requestURL = $"{baseURL}/fail";
var response = await _requestProvider.GetAsync<ResponseModel>(requestURL);
return response;
}

public async Task<ResponseModel> ExecuteSuccessAPI()
{
var requestURL = $"{baseURL}/success";
var response = await _requestProvider.GetAsync<ResponseModel>(requestURL);
return response;
}
}
}
23 changes: 23 additions & 0 deletions UndoAssessment/UndoAssessment/Services/IAssessmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using UndoAssessment.Models;

namespace UndoAssessment.Services
{
public interface IAssessmentService
{
/// <summary>
/// API call that returns success response
/// </summary>
/// <returns></returns>
Task<ResponseModel> ExecuteSuccessAPI();

/// <summary>
/// API call that returns failure response
/// </summary>
/// <returns></returns>
Task<ResponseModel> ExecuteFailureAPI();
}
}
16 changes: 16 additions & 0 deletions UndoAssessment/UndoAssessment/UndoAssessment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@
</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\AssessmentPage.xaml.cs">
<DependentUpon>AssessmentPage.xaml</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Views\AddUserInfoPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Views\AssessmentPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
39 changes: 39 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/AddUserInfoViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Essentials;
using Xamarin.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace UndoAssessment.ViewModels
{
public class AddUserInfoViewModel : BaseViewModel
{
public string UserName
{
get => _userName;
set => SetProperty(ref _userName, value);
}
private string _userName;

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

public Command SubmitUserInfoCommand { get; }
public AddUserInfoViewModel()
{
SubmitUserInfoCommand = new Command(OnSumbitUserInfo);
}

private async void OnSumbitUserInfo(object obj)
{
Preferences.Set(nameof(UserName), UserName);
Preferences.Set(nameof(Age), Age);
await Shell.Current.GoToAsync("..");
}
}
}
98 changes: 98 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/AssessmentViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using UndoAssessment.Models;
using UndoAssessment.Services;
using UndoAssessment.Views;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace UndoAssessment.ViewModels
{
public class AssessmentViewModel : BaseViewModel
{
private readonly IAssessmentService _assessmentService;

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

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

public Command SuccessCommand { get; }
public Command FailureCommand { get; }
public Command AddUserInfoCommand { get; }

public AssessmentViewModel()
{
_assessmentService = DependencyService.Get<IAssessmentService>();
SuccessCommand = new Command(OnSuccessTapped);
FailureCommand = new Command(OnFailureTapped);
AddUserInfoCommand = new Command(OnAddUserInfoTapped);
}
public void LoadUserData()
{
UserName = Preferences.Get(nameof(UserName), "");
Age = Preferences.Get(nameof(Age), "");
}

#region Commands
private async void OnAddUserInfoTapped(object obj)
{
await Shell.Current.GoToAsync("AddUserInfoPage");
}
private async void OnSuccessTapped(object obj)
{
try
{
var response = await _assessmentService.ExecuteSuccessAPI();
if (response != null)
{
await DisplayAlertMessage(response.Message);
}
else
{
await DisplayAlertMessage("No Response");
}
}
catch(Exception ex)
{
await DisplayAlertMessage(ex.Message);
}
}
private async void OnFailureTapped(object obj)
{
try
{
var response = await _assessmentService.ExecuteFailureAPI();
if (response != null)
{
await DisplayAlertMessage(response.Message);
}
else
{
await DisplayAlertMessage("No Response");
}
}
catch (Exception ex)
{
await DisplayAlertMessage(ex.Message);
}
}
#endregion

private async Task DisplayAlertMessage(string message)
{
await App.Current.MainPage.DisplayAlert("Assessment", message, "Ok");
}
}
}
Loading