Skip to content

Commit 3633aac

Browse files
committed
添加 [HashOptionSettings] 页面
1 parent 4bdb071 commit 3633aac

File tree

14 files changed

+234
-26
lines changed

14 files changed

+234
-26
lines changed

DotVast.HashTool.WinUI/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public App()
8585
services.AddSingleton<IFileService, FileService>();
8686

8787
// Views and ViewModels
88+
services.AddTransient<HashOptionSettingsViewModel>();
89+
services.AddTransient<HashOptionSettingsPage>();
8890
services.AddTransient<LicensesViewModel>();
8991
services.AddTransient<LicensesPage>();
9092
services.AddTransient<ResultsViewModel>();

DotVast.HashTool.WinUI/Controls/SettingItem/SettingItem.xaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
<Setter.Value>
1818
<ControlTemplate TargetType="dvhc:SettingItem">
1919
<Grid
20-
MinHeight="68"
2120
Padding="{TemplateBinding Padding}"
22-
HorizontalAlignment="Stretch"
23-
VerticalAlignment="Center"
2421
Background="{TemplateBinding Background}"
2522
BorderBrush="{TemplateBinding BorderBrush}"
2623
BorderThickness="{TemplateBinding BorderThickness}"
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
using System.Text.Json.Serialization;
2+
13
using DotVast.HashTool.WinUI.Services.Hash;
24

35
namespace DotVast.HashTool.WinUI.Models;
46

5-
public sealed partial class HashOption : ObservableObject
7+
public sealed partial class HashOption : ObservableRecipient
68
{
7-
[ObservableProperty]
89
private Hash _hash;
910

11+
[JsonIgnore]
12+
public Hash Hash
13+
{
14+
get => _hash;
15+
set => SetProperty(ref _hash, value);
16+
}
17+
1018
[ObservableProperty]
1119
private bool _isChecked;
1220

13-
public HashOption(Hash hash, bool isChecked = false)
21+
[ObservableProperty]
22+
[NotifyPropertyChangedRecipients]
23+
private bool _isEnabled;
24+
25+
public HashOption(Hash hash, bool isChecked = false, bool isEnabled = true)
1426
{
1527
_hash = hash;
1628
_isChecked = isChecked;
29+
_isEnabled = isEnabled;
1730
}
1831
}

DotVast.HashTool.WinUI/MultilingualResources/DotVast.HashTool.WinUI.en-US.xlf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@
241241
<source>重启应用</source>
242242
<target state="translated">Restart App</target>
243243
</trans-unit>
244+
<trans-unit id="HashOptionSettingsPage_Header" translate="yes" xml:space="preserve">
245+
<source>哈希选项</source>
246+
<target state="translated">Hash Option</target>
247+
</trans-unit>
248+
<trans-unit id="SettingsPage_HashOptions.Description" translate="yes" xml:space="preserve">
249+
<source>设置首页显示哪些哈希选项</source>
250+
<target state="translated">Set hash options which are displayed on the Home</target>
251+
</trans-unit>
252+
<trans-unit id="SettingsPage_HashOptions.Header" translate="yes" xml:space="preserve">
253+
<source>哈希选项</source>
254+
<target state="translated">Hash Option</target>
255+
</trans-unit>
244256
</group>
245257
</body>
246258
</file>

DotVast.HashTool.WinUI/Services/Hash/HashOptionsService.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal class HashOptionsService : IHashOptionsService
99

1010
private readonly ILocalSettingsService _localSettingsService;
1111

12-
public List<HashOption> HashOptions { get; set; } = new();
12+
public List<HashOption> HashOptions { get; } = new();
1313

