private async void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            var btn    = (MenuFlyoutItem)sender;
            var config = (IAdapterConfig)btn.DataContext;

            var(_, result) = await Utils.NotifyUser("Remove this config?", primaryCommandText : "Yes");

            if (result != ContentDialogResult.Primary)
            {
                return;
            }
            try
            {
                File.Delete(config.Path);
                adapterConfigs.Remove(config);
                if (config.Path == AdapterConfig.GetDefaultConfigFilePath())
                {
                    AdapterConfig.ClearDefaultConfigFilePath();
                }
            }
            catch (Exception ex)
            {
                await Utils.NotifyUser("Error while deleting config file: " + ex.Message);
            }
        }
Example #2
0
        private async void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            var btn = (MenuFlyoutItem)sender;

            IList <object>      configs;
            ContentDialogResult result;

            if (configList.SelectedItems.Count == 0)
            {
                var config = (IAdapterConfig)((FrameworkElement)sender).DataContext;
                configs = new List <object>()
                {
                    config
                };
                (_, result) = await UiUtils.NotifyUser("Remove this config?", primaryCommandText : "Yes");
            }
            else
            {
                configs     = configList.SelectedItems;
                (_, result) = await UiUtils.NotifyUser($"Remove {configList.SelectedItems.Count} configs?", primaryCommandText : "Yes");
            }
            if (result != ContentDialogResult.Primary)
            {
                return;
            }
            var deletedConfigs = new List <IAdapterConfig>(configs.Count);

            loadProgressBar.IsIndeterminate = true;
            loadProgressBar.Value           = 0;
            loadProgressBar.Visibility      = Visibility.Visible;
            try
            {
                var defaultConfigPath = AdapterConfig.GetDefaultConfigFilePath();
                var tasks             = configs.Select(async obj =>
                {
                    var config = (IAdapterConfig)obj;
                    var file   = await StorageFile.GetFileFromPathAsync(config.Path);
                    await file.DeleteAsync();
                    deletedConfigs.Add(config);
                    if (config.Path == defaultConfigPath)
                    {
                        AdapterConfig.ClearDefaultConfigFilePath();
                    }
                    loadProgressBar.IsIndeterminate = false;
                    loadProgressBar.Value           = (double)deletedConfigs.Count / configs.Count;
                });
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                await UiUtils.NotifyUser("Error while deleting config file: " + ex.ToString());
            }
            finally
            {
                if (deletedConfigs.Count == adapterConfigs.Count)
                {
                    adapterConfigs.Clear();
                }
                else
                {
                    foreach (var deleted in deletedConfigs)
                    {
                        adapterConfigs.Remove(deleted);
                    }
                }
                loadProgressBar.IsIndeterminate = true;
                loadProgressBar.Visibility      = Visibility.Collapsed;
            }
        }