diff --git a/BookAStar/BookAStar/App.xaml.cs b/BookAStar/BookAStar/App.xaml.cs index e3e7dec3..e7c2fd10 100644 --- a/BookAStar/BookAStar/App.xaml.cs +++ b/BookAStar/BookAStar/App.xaml.cs @@ -29,8 +29,7 @@ namespace BookAStar public static string AppName { get; set; } - // Exists in order to dispose of a static instance strongly typed, - // It Makes smaller code. + [Obsolete("Instead using this, use new static properties.")] public static App CurrentApp { get { return Current as App; } } public static bool MasterPresented @@ -253,8 +252,8 @@ namespace BookAStar } } } - private HubConnection chatHubConnection = null; - public HubConnection ChatHubConnection + private static HubConnection chatHubConnection = null; + public static HubConnection ChatHubConnection { get { @@ -273,9 +272,9 @@ 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 UserMessage + new ChatMessage { Message = m, SenderId = n, @@ -283,17 +282,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..8e1b2254 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/Data/DataManager.cs b/BookAStar/BookAStar/Data/DataManager.cs index a6c752ff..82e676b7 100644 --- a/BookAStar/BookAStar/Data/DataManager.cs +++ b/BookAStar/BookAStar/Data/DataManager.cs @@ -20,7 +20,7 @@ /// internal LocalEntity EstimationCache { get; set; } internal LocalEntity EstimateLinesTemplates { get; set; } - internal LocalEntity PrivateMessages { get; set; } + internal LocalEntity PrivateMessages { get; set; } protected static DataManager current ; public static DataManager Current @@ -42,7 +42,7 @@ AppState = new LocalEntity(s => s.Position); EstimationCache = new LocalEntity(e => e.Query.Id); EstimateLinesTemplates = new LocalEntity(l => l.Description); - PrivateMessages = new LocalEntity(m=> m.GetHashCode()); + PrivateMessages = new LocalEntity(m=> m.GetHashCode()); PrivateMessages.Load(); BookQueries.Load(); 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/Model/Social/Messaging/PrivateMessage.cs b/BookAStar/BookAStar/Model/Social/Messaging/PrivateMessage.cs index 8133a1f4..8dae22c6 100644 --- a/BookAStar/BookAStar/Model/Social/Messaging/PrivateMessage.cs +++ b/BookAStar/BookAStar/Model/Social/Messaging/PrivateMessage.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace BookAStar.Model.Social.Messaging { - public class UserMessage + public class ChatMessage { public DateTime Date { get; set; } public string SenderId { get; set; } 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..a04a76bf 100644 --- a/BookAStar/BookAStar/Pages/ChatPage.xaml +++ b/BookAStar/BookAStar/Pages/ChatPage.xaml @@ -1,33 +1,78 @@  + + + + + + + + + + + + + + + + + + + + + + + + + - + - @@ -36,61 +81,62 @@ - - - + + + - - + - + - + - - - + --> + + - + - - - - - + + + + \ No newline at end of file diff --git a/BookAStar/BookAStar/Pages/ChatPage.xaml.cs b/BookAStar/BookAStar/Pages/ChatPage.xaml.cs index 214a14f9..ca5ade7b 100644 --- a/BookAStar/BookAStar/Pages/ChatPage.xaml.cs +++ b/BookAStar/BookAStar/Pages/ChatPage.xaml.cs @@ -13,7 +13,8 @@ namespace BookAStar.Pages { public partial class ChatPage : TabbedPage { - public ObservableCollection Messages { get; set; } + public ObservableCollection Messages { get; set; } + public ObservableCollection Notifs { get; set; } public string ChatUser { get; set; } public ChatPage() @@ -28,6 +29,8 @@ namespace BookAStar.Pages try { + ConnectionState cs = App.ChatHubConnection.State; + await App.CurrentApp.ChatHubProxy.Invoke("Send", ChatUser, messageEntry.Text); messageEntry.Text = null; } @@ -38,19 +41,32 @@ namespace BookAStar.Pages IsBusy = false; }; - // contactPicker.DisplayProperty = "UserName"; - - messageList.ItemsSource = Messages = new ObservableCollection(); - PVList.ItemsSource = DataManager.Current.PrivateMessages; - App.CurrentApp.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged; - // DataManager.Current.Contacts + sendPVButton.Clicked += async (sender, args) => + { + IsBusy = true; + + try + { + await App.CurrentApp.ChatHubProxy.Invoke("SendPV", ChatUser, pvEntry.Text); + pvEntry.Text = null; + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + + IsBusy = false; + }; + messageList.ItemsSource = Messages = new ObservableCollection(); + notifList.ItemsSource = Notifs = new ObservableCollection(); + App.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged; MainSettings.UserChanged += MainSettings_UserChanged; MainSettings_UserChanged(this, null); App.CurrentApp.ChatHubProxy.On("addMessage", (n, m) => { - Messages.Add(new UserMessage + Messages.Add(new ChatMessage { Message = m, SenderId = n, @@ -58,9 +74,9 @@ namespace BookAStar.Pages }); }); - App.CurrentApp.ChatHubProxy.On("addMessage", (n, m) => + App.CurrentApp.ChatHubProxy.On("notify", (n, m) => { - Messages.Add(new UserMessage + Notifs.Add(new ChatMessage { Message = m, SenderId = n, @@ -69,15 +85,25 @@ namespace BookAStar.Pages }); } + private void ReconnectButton_Clicked(object sender, EventArgs e) + { + App.ChatHubConnection.Stop(); + App.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; + }); } } } 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; } } }