1414
public HashOptionsService(ILocalSettingsService localSettingsService)
1515
{
@@ -65,13 +65,20 @@ public async Task SetHashOptionAsync(HashOption hashOption)
6565

6666
private async Task<HashOption> LoadHashOptionFromSettingsAsync(Hash hash)
6767
{
68-
var isChecked = await _localSettingsService.ReadSettingAsync<bool>($"{SettingsKeyPrefix}{hash.Name}");
69-
70-
return new(hash, isChecked);
68+
var hashOption = await _localSettingsService.ReadSettingAsync<HashOption>($"{SettingsKeyPrefix}{hash.Name}");
69+
if (hashOption == null)
70+
{
71+
return new(hash);
72+
}
73+
else
74+
{
75+
hashOption.Hash = hash;
76+
return hashOption;
77+
}
7178
}
7279

7380
private async Task SaveHashOptionAsync(HashOption hashOption)
7481
{
75-
await _localSettingsService.SaveSettingAsync($"{SettingsKeyPrefix}{hashOption.Hash.Name}", hashOption.IsChecked);
82+
await _localSettingsService.SaveSettingAsync($"{SettingsKeyPrefix}{hashOption.Hash.Name}", hashOption);
7683
}
7784
}

DotVast.HashTool.WinUI/Services/PageService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using CommunityToolkit.Mvvm.ComponentModel;
2-
31
using DotVast.HashTool.WinUI.Contracts.Services;
42
using DotVast.HashTool.WinUI.ViewModels;
53
using DotVast.HashTool.WinUI.Views;
@@ -19,6 +17,7 @@ public PageService()
1917
Configure<TasksViewModel, TasksPage>();
2018
Configure<ResultsViewModel, ResultsPage>();
2119
Configure<LicensesViewModel, LicensesPage>();
20+
Configure<HashOptionSettingsViewModel, HashOptionSettingsPage>();
2221
}
2322

2423
public Type GetPageType(string key)

DotVast.HashTool.WinUI/Strings/zh-Hans/Resources.resw

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@
149149
<value>未授权的访问,请考虑以管理员身份重试。</value>
150150
<comment>提示权限不足的任务中止对话框的内容</comment>
151151
</data>
152+
<data name="HashOptionSettingsPage_Header" xml:space="preserve">
153+
<value>哈希选项</value>
154+
</data>
152155
<data name="HashResultType_File" xml:space="preserve">
153156
<value>文件</value>
154157
</data>
@@ -232,6 +235,12 @@
232235
<data name="SettingsPage_AlwaysOnTop.Header" xml:space="preserve">
233236
<value>窗口置顶</value>
234237
</data>
238+
<data name="SettingsPage_HashOptions.Description" xml:space="preserve">
239+
<value>设置首页显示哪些哈希选项</value>
240+
</data>
241+
<data name="SettingsPage_HashOptions.Header" xml:space="preserve">
242+
<value>哈希选项</value>
243+
</data>
235244
<data name="SettingsPage_Language.Description" xml:space="preserve">
236245
<value>更改应用中显示的语言</value>
237246
</data>

DotVast.HashTool.WinUI/Styles/Card.xaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
1+
<ResourceDictionary
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:dvhc="using:DotVast.HashTool.WinUI.Controls">
25

36
<Style x:Key="CardBorderStyle" TargetType="Border">
47
<Setter Property="BorderThickness" Value="1" />
@@ -16,4 +19,25 @@
1619
<Setter Property="Padding" Value="16,8" />
1720
</Style>
1821

