WIP private chat

This commit is contained in:
Paul Schneider 2016-12-15 14:19:53 +01:00
commit 34323d61ee
13 changed files with 286 additions and 157 deletions

View file

@ -0,0 +1,34 @@
using BookAStar.Data;
using BookAStar.Model.Social.Chat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace BookAStar.ViewModels.Messaging
{
public class ChatUserCollection : RemoteEntityRO<ChatUserInfo, string>
{
public ChatUserCollection() : base ("chat/users", u=>u.UserId)
{
}
public override void Merge(ChatUserInfo item)
{
var key = GetKey(item);
var existent = this.FirstOrDefault(u => u.UserId == key);
if (existent != null) {
existent.UserName = item.UserName;
existent.Roles = item.Roles;
existent.Avatar = item.Avatar;
existent.Connections = item.Connections;
}
else Add(item);
}
}
}

View file

@ -0,0 +1,159 @@

using BookAStar.Helpers;
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;
using YavscLib;
using System;
using Newtonsoft.Json;
namespace BookAStar.Model.Social.Chat
{
public class ChatUserInfo : ViewModel, IChatUserInfo
{
public string avatar;
public string Avatar
{
get
{
return avatar;
}
set
{
var newSource = UserHelpers.Avatar(value);
SetProperty<string>(ref avatar, value);
SetProperty<ImageSource>(ref avatarSource, newSource, "AvatarSource");
}
}
ImageSource avatarSource;
[JsonIgnore]
public ImageSource AvatarSource
{
get
{
return avatarSource;
}
}
public Connection [] Connections
{
get
{
return ObservableConnections?.ToArray();
}
set
{
ObservableConnections = new ObservableCollection<Connection>(value);
}
}
ObservableCollection<Connection> connections;
[JsonIgnore]
public ObservableCollection<Connection> ObservableConnections
{
get
{
return connections;
}
set
{
SetProperty<ObservableCollection<Connection>>(ref connections, value);
}
}
string[] roles;
public string[] Roles
{
get
{
return roles;
}
set
{
SetProperty<string[]>(ref roles, value);
NotifyPropertyChanged("RolesAsAString");
}
}
[JsonIgnore]
public string RolesAsAString
{
get
{
return string.Join(", ", Roles);
}
}
string userId;
public string UserId
{
get
{
return userId;
}
set
{
SetProperty<string>(ref userId, value);
}
}
string userName;
public string UserName
{
get
{
return userName;
}
set
{
SetProperty<string>(ref userName, value);
}
}
public bool IsConnected { get
{
return Connections.Length > 0;
} }
[JsonIgnore]
IConnection[] IChatUserInfo.Connections
{
get
{
return Connections;
}
set
{
throw new NotImplementedException();
}
}
public void OnConnected(string cxId)
{
// We do assume this cxId dosn't already exist in this list.
var cx = new Connection { ConnectionId = cxId, Connected = true };
if (ObservableConnections == null)
ObservableConnections = new ObservableCollection<Connection>();
ObservableConnections.Add(cx);
if (this.ObservableConnections.Count == 1)
NotifyPropertyChanged("IsConnected");
}
public void OnDisconnected(string cxId)
{
var existentcx = Connections.FirstOrDefault(cx => cx.ConnectionId == cxId);
if (existentcx != null)
{
this.ObservableConnections.Remove(existentcx);
if (this.ObservableConnections.Count == 0)
NotifyPropertyChanged("IsConnected");
}
}
}
}

View file

@ -7,6 +7,7 @@ using XLabs.Forms.Mvvm;
namespace BookAStar.ViewModels.Messaging
{
using Data;
using Model.Social.Chat;
using Model.Social.Messaging;
class ChatViewModel: ViewModel
@ -14,7 +15,7 @@ namespace BookAStar.ViewModels.Messaging
public ObservableCollection<ChatMessage> Messages { get; set; }
public ObservableCollection<ChatMessage> Notifs { get; set; }
public ObservableCollection<ChatMessage> PVs { get; set; }
public ObservableCollection<UserViewModel> Contacts { get; set; }
public ChatUserCollection ChatUsers { get; set; }
private ConnectionState state;
public ConnectionState State
@ -29,9 +30,7 @@ namespace BookAStar.ViewModels.Messaging
Messages = new ObservableCollection<ChatMessage>();
Notifs = new ObservableCollection<ChatMessage>();
PVs = DataManager.Instance.PrivateMessages;
Contacts =
new ObservableCollection<UserViewModel>(
DataManager.Instance.Contacts.Select(c=>new UserViewModel { Data = c }));
ChatUsers = DataManager.Instance.ChatUsers;
App.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
{
Messages.Add(new ChatMessage
@ -60,24 +59,33 @@ namespace BookAStar.ViewModels.Messaging
if (eventId == "connected")
OnUserConnected(cxId, userName);
else if (eventId == "disconnected")
OnUserDisconnected(userName);
OnUserDisconnected(cxId, userName);
});
}
private void OnUserConnected(string cxId, string userName)
{
var user = Contacts.SingleOrDefault(
c => c.Data.UserName == userName);
if (user != null)
user.ConnexionId = cxId;
var user = ChatUsers.SingleOrDefault(
c => c.UserName == userName);
if (user == null)
{
user = new ChatUserInfo {
UserName = userName
};
ChatUsers.Add(user);
}
user.OnConnected(cxId);
}
private void OnUserDisconnected (string userName)
private void OnUserDisconnected (string cxId, string userName)
{
var user = Contacts.SingleOrDefault(
c => c.Data.UserName == userName);
if (user != null)
user.ConnexionId = null;
var user = ChatUsers.SingleOrDefault(
c => c.UserName == userName);
if (user == null)
{
return;
}
user.OnDisconnected(cxId);
}
private void MainSettings_UserChanged(object sender, EventArgs e)

View file

@ -1,75 +0,0 @@
using BookAStar.Helpers;
using BookAStar.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;
namespace BookAStar.ViewModels.Messaging
{
class UserViewModel : ViewModel
{
private ClientProviderInfo data;
public ClientProviderInfo Data {
get
{
return data;
}
set
{
SetProperty(ref data, value);
NotifyPropertyChanged("Avatar");
}
}
private string connexionId;
public string ConnexionId
{
get
{
return connexionId;
}
set
{
SetProperty(ref connexionId, value);
NotifyPropertyChanged("Connected");
}
}
public bool Connected
{
get { return connexionId != null; }
}
[JsonIgnore]
public ImageSource Avatar
{
get
{
return UserHelpers.Avatar(Data.Avatar);
}
}
[JsonIgnore]
public ImageSource SmallAvatar
{
get
{
return UserHelpers.SmallAvatar(Data.Avatar, Data.UserName);
}
}
[JsonIgnore]
public ImageSource ExtraSmallAvatar
{
get
{
return UserHelpers.ExtraSmallAvatar(Data.Avatar, Data.UserName);
}
}
}
}