yavsc/BookAStar/BookAStar/Pages/ChatPage.xaml.cs

73 lines
2.1 KiB
C#
Raw Normal View History

2016-10-30 02:47:58 +02:00
using System;
2016-10-10 12:58:14 +02:00
using System.Diagnostics;
2016-10-30 02:47:58 +02:00
using Microsoft.AspNet.SignalR.Client;
2016-10-10 12:58:14 +02:00
using Xamarin.Forms;
namespace BookAStar.Pages
{
2016-10-30 02:47:58 +02:00
using Data;
using System.Linq;
2016-11-14 17:55:24 +01:00
using ViewModels.Messaging;
2016-10-25 23:20:26 +02:00
public partial class ChatPage : TabbedPage
2016-10-10 12:58:14 +02:00
{
public string ChatUser { get; set; }
public ChatPage()
{
InitializeComponent();
Title = "Chat";
2016-10-30 02:47:58 +02:00
BindingContext = new ChatViewModel();
2016-10-10 12:58:14 +02:00
2016-10-26 20:29:46 +02:00
sendButton.Clicked += async (sender, args) =>
{
2016-10-10 12:58:14 +02:00
IsBusy = true;
try
{
ConnectionState cs = App.ChatHubConnection.State;
2016-10-30 02:47:58 +02:00
await App.ChatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
2016-10-26 20:29:46 +02:00
messageEntry.Text = null;
2016-10-10 12:58:14 +02:00
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
IsBusy = false;
};
sendPVButton.Clicked += async (sender, args) =>
{
2016-10-30 02:47:58 +02:00
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
2016-11-06 17:48:08 +01:00
{
2016-10-30 02:47:58 +02:00
await App.ChatHubProxy.Invoke<string>("SendPV", user.ChatHubConnectionId, pvEntry.Text);
pvEntry.Text = null;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
IsBusy = false;
};
2016-10-30 02:47:58 +02:00
2016-10-10 12:58:14 +02:00
}
2016-11-06 17:48:08 +01:00
2016-10-26 20:29:46 +02:00
private void ChatHubConnection_StateChanged(StateChange obj)
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(
() => {
sendButton.IsEnabled = obj.NewState == ConnectionState.Connected;
});
2016-10-10 12:58:14 +02:00
}
}
}