示例#1
0
        public static async Task <StorageFile> SaveHostedConfigAsync(HostedConfig config)
        {
            var file = await IoUtils.SaveUniqueObjAsync(config.Path, config, await HostedConfigFolderTask);

            config.Path = file.Path;
            return(file);
        }
示例#2
0
        public static async Task <StorageFile> SaveSnapshotAsync(Snapshot snapshot, HostedConfig config)
        {
            var fileName = Path.GetFileName(config.Path);
            var file     = await(await SnapshotFolderTask).CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
            await IoUtils.SaveObjAsync(snapshot, file);

            snapshot.Path = file.Path;
            return(file);
        }
示例#3
0
        public static async Task <Snapshot> UpdateSnapshotAsync(HostedConfig hostedConfig, Snapshot oldSnapshot)
        {
            Snapshot newSnapshot;

            using (var source = await hostedConfig.Source.FetchAsync())
            {
                newSnapshot = await source.DecodeAsync(hostedConfig.Format);
            }
            var newSnapshotFile = await(await SnapshotFolderTask).CreateFileAsync(Path.GetFileName(hostedConfig.Path), CreationCollisionOption.ReplaceExisting);
            await IoUtils.SerializeToFilePathAsync(newSnapshotFile, newSnapshot, default);

            newSnapshot.Path = newSnapshotFile.Path;
            return(newSnapshot);
        }
示例#4
0
        public static async Task <Snapshot> GetSnapshotFromHostConfigAsync(HostedConfig config)
        {
            var         fileName = Path.GetFileName(config.Path);
            StorageFile file;

            try
            {
                file = await(await SnapshotFolderTask).GetFileAsync(fileName);
                var snapshot = await IoUtils.DeserializeFromFileAsync <Snapshot>(file, default);

                snapshot.Path = file.Path;
                return(snapshot);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }
        }
        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);
        }
 public HostedConfigListItem(HostedConfig config, Snapshot snapshot)
 {
     HostedConfig          = config;
     LoadSnapshotTask.Task = LastSucceededTask = Task.FromResult(snapshot);
 }
 public HostedConfigListItem(HostedConfig config)
 {
     HostedConfig = config;
     _            = StartLoadTask(HostedUtils.GetSnapshotFromHostConfigAsync(config));
 }