private void MarkAsEnabled(ConnectionViewModel connection)
 {
     foreach (var item in Items)
     {
         item.IsEnabled = item.Id == connection.Id;
     }
 }
        private async Task UpdateAsync(ConnectionViewModel proxy)
        {
            var proxyId = proxy.Id;

            if (proxy is SystemProxyViewModel && !HttpProxyWatcher.Current.IsEnabled)
            {
                proxyId = 0;
            }

            var status = await ProtoService.SendAsync(new PingProxy(proxyId));

            BeginOnUIThread(() =>
            {
                if (status is Seconds seconds)
                {
                    proxy.Seconds = seconds.SecondsValue;
                    proxy.Error   = null;
                    proxy.Status  = new ConnectionStatusReady(proxy.IsEnabled, seconds.SecondsValue);
                }
                else if (status is Error error)
                {
                    proxy.Seconds = 0;
                    proxy.Error   = error;
                    proxy.Status  = new ConnectionStatusError(error);
                }
            });
        }
        private async void EnableExecute(ConnectionViewModel connection)
        {
            MarkAsEnabled(connection);

            if (connection is ProxyViewModel proxy)
            {
                await ProtoService.SendAsync(new EnableProxy(proxy.Id));
            }
            else
            {
                await ProtoService.SendAsync(new DisableProxy());
            }

            Handle(CacheService.GetConnectionState(), CacheService.Options.EnabledProxyId);
        }
        private async Task UpdateAsync(ConnectionViewModel proxy)
        {
            var status = await ProtoService.SendAsync(new PingProxy(proxy.Id));

            if (status is Seconds seconds)
            {
                proxy.Seconds = seconds.SecondsValue;
                proxy.Error   = null;
                proxy.Status  = new ConnectionStatusReady(proxy.IsEnabled, seconds.SecondsValue);
            }
            else if (status is Error error)
            {
                proxy.Seconds = 0;
                proxy.Error   = error;
                proxy.Status  = new ConnectionStatusError(error);
            }
        }
        private async void EditExecute(ConnectionViewModel connection)
        {
            var dialog  = new ProxyPopup(connection as ProxyViewModel);
            var confirm = await dialog.ShowQueuedAsync();

            if (confirm != ContentDialogResult.Primary)
            {
                return;
            }

            var response = await ProtoService.SendAsync(new EditProxy(connection.Id, dialog.Server, dialog.Port, false, dialog.Type));

            if (response is Proxy proxy)
            {
                var index = Items.IndexOf(connection);
                Items.Remove(connection);

                var edited = new ProxyViewModel(proxy);
                Items.Insert(index, edited);
                await UpdateAsync(edited);
            }

            Handle(CacheService.GetConnectionState(), CacheService.Options.EnabledProxyId);
        }