Skip to content

Commit 7438d9b

Browse files
author
Javier Suárez Ruiz
committed
Completed Settings View
Init Rest API integration
1 parent 158fd0b commit 7438d9b

52 files changed

Lines changed: 1037 additions & 344 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Controls/CustomSwitch.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Input;
7+
using Xamarin.Forms;
8+
9+
namespace eShopOnContainers.Core.Controls
10+
{
11+
public class ToggleButton : ContentView
12+
{
13+
public static readonly BindableProperty CommandProperty =
14+
BindableProperty.Create("Command", typeof(ICommand), typeof(ToggleButton), null);
15+
16+
public static readonly BindableProperty CommandParameterProperty =
17+
BindableProperty.Create("CommandParameter", typeof(object), typeof(ToggleButton), null);
18+
19+
public static readonly BindableProperty CheckedProperty =
20+
BindableProperty.Create("Checked", typeof(bool), typeof(ToggleButton), false, BindingMode.TwoWay,
21+
null, propertyChanged: OnCheckedChanged);
22+
23+
public static readonly BindableProperty AnimateProperty =
24+
BindableProperty.Create("Animate", typeof(bool), typeof(ToggleButton), false);
25+
26+
public static readonly BindableProperty CheckedImageProperty =
27+
BindableProperty.Create("CheckedImage", typeof(ImageSource), typeof(ToggleButton), null);
28+
29+
public static readonly BindableProperty UnCheckedImageProperty =
30+
BindableProperty.Create("UnCheckedImage", typeof(ImageSource), typeof(ToggleButton), null);
31+
32+
private ICommand _toggleCommand;
33+
private Image _toggleImage;
34+
35+
public ToggleButton()
36+
{
37+
Initialize();
38+
}
39+
40+
public ICommand Command
41+
{
42+
get { return (ICommand)GetValue(CommandProperty); }
43+
set { SetValue(CommandProperty, value); }
44+
}
45+
46+
public object CommandParameter
47+
{
48+
get { return GetValue(CommandParameterProperty); }
49+
set { SetValue(CommandParameterProperty, value); }
50+
}
51+
52+
public bool Checked
53+
{
54+
get { return (bool)GetValue(CheckedProperty); }
55+
set { SetValue(CheckedProperty, value); }
56+
}
57+
58+
public bool Animate
59+
{
60+
get { return (bool)GetValue(AnimateProperty); }
61+
set { SetValue(CheckedProperty, value); }
62+
}
63+
64+
public ImageSource CheckedImage
65+
{
66+
get { return (ImageSource)GetValue(CheckedImageProperty); }
67+
set { SetValue(CheckedImageProperty, value); }
68+
}
69+
70+
public ImageSource UnCheckedImage
71+
{
72+
get { return (ImageSource)GetValue(UnCheckedImageProperty); }
73+
set { SetValue(UnCheckedImageProperty, value); }
74+
}
75+
76+
public ICommand ToogleCommand
77+
{
78+
get
79+
{
80+
return _toggleCommand
81+
?? (_toggleCommand = new Command(() =>
82+
{
83+
if (Checked)
84+
{
85+
Checked = false;
86+
}
87+
else
88+
{
89+
Checked = true;
90+
}
91+
92+
if (Command != null)
93+
{
94+
Command.Execute(CommandParameter);
95+
}
96+
}));
97+
}
98+
}
99+
100+
private void Initialize()
101+
{
102+
_toggleImage = new Image();
103+
104+
Animate = true;
105+
106+
GestureRecognizers.Add(new TapGestureRecognizer
107+
{
108+
Command = ToogleCommand
109+
});
110+
111+
_toggleImage.Source = UnCheckedImage;
112+
Content = _toggleImage;
113+
}
114+
115+
protected override void OnParentSet()
116+
{
117+
base.OnParentSet();
118+
_toggleImage.Source = UnCheckedImage;
119+
Content = _toggleImage;
120+
}
121+
122+
private static async void OnCheckedChanged(BindableObject bindable, object oldValue, object newValue)
123+
{
124+
var toggleButton = (ToggleButton)bindable;
125+
126+
if (Equals(newValue, null) && !Equals(oldValue, null))
127+
return;
128+
129+
if (toggleButton.Checked)
130+
{
131+
toggleButton._toggleImage.Source = toggleButton.CheckedImage;
132+
}
133+
else
134+
{
135+
toggleButton._toggleImage.Source = toggleButton.UnCheckedImage;
136+
}
137+
138+
toggleButton.Content = toggleButton._toggleImage;
139+
140+
if (toggleButton.Animate)
141+
{
142+
await toggleButton.ScaleTo(0.9, 50, Easing.Linear);
143+
await Task.Delay(100);
144+
await toggleButton.ScaleTo(1, 50, Easing.Linear);
145+
}
146+
}
147+
}
148+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace eShopOnContainers.Core.Exceptions
4+
{
5+
public class ServiceAuthenticationException : Exception
6+
{
7+
public string Content { get; }
8+
9+
public ServiceAuthenticationException()
10+
{
11+
}
12+
13+
public ServiceAuthenticationException(string content)
14+
{
15+
Content = content;
16+
}
17+
}
18+
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/GlobalSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
public static class GlobalSetting
44
{
55
public const string RegisterWebsite = "http://104.40.62.65/Account/Register";
6+
7+
public const string CatalogEndpoint = "http://104.40.62.65:5101/api/v1/catalog";
68
}
79
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Threading.Tasks;
4+
using eShopOnContainers.Core.Models.Catalog;
5+
using eShopOnContainers.Core.Services.RequestProvider;
6+
using System.Collections.Generic;
7+
using eShopOnContainers.Core.Extensions;
8+
9+
namespace eShopOnContainers.Core.Services.Catalog
10+
{
11+
public class CatalogService : ICatalogService
12+
{
13+
private readonly IRequestProvider _requestProvider;
14+
15+
public CatalogService(IRequestProvider requestProvider)
16+
{
17+
_requestProvider = requestProvider;
18+
}
19+
20+
public Task<ObservableCollection<CatalogItem>> FilterAsync(string catalogBrand, string catalogType)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
public async Task<ObservableCollection<CatalogItem>> GetCatalogAsync()
26+
{
27+
UriBuilder builder = new UriBuilder(GlobalSetting.CatalogEndpoint);
28+
29+
builder.Path = "/items";
30+
31+
string uri = builder.ToString();
32+
33+
IEnumerable<CatalogItem> catalogItems =
34+
await _requestProvider.GetAsync<IEnumerable<CatalogItem>>(uri);
35+
36+
return catalogItems.ToObservableCollection();
37+
}
38+
39+
public Task<ObservableCollection<CatalogBrand>> GetCatalogBrandAsync()
40+
{
41+
throw new NotImplementedException();
42+
}
43+
44+
public Task<CatalogItem> GetCatalogItemAsync(string id)
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
public Task<ObservableCollection<CatalogType>> GetCatalogTypeAsync()
50+
{
51+
throw new NotImplementedException();
52+
}
53+
}
54+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
3+
namespace eShopOnContainers.Core.Services.RequestProvider
4+
{
5+
public interface IRequestProvider
6+
{
7+
Task<TResult> GetAsync<TResult>(string uri);
8+
9+
Task<TResult> PostAsync<TResult>(string uri, TResult data);
10+
11+
Task<TResult> PostAsync<TRequest, TResult>(string uri, TRequest data);
12+
13+
Task<TResult> PutAsync<TResult>(string uri, TResult data);
14+
15+
Task<TResult> PutAsync<TRequest, TResult>(string uri, TRequest data);
16+
}
17+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using eShopOnContainers.Core.Exceptions;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Converters;
4+
using Newtonsoft.Json.Serialization;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace eShopOnContainers.Core.Services.RequestProvider
12+
{
13+
public class RequestProvider : IRequestProvider
14+
{
15+
private readonly JsonSerializerSettings _serializerSettings;
16+
17+
public RequestProvider()
18+
{
19+
_serializerSettings = new JsonSerializerSettings
20+
{
21+
ContractResolver = new CamelCasePropertyNamesContractResolver(),
22+
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
23+
NullValueHandling = NullValueHandling.Ignore
24+
};
25+
26+
_serializerSettings.Converters.Add(new StringEnumConverter());
27+
}
28+
29+
public async Task<TResult> GetAsync<TResult>(string uri)
30+
{
31+
HttpClient httpClient = CreateHttpClient();
32+
HttpResponseMessage response = await httpClient.GetAsync(uri);
33+
34+
await HandleResponse(response);
35+
36+
string serialized = await response.Content.ReadAsStringAsync();
37+
TResult result = await Task.Run(() => JsonConvert.DeserializeObject<TResult>(serialized, _serializerSettings));
38+
39+
return result;
40+
}
41+
42+
public Task<TResult> PostAsync<TResult>(string uri, TResult data)
43+
{
44+
return PostAsync<TResult, TResult>(uri, data);
45+
}
46+
47+
public async Task<TResult> PostAsync<TRequest, TResult>(string uri, TRequest data)
48+
{
49+
HttpClient httpClient = CreateHttpClient();
50+
string serialized = await Task.Run(() => JsonConvert.SerializeObject(data, _serializerSettings));
51+
HttpResponseMessage response = await httpClient.PostAsync(uri, new StringContent(serialized, Encoding.UTF8, "application/json"));
52+
53+
await HandleResponse(response);
54+
55+
string responseData = await response.Content.ReadAsStringAsync();
56+
57+
return await Task.Run(() => JsonConvert.DeserializeObject<TResult>(responseData, _serializerSettings));
58+
}
59+
60+
public Task<TResult> PutAsync<TResult>(string uri, TResult data)
61+
{
62+
return PutAsync<TResult, TResult>(uri, data);
63+
}
64+
65+
public async Task<TResult> PutAsync<TRequest, TResult>(string uri, TRequest data)
66+
{
67+
HttpClient httpClient = CreateHttpClient();
68+
string serialized = await Task.Run(() => JsonConvert.SerializeObject(data, _serializerSettings));
69+
HttpResponseMessage response = await httpClient.PutAsync(uri, new StringContent(serialized, Encoding.UTF8, "application/json"));
70+
71+
await HandleResponse(response);
72+
73+
string responseData = await response.Content.ReadAsStringAsync();
74+
75+
return await Task.Run(() => JsonConvert.DeserializeObject<TResult>(responseData, _serializerSettings));
76+
}
77+
78+
private HttpClient CreateHttpClient()
79+
{
80+
var httpClient = new HttpClient();
81+
82+
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
83+
84+
return httpClient;
85+
}
86+
87+
private async Task HandleResponse(HttpResponseMessage response)
88+
{
89+
if (!response.IsSuccessStatusCode)
90+
{
91+
var content = await response.Content.ReadAsStringAsync();
92+
93+
if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
94+
{
95+
throw new ServiceAuthenticationException(content);
96+
}
97+
98+
throw new HttpRequestException(content);
99+
}
100+
}
101+
}
102+
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/Base/ViewModelLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected ViewModelLocator()
2424
{
2525
_unityContainer = new UnityContainer();
2626

27-
// services
27+
// Services
2828
_unityContainer.RegisterType<IDialogService, DialogService>();
2929
RegisterSingleton<INavigationService, NavigationService>();
3030
_unityContainer.RegisterType<IOpenUrlService, OpenUrlService>();
@@ -33,7 +33,7 @@ protected ViewModelLocator()
3333
_unityContainer.RegisterType<IOrdersService, OrdersMockService>();
3434
_unityContainer.RegisterType<IUserService, UserMockService>();
3535

36-
// view models
36+
// View models
3737
_unityContainer.RegisterType<CartViewModel>();
3838
_unityContainer.RegisterType<CatalogViewModel>();
3939
_unityContainer.RegisterType<CheckoutViewModel>();

0 commit comments

Comments
 (0)