Merge branch 'vnext' of github.com:pazof/yavsc into vnext
This commit is contained in:
@ -138,7 +138,7 @@ namespace BookAStar
|
||||
{
|
||||
ViewFactory.EnableCache = true;
|
||||
ViewFactory.Register<ChatPage, ChatViewModel>(
|
||||
r=> new ChatViewModel { UserName = MainSettings.UserName }
|
||||
r=> new ChatViewModel { ChatUser = MainSettings.UserName }
|
||||
);
|
||||
ViewFactory.Register<DashboardPage, DashboardViewModel>(
|
||||
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
|
||||
{
|
||||
|
@ -26,6 +26,9 @@ namespace BookAStar.Model
|
||||
return UserHelpers.Avatar(Avatar);
|
||||
}
|
||||
}
|
||||
|
||||
public string ChatHubConnectionId { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return UserName;
|
||||
|
@ -9,7 +9,6 @@
|
||||
xmlns:local="clr-namespace:BookAStar;Assembly:BookAStar"
|
||||
xmlns:extensions="clr-namespace:BookAStar.Extensions;assembly=BookAStar"
|
||||
>
|
||||
|
||||
|
||||
<TabbedPage.Resources>
|
||||
<ResourceDictionary>
|
||||
@ -51,19 +50,15 @@
|
||||
<StackLayout Spacing = "12"
|
||||
Orientation = "Horizontal"
|
||||
Padding="5, 0, 5, 0">
|
||||
<Image
|
||||
x:Name="reconnectImage"
|
||||
Style="{Binding Source={x:Static local:App.ChatHubConnection},
|
||||
Path=State,
|
||||
Converter={StaticResource cxToStyleImage}}" />
|
||||
<Image Style="{Binding Path=State, Converter={StaticResource cxToStyleImage}}" />
|
||||
|
||||
<Entry x:Name="messageEntry" Placeholder = "enter your Message"
|
||||
VerticalOptions = "Center"
|
||||
HorizontalOptions = "FillAndExpand"></Entry>
|
||||
<Button x:Name="sendButton" Text = "Send" HorizontalOptions = "End"
|
||||
VerticalOptions = "Center"></Button>
|
||||
</StackLayout>
|
||||
<ListView x:Name="messageList" HasUnevenRows="true"
|
||||
>
|
||||
<ListView x:Name="messageList" HasUnevenRows="true" ItemsSource="{Binding Messages}">
|
||||
<ListView.ItemTemplate HeightRequest="80" VerticalOptions="StartAndExpand">
|
||||
<DataTemplate>
|
||||
<ViewCell>
|
||||
@ -83,7 +78,7 @@
|
||||
</ContentPage>
|
||||
<ContentPage Title="Notifications" Icon="exclam.png" >
|
||||
<StackLayout Padding="5, 5, 5, 5">
|
||||
<ListView x:Name="notifList" HasUnevenRows="true">
|
||||
<ListView x:Name="notifList" HasUnevenRows="true" ItemsSource="{Binding Notifs}">
|
||||
<ListView.ItemTemplate HeightRequest="80" VerticalOptions="StartAndExpand">
|
||||
<DataTemplate>
|
||||
<ViewCell>
|
||||
@ -113,11 +108,11 @@
|
||||
<!--
|
||||
<controls:ExtendedPicker x:Name="contactPicker" Display="{Binding UserName}"></controls:ExtendedPicker>
|
||||
-->
|
||||
<controls:ExtendedPicker x:Name="contactPicker" />
|
||||
<controls:ExtendedPicker x:Name="contactPicker" ItemsSource="{Binding Contacts}" />
|
||||
<Button x:Name="sendPVButton" Text = "Send" HorizontalOptions = "End"
|
||||
VerticalOptions = "Center"></Button>
|
||||
</StackLayout>
|
||||
<ListView x:Name="PVList" HasUnevenRows="true">
|
||||
<ListView x:Name="PVList" HasUnevenRows="true" ItemsSource="{Binding PVs}">
|
||||
<ListView.ItemTemplate HeightRequest="80"
|
||||
VerticalOptions="StartAndExpand">
|
||||
<DataTemplate>
|
||||
|
@ -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<ChatMessage> Messages { get; set; }
|
||||
public ObservableCollection<ChatMessage> 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<string>("Send", ChatUser, messageEntry.Text);
|
||||
await App.ChatHubProxy.Invoke<string>("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<string>("SendPV", ChatUser, pvEntry.Text);
|
||||
await App.ChatHubProxy.Invoke<string>("SendPV", user.ChatHubConnectionId, pvEntry.Text);
|
||||
pvEntry.Text = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
|
||||
IsBusy = false;
|
||||
};
|
||||
messageList.ItemsSource = Messages = new ObservableCollection<ChatMessage>();
|
||||
notifList.ItemsSource = Notifs = new ObservableCollection<ChatMessage>();
|
||||
App.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged;
|
||||
MainSettings.UserChanged += MainSettings_UserChanged;
|
||||
MainSettings_UserChanged(this, null);
|
||||
|
||||
App.CurrentApp.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
|
||||
{
|
||||
Messages.Add(new ChatMessage
|
||||
{
|
||||
Message = m,
|
||||
SenderId = n,
|
||||
Date = DateTime.Now
|
||||
});
|
||||
});
|
||||
|
||||
App.CurrentApp.ChatHubProxy.On<string, string>("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)
|
||||
{
|
||||
|
@ -17,7 +17,6 @@
|
||||
</ContentPage.Resources>
|
||||
<ScrollView>
|
||||
<StackLayout Padding="10,10,10,10" x:Name="mainLayout">
|
||||
|
||||
<Grid MinimumHeightRequest="12">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*" />
|
||||
|
@ -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<ChatMessage> Messages { get; set; }
|
||||
public ObservableCollection<ChatMessage> Notifs { get; set; }
|
||||
public ObservableCollection<ChatMessage> PVs { get; set; }
|
||||
public ObservableCollection<ClientProviderInfo> Contacts { get; set; }
|
||||
private string chatUser;
|
||||
public string ChatUser
|
||||
{
|
||||
get
|
||||
{
|
||||
return chatUser;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetProperty<string>(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<ChatMessage>();
|
||||
Notifs = new ObservableCollection<ChatMessage>();
|
||||
PVs = DataManager.Current.PrivateMessages;
|
||||
Contacts = DataManager.Current.Contacts;
|
||||
App.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
|
||||
{
|
||||
Messages.Add(new ChatMessage
|
||||
{
|
||||
Message = m,
|
||||
SenderId = n,
|
||||
Date = DateTime.Now
|
||||
});
|
||||
});
|
||||
|
||||
App.ChatHubProxy.On<string, string, string>("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<ConnectionState>(ref state, obj.NewState, "State");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user