22+
<Style x:Key="CardSettingItem" TargetType="dvhc:SettingItem">
23+
<Setter Property="BorderThickness" Value="1" />
24+
<Setter Property="BorderBrush" Value="{ThemeResource CardStrokeColorDefaultBrush}" />
25+
<Setter Property="Background" Value="{ThemeResource CardBackgroundFillColorDefaultBrush}" />
26+
<Setter Property="CornerRadius" Value="4" />
27+
<Setter Property="Padding" Value="16,12" />
28+
<Setter Property="MinHeight" Value="68" />
29+
</Style>
30+
31+
<Style x:Key="CardSettingItemNoBorder" BasedOn="{StaticResource CardSettingItem}" TargetType="dvhc:SettingItem">
32+
<Setter Property="BorderThickness" Value="0" />
33+
<Setter Property="Background" Value="Transparent" />
34+
</Style>
35+
36+
<Style x:Key="CardButtonSettingItem" BasedOn="{StaticResource DefaultButtonStyle}" TargetType="Button">
37+
<Setter Property="Background" Value="{ThemeResource CardBackgroundFillColorDefaultBrush}" />
38+
<Setter Property="Padding" Value="0" />
39+
<Setter Property="HorizontalAlignment" Value="Stretch" />
40+
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
41+
</Style>
42+
1943
</ResourceDictionary>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Diagnostics;
2+
3+
using CommunityToolkit.Mvvm.Messaging.Messages;
4+
5+
using DotVast.HashTool.WinUI.Contracts.Services;
6+
using DotVast.HashTool.WinUI.Contracts.ViewModels;
7+
using DotVast.HashTool.WinUI.Models;
8+
9+
namespace DotVast.HashTool.WinUI.ViewModels;
10+
11+
public partial class HashOptionSettingsViewModel : ObservableRecipient, INavigationAware
12+
{
13+
private readonly IHashOptionsService _hashOptionsService;
14+
15+
public HashOptionSettingsViewModel(IHashOptionsService hashOptionsService)
16+
{
17+
_hashOptionsService = hashOptionsService;
18+
}
19+
20+
public List<HashOption> HashOptions => _hashOptionsService.HashOptions;
21+
22+
#region Messenger
23+
24+
protected override void OnActivated()
25+
{
26+
// PropertyChangedMessage[HashOption.IsEnabled]
27+
Messenger.Register<HashOptionSettingsViewModel, PropertyChangedMessage<bool>>(this, async (r, m) =>
28+
{
29+
if (m.Sender is HashOption hashOption && m.PropertyName == nameof(HashOption.IsEnabled))
30+
{
31+
Debug.WriteLine($"---------------- {DateTime.Now} -- HashOptionSettingsViewModel.Messenger.PropertyChangedMessage[HashOption.IsEnabled]");
32+
Debug.WriteLine($"Hash.Name: {hashOption.Hash.Name}");
33+
Debug.WriteLine($"IsEnabled:{hashOption.IsEnabled}");
34+
35+
await _hashOptionsService.SetHashOptionAsync(hashOption);
36+
}
37+
});
38+
}
39+
40+
#endregion Messenger
41+
42+
#region INavigationAware
43+
44+
public void OnNavigatedTo(object? parameter)
45+
{
46+
IsActive = true;
47+
}
48+
49+
public void OnNavigatedFrom()
50+
{
51+
IsActive = false;
52+
}
53+
54+
#endregion INavigationAware
55+
}

DotVast.HashTool.WinUI/ViewModels/HomeViewModel.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System.Diagnostics;
12
using System.Text;
23

4+
using CommunityToolkit.Mvvm.Messaging.Messages;
35
using CommunityToolkit.WinUI;
46

57
using DotVast.HashTool.WinUI.Contracts.Services;
@@ -41,8 +43,6 @@ public HomeViewModel(
4143
_computeHashService.AtomProgressChanged += (sender, e) => AtomProgressBar.Val = e;
4244
_computeHashService.TaskProgressChanged += (sender, e) => (TaskProgressBar.Val, TaskProgressBar.Max) = e;
4345

44-
HashOptions = hashOptionsService.HashOptions;
45-
4646
IsActive = true;
4747
}
4848

