diff --git a/BookAStar/BookAStar/App.xaml.cs b/BookAStar/BookAStar/App.xaml.cs index e7c2fd10..e287ce0d 100644 --- a/BookAStar/BookAStar/App.xaml.cs +++ b/BookAStar/BookAStar/App.xaml.cs @@ -138,7 +138,7 @@ namespace BookAStar { ViewFactory.EnableCache = true; ViewFactory.Register( - r=> new ChatViewModel { UserName = MainSettings.UserName } + r=> new ChatViewModel { ChatUser = MainSettings.UserName } ); ViewFactory.Register( resolver => new DashboardViewModel()); @@ -304,8 +304,8 @@ namespace BookAStar // TODO log in debug binaries } - private IHubProxy chatHubProxy = null; - public IHubProxy ChatHubProxy + private static IHubProxy chatHubProxy = null; + public static IHubProxy ChatHubProxy { get { diff --git a/BookAStar/BookAStar/Model/ClientProviderInfo.cs b/BookAStar/BookAStar/Model/ClientProviderInfo.cs index 7badf372..1a90aa20 100644 --- a/BookAStar/BookAStar/Model/ClientProviderInfo.cs +++ b/BookAStar/BookAStar/Model/ClientProviderInfo.cs @@ -26,6 +26,9 @@ namespace BookAStar.Model return UserHelpers.Avatar(Avatar); } } + + public string ChatHubConnectionId { get; set; } + public override string ToString() { return UserName; diff --git a/BookAStar/BookAStar/Pages/ChatPage.xaml b/BookAStar/BookAStar/Pages/ChatPage.xaml index a04a76bf..fed06245 100644 --- a/BookAStar/BookAStar/Pages/ChatPage.xaml +++ b/BookAStar/BookAStar/Pages/ChatPage.xaml @@ -9,7 +9,6 @@ xmlns:local="clr-namespace:BookAStar;Assembly:BookAStar" xmlns:extensions="clr-namespace:BookAStar.Extensions;assembly=BookAStar" > - @@ -51,19 +50,15 @@ - + + - + @@ -83,7 +78,7 @@ - + @@ -113,11 +108,11 @@ - + - + diff --git a/BookAStar/BookAStar/Pages/ChatPage.xaml.cs b/BookAStar/BookAStar/Pages/ChatPage.xaml.cs index ca5ade7b..920fbe92 100644 --- a/BookAStar/BookAStar/Pages/ChatPage.xaml.cs +++ b/BookAStar/BookAStar/Pages/ChatPage.xaml.cs @@ -1,20 +1,16 @@ -using BookAStar.Data; -using BookAStar.Model.Social.Messaging; -using Microsoft.AspNet.SignalR.Client; -using System; -using System.Collections.ObjectModel; +using System; using System.Diagnostics; +using Microsoft.AspNet.SignalR.Client; using Xamarin.Forms; -using XLabs.Caching; -using XLabs.Forms.Controls; -using XLabs.Ioc; namespace BookAStar.Pages { + using Data; + using Model; + using System.Linq; + using ViewModels; public partial class ChatPage : TabbedPage { - public ObservableCollection Messages { get; set; } - public ObservableCollection Notifs { get; set; } public string ChatUser { get; set; } public ChatPage() @@ -22,6 +18,7 @@ namespace BookAStar.Pages InitializeComponent(); Title = "Chat"; + BindingContext = new ChatViewModel(); sendButton.Clicked += async (sender, args) => { @@ -31,7 +28,7 @@ namespace BookAStar.Pages { ConnectionState cs = App.ChatHubConnection.State; - await App.CurrentApp.ChatHubProxy.Invoke("Send", ChatUser, messageEntry.Text); + await App.ChatHubProxy.Invoke("Send", ChatUser, messageEntry.Text); messageEntry.Text = null; } catch (Exception ex) @@ -44,45 +41,24 @@ namespace BookAStar.Pages sendPVButton.Clicked += async (sender, args) => { + string userName = contactPicker.SelectedItem as string; + if (string.IsNullOrEmpty(userName)) return; + var user = DataManager.Current.Contacts.Single( + c => c.UserName == userName); + if (string.IsNullOrEmpty(user.ChatHubConnectionId)) return; IsBusy = true; - try { - await App.CurrentApp.ChatHubProxy.Invoke("SendPV", ChatUser, pvEntry.Text); + await App.ChatHubProxy.Invoke("SendPV", user.ChatHubConnectionId, 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 ChatMessage - { - Message = m, - SenderId = n, - Date = DateTime.Now - }); - }); - - App.CurrentApp.ChatHubProxy.On("notify", (n, m) => - { - Notifs.Add(new ChatMessage - { - Message = m, - SenderId = n, - Date = DateTime.Now - }); - }); + } private void ReconnectButton_Clicked(object sender, EventArgs e) @@ -91,12 +67,6 @@ namespace BookAStar.Pages 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) { diff --git a/BookAStar/BookAStar/Pages/EditEstimatePage.xaml b/BookAStar/BookAStar/Pages/EditEstimatePage.xaml index 3e4a4bc4..28b6604c 100644 --- a/BookAStar/BookAStar/Pages/EditEstimatePage.xaml +++ b/BookAStar/BookAStar/Pages/EditEstimatePage.xaml @@ -17,7 +17,6 @@ - diff --git a/BookAStar/BookAStar/ViewModels/ChatViewModel.cs b/BookAStar/BookAStar/ViewModels/ChatViewModel.cs index 950d4627..46fa210a 100644 --- a/BookAStar/BookAStar/ViewModels/ChatViewModel.cs +++ b/BookAStar/BookAStar/ViewModels/ChatViewModel.cs @@ -1,16 +1,103 @@ -using System; -using System.Collections.Generic; +using Microsoft.AspNet.SignalR.Client; +using System; +using System.Collections.ObjectModel; using System.Linq; -using System.Text; -using System.Threading.Tasks; using XLabs.Forms.Mvvm; namespace BookAStar.ViewModels { + using Data; + using Model; + using Model.Social.Messaging; + class ChatViewModel: ViewModel { - public string UserName { - get; set; + public ObservableCollection Messages { get; set; } + public ObservableCollection Notifs { get; set; } + public ObservableCollection PVs { get; set; } + public ObservableCollection Contacts { get; set; } + private string chatUser; + public string ChatUser + { + get + { + return chatUser; + } + set + { + SetProperty(ref chatUser, value); + } + } + private ConnectionState state; + public ConnectionState State + { + get { return state; } + } + + public ChatViewModel() + { + App.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged; + MainSettings.UserChanged += MainSettings_UserChanged; + Messages = new ObservableCollection(); + Notifs = new ObservableCollection(); + PVs = DataManager.Current.PrivateMessages; + Contacts = DataManager.Current.Contacts; + App.ChatHubProxy.On("addMessage", (n, m) => + { + Messages.Add(new ChatMessage + { + Message = m, + SenderId = n, + Date = DateTime.Now + }); + }); + + App.ChatHubProxy.On("notify", (eventId, cxId, userName) => + { + // TODO make admin possible + // by assigning a server side username to anonymous. + // From now, don't log anonymous + if (!string.IsNullOrEmpty(userName)) + { + Notifs.Add(new ChatMessage + { + Message = eventId, + SenderId = userName, + Date = DateTime.Now + }); + if (eventId == "connected") + OnUserConnected(cxId, userName); + else if (eventId == "disconnected") + OnUserDisconnected(userName); + } + }); + ChatUser = MainSettings.UserName; + } + + private void OnUserConnected(string cxId, string userName) + { + var user = Contacts.SingleOrDefault( + c => c.UserName == userName); + if (user != null) + user.ChatHubConnectionId = cxId; + } + + private void OnUserDisconnected (string userName) + { + var user = Contacts.SingleOrDefault( + c => c.UserName == userName); + if (user != null) + user.ChatHubConnectionId = null; + } + + private void MainSettings_UserChanged(object sender, EventArgs e) + { + ChatUser = MainSettings.UserName; + } + + private void ChatHubConnection_StateChanged(StateChange obj) + { + SetProperty(ref state, obj.NewState, "State"); } } }