Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LogViewModel"/> class.
        /// </summary>
        public ChannelsViewModel()
        {
            ChannelsCollection     = new ObservableCollection <AblyChannel>();
            UnSubscribeFromChannel = new Command <string>(async channelName =>
            {
                await Ably.Channels.Get(channelName).Push.UnsubscribeDevice();
                await ExecuteLoadItemsCommand(); // Make sure we reload the channels list

                Message          = $"Unsubscribed from channel {channelName}";
                MessageIsVisible = true;

                DelayAction(() =>
                {
                    Message          = string.Empty;
                    MessageIsVisible = false;
                    return(Task.CompletedTask);
                });
            });
            LoadChannelsCommand = new Command(async() => await ExecuteLoadItemsCommand());
            SubscribeToChannel  = new Command(async() =>
            {
                MessageIsVisible = false;

                if (string.IsNullOrEmpty(ChannelName))
                {
                    Message = "Please enter a channel name";
                    return;
                }

                if (ChannelName.StartsWith("push:") == false)
                {
                    Message = "Make sure the 'push:' channel namespace is set.";
                    return;
                }

                try
                {
                    await Ably.Channels.Get(ChannelName).Push.SubscribeDevice();
                    await ExecuteLoadItemsCommand(); // Make sure we reload the channels list
                }
                catch (AblyException e)
                {
                    Message = $"Error subscribing device to channel. Messages: {e.Message}. Code: {e.ErrorInfo.Code}";
                }

                ChannelName = string.Empty;
            });
        }