@@ -92,10 +92,7 @@ public HomeViewModel(
9292
/// <summary>
9393
/// Hash 选项.
9494
/// </summary>
95-
public List<HashOption> HashOptions
96-
{
97-
get;
98-
}
95+
public IEnumerable<HashOption> HashOptions => _hashOptionsService.HashOptions.Where(i => i.IsEnabled);
9996

10097
public record TextEncoding(string Name, Encoding Encoding);
10198

@@ -238,6 +235,19 @@ await dispatcherQueue.EnqueueAsync(() => ShowTipMessage(
238235
Localization.Tip_FileSkipped_Title,
239236
string.Format(Localization.Tip_FileSkipped_FileNotFound, m.Value)));
240237
});
238+
239+
// PropertyChangedMessage[HashOption.IsEnabled]
240+
Messenger.Register<HomeViewModel, PropertyChangedMessage<bool>>(this, (r, m) =>
241+
{
242+
if (m.Sender is HashOption hashOption && m.PropertyName == nameof(HashOption.IsEnabled))
243+
{
244+
Debug.WriteLine($"---------------- {DateTime.Now} -- HomeViewModel.Messenger.PropertyChangedMessage[HashOption.IsEnabled]");
245+
Debug.WriteLine($"Hash.Name: {hashOption.Hash.Name}");
246+
Debug.WriteLine($"IsEnabled:{hashOption.IsEnabled}");
247+
248+
OnPropertyChanged(nameof(HashOptions));
249+
}
250+
});
241251
}
242252

243253
#endregion Messenger
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<Page
2+
x:Class="DotVast.HashTool.WinUI.Views.HashOptionSettingsPage"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:models="using:DotVast.HashTool.WinUI.Models"
8+
mc:Ignorable="d">
9+
<Page.Resources>
10+
<DataTemplate x:Key="HashOptionDataTemplate" x:DataType="models:HashOption">
11+
<Grid Style="{StaticResource CardGridStyle}">
12+
<Grid.ColumnDefinitions>
13+
<ColumnDefinition Width="*" />
14+
<ColumnDefinition Width="Auto" />
15+
</Grid.ColumnDefinitions>
16+
<TextBlock VerticalAlignment="Center" Text="{x:Bind Hash.Name}" />
17+
<ToggleSwitch
18+
Grid.Column="1"
19+
MinWidth="0"
20+
IsOn="{x:Bind IsEnabled, Mode=TwoWay}" />
21+
</Grid>
22+
</DataTemplate>
23+
</Page.Resources>
24+
25+
<ScrollViewer VerticalScrollBarVisibility="Auto">
26+
<Grid x:Name="ContentArea" Style="{StaticResource NavigationViewPageContentStyle}">
27+
<ItemsRepeater ItemTemplate="{StaticResource HashOptionDataTemplate}" ItemsSource="{x:Bind ViewModel.HashOptions}">
28+
<ItemsRepeater.Layout>
29+
<UniformGridLayout
30+
ItemsStretch="Fill"
31+
MinColumnSpacing="16"
32+
MinItemWidth="180"
33+
MinRowSpacing="8" />
34+
</ItemsRepeater.Layout>
35+
</ItemsRepeater>
36+
</Grid>
37+
</ScrollViewer>
38+
</Page>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using DotVast.HashTool.WinUI.Behaviors;
2+
using DotVast.HashTool.WinUI.ViewModels;
3+
4+
using Microsoft.UI.Xaml.Controls;
5+
6+
namespace DotVast.HashTool.WinUI.Views;
7+
8+
public sealed partial class HashOptionSettingsPage : Page
9+
{
10+
public HashOptionSettingsViewModel ViewModel
11+
{
12+
get;
13+
}
14+
15+
public HashOptionSettingsPage()
16+
{
17+
ViewModel = App.GetService<HashOptionSettingsViewModel>();
18+
InitializeComponent();
19+
NavigationViewHeaderBehavior.SetHeaderContext(this, Localization.HashOptionSettingsPage_Header);
20+
}
21+
}

DotVast.HashTool.WinUI/Views/HomePage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
<!-- 哈希算法选择 -->
5151
<Border Style="{StaticResource CardBorderStyle}">
52-
<ItemsRepeater ItemsSource="{x:Bind ViewModel.HashOptions}">
52+
<ItemsRepeater ItemsSource="{x:Bind ViewModel.HashOptions, Mode=OneWay}">
5353
<ItemsRepeater.Layout>
5454
<UniformGridLayout ItemsStretch="Fill" />
5555
</ItemsRepeater.Layout>

0 commit comments

Comments
 (0)