From 8245cf4098314bfc5d8ea2072c1e95862fdc706f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 3 Oct 2016 18:08:53 +0200 Subject: [PATCH] refactoring --- BookAStar/BookAStar/Helpers/LocaLEntity.cs | 34 ++++++++++++++ BookAStar/BookAStar/Helpers/RemoteEntity.cs | 50 +-------------------- 2 files changed, 36 insertions(+), 48 deletions(-) create mode 100644 BookAStar/BookAStar/Helpers/LocaLEntity.cs diff --git a/BookAStar/BookAStar/Helpers/LocaLEntity.cs b/BookAStar/BookAStar/Helpers/LocaLEntity.cs new file mode 100644 index 00000000..2f86ecdb --- /dev/null +++ b/BookAStar/BookAStar/Helpers/LocaLEntity.cs @@ -0,0 +1,34 @@ + +using System; +using System.Collections.ObjectModel; +using System.Linq; + +namespace BookAStar +{ + public class LocalEntity : ObservableCollection where K : IEquatable + { + public V CurrentItem { get; protected set; } + protected Func GetKey { get; set; } + public LocalEntity(Func getKey) : base() + { + if (getKey == null) throw new InvalidOperationException(); + GetKey = getKey; + } + public virtual void Merge(V item) + { + var key = GetKey(item); + if (this.Any(x => GetKey(x).Equals(key))) + { + Remove(LocalGet(key)); + } + Add(item); + CurrentItem = item; + } + public V LocalGet(K key) + { + CurrentItem = this.Single(x => GetKey(x).Equals(key)); + return CurrentItem; + } + } + +} \ No newline at end of file diff --git a/BookAStar/BookAStar/Helpers/RemoteEntity.cs b/BookAStar/BookAStar/Helpers/RemoteEntity.cs index 16c48352..0245dcc9 100644 --- a/BookAStar/BookAStar/Helpers/RemoteEntity.cs +++ b/BookAStar/BookAStar/Helpers/RemoteEntity.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Windows.Input; -using System.Linq; using System.Net.Http; using Newtonsoft.Json; using System.Threading.Tasks; @@ -11,42 +9,6 @@ using System.Net; namespace BookAStar { - public class NotIdentifiedException : Exception { - public NotIdentifiedException(string message) : base(message) { } - } - - public class ServiceNotAvailable : Exception - { - public ServiceNotAvailable(string message, Exception innerException) : base(message, innerException) { } - - } - - public class LocalEntity : ObservableCollection where K : IEquatable - { - public V CurrentItem { get; protected set; } - protected Func GetKey { get; set; } - public LocalEntity(Func getKey) : base() - { - if (getKey == null) throw new InvalidOperationException(); - GetKey = getKey; - } - public virtual void Merge(V item) - { - var key = GetKey(item); - if (this.Any(x => GetKey(x).Equals(key))) - { - Remove(LocalGet(key)); - } - Add(item); - CurrentItem = item; - } - public V LocalGet(K key) - { - CurrentItem = this.Single(x => GetKey(x).Equals(key)); - return CurrentItem; - } - } - public class RemoteEntity : LocalEntity, ICommand where K : IEquatable { private string _controller; @@ -75,8 +37,6 @@ namespace BookAStar if (CanExecuteChanged != null) CanExecuteChanged.Invoke(this, new EventArgs()); } - - /// /// Refresh the collection @@ -85,12 +45,8 @@ namespace BookAStar public async void Execute(object parameter) { BeforeExecute(); - using (HttpClient client = new HttpClient()) + using (HttpClient client = CreateClient()) { - - // Update credentials - client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( - "Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken); // Get the whole data try { @@ -116,14 +72,13 @@ namespace BookAStar AfterExecuting(); } - - private void AfterExecuting() { IsExecuting = false; if (CanExecuteChanged != null) CanExecuteChanged.Invoke(this, new EventArgs()); } + public async Task Get(K key) { var item = LocalGet(key); @@ -147,7 +102,6 @@ namespace BookAStar { var content = await response.Content.ReadAsStringAsync(); item = JsonConvert.DeserializeObject(content); - // LocalData.Clear(); Merge(item); } }