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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,4 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
.idea/
6,705 changes: 6,141 additions & 564 deletions UndoAssessment/UndoAssessment.Android/Resources/Resource.designer.cs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion UndoAssessment/UndoAssessment/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public App ()
{
InitializeComponent();

DependencyService.Register<MockDataStore>();
Startup.Init();

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="Assessment" 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(CreateUserPage), typeof(CreateUserPage));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UndoAssessment.Configuration;

public class AssessmentConfiguration
{
public string BaseAddress { get; set; }
public string Success { get; set; }
public string Fail { get; set; }
}
14 changes: 14 additions & 0 deletions UndoAssessment/UndoAssessment/Contracts/FailResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Newtonsoft.Json;

namespace UndoAssessment.Contracts;

public class FailResponse
{
[JsonProperty("errorCode")]
public int ErrorCode { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
}
6 changes: 6 additions & 0 deletions UndoAssessment/UndoAssessment/Contracts/Messaging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace UndoAssessment.Contracts;

public static class Messaging
{
public const string ID_CHANGED_MESSAGE = "Id changed";
}
13 changes: 13 additions & 0 deletions UndoAssessment/UndoAssessment/Contracts/SuccessResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace UndoAssessment.Contracts;

public class SuccessResponse
{
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
}
8 changes: 8 additions & 0 deletions UndoAssessment/UndoAssessment/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UndoAssessment.Models;

public class User
{
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
65 changes: 65 additions & 0 deletions UndoAssessment/UndoAssessment/Services/AssessmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using UndoAssessment.Configuration;
using UndoAssessment.Contracts;

namespace UndoAssessment.Services;

public class AssessmentService
{
private readonly HttpClient _httpClient;
private readonly ILogger<AssessmentService> _logger;
private readonly AssessmentConfiguration _assessmentConfiguration;

private const string DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm:ss";

public AssessmentService(HttpClient httpClient, IOptions<AssessmentConfiguration> assessmentConfiguration,
ILogger<AssessmentService> logger)
{
_httpClient = httpClient;
_logger = logger;
_assessmentConfiguration = assessmentConfiguration.Value;

_httpClient.BaseAddress = new Uri(_assessmentConfiguration.BaseAddress);
}

public async Task<SuccessResponse> GetSuccessResponseAsync()
{
try
{
var json = await _httpClient.GetStringAsync(_assessmentConfiguration.Success);
var response = JsonConvert.DeserializeObject<SuccessResponse>(json,
new IsoDateTimeConverter { DateTimeFormat = DATE_TIME_FORMAT });
return response;
}
catch (Exception exception)
{
_logger.LogError(exception, "Failed with message: {Message}", exception.Message);
}

return null;
}

public async Task<FailResponse> GetFailResponseAsync()
{
try
{
var httpResponse = await _httpClient.GetAsync(_assessmentConfiguration.Fail);
var json = await httpResponse.Content.ReadAsStringAsync();
var response = JsonConvert.DeserializeObject<FailResponse>(json,
new IsoDateTimeConverter { DateTimeFormat = DATE_TIME_FORMAT });
return response;
}
catch (Exception exception)
{
_logger.LogError(exception, "Failed with message: {Message}", exception.Message);
}

return null;
}
}
4 changes: 2 additions & 2 deletions UndoAssessment/UndoAssessment/Services/MockDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<bool> AddItemAsync(Item item)

public async Task<bool> UpdateItemAsync(Item item)
{
var oldItem = items.Where((Item arg) => arg.Id == item.Id).FirstOrDefault();
var oldItem = items.FirstOrDefault(arg => arg.Id == item.Id);
items.Remove(oldItem);
items.Add(item);

Expand All @@ -41,7 +41,7 @@ public async Task<bool> UpdateItemAsync(Item item)

public async Task<bool> DeleteItemAsync(string id)
{
var oldItem = items.Where((Item arg) => arg.Id == id).FirstOrDefault();
var oldItem = items.FirstOrDefault(arg => arg.Id == id);
items.Remove(oldItem);

return await Task.FromResult(true);
Expand Down
44 changes: 44 additions & 0 deletions UndoAssessment/UndoAssessment/Services/UserDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UndoAssessment.Models;

namespace UndoAssessment.Services;

public class UserDataStore : IDataStore<User>
{
readonly List<User> _items;

public UserDataStore()
{
_items = new List<User>();
}

public async Task<bool> AddItemAsync(User item)
{
_items.Add(item);

return await Task.FromResult(true);
}

public Task<bool> UpdateItemAsync(User item)
{
throw new System.NotImplementedException();
}

public Task<bool> DeleteItemAsync(string id)
{
throw new System.NotImplementedException();
}

public Task<User> GetItemAsync(string id)
{
var item = _items.FirstOrDefault((i) => i.Id == id);
return Task.FromResult(item);
}

public async Task<IEnumerable<User>> GetItemsAsync(bool forceRefresh = false)
{
throw new System.NotImplementedException();
}
}
64 changes: 64 additions & 0 deletions UndoAssessment/UndoAssessment/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Polly;
using UndoAssessment.Configuration;
using UndoAssessment.Models;
using UndoAssessment.Services;
using UndoAssessment.ViewModels;
using Xamarin.Essentials;

namespace UndoAssessment
{
public class Startup
{
public static IServiceProvider ServiceProvider { get; set; }

public static void Init()
{
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream("UndoAssessment.appsettings.json");

if (stream is null)
return;

var host = new HostBuilder()
.ConfigureHostConfiguration(c =>
{
c.AddCommandLine(new string[] { $"ContentRoot={FileSystem.AppDataDirectory}" });
c.AddJsonStream(stream);
})
.ConfigureServices(ConfigureServices)
.ConfigureLogging(l => l.AddConsole(opt => { opt.DisableColors = true; }))
.Build();

ServiceProvider = host.Services;
}

private static void ConfigureServices(HostBuilderContext ctx, IServiceCollection services)
{
services.Configure<AssessmentConfiguration>(ctx.Configuration.GetSection(nameof(AssessmentService)));

services.AddHttpClient<AssessmentService>()
.AddTransientHttpErrorPolicy(builder => builder
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));

if (ctx.HostingEnvironment.IsDevelopment())
services.AddSingleton<IDataStore<Item>, MockDataStore>();

services.AddSingleton<IDataStore<User>, UserDataStore>();
services.AddTransient<AssessmentService>();

services.AddTransient<LoginViewModel>();
services.AddTransient<ItemsViewModel>();
services.AddTransient<ItemDetailViewModel>();
services.AddTransient<AboutViewModel>();
services.AddTransient<NewItemViewModel>();
services.AddTransient<AssessmentViewModel>();
services.AddTransient<CreateUserViewModel>();
}
}
}
14 changes: 14 additions & 0 deletions UndoAssessment/UndoAssessment/UndoAssessment.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<PackageReference Include="Xamarin.CommunityToolkit" Version="2.0.6" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2578" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.6" />
</ItemGroup>

<ItemGroup>
<None Remove="appsettings.json" />
<EmbeddedResource Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
Loading