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
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
<application android:label="UndoAssessment.Android" android:theme="@style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

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

2 changes: 2 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="Assessment" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:AssessmentPage}" />
</TabBar>

<!--
Expand All @@ -40,6 +41,7 @@
-->
<TabBar>
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
<ShellContent Route="UserPage" ContentTemplate="{DataTemplate local:UserPage}" />
</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(UserPage), typeof(UserPage));
}

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

namespace UndoAssessment.Models
{
public class ResponseModel
{
public string ErrorCode { get; set; }
public string Message { get; set; }
public string Date { get; set; }
}
}
24 changes: 24 additions & 0 deletions UndoAssessment/UndoAssessment/Services/RequestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UndoAssessment.Models;

namespace UndoAssessment.Services
{
public class RequestService
{
public RequestService() { }

public async Task<ResponseModel> SendRequest(string url)
{
HttpClient client = new HttpClient();
var result = await client.GetAsync(url);
var stringResult = await result.Content.ReadAsStringAsync();
var jsonResult = JsonConvert.DeserializeObject<ResponseModel>(stringResult);
return jsonResult;
}
}
}
10 changes: 10 additions & 0 deletions UndoAssessment/UndoAssessment/UndoAssessment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
</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>
<EmbeddedResource Update="Views\AssessmentPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Views\UserPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace UndoAssessment.ViewModels
{
public static class AssessmentContextViewModel
{
private static AssessmentViewModel _assessmentViewModel = new AssessmentViewModel();
public static AssessmentViewModel AssessmentViewModel
{
get
{
return _assessmentViewModel;
}
}
}
}
94 changes: 94 additions & 0 deletions UndoAssessment/UndoAssessment/ViewModels/AssessmentViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using UndoAssessment.Models;
using UndoAssessment.Services;
using UndoAssessment.Views;
using Xamarin.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace UndoAssessment.ViewModels
{
public class AssessmentViewModel : BaseViewModel
{
public Command SuccessServiceCommand { get; }
public Command ErrorServiceCommand { get; }
public Command AddUserCommand { get; }
public Command AddCommand { get; }

private RequestService _service { get; set; }

private string userName;
private int age;

public AssessmentViewModel()
{
SuccessServiceCommand = new Command(async () => await sendSuccessRequest());
ErrorServiceCommand = new Command(async () => await sendErrorRequest());
AddUserCommand = new Command(async () => await goToAddUser());
AddCommand = new Command(async () => await addUser());
_service = new RequestService();
}

private async Task sendSuccessRequest()
{
IsBusy = true;
var model = await _service.SendRequest("https://malkarakundostagingpublicapi.azurewebsites.net/success");
IsBusy = false;
if (model == null)
{
Device.BeginInvokeOnMainThread(async () => {
await Shell.Current.DisplayAlert("Error", "System error occurred!", "Ok");
});
}

await showSuccessMessage(model);
}

private async Task sendErrorRequest()
{
IsBusy = true;
var model = await _service.SendRequest("https://malkarakundostagingpublicapi.azurewebsites.net/fail");
IsBusy = false;
if (model == null)
{
await Shell.Current.DisplayAlert("Error", "System error occurred!", "Ok");
}

await showErrorMessage(model);
}

private async Task goToAddUser()
{
await Shell.Current.GoToAsync($"//{nameof(UserPage)}");
}

private async Task showSuccessMessage(ResponseModel model)
{
await Shell.Current.DisplayAlert("Success", $"{model.Message} {model.Date}", "OK");
}

private async Task showErrorMessage(ResponseModel model)
{
await Shell.Current.DisplayAlert("Error", $"{model.ErrorCode} {model.Message} {model.Date}", "Ok");
}

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

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

private async Task addUser()
{
await Shell.Current.GoToAsync("..");
}
}
}
19 changes: 19 additions & 0 deletions UndoAssessment/UndoAssessment/Views/AssessmentPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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.AssessmentPage">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add User" Command="{Binding AddUserCommand}"></ToolbarItem>
</ContentPage.ToolbarItems>

<ContentPage.Content>
<StackLayout>
<StackLayout>
<Label Text="{Binding UserName}"></Label>
<Label Text="{Binding Age}"></Label>
</StackLayout>
<Button Text="Success" Command="{Binding SuccessServiceCommand}"></Button>
<Button Text="Error" Command="{Binding ErrorServiceCommand}"></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
16 changes: 16 additions & 0 deletions UndoAssessment/UndoAssessment/Views/AssessmentPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UndoAssessment.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace UndoAssessment.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AssessmentPage : ContentPage
{
public AssessmentPage()
{
InitializeComponent();
BindingContext = AssessmentContextViewModel.AssessmentViewModel;
}
}
}
12 changes: 12 additions & 0 deletions UndoAssessment/UndoAssessment/Views/UserPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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.UserPage">
<ContentPage.Content>
<StackLayout>
<Entry Placeholder="Name" Text="{Binding UserName}"></Entry>
<Entry Placeholder="Age" Text="{Binding Age}" Keyboard="Numeric"></Entry>
<Button Text="Add" Command="{Binding AddCommand}"></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
21 changes: 21 additions & 0 deletions UndoAssessment/UndoAssessment/Views/UserPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UndoAssessment.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace UndoAssessment.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class UserPage : ContentPage
{
public UserPage()
{
InitializeComponent();
BindingContext = AssessmentContextViewModel.AssessmentViewModel;
}
}
}