WIP Chat
This commit is contained in:
parent
509ffafef8
commit
ca1e89a818
14 changed files with 399 additions and 171 deletions
|
|
@ -1,56 +1,83 @@
|
|||
using Microsoft.AspNet.SignalR.Client;
|
||||
using BookAStar.Data;
|
||||
using BookAStar.Model.Social.Messaging;
|
||||
using Microsoft.AspNet.SignalR.Client;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using Xamarin.Forms;
|
||||
using XLabs.Caching;
|
||||
using XLabs.Forms.Controls;
|
||||
using XLabs.Ioc;
|
||||
|
||||
namespace BookAStar.Pages
|
||||
{
|
||||
public partial class ChatPage : TabbedPage
|
||||
{
|
||||
public ObservableCollection<string> Messages { get; set; }
|
||||
public ObservableCollection<UserMessage> Messages { get; set; }
|
||||
public string ChatUser { get; set; }
|
||||
|
||||
private HubConnection chatHubConnection;
|
||||
private IHubProxy chatHubProxy;
|
||||
|
||||
public ChatPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
Title = "Chat";
|
||||
|
||||
sendButton.Clicked += async (sender, args) => {
|
||||
sendButton.Clicked += async (sender, args) =>
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
try
|
||||
{
|
||||
await chatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
|
||||
await App.CurrentApp.ChatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
|
||||
messageEntry.Text = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
|
||||
IsBusy = false;
|
||||
};
|
||||
messageList.ItemsSource = Messages = new ObservableCollection<string>();
|
||||
// contactPicker.DisplayProperty = "UserName";
|
||||
|
||||
messageList.ItemsSource = Messages = new ObservableCollection<UserMessage>();
|
||||
PVList.ItemsSource = DataManager.Current.PrivateMessages;
|
||||
App.CurrentApp.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged;
|
||||
// DataManager.Current.Contacts
|
||||
|
||||
MainSettings.UserChanged += MainSettings_UserChanged;
|
||||
MainSettings_UserChanged(this, null);
|
||||
|
||||
App.CurrentApp.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
|
||||
{
|
||||
Messages.Add(new UserMessage
|
||||
{
|
||||
Message = m,
|
||||
SenderId = n,
|
||||
Date = DateTime.Now
|
||||
});
|
||||
});
|
||||
|
||||
App.CurrentApp.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
|
||||
{
|
||||
Messages.Add(new UserMessage
|
||||
{
|
||||
Message = m,
|
||||
SenderId = n,
|
||||
Date = DateTime.Now
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected override async void OnAppearing()
|
||||
private void MainSettings_UserChanged(object sender, EventArgs e)
|
||||
{
|
||||
IsBusy = true;
|
||||
chatHubConnection = new HubConnection(Constants.SignalRHubsUrl);
|
||||
|
||||
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
|
||||
|
||||
chatHubProxy.On<string, string>("AddMessage", (n, m) => {
|
||||
Messages.Add(string.Format("{0} says: {1}", n, m));
|
||||
});
|
||||
await chatHubConnection.Start();
|
||||
IsBusy = false;
|
||||
sendButton.IsEnabled = true;
|
||||
ChatUser = MainSettings.UserName;
|
||||
contactPicker.ItemsSource = DataManager.Current.Contacts;
|
||||
}
|
||||
|
||||
private void ChatHubConnection_StateChanged(StateChange obj)
|
||||
{
|
||||
sendButton.IsEnabled = obj.NewState == ConnectionState.Connected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue