Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa committed May 17, 2023
1 parent 210c493 commit b529bd1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 46 deletions.
26 changes: 22 additions & 4 deletions src/Files.App/Dialogs/AddItemDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Grid.RowSpan="4"
BorderThickness="0"
CornerRadius="{StaticResource OverlayCornerRadius}"
DataContext="{x:Bind ViewModel, Mode=OneWay}"
Loaded="AddItemDialog_Loaded"
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
SecondaryButtonText="{helpers:ResourceString Name=Cancel}"
Expand All @@ -23,14 +24,17 @@
<converters:ImageModelToImageConverter x:Key="ImageModelToImageConverter" />
</ContentDialog.Resources>

<Grid RowSpacing="25">
<Grid RowSpacing="24">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="SubtitleArea">
<TextBlock x:Name="Description" Text="{helpers:ResourceString Name=AddDialogDescription/Text}" />
</Grid>

<TextBlock
x:Name="Description"
Grid.Row="0"
Text="{helpers:ResourceString Name=AddDialogDescription/Text}" />

<Grid
x:Name="SelectionListContent"
Grid.Row="1"
Expand All @@ -41,11 +45,15 @@
IsItemClickEnabled="True"
ItemClick="ListView_ItemClick"
ItemsSource="{x:Bind ViewModel.AddItemsList}">

<ListView.ItemTemplate>
<DataTemplate x:DataType="vm:AddItemDialogListItemViewModel">
<Grid Height="50">

<StackPanel VerticalAlignment="Center" Orientation="Horizontal">

<Grid Margin="0,0,10,0" VerticalAlignment="Center">

<Viewbox
x:Name="IconRoot"
Width="24"
Expand All @@ -54,6 +62,7 @@
VerticalAlignment="Center">
<FontIcon Glyph="{x:Bind Glyph, Mode=OneWay}" />
</Viewbox>

<Image
Width="24"
Height="24"
Expand All @@ -62,16 +71,25 @@
VerticalAlignment="Center"
Source="{x:Bind Icon, Mode=OneWay, Converter={StaticResource ImageModelToImageConverter}}"
Stretch="Uniform" />

</Grid>

<StackPanel>

<TextBlock Text="{x:Bind Header, Mode=OneWay}" />

<TextBlock Foreground="Gray" Text="{x:Bind SubHeader, Mode=OneWay}" />

</StackPanel>

</StackPanel>

</Grid>
</DataTemplate>
</ListView.ItemTemplate>

</ListView>
</Grid>

</Grid>
</ContentDialog>
37 changes: 16 additions & 21 deletions src/Files.App/Dialogs/AddItemDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.DependencyInjection;
using Files.Backend.Services;
using Files.Backend.ViewModels.Dialogs;
using Files.Backend.ViewModels.Dialogs.AddItemDialog;
using Files.Shared.Enums;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Threading.Tasks;

// The Content Dialog item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Files.App.Dialogs
{
public sealed partial class AddItemDialog : ContentDialog, IDialog<AddItemDialogViewModel>
{
private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
private readonly IAddItemService _addItemService;

public AddItemDialogViewModel ViewModel
{
get => (AddItemDialogViewModel)DataContext;
set => DataContext = value;
}
public AddItemDialogViewModel ViewModel { get; set; }

public AddItemDialog()
{
InitializeComponent();
}

public new async Task<DialogResult> ShowAsync() => (DialogResult)await base.ShowAsync();

private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
ViewModel.ResultType = (e.ClickedItem as AddItemDialogListItemViewModel).ItemResult;
Hide();
// Dependency Injection
_addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
}

public new async Task<DialogResult> ShowAsync()
=> (DialogResult)await base.ShowAsync();

private async void AddItemDialog_Loaded(object sender, RoutedEventArgs e)
{
var itemTypes = await addItemService.GetNewEntriesAsync();
var itemTypes = await _addItemService.GetNewEntriesAsync();
await ViewModel.AddItemsToList(itemTypes);

// Focus on the list view so users can use keyboard navigation
AddItemsListView.Focus(FocusState.Programmatic);
}

private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
ViewModel.ResultType = ((AddItemDialogListItemViewModel)e.ClickedItem)?.ItemResult ?? throw new ArgumentNullException();

Hide();
}
}
}
}
15 changes: 15 additions & 0 deletions src/Files.App/Dialogs/CreateArchiveDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock
Grid.Column="0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=Name}" />

<TextBox
x:Name="FileNameBox"
Grid.Column="1"
Expand All @@ -54,6 +56,7 @@
Target="{x:Bind FileNameBox}" />
</TextBox.Resources>
</TextBox>

</Grid>

<!-- Archive Options -->
Expand Down Expand Up @@ -81,6 +84,7 @@
Grid.Column="0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=Format}" />

<ComboBox
x:Name="FileFormatSelector"
Grid.Row="0"
Expand All @@ -97,6 +101,7 @@
Grid.Column="0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=CompressionLevel}" />

<ComboBox
x:Name="CompressionLevelSelector"
Grid.Row="1"
Expand All @@ -114,6 +119,7 @@
Grid.Column="0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=SplittingSize}" />

<ComboBox
x:Name="SplittingSizeSelector"
Grid.Row="2"
Expand All @@ -126,14 +132,17 @@
SelectedValuePath="Key">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:SplittingSizeItem">

<StackPanel Orientation="Horizontal" Spacing="4">
<TextBlock Text="{x:Bind Label}" />
<TextBlock Text="{x:Bind Separator}" Visibility="{Binding IsDropDownOpen, ElementName=SplittingSizeSelector}" />
<TextBlock Text="{x:Bind Description}" Visibility="{Binding IsDropDownOpen, ElementName=SplittingSizeSelector}" />
</StackPanel>

</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

</Grid>

<!-- Encryption Options -->
Expand All @@ -142,15 +151,18 @@
Header="{helpers:ResourceString Name=Encryption}"
IsExpanded="{x:Bind ViewModel.UseEncryption, Mode=TwoWay}">
<Expander.Content>

<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock
Grid.Column="0"
VerticalAlignment="Center"
Text="{helpers:ResourceString Name=Password}" />

<PasswordBox
x:Name="PasswordBox"
Grid.Column="1"
Expand All @@ -160,8 +172,11 @@
Password="{x:Bind ViewModel.Password, Mode=TwoWay}"
PasswordRevealMode="Peek"
PlaceholderText="{helpers:ResourceString Name=Password}" />

</Grid>

</Expander.Content>
</Expander>

</StackPanel>
</ContentDialog>
30 changes: 15 additions & 15 deletions src/Files.App/Dialogs/CreateArchiveDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.ComponentModel;
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Filesystem.Archive;
using Files.Backend.Models;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;

namespace Files.App.Dialogs
Expand Down Expand Up @@ -67,7 +59,8 @@ public CreateArchiveDialog()
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
}

public new Task<ContentDialogResult> ShowAsync() => SetContentDialogRoot(this).ShowAsync().AsTask();
public new Task<ContentDialogResult> ShowAsync()
=> SetContentDialogRoot(this).ShowAsync().AsTask();

private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
{
Expand All @@ -83,6 +76,7 @@ private void ContentDialog_Loaded(object _, RoutedEventArgs e)
FileNameBox.SelectionStart = FileNameBox.Text.Length;
FileNameBox.Focus(FocusState.Programmatic);
}

private void ContentDialog_Closing(ContentDialog _, ContentDialogClosingEventArgs e)
{
InvalidNameWarning.IsOpen = false;
Expand Down Expand Up @@ -140,7 +134,8 @@ public CompressionLevelItem CompressionLevel
set => SetProperty(ref compressionLevel, value);
}

public bool CanSplit => FileFormat.Key is ArchiveFormats.SevenZip;
public bool CanSplit
=> FileFormat.Key is ArchiveFormats.SevenZip;

private SplittingSizeItem splittingSize;
public SplittingSizeItem SplittingSize
Expand Down Expand Up @@ -175,7 +170,8 @@ public string Password
{
new(ArchiveFormats.Zip, ".zip"),
new(ArchiveFormats.SevenZip, ".7z"),
}.ToImmutableList();
}
.ToImmutableList();

public IImmutableList<CompressionLevelItem> CompressionLevels { get; } = new List<CompressionLevelItem>
{
Expand All @@ -185,7 +181,8 @@ public string Password
new CompressionLevelItem(ArchiveCompressionLevels.Low, "CompressionLevelLow".GetLocalizedResource()),
new CompressionLevelItem(ArchiveCompressionLevels.Fast, "CompressionLevelFast".GetLocalizedResource()),
new CompressionLevelItem(ArchiveCompressionLevels.None, "CompressionLevelNone".GetLocalizedResource()),
}.ToImmutableList();
}
.ToImmutableList();

public IImmutableList<SplittingSizeItem> SplittingSizes { get; } = new List<SplittingSizeItem>
{
Expand All @@ -200,7 +197,8 @@ public string Password
new(ArchiveSplittingSizes.Mo5120, ToSizeText(5120)),
new(ArchiveSplittingSizes.Dvd8128, ToSizeText(8128), "DVD".GetLocalizedResource()),
new(ArchiveSplittingSizes.Bd23040, ToSizeText(23040), "Bluray".GetLocalizedResource()),
}.ToImmutableList();
}
.ToImmutableList();

public DialogViewModel()
{
Expand All @@ -209,7 +207,8 @@ public DialogViewModel()
splittingSize = SplittingSizes.First(size => size.Key is ArchiveSplittingSizes.None);
}

private static string ToSizeText(ulong megaBytes) => ByteSize.FromMebiBytes(megaBytes).ShortString;
private static string ToSizeText(ulong megaBytes)
=> ByteSize.FromMebiBytes(megaBytes).ShortString;

public record FileFormatItem(ArchiveFormats Key, string Label);

Expand All @@ -219,6 +218,7 @@ public record CompressionLevelItem(ArchiveCompressionLevels Key, string Label);

internal record SplittingSizeItem(ArchiveSplittingSizes Key, string Label, string Description = "")
{
public string Separator => string.IsNullOrEmpty(Description) ? string.Empty : "-";
public string Separator
=> string.IsNullOrEmpty(Description) ? string.Empty : "-";
}
}
2 changes: 2 additions & 0 deletions src/Files.App/Dialogs/CreateShortcutDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@
PreferredPlacement="Bottom" />
</TextBox.Resources>
</TextBox>

<Button
x:Name="SelectDestination"
Grid.Row="2"
Grid.Column="1"
Command="{x:Bind ViewModel.SelectDestinationCommand}"
Content="{helpers:ResourceString Name=Browse}" />

</Grid>
</Border>
</ContentDialog>
10 changes: 4 additions & 6 deletions src/Files.App/Dialogs/CreateShortcutDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

using Files.App.ViewModels.Dialogs;
using Files.Backend.ViewModels.Dialogs;
using Files.Shared.Enums;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using System;
using System.Threading.Tasks;

namespace Files.App.Dialogs
{
Expand All @@ -30,12 +27,13 @@ public CreateShortcutDialog()
});
}

public new async Task<DialogResult> ShowAsync()
=> (DialogResult)await base.ShowAsync();

private void CreateShortcutDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
this.Closing -= CreateShortcutDialog_Closing;
InvalidPathWarning.IsOpen = false;
}

public new async Task<DialogResult> ShowAsync() => (DialogResult)await base.ShowAsync();
}
}
}

0 comments on commit b529bd1

Please sign in to comment.