コード例 #1
0
        private void hostedConfigList_ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = (HostedConfigListItem)e.ClickedItem;

            hostedConfigList.PrepareConnectedAnimation("hostedConfigName", item, "nameText");
            itemForBackNavigation = item;
            Frame.Navigate(typeof(HostedConfigPage), item);
        }
コード例 #2
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            HostedConfigListItem = (HostedConfigListItem)e.Parameter;
            var nameAnimation = ConnectedAnimationService.GetForCurrentView().GetAnimation(CONNECTED_ANIMATION_HOSTED_CONFIG_NAME);

            if (nameAnimation != null)
            {
                nameAnimation.TryStart(hostedConfigNameTextBlock);
            }
            Bindings.Update();
        }
コード例 #3
0
        private async Task AddSubscribeAsync()
        {
            var cancellationToken = LoadSnapshotCancellationSource.Token;
            var config            = new HostedConfig();

            // Create source
            switch (sourcePivot.SelectedIndex)
            {
            case 0:
                // URL
                if (string.IsNullOrWhiteSpace(urlText.Text))
                {
                    throw new OperationCanceledException("Empty input");
                }
                if (!Uri.IsWellFormedUriString(urlText.Text, UriKind.Absolute))
                {
                    throw new InvalidDataException("The URL is invalid");
                }
                var urlSource = new UrlSource()
                {
                    Url = urlText.Text
                };
                config.Source = urlSource;
                break;

            case 1:
                // File
                if (!(previewFileNameText.DataContext is StorageFile file))
                {
                    file = await FileOpenPicker.PickSingleFileAsync();
                }
                if (file == null)
                {
                    throw new InvalidDataException("No file is chosen");
                }
                config.Source = new FileSource(file);
                break;

            default:
                throw new NotSupportedException("The source type is not supported yet");
            }

            // Create format
            Snapshot snapshot;

            cancellationToken.ThrowIfCancellationRequested();
            switch (((NewHostedConfigType)hostedTypeGridView.SelectedItem).Id)
            {
            case "ssd":
                config.Format = new Ssd();
                break;

            case "clash":
                config.Format = new Clash();
                break;

            default:
                throw new NotSupportedException("The hosted config type is not supported yet");
            }
            using (var source = await config.Source.FetchAsync().AsTask(cancellationToken))
            {
                snapshot = await source.DecodeAsync(config.Format).AsTask(cancellationToken);
            }
            config.Name = config.Source.GetFileName();

            // Save
            cancellationToken.ThrowIfCancellationRequested();
            var configFile = await HostedUtils.SaveHostedConfigAsync(config);

            try
            {
                _ = CachedFileManager.CompleteUpdatesAsync(configFile);
                cancellationToken.ThrowIfCancellationRequested();
                // No need to update local file
                _ = await HostedUtils.SaveSnapshotAsync(snapshot, config);
            }
            catch (Exception)
            {
                // Rollback if snapshot is not saved
                await configFile.DeleteAsync();

                throw;
            }

            if (Frame.CanGoBack)
            {
                Frame.GoBack();
            }
            var newItem = new HostedConfigListItem(config, snapshot);
            await Task.Delay(300);

            HostedConfigListItems?.Add(newItem);
            await Task.Delay(800);

            HostedConfigListPage.itemForBackNavigation = newItem;
            Frame.Navigate(typeof(HostedConfigPage), newItem);
        }