public FriendListEntryViewModel(Tox tox, uint friendNumber) { //var publicKey = tox.GetFriendPublicKey(friendNumber); var status = tox.Events().Friends.StatusChanged .Where(x => x.FriendNumber == friendNumber) .Select(x => x.Status); status.Select(x => x == ToxUserStatus.Away) .ToPropertyEx(this, x => x.IsAway); status.Select(x => x == ToxUserStatus.Busy) .ToPropertyEx(this, x => x.IsBusy); tox.Events().Friends.NameChanged .Where(x => x.FriendNumber == friendNumber) .Select(x => x.Name) .StartWith(tox.GetFriendName(friendNumber, out _)) .ToPropertyEx(this, x => x.Name); tox.Events().Friends.StatusMessageChanged .Where(x => x.FriendNumber == friendNumber) .Select(x => x.StatusMessage) .StartWith(tox.GetFriendStatusMessage(friendNumber, out _)) .ToPropertyEx(this, x => x.StatusMessage); this.Remove = ReactiveCommand.Create(() => friendNumber); this.Conversation = new FriendConversationViewModel(tox, friendNumber); }
public FriendConversationViewModel(Tox tox, uint friendNumber) { var messages = new SourceList <MessageViewModel>(); messages.Connect() .AutoRefresh(x => x.Read) .Filter(x => !x.Read) .AsObservableList() .CountChanged .ToPropertyEx(this, x => x.UnreadCount); this.SendMessage = ReactiveCommand.Create <string>(msg => { if (!string.IsNullOrWhiteSpace(msg)) { _ = tox.SendMessage(friendNumber, msg, ToxMessageType.Message, out _); messages.Add(new MessageViewModel("Me", msg, DateTime.Now.Ticks)); } }); ReadOnlyObservableCollection <MessageViewModel> msgs; messages.Connect() .Bind(out msgs) .Subscribe(); this.Messages = msgs; this.SendTypedMessage = ReactiveCommand.Create(() => { var msg = this.InputMessage; this.InputMessage = string.Empty; return(msg); }, this.WhenAnyValue(x => x.InputMessage, (string s) => !string.IsNullOrWhiteSpace(s))); this.WhenAnyValue(x => x.InputMessage) .Select(msg => !string.IsNullOrWhiteSpace(msg)) .Where(x => x) .Do(x => tox.SetTypingStatus(friendNumber, true, out _)) .Throttle(TimeSpan.FromMilliseconds(500)) .Subscribe(x => tox.SetTypingStatus(friendNumber, false, out _)); tox.Events().Friends .Message .Where(x => x.FriendNumber == friendNumber && x.MessageType == ToxMessageType.Message) .Select(x => x.Message) .Subscribe(msg => messages.Add(new MessageViewModel(tox.GetFriendName(friendNumber, out _), msg, DateTime.Now.Ticks))); this.WhenAnyObservable(x => x.SendTypedMessage) .InvokeCommand(this, x => x.SendMessage); }