diff --git a/BookAStar/BookAStar/App.xaml.cs b/BookAStar/BookAStar/App.xaml.cs index f6df66c3..beabc53f 100644 --- a/BookAStar/BookAStar/App.xaml.cs +++ b/BookAStar/BookAStar/App.xaml.cs @@ -273,7 +273,7 @@ namespace BookAStar chatHubConnection.Error += ChatHubConnection_Error; chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub"); - chatHubProxy.On("PV", (n, m) => { + chatHubProxy.On("addPV", (n, m) => { DataManager.Current.PrivateMessages.Add( new ChatMessage { @@ -283,17 +283,21 @@ namespace BookAStar } ); }); + MainSettings_UserChanged(this, null); } private void MainSettings_UserChanged(object sender, EventArgs e) { - if (MainSettings.CurrentUser != null) + if (MainSettings.CurrentUser == null) { - if (chatHubConnection.Headers.ContainsKey("Bearer")) - chatHubConnection.Headers.Remove("Bearer"); - chatHubConnection.Headers.Add("Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken); + chatHubConnection.Headers.Clear(); + } + else + { + var token = MainSettings.CurrentUser.YavscTokens.AccessToken; + chatHubConnection.Headers.Add( + "Authorization", $"Bearer {token}"); } - else chatHubConnection.Headers.Remove("Bearer"); } private void ChatHubConnection_Error(Exception obj) diff --git a/BookAStar/BookAStar/BookAStar.csproj b/BookAStar/BookAStar/BookAStar.csproj index 9a7cf243..8dba105c 100644 --- a/BookAStar/BookAStar/BookAStar.csproj +++ b/BookAStar/BookAStar/BookAStar.csproj @@ -48,6 +48,7 @@ + @@ -376,6 +377,12 @@ Designer + + + + + + diff --git a/BookAStar/BookAStar/Converters/BooleanToObjectConverter.cs b/BookAStar/BookAStar/Converters/BooleanToObjectConverter.cs index 97c4df0e..8cd28a88 100644 --- a/BookAStar/BookAStar/Converters/BooleanToObjectConverter.cs +++ b/BookAStar/BookAStar/Converters/BooleanToObjectConverter.cs @@ -8,7 +8,7 @@ using Xamarin.Forms; namespace BookAStar.Converters { - class BooleanToObjectConverter : IValueConverter + public class BooleanToObjectConverter : IValueConverter { public T FalseObject { set; get; } diff --git a/BookAStar/BookAStar/Converters/SignalRConnectionStateToObject.cs b/BookAStar/BookAStar/Converters/SignalRConnectionStateToObject.cs new file mode 100644 index 00000000..21ba1efa --- /dev/null +++ b/BookAStar/BookAStar/Converters/SignalRConnectionStateToObject.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNet.SignalR.Client; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xamarin.Forms; + +namespace BookAStar.Converters +{ + public class SignalRConnectionStateToObject : IValueConverter + { + public T ConnectedObject { set; get; } + + public T ConnectingObject { set; get; } + + public T DisconnectedObject { set; get; } + + public T ReconnectingObject { set; get; } + + public object Convert(object value, Type targetType, + object parameter, CultureInfo culture) + { + switch ((ConnectionState)value) { + case ConnectionState.Connected: return ConnectedObject; + case ConnectionState.Connecting: return ConnectingObject; + case ConnectionState.Disconnected: return DisconnectedObject; + case ConnectionState.Reconnecting: return ReconnectingObject; + } + Debug.Assert(false); + return null; + } + + public object ConvertBack(object value, Type targetType, + object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/BookAStar/BookAStar/Images/Chat/connected.png b/BookAStar/BookAStar/Images/Chat/connected.png new file mode 100644 index 00000000..1b9883e6 Binary files /dev/null and b/BookAStar/BookAStar/Images/Chat/connected.png differ diff --git a/BookAStar/BookAStar/Images/Chat/connecting.png b/BookAStar/BookAStar/Images/Chat/connecting.png new file mode 100644 index 00000000..99e7fe8c Binary files /dev/null and b/BookAStar/BookAStar/Images/Chat/connecting.png differ diff --git a/BookAStar/BookAStar/Images/Chat/disconnected.png b/BookAStar/BookAStar/Images/Chat/disconnected.png new file mode 100644 index 00000000..74f24a53 Binary files /dev/null and b/BookAStar/BookAStar/Images/Chat/disconnected.png differ diff --git a/BookAStar/BookAStar/Images/Chat/reconnecting.png b/BookAStar/BookAStar/Images/Chat/reconnecting.png new file mode 100644 index 00000000..a30b22e9 Binary files /dev/null and b/BookAStar/BookAStar/Images/Chat/reconnecting.png differ diff --git a/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs b/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs index b0b45cb6..4b08b0f9 100644 --- a/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs +++ b/BookAStar/BookAStar/Pages/BookQueriesPage.xaml.cs @@ -6,13 +6,20 @@ using XLabs.Platform.Services; namespace BookAStar.Pages { - + using Data; public partial class BookQueriesPage : ContentPage { public BookQueriesPage() { InitializeComponent(); - BindingContext = new BookQueriesViewModel(); + var model = new BookQueriesViewModel(); + model.RefreshQueries = + new Command( () => { + DataManager.Current.BookQueries.Execute(null); + this.list.EndRefresh(); + }); + + BindingContext = model; } private void OnViewDetail(object sender, ItemTappedEventArgs e) @@ -20,18 +27,5 @@ namespace BookAStar.Pages BookQueryData data = e.Item as BookQueryData; App.NavigationService.NavigateTo(true,data); } - protected override void OnSizeAllocated(double width, double height) - { - /* TODO find a responsive layout - if (width > height) - { - mainLayout.Orientation = StackOrientation.Horizontal; - } - else - { - mainLayout.Orientation = StackOrientation.Vertical; - } */ - base.OnSizeAllocated(width, height); - } } } diff --git a/BookAStar/BookAStar/Pages/ChatPage.xaml b/BookAStar/BookAStar/Pages/ChatPage.xaml index adbb5844..0b3c6353 100644 --- a/BookAStar/BookAStar/Pages/ChatPage.xaml +++ b/BookAStar/BookAStar/Pages/ChatPage.xaml @@ -4,30 +4,74 @@ x:Class="BookAStar.Pages.ChatPage" xmlns:views="clr-namespace:BookAStar.Views;assembly=BookAStar" xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms" - xmlns:toolkit="clr-namespace:XLabs.Forms.Mvvm;assembly=XLabs.Forms" + xmlns:toolkit="clr-namespace:XLabs.Forms.Mvvm;assembly=XLabs.Forms" + xmlns:converters="clr-namespace:BookAStar.Converters;assembly=BookAStar" + xmlns:local="clr-namespace:BookAStar;Assembly:BookAStar" > + + + + - + - @@ -36,61 +80,62 @@ - - - + + + - - + - + - + - - - + --> + + - + - - - - - + + + + \ No newline at end of file diff --git a/BookAStar/BookAStar/Pages/ChatPage.xaml.cs b/BookAStar/BookAStar/Pages/ChatPage.xaml.cs index 9ffc54d2..cb78f52f 100644 --- a/BookAStar/BookAStar/Pages/ChatPage.xaml.cs +++ b/BookAStar/BookAStar/Pages/ChatPage.xaml.cs @@ -29,6 +29,8 @@ namespace BookAStar.Pages try { + ConnectionState cs = App.CurrentApp.ChatHubConnection.State; + await App.CurrentApp.ChatHubProxy.Invoke("Send", ChatUser, messageEntry.Text); messageEntry.Text = null; } @@ -39,13 +41,25 @@ namespace BookAStar.Pages IsBusy = false; }; - // contactPicker.DisplayProperty = "UserName"; - + + sendPVButton.Clicked += async (sender, args) => + { + IsBusy = true; + + try + { + await App.CurrentApp.ChatHubProxy.Invoke("SendPV", ChatUser, messageEntry.Text); + messageEntry.Text = null; + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + + IsBusy = false; + }; messageList.ItemsSource = Messages = new ObservableCollection(); - PVList.ItemsSource = DataManager.Current.PrivateMessages; App.CurrentApp.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged; - // DataManager.Current.Contacts - MainSettings.UserChanged += MainSettings_UserChanged; MainSettings_UserChanged(this, null); @@ -70,15 +84,26 @@ namespace BookAStar.Pages }); } + private void ReconnectButton_Clicked(object sender, EventArgs e) + { + App.CurrentApp.ChatHubConnection.Stop(); + App.CurrentApp.ChatHubConnection.Start(); + } + private void MainSettings_UserChanged(object sender, EventArgs e) { ChatUser = MainSettings.UserName; contactPicker.ItemsSource = DataManager.Current.Contacts; + PVList.ItemsSource = DataManager.Current.PrivateMessages; } private void ChatHubConnection_StateChanged(StateChange obj) { - sendButton.IsEnabled = obj.NewState == ConnectionState.Connected; + Xamarin.Forms.Device.BeginInvokeOnMainThread( + () => { + sendButton.IsEnabled = obj.NewState == ConnectionState.Connected; + reconnectButton.IsEnabled = obj.NewState != ConnectionState.Connected; + }); } } } diff --git a/BookAStar/BookAStar/ViewModels/BookQueriesViewModel.cs b/BookAStar/BookAStar/ViewModels/BookQueriesViewModel.cs index a80cf127..76becbeb 100644 --- a/BookAStar/BookAStar/ViewModels/BookQueriesViewModel.cs +++ b/BookAStar/BookAStar/ViewModels/BookQueriesViewModel.cs @@ -22,7 +22,7 @@ namespace BookAStar.ViewModels public ICommand RefreshQueries { - get { return DataManager.Current.BookQueries; } + get; set; } } }