public RoomViewModel(JabbrConnection jabbr, Room room) : base() { Connection = jabbr; Room = room; TypedMessage = string.Empty; Messages = new ObservableCollection<MessageViewModel> (); subTokMessageReceived = Messenger.SubscribeOnMainThread<JabbrMessageReceivedMessage> (msg => { if (!msg.RoomName.Equals(Room.Name, StringComparison.OrdinalIgnoreCase)) return; lock (Messages) Messages.Add(new MessageViewModel(msg.Message)); }); subTokUserJoin = Messenger.Subscribe<JabbrUserJoinedMessage> (msg => { if (!msg.RoomName.Equals(Room.Name, StringComparison.OrdinalIgnoreCase)) return; var uvm = new UserViewModel(msg.User); lock(Users) { if (!Users.Contains(uvm)) _users.Add(uvm); } RaisePropertyChanged(() => Users); }); subTokUserLeft = Messenger.Subscribe<JabbrUserLeftMessage> (msg => { if (!msg.RoomName.Equals(Room.Name, StringComparison.OrdinalIgnoreCase)) return; var uvm = new UserViewModel(msg.User); lock(Users) { if (Users.Contains(uvm)) _users.Remove(uvm); } RaisePropertyChanged(() => Users); }); subTokCurrentRoomChgd = Messenger.Subscribe<CurrentRoomChangedMessage>(msg => { IsCurrent = msg.NewRoom.Room.Name.Equals(this.Room.Name, StringComparison.OrdinalIgnoreCase); RaisePropertyChanged(() => IsCurrent); }); LoadRoom (); }
public async void LoadRoom() { try { var room = await Connection.Client.GetRoomInfo (this.Room.Name); if (room == null) return; this.Room = room; Mvx.Trace("Users In Room: " + room.Users.Count()); if (room.RecentMessages != null && room.RecentMessages.Count() > 0) { lock(Messages) { foreach (var msg in room.RecentMessages.Reverse()) Messages.Insert (0, new MessageViewModel(msg)); } } if (room.Users != null && room.Users.Count() > 0) { lock(Users) { foreach (var u in room.Users) { var uvm = new UserViewModel(u); _users.Add(uvm); } } RaisePropertyChanged(() => Users); } } catch (Exception ex) { Mvx.Error(ex.ToString()); } RaisePropertyChanged (() => Room); }