From 4a168d16993b6c6e60fac0ef34c4619f786afcf8 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 18 Sep 2016 00:11:35 +0200 Subject: [PATCH] WIP Book queries --- .../Services/GcmListenerService.cs | 28 ++---- BookAStar/BookAStar/App.xaml.cs | 95 ++++++++++++------- BookAStar/BookAStar/Helpers/RemoteEntity.cs | 30 +++--- .../BookAStar/Pages/BookQueriesPage.xaml | 46 +++++++++ .../BookAStar/Pages/BookQueriesPage.xaml.cs | 46 +++++++++ 5 files changed, 179 insertions(+), 66 deletions(-) create mode 100644 BookAStar/BookAStar/Pages/BookQueriesPage.xaml create mode 100644 BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs diff --git a/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs b/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs index f48c44de..9dc7f15a 100644 --- a/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs +++ b/BookAStar/BookAStar.Droid/Services/GcmListenerService.cs @@ -43,26 +43,18 @@ namespace BookAStar.Droid.Services var topic = data.GetString("Topic"); if (topic == "BookQuery") { - DateTime startdate,enddate; + DateTime eventdate,enddate; - var sdatestr = data.GetString("StartDate"); - DateTime.TryParse(sdatestr, out startdate); - - var enddatestr = data.GetString("EndDate"); - DateTime.TryParse(enddatestr, out enddate); + var sdatestr = data.GetString("EventDate"); + DateTime.TryParse(sdatestr, out eventdate); var locationJson = data.GetString("Location"); var location = JsonConvert.DeserializeObject(locationJson); - var cid = long.Parse(data.GetString("CommandId")); + var cid = long.Parse(data.GetString("Id")); var bq = new BookQueryData { - Title = data.GetString("Title"), - Description = data.GetString("Description"), - Comment = data.GetString("Comment"), - StartDate = startdate, - EndDate = enddate, - CommandId = cid, - Address = location + Id = cid, + Location = location }; SendBookQueryNotification(bq); @@ -96,19 +88,19 @@ namespace BookAStar.Droid.Services var count = bookquerynotifications.Length; var multiple = count > 1; var title = - multiple ? $"{count} demandes" : bquery.Title; - var message = bquery.Description; + multiple ? $"{count} demandes" : bquery.Client.UserName; + var message = $"{bquery.EventDate} {bquery.Client.UserName} {bquery.Location.Address}"; var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); - intent.PutExtra("BookQueryId", bquery.CommandId); + intent.PutExtra("BookQueryId", bquery.Id); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); Notification.InboxStyle inboxStyle = new Notification.InboxStyle(); int maxil = 5; for (int cn = 0; cn < count && cn < maxil; cn++) { - inboxStyle.AddLine(bookquerynotifications[cn].Description); + inboxStyle.AddLine(bookquerynotifications[cn].Client.UserName); } if (count > maxil) inboxStyle.SetSummaryText($"Plus {count - maxil} autres"); diff --git a/BookAStar/BookAStar/App.xaml.cs b/BookAStar/BookAStar/App.xaml.cs index 8c6bcd19..15f18aad 100644 --- a/BookAStar/BookAStar/App.xaml.cs +++ b/BookAStar/BookAStar/App.xaml.cs @@ -1,4 +1,6 @@ -using BookAStar.Model.Auth.Account; +using BookAStar.Model; +using BookAStar.Model.Auth.Account; +using BookAStar.Pages; using System; using System.Diagnostics; using System.Net; @@ -25,25 +27,32 @@ namespace BookAStar SettingsPage settingsPage; PinPage pinPage; ContentPage deviceInfoPage; + BookQueryPage bookQueryPage; + BookQueriesPage bookQueriesPage; public static IPlatform PlateformSpecificInstance { get; set; } public static string AppName { get; set; } public static App CurrentApp { get { return Current as App; } } public DataManager DataManager { get; set; } - public App (IPlatform instance) - { + public App(IPlatform instance) + { DataManager = new DataManager(); deviceInfoPage = new DeviceInfoPage(instance.GetDeviceInfo()); + bookQueriesPage = new BookQueriesPage(); PlateformSpecificInstance = instance; - searchPage = new SearchPage { Title = "Trouvez votre artiste" - , Icon = "glyphish_07_map_marker.png" - }; - mp = new NavigationPage (searchPage); - settingsPage = new SettingsPage { Title = "Settings" - , Icon = "ic_corp_icon.png" - }; + searchPage = new SearchPage + { + Title = "Trouvez votre artiste", + Icon = "glyphish_07_map_marker.png" + }; + mp = new NavigationPage(searchPage); + settingsPage = new SettingsPage + { + Title = "Settings", + Icon = "ic_corp_icon.png" + }; var r = this.Resources; //var hasLabelStyle = r.ContainsKey("labelStyle"); @@ -52,39 +61,53 @@ namespace BookAStar // null var appsstyle = settingsPage.Style; // appsstyle.CanCascade = true; MainPage = mp; - ToolbarItem tiSetts = new ToolbarItem () { Text = "Settings" - , Icon = "ic_corp_icon.png" + ToolbarItem tiSetts = new ToolbarItem() + { + Text = "Settings", + Icon = "ic_corp_icon.png" }; - mp.ToolbarItems.Add (tiSetts); - tiSetts.Clicked += (object sender, EventArgs e) => { - if (settingsPage.Parent==null) - mp.Navigation.PushAsync(settingsPage); - else { + mp.ToolbarItems.Add(tiSetts); + tiSetts.Clicked += (object sender, EventArgs e) => + { + if (settingsPage.Parent == null) + mp.Navigation.PushAsync(settingsPage); + else + { settingsPage.Focus(); } - }; + }; ToolbarItem tiQueries = new ToolbarItem { Text = "Demandes" }; - tiQueries.Clicked += (object sender, EventArgs e) => { - mp.Navigation.PushAsync(new Pages.MakeAnEstimatePage()); + + + tiQueries.Clicked += (object sender, EventArgs e) => + { + if (bookQueriesPage.Parent == null) + mp.Navigation.PushAsync(bookQueriesPage); + else bookQueriesPage.Focus(); + }; + mp.ToolbarItems.Add(tiQueries); + ToolbarItem tiMap = new ToolbarItem + { + Text = "Carte", + Icon = "glyphish_07_map_marker.png" + }; + mp.ToolbarItems.Add(tiMap); + pinPage = new PinPage + { + Title = "Carte", + Icon = "glyphish_07_map_marker.png" }; - mp.ToolbarItems.Add(tiQueries); - ToolbarItem tiMap = new ToolbarItem { Text = "Carte", - Icon = "glyphish_07_map_marker.png" - }; - mp.ToolbarItems.Add (tiMap); - pinPage = new PinPage { Title = "Carte", - Icon = "glyphish_07_map_marker.png" - }; - tiMap.Clicked += (object sender, EventArgs e) => { + tiMap.Clicked += (object sender, EventArgs e) => + { if (pinPage.Parent == null) mp.Navigation.PushAsync(pinPage); else pinPage.Focus(); }; - + } public void ShowDeviceInfo() @@ -97,13 +120,19 @@ namespace BookAStar public void PostDeviceInfo() { var res = PlateformSpecificInstance.InvokeApi( - "gcm/register", + "gcm/register", PlateformSpecificInstance.GetDeviceInfo()); } - public void ShowBookQuery(long queryId) + public void ShowBookQuery(BookQueryData data) + { + bookQueriesPage.BindingContext = data; + mp.Navigation.PushAsync(bookQueriesPage); + } + + public void CloseWindow() { - mp.Navigation.PushAsync(new BookQueryPage(queryId)); + mp.Navigation.PopAsync(); } } } diff --git a/BookAStar/BookAStar/Helpers/RemoteEntity.cs b/BookAStar/BookAStar/Helpers/RemoteEntity.cs index 831b7af9..37bf8947 100644 --- a/BookAStar/BookAStar/Helpers/RemoteEntity.cs +++ b/BookAStar/BookAStar/Helpers/RemoteEntity.cs @@ -9,28 +9,25 @@ using System.Threading.Tasks; namespace BookAStar { - public class RemoteEntity : ICommand where K : IEquatable + public class RemoteEntity : ObservableCollection, ICommand where K : IEquatable { private string _controller; public event EventHandler CanExecuteChanged; public bool IsExecuting { get; private set; } - public ObservableCollection LocalData; - private Func _getKey ; private HttpClient client; private Uri controllerUri; - + protected Func GetKey { get; set; } public bool CanExecute(object parameter) { return !IsExecuting; } - public RemoteEntity(string controllerName, Func getKey) + public RemoteEntity(string controllerName, Func getKey):base() { if (string.IsNullOrWhiteSpace(controllerName) || getKey == null) throw new InvalidOperationException(); _controller = controllerName; - _getKey = getKey; - LocalData = new ObservableCollection(); + GetKey = getKey; client = new HttpClient(); controllerUri = new Uri(MainSettings.YavscApiUrl + "/" + _controller); } @@ -51,6 +48,9 @@ namespace BookAStar public async void Execute(object parameter) { BeforeExecute(); + // Update credentials + client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( + "Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken); // Get the whole data var response = await client.GetAsync(controllerUri); @@ -61,25 +61,25 @@ namespace BookAStar // LocalData.Clear(); foreach (var item in col) { - Update(item); + UpdateOrAdd(item); } } AfterExecuting(); } - private void Update (V item) + protected virtual void UpdateOrAdd (V item) { - var key = _getKey(item); - if (LocalData.Any(x => _getKey(x).Equals(key))) + var key = GetKey(item); + if (this.Any(x => GetKey(x).Equals(key))) { - LocalData.Remove(LocalGet(key)); + Remove(LocalGet(key)); } - LocalData.Add(item); + Add(item); } public V LocalGet(K key) { - return LocalData.Single(x => _getKey(x).Equals(key)); + return this.Single(x => GetKey(x).Equals(key)); } private void AfterExecuting() @@ -102,7 +102,7 @@ namespace BookAStar var content = await response.Content.ReadAsStringAsync(); item = JsonConvert.DeserializeObject(content); // LocalData.Clear(); - Update(item); + UpdateOrAdd(item); } AfterExecuting(); diff --git a/BookAStar/BookAStar/Pages/BookQueriesPage.xaml b/BookAStar/BookAStar/Pages/BookQueriesPage.xaml new file mode 100644 index 00000000..d2f46536 --- /dev/null +++ b/BookAStar/BookAStar/Pages/BookQueriesPage.xaml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs b/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs new file mode 100644 index 00000000..c278d4e5 --- /dev/null +++ b/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs @@ -0,0 +1,46 @@ +using BookAStar.Model; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; +using Xamarin.Forms; + +namespace BookAStar.Pages +{ + public partial class BookQueriesPage : ContentPage + { + public BookQueriesPage() + { + InitializeComponent(); + BindingContext = App.CurrentApp.DataManager.BookQueries; + list.ItemsSource = BindingContext as ObservableCollection; + list.RefreshCommand = BindingContext as ICommand; + list.IsPullToRefreshEnabled = true; + } + + public RemoteEntity Queries + { + get + { + return BindingContext!=null? BindingContext as RemoteEntity:null; + } + } + + protected override void OnAppearing() + { + base.OnAppearing(); + if (!Queries.IsExecuting) + Queries.Execute(null); + } + + private void OnViewDetail(object sender, ItemTappedEventArgs e) + { + BookQueryData data = e.Item as BookQueryData; + App.CurrentApp.ShowBookQuery(data); + throw new NotImplementedException(); + } + } +}