Exemplo n.º 1
0
        public VoiceChannelPageViewModel()
        {
            VoiceChannelName = new ReactiveProperty <string>();
            Users            = new ObservableCollection <SocketUser>();

            VoiceChannel = new ReactiveProperty <SocketVoiceChannel>();

            IsConnectedVoiceChannel =
                Observable.CombineLatest(
                    VoiceChannel,
                    DiscordContext.ObserveProperty(x => x.CurrentVoiceChannel)
                    )
                .Select(x => x[0] != null && x[0] == x[1])
                .ToReadOnlyReactiveProperty();
        }
Exemplo n.º 2
0
        public MenuViewModel(DiscordContext discordContext, INavigationService navService)
        {
            DiscordContext    = discordContext;
            NavigationService = navService;

            Guilds        = DiscordContext.Guilds;
            SelectedGuild = new ReactiveProperty <SocketGuild>();
            GuildName     = new ReactiveProperty <string>("");

            DiscordContext.Guilds.CollectionChangedAsObservable()
            .Subscribe(_ =>
            {
                RaisePropertyChanged(nameof(Guilds));
            });

            LoginUserName = DiscordContext.ObserveProperty(x => x.CurrentUser)
                            .Select(x => x?.Username ?? "")
                            .ToReadOnlyReactiveProperty();

            LoginUserAvaterUrl = DiscordContext.ObserveProperty(x => x.CurrentUser)
                                 .Select(x => x?.GetAvatarUrl() ?? "")
                                 .ToReadOnlyReactiveProperty();


            _TextChannels      = new ObservableCollection <SocketTextChannel>();
            TextChannels       = _TextChannels.ToReadOnlyReactiveCollection();
            CurrentTextChannel = DiscordContext.ObserveProperty(x => x.CurrentTextChannel)
                                 .ToReadOnlyReactiveProperty();

            _VoiceChannels      = new ObservableCollection <SocketVoiceChannel>();
            VoiceChannels       = _VoiceChannels.ToReadOnlyReactiveCollection();
            CurrentVoiceChannel = DiscordContext.ObserveProperty(x => x.CurrentVoiceChannel)
                                  .ToReadOnlyReactiveProperty();

            AfkChannel    = new ReactiveProperty <SocketVoiceChannel>();
            HasAfkChannel = AfkChannel.Select(x => x != null)
                            .ToReactiveProperty();


            SelectedGuild.Subscribe(async guild =>
            {
                using (var releaser = await _GuildChangingLock.LockAsync())
                {
                    if (guild != null)
                    {
                        _TextChannels.Clear();
                        _VoiceChannels.Clear();
                        AfkChannel.Value = null;

                        GuildName.Value = guild.Name;

                        DiscordContext.DiscordSocketClient.ChannelCreated   += Discord_ChannelCreated;
                        DiscordContext.DiscordSocketClient.ChannelDestroyed += Discord_ChannelDestroyed;
                        DiscordContext.DiscordSocketClient.ChannelUpdated   += Discord_ChannelUpdated;


                        // Listup Text Channels
                        var channels = guild.TextChannels.ToList();
                        channels.Sort((x, y) => x.Position - y.Position);
                        foreach (var textChannel in channels)
                        {
                            _TextChannels.Add(textChannel);
                        }


                        // Listup Voice Channels
                        var afkChannel    = guild.AFKChannel;
                        var afkChannelId  = afkChannel?.Id;
                        var voiceChannels =
                            guild.VoiceChannels
                            .Where(x => x.Id != afkChannelId)
                            .ToList();
                        voiceChannels.Sort((x, y) => x.Position - y.Position);

                        foreach (var channel in voiceChannels)
                        {
                            _VoiceChannels.Add(channel);
                        }

                        if (afkChannel != null)
                        {
                            AfkChannel.Value = afkChannel;
                        }
                    }
                    else
                    {
                        await Task.Delay(300);
                        _TextChannels.Clear();
                        _VoiceChannels.Clear();
                        AfkChannel.Value = null;
                        GuildName.Value  = null;
                    }
                }
            });
        }