From f92591c2ff7f86183ce4740d4fe8bb4b493b8eb7 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 15 Nov 2016 12:06:42 +0100 Subject: [PATCH] Vue sur les fichiers distants --- BookAStar/BookAStar/App.xaml.cs | 23 +++- BookAStar/BookAStar/BookAStar.csproj | 1 - BookAStar/BookAStar/Data/DataManager.cs | 5 +- BookAStar/BookAStar/Data/LocaLEntity.cs | 18 ++- .../BookAStar/Data/NonCrUD/RemoteFiles.cs | 44 ++++++- BookAStar/BookAStar/Data/RemoteEntity.cs | 2 +- .../BookAStar/Helpers/ObservableString.cs | 59 ---------- .../BookAStar/Interfaces/ILocalEntity.cs | 6 +- .../Pages/Estimate/BookQueryPage.xaml.cs | 4 +- .../Pages/Estimate/EditEstimatePage.xaml.cs | 12 +- .../Pages/UserProfile/DashboardPage.xaml.cs | 5 +- .../Pages/UserProfile/UserFiles.xaml | 108 +++++++++++++----- .../Pages/UserProfile/UserFiles.xaml.cs | 42 +++++-- BookAStar/BookAStar/Settings/MainSettings.cs | 30 ++++- .../UserProfile/DirectoryInfoViewModel.cs | 71 +++++++++--- 15 files changed, 282 insertions(+), 148 deletions(-) delete mode 100644 BookAStar/BookAStar/Helpers/ObservableString.cs diff --git a/BookAStar/BookAStar/App.xaml.cs b/BookAStar/BookAStar/App.xaml.cs index a339ede9..436e0766 100644 --- a/BookAStar/BookAStar/App.xaml.cs +++ b/BookAStar/BookAStar/App.xaml.cs @@ -25,6 +25,7 @@ namespace BookAStar using ViewModels.UserProfile; using Pages.UserProfile; using ViewModels.EstimateAndBilling; + using System.Net; public partial class App : Application // superclass new in 1.3 { @@ -109,7 +110,7 @@ namespace BookAStar BindingContext = page.BindingContext }); } - DataManager.Current.AppState.SaveCollection(); + DataManager.Current.AppState.SaveEntity(); } // called on app startup, after OnStartup, not on rotation @@ -124,7 +125,7 @@ namespace BookAStar pageType, true, pageState.BindingContext); } DataManager.Current.AppState.Clear(); - DataManager.Current.AppState.SaveCollection(); + DataManager.Current.AppState.SaveEntity(); } // FIXME Not called? @@ -206,6 +207,7 @@ namespace BookAStar masterDetail.Detail = new NavigationPage(home); ToolbarItem tiSetts = new ToolbarItem() { + Priority = 0, Text = "Paramètres", Icon = "ic_corp_icon.png", Command = new Command( @@ -215,6 +217,7 @@ namespace BookAStar ToolbarItem tiHome = new ToolbarItem() { + Priority = -1, Text = "Accueil", Icon = "icon.png", Command = new Command( @@ -224,6 +227,7 @@ namespace BookAStar ToolbarItem tiPubChat= new ToolbarItem() { + Priority = 1, Text = "Chat", Icon = "chat_icon_s.png", Command = new Command( @@ -233,6 +237,7 @@ namespace BookAStar masterDetail.ToolbarItems.Add(tiHome); masterDetail.ToolbarItems.Add(tiSetts); masterDetail.ToolbarItems.Add(tiPubChat); + this.MainPage = masterDetail; NavigationService = new NavigationService(masterDetail.Detail.Navigation); } @@ -290,7 +295,19 @@ namespace BookAStar // Start the Hub connection public async void StartHubConnection () { - await chatHubConnection.Start(); + try + { + await chatHubConnection.Start(); + } + catch (WebException webex ) + { + // TODO use webex, set this cx down status somewhere, + // & display it & maybe try again later. + } + catch (Exception ex) + { + // TODO use ex + } } public void SetupHubConnection() diff --git a/BookAStar/BookAStar/BookAStar.csproj b/BookAStar/BookAStar/BookAStar.csproj index b4071ac0..d51a6088 100644 --- a/BookAStar/BookAStar/BookAStar.csproj +++ b/BookAStar/BookAStar/BookAStar.csproj @@ -78,7 +78,6 @@ - diff --git a/BookAStar/BookAStar/Data/DataManager.cs b/BookAStar/BookAStar/Data/DataManager.cs index bd3b3c98..84e08776 100644 --- a/BookAStar/BookAStar/Data/DataManager.cs +++ b/BookAStar/BookAStar/Data/DataManager.cs @@ -7,6 +7,7 @@ using Model.Social.Messaging; using Model.FileSystem; using ViewModels.EstimateAndBilling; + using NonCrUD; public class DataManager { @@ -14,7 +15,7 @@ public RemoteEntityRO BookQueries { get; set; } public RemoteEntity Estimates { get; set; } public RemoteEntity Blogspot { get; set; } - internal RemoteEntity RemoteFiles { get; set; } + internal RemoteFilesEntity RemoteFiles { get; set; } public LocalEntity Contacts { get; set; } internal LocalEntity AppState { get; set; } @@ -48,7 +49,7 @@ EstimationCache = new LocalEntity(e => e.Query.Id); EstimateLinesTemplates = new LocalEntity(l => l.Description); PrivateMessages = new LocalEntity(m=> m.GetHashCode()); - RemoteFiles = new RemoteEntity ("fs", d => d.SubPath); + RemoteFiles = new RemoteFilesEntity (); PrivateMessages.Load(); BookQueries.Load(); diff --git a/BookAStar/BookAStar/Data/LocaLEntity.cs b/BookAStar/BookAStar/Data/LocaLEntity.cs index 56d800ce..9b6013f6 100644 --- a/BookAStar/BookAStar/Data/LocaLEntity.cs +++ b/BookAStar/BookAStar/Data/LocaLEntity.cs @@ -40,14 +40,24 @@ namespace BookAStar.Data return CurrentItem; } - public void Load() + public virtual bool Load() { - this.Populate(); + return this.Populate(); } - public void Load(string subKey) + public virtual bool Load(string subKey) { - this.Populate(subKey); + return (this.Populate(subKey)); + } + public virtual bool Seek(int index) + { + + if (this.Count>index && index >=0) + { + CurrentItem = this[index]; + return true; + } + return false; } } diff --git a/BookAStar/BookAStar/Data/NonCrUD/RemoteFiles.cs b/BookAStar/BookAStar/Data/NonCrUD/RemoteFiles.cs index 01878198..aa7e6eaa 100644 --- a/BookAStar/BookAStar/Data/NonCrUD/RemoteFiles.cs +++ b/BookAStar/BookAStar/Data/NonCrUD/RemoteFiles.cs @@ -6,10 +6,17 @@ namespace BookAStar.Data.NonCrUD { using Helpers; using Model.FileSystem; - - public class RemoteFiles : RemoteEntity + using System.Linq; + /* + public class DirectoryEntryChangingEvent : EventArgs { - public RemoteFiles() : base("fs", d => d) + public UserDirectoryInfo OldItem { get; set; } + public UserDirectoryInfo NewItem { get; set; } + }*/ + + public class RemoteFilesEntity : RemoteEntity + { + public RemoteFilesEntity() : base("fs", d => d) { } @@ -23,14 +30,15 @@ namespace BookAStar.Data.NonCrUD try { var subpath = parameter as string; - string path = ControllerUri.AbsolutePath + ((subpath != null) ? "/" + subpath : null); - using (var response = await client.GetAsync(ControllerUri)) + string path = ControllerUri.AbsoluteUri + ((subpath != null) ? "/" + subpath : null); + using (var response = await client.GetAsync(path)) { if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); var di = JsonConvert.DeserializeObject(content); this.Merge(di); + this.SaveEntity(); } else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) throw new Exception("Bad request"); @@ -43,8 +51,32 @@ namespace BookAStar.Data.NonCrUD throw new Exception("Remote call failed", ex); } } + AfterExecuting(); } - + /* + public override void Merge(UserDirectoryInfo item) + { + var key = GetKey(item); + DirectoryEntryChangingEvent itemChanged = null; + if (this.Any(x => GetKey(x).Equals(key))) + { + var old = LocalGet(key); + itemChanged = new DirectoryEntryChangingEvent + { + OldItem = old, + NewItem = item + }; + Remove(old); + } + Add(item); + CurrentItem = item; + if (DirectoryEntryChanged != null && itemChanged != null) + DirectoryEntryChanged.Invoke(this, itemChanged); + } + + public event EventHandler DirectoryEntryChanged; + */ + } } diff --git a/BookAStar/BookAStar/Data/RemoteEntity.cs b/BookAStar/BookAStar/Data/RemoteEntity.cs index 96bbb9df..3a4b8b62 100644 --- a/BookAStar/BookAStar/Data/RemoteEntity.cs +++ b/BookAStar/BookAStar/Data/RemoteEntity.cs @@ -65,7 +65,7 @@ namespace BookAStar.Data { Merge(item); } - this.SaveCollection(); + this.SaveEntity(); } } } diff --git a/BookAStar/BookAStar/Helpers/ObservableString.cs b/BookAStar/BookAStar/Helpers/ObservableString.cs deleted file mode 100644 index 2049d950..00000000 --- a/BookAStar/BookAStar/Helpers/ObservableString.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace BookAStar -{ - public class ObservableString : IObservable, IDisposable - { - public ObservableString(string data) - { - this.data = data; - } - private string data = null; - private List> observers = new List>(); - - public string Data - { - get - { - return data; - } - - set - { - if (data != value) - { - data = value; - foreach (var obs in observers) - obs.OnCompleted(); - } - } - } - - public override string ToString() - { - return data; - } - - public static explicit operator ObservableString (string data) - { - return new ObservableString(data); - } - - public static explicit operator string (ObservableString observable) - { - return observable.ToString(); - } - - public void Dispose() - { - observers = null; - } - - public IDisposable Subscribe(IObserver observer) - { - observers.Add(observer); - return this; - } - } -} \ No newline at end of file diff --git a/BookAStar/BookAStar/Interfaces/ILocalEntity.cs b/BookAStar/BookAStar/Interfaces/ILocalEntity.cs index 8e1f1945..44ea2f79 100644 --- a/BookAStar/BookAStar/Interfaces/ILocalEntity.cs +++ b/BookAStar/BookAStar/Interfaces/ILocalEntity.cs @@ -1,10 +1,11 @@ using System; +using System.Collections; namespace BookAStar { public interface ILoadable { - void Load(); + bool Load(); } public interface IPersistentOnDemand : ILoadable @@ -12,7 +13,7 @@ namespace BookAStar void Save(); } - public interface ILocalEntity : ILoadable where K : IEquatable + public interface ILocalEntity : IList, ILoadable where K : IEquatable { V CurrentItem { get; } @@ -22,5 +23,6 @@ namespace BookAStar void Merge(V item); + bool Seek(int index); } } \ No newline at end of file diff --git a/BookAStar/BookAStar/Pages/Estimate/BookQueryPage.xaml.cs b/BookAStar/BookAStar/Pages/Estimate/BookQueryPage.xaml.cs index a09a7e63..0f73d1ed 100644 --- a/BookAStar/BookAStar/Pages/Estimate/BookQueryPage.xaml.cs +++ b/BookAStar/BookAStar/Pages/Estimate/BookQueryPage.xaml.cs @@ -58,7 +58,7 @@ namespace BookAStar.Pages private void OnEditEstimate(object sender, EventArgs ev) { - var bookQueryViewModel = (BookQueryViewModel)BindingContext; + var bookQueryViewModel = (BookQueryViewModel) BindingContext; var editEstimateViewModel = bookQueryViewModel.DraftEstimate; if (editEstimateViewModel == null) @@ -70,7 +70,7 @@ namespace BookAStar.Pages if (estimateToEdit == null) { DataManager.Current.Contacts.Merge(BookQuery.Client); - DataManager.Current.Contacts.SaveCollection(); + DataManager.Current.Contacts.SaveEntity(); estimateToEdit = new Estimate() { ClientId = BookQuery.Client.UserId, diff --git a/BookAStar/BookAStar/Pages/Estimate/EditEstimatePage.xaml.cs b/BookAStar/BookAStar/Pages/Estimate/EditEstimatePage.xaml.cs index a280e617..8c1e2392 100644 --- a/BookAStar/BookAStar/Pages/Estimate/EditEstimatePage.xaml.cs +++ b/BookAStar/BookAStar/Pages/Estimate/EditEstimatePage.xaml.cs @@ -24,7 +24,7 @@ namespace BookAStar.Pages private void EditEstimatePage_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { - DataManager.Current.EstimationCache.SaveCollection(); + DataManager.Current.EstimationCache.SaveEntity(); } protected override void OnSizeAllocated(double width, double height) @@ -47,7 +47,7 @@ namespace BookAStar.Pages var lineView = new BillingLineViewModel(com) { ValidateCommand = new Command(() => { bill.Add(com); - bill.SaveCollection(); + DataManager.Current.EstimationCache.SaveEntity(); })}; App.NavigationService.NavigateTo( true, @@ -60,7 +60,7 @@ namespace BookAStar.Pages var lineView = new BillingLineViewModel(line) { ValidateCommand = new Command(() => { - bill.SaveCollection(); + DataManager.Current.EstimationCache.SaveEntity(); }) }; lineView.PropertyChanged += LineView_PropertyChanged; @@ -71,7 +71,7 @@ namespace BookAStar.Pages private void LineView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { - DataManager.Current.EstimationCache.SaveCollection(); + DataManager.Current.EstimationCache.SaveEntity(); } protected void OnEstimateValidated(object sender, EventArgs e) @@ -88,9 +88,9 @@ namespace BookAStar.Pages { DataManager.Current.Estimates.Update(evm.Data); } - DataManager.Current.Estimates.SaveCollection(); + DataManager.Current.Estimates.SaveEntity(); DataManager.Current.EstimationCache.Remove(evm); - DataManager.Current.EstimationCache.SaveCollection(); + DataManager.Current.EstimationCache.SaveEntity(); Navigation.PopAsync(); } } diff --git a/BookAStar/BookAStar/Pages/UserProfile/DashboardPage.xaml.cs b/BookAStar/BookAStar/Pages/UserProfile/DashboardPage.xaml.cs index f8232cab..07e37cf7 100644 --- a/BookAStar/BookAStar/Pages/UserProfile/DashboardPage.xaml.cs +++ b/BookAStar/BookAStar/Pages/UserProfile/DashboardPage.xaml.cs @@ -6,6 +6,7 @@ namespace BookAStar.Pages.UserProfile { using Data; using ViewModels.UserProfile; + public partial class DashboardPage : ContentPage { @@ -23,10 +24,12 @@ namespace BookAStar.Pages.UserProfile ShowPage(null, true); }); } + public void OnManageFiles(object sender, EventArgs e) { - ShowPage(new object[] { new DirectoryInfoViewModel() }, true); + ShowPage(null, true); } + public void OnViewPerformerStatus(object sender, EventArgs e) { ShowPage(null, true); diff --git a/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml b/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml index 84ab893c..a9c407fb 100644 --- a/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml +++ b/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml @@ -1,35 +1,81 @@  - \ No newline at end of file diff --git a/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml.cs b/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml.cs index 429f02d3..34de11ad 100644 --- a/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml.cs +++ b/BookAStar/BookAStar/Pages/UserProfile/UserFiles.xaml.cs @@ -4,31 +4,49 @@ namespace BookAStar.Pages.UserProfile { using ViewModels.UserProfile; using Data; + using System.Windows.Input; + public partial class UserFiles : ContentPage { protected DirectoryInfoViewModel model; - public UserFiles() { InitializeComponent(); - - var currentDir = DataManager.Current.RemoteFiles.CurrentItem; - - BindingContext = new DirectoryInfoViewModel(currentDir) + var current = DataManager.Current.RemoteFiles.CurrentItem; + if (current != null) + BindingContext = new DirectoryInfoViewModel(current); + else BindingContext = new DirectoryInfoViewModel { - RefreshCommand = new Command(() => - { - DataManager.Current.RemoteFiles.Execute(null); - - this.dirlist.EndRefresh(); - this.filelist.EndRefresh(); - }) + UserName = MainSettings.UserName }; } + public UserFiles(DirectoryInfoViewModel model) + { + InitializeComponent(); + BindingContext = model; + } + protected override void OnBindingContextChanged() { + model = BindingContext as DirectoryInfoViewModel; + if (model != null) + model.RefreshCommand = new Command(() => + { + DataManager.Current.RemoteFiles.Execute(null); + var item = DataManager.Current.RemoteFiles.CurrentItem; + if (item != null) + model.InnerModel = item; + // this.dirlist.EndRefresh(); + // this.filelist.EndRefresh(); + }); base.OnBindingContextChanged(); } + protected override void OnAppearing() + { + base.OnAppearing(); + model.RefreshCommand.Execute(model.SubPath); + } + } } diff --git a/BookAStar/BookAStar/Settings/MainSettings.cs b/BookAStar/BookAStar/Settings/MainSettings.cs index 0e573ac7..e85017d3 100644 --- a/BookAStar/BookAStar/Settings/MainSettings.cs +++ b/BookAStar/BookAStar/Settings/MainSettings.cs @@ -265,9 +265,9 @@ namespace BookAStar /// /// /// - public static void Populate(this IList collection, string subKey = null) + public static bool Populate(this ILocalEntity collection, string subKey = null) where K : IEquatable { - var key = $"{EntityDataSettingsPrefix}/{subKey}/{typeof(V).FullName}"; + var key = GetCollectionKey(subKey); var data = AppSettings.GetValueOrDefault(key, null); if (!string.IsNullOrWhiteSpace(data)) { @@ -275,7 +275,15 @@ namespace BookAStar if (items != null) foreach (var item in items) collection.Add(item); + var cursorKey = GetCursorKey(subKey); + var index = AppSettings.GetValueOrDefault(cursorKey, -1); + if (index>=0) + { + collection.Seek(index); + } + return true; } + return false; } /// @@ -284,11 +292,25 @@ namespace BookAStar /// /// /// - public static void SaveCollection(this IList collection, string subKey=null) + public static void SaveEntity(this ILocalEntity collection, string subKey=null) where K : IEquatable { if (collection == null) return; - var key = $"{EntityDataSettingsPrefix}/{subKey}/{typeof(V).FullName}"; + var key = GetCollectionKey(subKey); + var cursorKey = GetCursorKey(subKey); AppSettings.AddOrUpdateValue(key, JsonConvert.SerializeObject(collection)); + if (collection.CurrentItem!=null) + { + int index = collection.IndexOf(collection.CurrentItem) ; + AppSettings.AddOrUpdateValue(cursorKey, index); + } + } + private static string GetCursorKey(string subKey) + { + return $"{EntityDataSettingsPrefix}/{subKey}/{typeof(V).FullName}/c"; + } + private static string GetCollectionKey(string subKey) + { + return $"{EntityDataSettingsPrefix}/{subKey}/{typeof(V).FullName}"; } } } diff --git a/BookAStar/BookAStar/ViewModels/UserProfile/DirectoryInfoViewModel.cs b/BookAStar/BookAStar/ViewModels/UserProfile/DirectoryInfoViewModel.cs index 595f8a9c..d8ba11c2 100644 --- a/BookAStar/BookAStar/ViewModels/UserProfile/DirectoryInfoViewModel.cs +++ b/BookAStar/BookAStar/ViewModels/UserProfile/DirectoryInfoViewModel.cs @@ -5,28 +5,71 @@ using XLabs.Forms.Mvvm; namespace BookAStar.ViewModels.UserProfile { + using System.ComponentModel; using Model.FileSystem; public class DirectoryInfoViewModel : ViewModel { - public ObservableString SubPath { get; set; } - public ObservableCollection SubDirectories { get; set; } - public ObservableCollection FileInfo { get; set; } - public ICommand RefreshCommand { get; set; } - public DirectoryInfoViewModel (UserDirectoryInfo model=null) + private string subPath; + public string SubPath { - Data.DataManager.Current.RemoteFiles.CollectionChanged += RemoteFiles_CollectionChanged; - - if (model == null) - model = Data.DataManager.Current.RemoteFiles.CurrentItem; - SubDirectories = new ObservableCollection (model.SubDirectories); - SubPath = new ObservableString (model.SubPath); - FileInfo = new ObservableCollection (model.Files); + get { return subPath; } + set { SetProperty(ref subPath, value); } } - private void RemoteFiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + private string userName; + public string UserName { - throw new System.NotImplementedException(); + get { return userName; } + set { SetProperty(ref userName, value); } + } + + private ObservableCollection subDirectories; + public ObservableCollection SubDirectories + { + get { return subDirectories; } + set { SetProperty< ObservableCollection < string >>( ref subDirectories, value) ; } + } + + private ObservableCollection fileInfo; + public ObservableCollection FileInfo + { + get { return fileInfo; } + set { SetProperty> ( ref fileInfo, value ); } + } + + UserDirectoryInfo model; + public UserDirectoryInfo InnerModel { + get { return model; } + set { + if (SetProperty(ref model, value)) + if (model == null) + { + SubDirectories = new ObservableCollection(); + FileInfo = new ObservableCollection(); + SubPath = ""; + UserName = ""; + } + else + { + SubDirectories = new ObservableCollection(model.SubDirectories); + FileInfo = new ObservableCollection(model.Files); + SubPath = model.SubPath; + UserName = model.UserName; + } + } + } + + public DirectoryInfoViewModel(UserDirectoryInfo model = null) + { + this.InnerModel = model; + } + + private ICommand refreshCommand; + public ICommand RefreshCommand + { + get { return refreshCommand; } + set { SetProperty(ref refreshCommand, value); } } } }