Skip to content

Commit

Permalink
#18 Make SIQuester.ViewModel cross-platform; enable new format for ne…
Browse files Browse the repository at this point in the history
…w packages; add more localization
  • Loading branch information
VladimirKhil committed Oct 29, 2023
1 parent 4730b6b commit 29a1181
Show file tree
Hide file tree
Showing 29 changed files with 346 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
/// </summary>
public interface IClipboardService
{
/// <summary>
/// Queries the Clipboard for the presence of data in a specified data format.
/// </summary>
/// <param name="format">Data format.</param>
bool ContainsData(string format);

/// <summary>
/// Retrieves data in a specified format from the Clipboard.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SIPackages;

namespace SIQuester.ViewModel.Contracts;

/// <summary>
/// Provides method for creating documents.
/// </summary>
public interface IDocumentViewModelFactory
{
/// <summary>
/// Creates view model for the document.
/// </summary>
/// <param name="document">Package document.</param>
/// <param name="fileName">Package file name.</param>
QDocument CreateViewModelFor(SIDocument document, string? fileName = null);
}
16 changes: 0 additions & 16 deletions src/SIQuester/SIQuester.ViewModel/ModelViewBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using SIPackages.Core;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace SIQuester.ViewModel;

Expand All @@ -10,21 +9,6 @@ namespace SIQuester.ViewModel;
/// </summary>
public abstract class ModelViewBase : INotifyPropertyChanged, IDisposable
{
/// <summary>
/// Allows to keep bindings to common application commands.
/// </summary>
public CommandBindingCollection CommandBindings { get; } = new();

protected void AddCommandBinding(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute = null)
{
var commandBinding = canExecute != null ?
new CommandBinding(command, executed, canExecute)
: new CommandBinding(command, executed);

CommandManager.RegisterClassCommandBinding(GetType(), commandBinding);
CommandBindings.Add(commandBinding);
}

protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public abstract class PlatformManager

public IServiceProvider ServiceProvider { get; set; }

/// <summary>
/// Gets well-known font family names.
/// </summary>
public abstract string[] FontFamilies { get; }

protected PlatformManager()
{
Instance = this;
Expand Down
3 changes: 1 addition & 2 deletions src/SIQuester/SIQuester.ViewModel/SIQuester.ViewModel.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AssemblyTitle>SIQuester.ViewModel</AssemblyTitle>
<Product>SIQuester.ViewModel</Product>
<Description>SIQuester business logic</Description>
Expand Down Expand Up @@ -32,7 +32,6 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="MahApps.Metro" Version="2.4.10" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
<PackageReference Include="YamlDotNet" Version="13.1.1" />
</ItemGroup>
Expand Down
26 changes: 26 additions & 0 deletions src/SIQuester/SIQuester.ViewModel/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using SIQuester.ViewModel.Contracts;
using SIQuester.ViewModel.Services;
using SIStorageService.ViewModel;

namespace SIQuester.ViewModel;

/// <summary>
/// Allows to register SIQuester view model in <see cref="IServiceCollection" />.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers SIQuester view model in <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">Services collection.</param>
public static IServiceCollection AddSIQuester(this IServiceCollection services)
{
services.AddSingleton<IPackageTemplatesRepository, PackageTemplatesRepository>();
services.AddSingleton<StorageViewModel>();
services.AddSingleton<StorageContextViewModel>();
services.AddSingleton<IDocumentViewModelFactory, DocumentViewModelFactory>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Logging;
using SIPackages;
using SIQuester.ViewModel.Contracts;
using SIQuester.ViewModel.Contracts.Host;

namespace SIQuester.ViewModel.Services;

/// <inheritdoc />
internal class DocumentViewModelFactory : IDocumentViewModelFactory
{
private readonly StorageContextViewModel _storageContextViewModel;
private readonly IClipboardService _clipboardService;
private readonly ILoggerFactory _loggerFactory;

public DocumentViewModelFactory(
StorageContextViewModel storageContextViewModel,
IClipboardService clipboardService,
ILoggerFactory loggerFactory)
{
_storageContextViewModel = storageContextViewModel;
_clipboardService = clipboardService;
_loggerFactory = loggerFactory;
}

public QDocument CreateViewModelFor(SIDocument document, string? fileName = null) => new(document, _storageContextViewModel, _clipboardService, _loggerFactory)
{
FileName = fileName ?? document.Package.Name
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SIQuester.ViewModel.Services;

/// <inheritdoc />
/// <inheritdoc cref="IPackageTemplatesRepository" />
public sealed class PackageTemplatesRepository : IPackageTemplatesRepository
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Extensions.Logging;
using SIPackages;
using SIPackages;
using SIPackages.Core;
using SIQuester.Model;
using SIQuester.ViewModel.Configuration;
using SIQuester.ViewModel.Contracts;
using SIQuester.ViewModel.Properties;
using System.Windows.Input;
using Utils.Commands;
Expand Down Expand Up @@ -57,13 +57,13 @@ public int To

public ICommand Select2 { get; private set; }

private readonly ILoggerFactory _loggerFactory;
private readonly IDocumentViewModelFactory _documentViewModelFactory;

public SelectThemesViewModel(QDocument document, AppOptions appOptions, ILoggerFactory loggerFactory)
public SelectThemesViewModel(QDocument document, AppOptions appOptions, IDocumentViewModelFactory documentViewModelFactory)
{
_document = document;
_appOptions = appOptions;
_loggerFactory = loggerFactory;
_documentViewModelFactory = documentViewModelFactory;

Themes = _document.Document.Package.Rounds
.SelectMany(round => round.Themes)
Expand All @@ -86,7 +86,7 @@ private async void Select_Executed(object? arg)
var allthemes = new List<Theme>();
_document.Document.Package.Rounds.ForEach(round => round.Themes.ForEach(allthemes.Add));

var targetDocument = new QDocument(newDocument, _document.StorageContext, _loggerFactory) { FileName = newDocument.Package.Name };
var targetDocument = _documentViewModelFactory.CreateViewModelFor(newDocument);

for (var index = _from; index <= _to; index++)
{
Expand Down Expand Up @@ -120,7 +120,7 @@ private async void Select2_Executed(object? arg)

var allthemes = Themes.Where(st => st.IsSelected).Select(st => st.Theme);

var targetDocument = new QDocument(newDocument, _document.StorageContext, _loggerFactory) { FileName = newDocument.Package.Name };
var targetDocument = _documentViewModelFactory.CreateViewModelFor(newDocument);

foreach (var theme in allthemes)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using Notions;
using Notions;
using SIPackages;
using SIPackages.Core;
using SIQuester.ViewModel.Configuration;
Expand Down Expand Up @@ -71,29 +70,29 @@ private async void LoadTours()
}
}

private readonly StorageContextViewModel _storageContextViewModel;
private readonly IDocumentViewModelFactory _documentViewModelFactory;
private readonly AppOptions _appOptions;
private readonly IChgkDbClient _chgkDbClient;
private readonly ILoggerFactory _loggerFactory;

public ImportDBStorageViewModel(
StorageContextViewModel storageContextViewModel,
IDocumentViewModelFactory documentViewModelFactory,
IChgkDbClient chgkDbClient,
AppOptions appOptions,
ILoggerFactory loggerFactory)
AppOptions appOptions)
{
_storageContextViewModel = storageContextViewModel;
_documentViewModelFactory = documentViewModelFactory;
_appOptions = appOptions;
_chgkDbClient = chgkDbClient;
_loggerFactory = loggerFactory;
}

public async Task SelectNodeAsync(DBNode item)
{
async Task<QDocument> loader(CancellationToken cancellationToken)
{
var siDoc = await SelectAsync(item, cancellationToken);
return new QDocument(siDoc, _storageContextViewModel, _loggerFactory) { FileName = siDoc.Package.Name, Changed = true };
var siDocument = await SelectAsync(item, cancellationToken);
var documentViewModel = _documentViewModelFactory.CreateViewModelFor(siDocument);
documentViewModel.Changed = true;

return documentViewModel;
};

var loaderViewModel = new DocumentLoaderViewModel(item.Name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Microsoft.Extensions.Logging;
using SIQuester.ViewModel.Configuration;
using SIQuester.ViewModel.Configuration;
using SIQuester.ViewModel.Contracts;
using SIQuester.ViewModel.Properties;
using SIStorageService.ViewModel;
using System.ComponentModel;
using System.Net;

namespace SIQuester.ViewModel;

/// <summary>
/// Allows to import package from SIStorage.
/// </summary>
public sealed class ImportSIStorageViewModel : WorkspaceViewModel
{
private static readonly HttpClient HttpClient = new() { DefaultRequestVersion = HttpVersion.Version20 };
Expand All @@ -15,24 +18,20 @@ public sealed class ImportSIStorageViewModel : WorkspaceViewModel

public override string Header => Resources.SIStorage;

private readonly StorageContextViewModel _storageContextViewModel;

public bool IsProgress => Storage.IsLoading || Storage.IsLoadingPackages;

private readonly AppOptions _appOptions;
private readonly ILoggerFactory _loggerFactory;
private readonly IDocumentViewModelFactory _documentViewModelFactory;

private readonly CancellationTokenSource _cancellationTokenSource = new();

public ImportSIStorageViewModel(
StorageContextViewModel storageContextViewModel,
StorageViewModel siStorage,
AppOptions appOptions,
ILoggerFactory loggerFactory)
IDocumentViewModelFactory documentViewModelFactory)
{
_storageContextViewModel = storageContextViewModel;
_appOptions = appOptions;
_loggerFactory = loggerFactory;
_documentViewModelFactory = documentViewModelFactory;

Storage = siStorage;

Expand Down Expand Up @@ -78,7 +77,7 @@ async Task<QDocument> loader(Uri uri, CancellationToken cancellationToken)
doc.Upgrade();
}

return new QDocument(doc, _storageContextViewModel, _loggerFactory) { FileName = doc.Package.Name };
return _documentViewModelFactory.CreateViewModelFor(doc);
};

var package = Storage.CurrentPackage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ public int Progress
private readonly CancellationTokenSource _tokenSource = new();
private readonly TaskScheduler _scheduler;

private readonly StorageContextViewModel _storageContextViewModel;
private readonly AppOptions _appOptions;
private string _badTextCopy = "";

Expand Down Expand Up @@ -352,7 +351,7 @@ public string SkipToolTip

public event Action<int, int, string?, bool>? HighlightText;

private readonly ILoggerFactory _loggerFactory;
private readonly IDocumentViewModelFactory _documentViewModelFactory;

private Encoding _textEncoding = Encoding.UTF8;

Expand All @@ -377,15 +376,13 @@ public Encoding TextEncoding
/// <summary>
/// Initializes a new instance of <see cref="ImportTextViewModel" /> class.
/// </summary>
/// <param name="storageContextViewModel">Well-known SIStorage facets holder.</param>
/// <param name="appOptions">Application options.</param>
/// <param name="clipboardService">Clipboard access service.</param>
/// <param name="loggerFactory">Factory to create loggers.</param>
public ImportTextViewModel(StorageContextViewModel storageContextViewModel, AppOptions appOptions, IClipboardService clipboardService, ILoggerFactory loggerFactory)
/// <param name="documentViewModelFactory">Factory to create documents.</param>
public ImportTextViewModel(AppOptions appOptions, IClipboardService clipboardService, IDocumentViewModelFactory documentViewModelFactory)
{
_storageContextViewModel = storageContextViewModel;
_appOptions = appOptions;
_loggerFactory = loggerFactory;
_documentViewModelFactory = documentViewModelFactory;
_scheduler = TaskScheduler.FromCurrentSynchronizationContext();

var trashAlias = new EditAlias(Resources.Trash, "#FFD3D3D3");
Expand Down Expand Up @@ -569,10 +566,10 @@ private void AnalyzeFinished(Task<Tuple<bool, int>> task)
var themesNum = task.Result.Item2;
if (task.Result.Item1)
{
if (!task.IsCanceled)
if (!task.IsCanceled && _existing != null)
{
PlatformManager.Instance.Inform($"{Resources.Success} {themesNum}.");
OnNewItem(new QDocument(_existing, _storageContextViewModel, _loggerFactory) { FileName = _existing.Package.Name });
OnNewItem(_documentViewModelFactory.CreateViewModelFor(_existing));
}
}
}
Expand Down Expand Up @@ -825,7 +822,7 @@ public void Clean()
exc => OnError(exc, ""),
CancellationToken.None);

OnNewItem(new QDocument(_existing, _storageContextViewModel, _loggerFactory) { FileName = _existing.Package.Name });
OnNewItem(_documentViewModelFactory.CreateViewModelFor(_existing));
}
}

Expand Down
Loading

0 comments on commit 29a1181

Please sign in to comment.