Skip to content

Commit

Permalink
Use partial properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ashotjanibekyan committed Nov 13, 2024
1 parent cf475e0 commit fadfdbd
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/CrossWikiEditor.Core/CrossWikiEditor.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0-preview1" />
<PackageReference Include="CXuesong.MW.WikiClientLibrary" Version="0.8.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.70" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
Expand Down
43 changes: 29 additions & 14 deletions src/CrossWikiEditor.Core/ViewModels/AddOrEditProfileViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
namespace CrossWikiEditor.Core.ViewModels;

public sealed partial class AddOrEditProfileViewModel(IFileDialogService fileDialogService,
public sealed partial class AddOrEditProfileViewModel : ViewModelBase
{
private readonly IFileDialogService _fileDialogService;
private readonly IProfileRepository _profileRepository;
private readonly int _id;

public AddOrEditProfileViewModel(
IFileDialogService fileDialogService,
IProfileRepository profileRepository,
int id)
: ViewModelBase
{
public bool IsEdit => id != -1;
{
_fileDialogService = fileDialogService;
_profileRepository = profileRepository;
_id = id;
Username = string.Empty;
Password = string.Empty;
DefaultSettingsPath = string.Empty;
Notes = string.Empty;
}

public bool IsEdit => _id != -1;

[ObservableProperty] private string _username = string.Empty;
[ObservableProperty] private string _password = string.Empty;
[ObservableProperty] private string _defaultSettingsPath = string.Empty;
[ObservableProperty] private bool _shouldSavePassword;
[ObservableProperty] private bool _shouldSelectDefaultSettings;
[ObservableProperty] private string _notes = string.Empty;
[ObservableProperty] public partial string Username { get; set; }
[ObservableProperty] public partial string Password { get; set; }
[ObservableProperty] public partial string DefaultSettingsPath { get; set; }
[ObservableProperty] public partial bool ShouldSavePassword { get; set; }
[ObservableProperty] public partial bool ShouldSelectDefaultSettings { get; set; }
[ObservableProperty] public partial string Notes { get; set; }

[RelayCommand]
private async Task Browse()
{
string[]? result = await fileDialogService.OpenFilePickerAsync("Select settings file", false, ["*.xml"]);
string[]? result = await _fileDialogService.OpenFilePickerAsync("Select settings file", false, ["*.xml"]);
if (result?.Length == 1)
{
DefaultSettingsPath = result[0];
Expand Down Expand Up @@ -56,12 +71,12 @@ private void Save(IDialog dialog)

if (IsEdit)
{
profile.Id = id;
profileRepository.Update(profile);
profile.Id = _id;
_profileRepository.Update(profile);
}
else
{
profileRepository.Insert(profile);
_profileRepository.Insert(profile);
}

dialog.Close(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,35 @@ public MoreViewModel(
_dialogService = dialogService;
_settingsService = settingsService;
_moreOptions = _settingsService.GetCurrentSettings().MoreOptions;
IsAppendOrPrependEnabled = false;
IsAppend = true;
AppendOrPrependContent = string.Empty;
FileType = FileTaskType.None;
SourceFile = string.Empty;
ReplaceFileOrComment = string.Empty;
CategoryType = CategoryTaskType.None;
SourceCategory = string.Empty;
ReplaceCategory = string.Empty;
PopulateProperties();
}

[ObservableProperty] private bool _isAppendOrPrependEnabled = false;
[ObservableProperty] private bool _isAppend = true;
[ObservableProperty] private string _appendOrPrependContent = string.Empty;
[ObservableProperty] private int _appendOrPrependNewLines = 0;
[ObservableProperty] private bool _shouldSortMetadataAfterAppendOrPrepend;

[ObservableProperty] private FileTaskType _fileType = FileTaskType.None;
[ObservableProperty] private string _sourceFile = string.Empty;
[ObservableProperty] private string _replaceFileOrComment = string.Empty;
[ObservableProperty] private bool _skipIfNoFileChanged;
[ObservableProperty] public partial bool IsAppendOrPrependEnabled { get; set; }
[ObservableProperty] public partial bool IsAppend { get; set; }
[ObservableProperty] public partial string AppendOrPrependContent { get; set; }
[ObservableProperty] public partial int AppendOrPrependNewLines { get; set; }
[ObservableProperty] public partial bool ShouldSortMetadataAfterAppendOrPrepend { get; set; }

[ObservableProperty] private CategoryTaskType _categoryType = CategoryTaskType.None;
[ObservableProperty] private string _sourceCategory = string.Empty;
[ObservableProperty] private string _replaceCategory = string.Empty;
[ObservableProperty] private bool _skipIfNoCategoryChanged;
[ObservableProperty] private bool _removeSortkey;
[ObservableProperty] public partial FileTaskType FileType { get; set; }
[ObservableProperty] public partial string SourceFile { get; set; }
[ObservableProperty] public partial string ReplaceFileOrComment { get; set; }
[ObservableProperty] public partial bool SkipIfNoFileChanged { get; set; }

[ObservableProperty] public partial CategoryTaskType CategoryType { get; set; }
[ObservableProperty] public partial string SourceCategory { get; set; }
[ObservableProperty] public partial string ReplaceCategory { get; set; }
[ObservableProperty] public partial bool SkipIfNoCategoryChanged { get; set; }
[ObservableProperty] public partial bool RemoveSortkey { get; set; }

partial void OnIsAppendOrPrependEnabledChanged(bool value) => _moreOptions.IsAppendPrependEnabled = value;
partial void OnIsAppendChanged(bool value) => _moreOptions.IsAppend = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ public OptionsViewModel(
_dialogService = dialogService;
_settingsService = settingsService;
_generalOptions = _settingsService.GetCurrentSettings().GeneralOptions;
NormalFindAndReplaceRules = [];
PopulateProperties();
PropertyChanged += OptionsViewModel_PropertyChanged;
}

[ObservableProperty] private bool _autoTag;
[ObservableProperty] private bool _applyGeneralFixes;
[ObservableProperty] private bool _unicodifyWholePage;
[ObservableProperty] private bool _findAndReplace;
[ObservableProperty] private bool _skipIfNoReplacement;
[ObservableProperty] private bool _skipIfOnlyMinorReplacementMade;
[ObservableProperty] private bool _regexTypoFixing;
[ObservableProperty] private bool _skipIfNoTypoFixed;
[ObservableProperty] private NormalFindAndReplaceRules _normalFindAndReplaceRules = [];
[ObservableProperty] public partial bool AutoTag { get;set; }
[ObservableProperty] public partial bool ApplyGeneralFixes { get; set; }
[ObservableProperty] public partial bool UnicodifyWholePage { get; set; }
[ObservableProperty] public partial bool FindAndReplace { get; set; }
[ObservableProperty] public partial bool SkipIfNoReplacement { get; set; }
[ObservableProperty] public partial bool SkipIfOnlyMinorReplacementMade { get; set; }
[ObservableProperty] public partial bool RegexTypoFixing { get; set; }
[ObservableProperty] public partial bool SkipIfNoTypoFixed { get; set; }
[ObservableProperty] public partial NormalFindAndReplaceRules NormalFindAndReplaceRules { get; set; }

[RelayCommand]
private async Task OpenNormalFindAndReplaceDialog()
Expand Down
93 changes: 59 additions & 34 deletions src/CrossWikiEditor.Core/ViewModels/DatabaseScannerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,70 @@

namespace CrossWikiEditor.Core.ViewModels;

public sealed partial class DatabaseScannerViewModel(ISettingsService settingsService,
IWikiClientCache wikiClientCache,
IFileDialogService fileDialogService) : ViewModelBase
public sealed partial class DatabaseScannerViewModel : ViewModelBase
{
private Task? _scannerTask;
private Task? _updateUiTask;
private CancellationTokenSource _scannerCancellationTokenSource = new();
private readonly ConcurrentQueue<string> _titlesQueue = new();
private readonly ISettingsService _settingsService;
private readonly IWikiClientCache _wikiClientCache;
private readonly IFileDialogService _fileDialogService;
public EventHandler<string>? _convertedTextChanged;

[ObservableProperty] private ObservableCollection<WikiNamespace> _subjectNamespaces = [];
[ObservableProperty] private ObservableCollection<WikiNamespace> _talkNamespaces = [];
[ObservableProperty] private ObservableCollection<WikiPageModel> _pages = [];

[ObservableProperty] private bool _isTitleContainsEnabled;
[ObservableProperty] private bool _isTitleNotContainsEnabled;
[ObservableProperty] private string _titleContains = string.Empty;
[ObservableProperty] private string _titleNotContains = string.Empty;
[ObservableProperty] private bool _isTitleContainsRegex;
[ObservableProperty] private bool _isTitleContainsCaseSensitive;

[ObservableProperty] private bool _isAllTalkChecked;
[ObservableProperty] private bool _isAllSubjectChecked;
[ObservableProperty] private string _databaseFile = string.Empty;
[ObservableProperty] private string _siteName = string.Empty;
[ObservableProperty] private string _base = string.Empty;
[ObservableProperty] private string _generator = string.Empty;
[ObservableProperty] private string _case = string.Empty;

[ObservableProperty] private bool _isSearchDateChecked;
[ObservableProperty] private DateTimeOffset _selectedStartDate;
[ObservableProperty] private DateTimeOffset _selectedEndDate;
[ObservableProperty] private DateTimeOffset _minStartYear = new(new DateTime(2000, 1, 1));
[ObservableProperty] private DateTimeOffset _minEndYear = new(new DateTime(2000, 1, 1));

[ObservableProperty] private string _convertedText = string.Empty;
[ObservableProperty] private bool _isAlphabetisedHeading;
[ObservableProperty] private int _numberOfPagesOnEachSection = 25;
[ObservableProperty] private bool _isNumericList;
public DatabaseScannerViewModel(
ISettingsService settingsService,
IWikiClientCache wikiClientCache,
IFileDialogService fileDialogService)
{
_settingsService = settingsService;
_wikiClientCache = wikiClientCache;
_fileDialogService = fileDialogService;
SubjectNamespaces = [];
TalkNamespaces = [];
Pages = [];
TitleContains = string.Empty;
TitleNotContains = string.Empty;
DatabaseFile = string.Empty;
SiteName = string.Empty;
Base = string.Empty;
Generator = string.Empty;
Case = string.Empty;
MinStartYear = new(new DateTime(2000, 1, 1));
MinEndYear = new(new DateTime(2000, 1, 1));
ConvertedText = string.Empty;
NumberOfPagesOnEachSection = 25;
}

[ObservableProperty] public partial ObservableCollection<WikiNamespace> SubjectNamespaces { get; set; }
[ObservableProperty] public partial ObservableCollection<WikiNamespace> TalkNamespaces { get; set; }
[ObservableProperty] public partial ObservableCollection<WikiPageModel> Pages { get; set; }

[ObservableProperty] public partial bool IsTitleContainsEnabled { get; set;}
[ObservableProperty] public partial bool IsTitleNotContainsEnabled { get; set; }
[ObservableProperty] public partial string TitleContains {get;set;}
[ObservableProperty] public partial string TitleNotContains { get; set; }
[ObservableProperty] public partial bool IsTitleContainsRegex {get;set;}
[ObservableProperty] public partial bool IsTitleContainsCaseSensitive { get; set; }

[ObservableProperty] public partial bool IsAllTalkChecked { get; set; }
[ObservableProperty] public partial bool IsAllSubjectChecked { get; set; }
[ObservableProperty] public partial string DatabaseFile { get; set; }
[ObservableProperty] public partial string SiteName { get; set; }
[ObservableProperty] public partial string Base { get; set; }
[ObservableProperty] public partial string Generator { get; set; }
[ObservableProperty] public partial string Case { get; set; }

[ObservableProperty] public partial bool IsSearchDateChecked { get; set; }
[ObservableProperty] public partial DateTimeOffset SelectedStartDate { get; set; }
[ObservableProperty] public partial DateTimeOffset SelectedEndDate { get; set; }
[ObservableProperty] public partial DateTimeOffset MinStartYear { get; set; }
[ObservableProperty] public partial DateTimeOffset MinEndYear { get; set; }

[ObservableProperty] public partial string ConvertedText { get; set; }
[ObservableProperty] public partial bool IsAlphabetisedHeading { get; set; }
[ObservableProperty] public partial int NumberOfPagesOnEachSection { get; set; }
[ObservableProperty] public partial bool IsNumericList { get; set; }

partial void OnIsAllTalkCheckedChanged(bool value)
{
Expand All @@ -59,7 +84,7 @@ partial void OnIsAllSubjectCheckedChanged(bool value)
[RelayCommand]
public async Task BrowseCommand()
{
string[]? result = await fileDialogService.OpenFilePickerAsync("Open Database dump", false);
string[]? result = await _fileDialogService.OpenFilePickerAsync("Open Database dump", false);
if (result is not { Length: 1 })
{
return;
Expand All @@ -80,7 +105,7 @@ private void Start()

_updateUiTask = Task.Run(async () =>
{
WikiSite wikiSite = await wikiClientCache.GetWikiSite(settingsService.CurrentApiUrl);
WikiSite wikiSite = await _wikiClientCache.GetWikiSite(_settingsService.CurrentApiUrl);
while (_scannerTask != null)
{
UpdateUi(wikiSite);
Expand Down
55 changes: 33 additions & 22 deletions src/CrossWikiEditor.Core/ViewModels/FilterViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
namespace CrossWikiEditor.Core.ViewModels;

public partial class FilterViewModel(List<WikiNamespace> subjectNamespaces, List<WikiNamespace> talkNamespaces,
TextFileListProvider textFileListProvider)
: ViewModelBase
public partial class FilterViewModel : ViewModelBase
{
private readonly TextFileListProvider _textFileListProvider;

public FilterViewModel(
List<WikiNamespace> subjectNamespaces,
List<WikiNamespace> talkNamespaces,
TextFileListProvider textFileListProvider)
{
_textFileListProvider = textFileListProvider;
SubjectNamespaces = subjectNamespaces.ToObservableCollection();
TalkNamespaces = talkNamespaces.ToObservableCollection();
Pages = [];
SetOperations = new[] { Models.SetOperations.SymmetricDifference, Models.SetOperations.Intersection }.ToObservableCollection();
RemoveTitlesContaining = string.Empty;
KeepTitlesContaining = string.Empty;
SelectedSetOperations = Models.SetOperations.SymmetricDifference;
}

[RelayCommand]
private void Save(IDialog dialog)
{
Expand All @@ -29,11 +44,11 @@ private void Close(IDialog dialog)
[RelayCommand]
private async Task OpenFile()
{
await textFileListProvider.GetAdditionalParams();
await _textFileListProvider.GetAdditionalParams();

if (textFileListProvider.CanMake)
if (_textFileListProvider.CanMake)
{
Result<List<WikiPageModel>> result = await textFileListProvider.MakeList();
Result<List<WikiPageModel>> result = await _textFileListProvider.MakeList();
if (result is { IsSuccessful: true, Value: not null })
{
Pages = result.Value.ToObservableCollection();
Expand Down Expand Up @@ -61,20 +76,16 @@ partial void OnIsAllSubjectCheckedChanged(bool value)
.ToObservableCollection();
}

[ObservableProperty] private ObservableCollection<WikiNamespace> _subjectNamespaces = subjectNamespaces.ToObservableCollection();
[ObservableProperty] private ObservableCollection<WikiNamespace> _talkNamespaces = talkNamespaces.ToObservableCollection();
[ObservableProperty] private ObservableCollection<WikiPageModel> _pages = [];

[ObservableProperty]
private ObservableCollection<SetOperations> _setOperations =
new[] { Models.SetOperations.SymmetricDifference, Models.SetOperations.Intersection }.ToObservableCollection();

[ObservableProperty] private bool _isAllTalkChecked;
[ObservableProperty] private bool _isAllSubjectChecked;
[ObservableProperty] private bool _useRegex;
[ObservableProperty] private bool _sortAlphabetically;
[ObservableProperty] private bool _removeDuplicates;
[ObservableProperty] private string _removeTitlesContaining = string.Empty;
[ObservableProperty] private string _keepTitlesContaining = string.Empty;
[ObservableProperty] private SetOperations _selectedSetOperations = Models.SetOperations.SymmetricDifference;
[ObservableProperty] public partial ObservableCollection<WikiNamespace> SubjectNamespaces { get; set; }
[ObservableProperty] public partial ObservableCollection<WikiNamespace> TalkNamespaces { get; set; }
[ObservableProperty] public partial ObservableCollection<WikiPageModel> Pages { get; set; }
[ObservableProperty] public partial ObservableCollection<SetOperations> SetOperations { get; set; }
[ObservableProperty] public partial bool IsAllTalkChecked { get; set; }
[ObservableProperty] public partial bool IsAllSubjectChecked { get; set; }
[ObservableProperty] public partial bool UseRegex { get; set; }
[ObservableProperty] public partial bool SortAlphabetically { get; set; }
[ObservableProperty] public partial bool RemoveDuplicates { get; set; }
[ObservableProperty] public partial string RemoveTitlesContaining { get; set; }
[ObservableProperty] public partial string KeepTitlesContaining { get; set; }
[ObservableProperty] public partial SetOperations SelectedSetOperations { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ public sealed partial class EditBoxViewModel : ViewModelBase
{
public EditBoxViewModel(IMessengerWrapper messenger)
{
Content = string.Empty;
messenger.Register<PageUpdatingMessage>(this, (recipient, message) => Content = message.NewContent);
}

[ObservableProperty] private string _content = string.Empty;
[ObservableProperty] public partial string Content { get; set; }
}

0 comments on commit fadfdbd

Please sign in to comment.