Skip to content

Commit

Permalink
Work on async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kierepka committed Feb 22, 2025
1 parent d741bd2 commit af7cda7
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 218 deletions.
35 changes: 35 additions & 0 deletions ChatAAC/Abstractions/Services/IBoardLoaderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Abstractions/Services/IBoardLoaderService.cs

using System.Threading;
using System.Threading.Tasks;
using ChatAAC.Models.Obf;

namespace ChatAAC.Abstractions.Services;

public interface IBoardLoaderService
{
/// <summary>
/// Ładuje plik .obf lub .obz asynchronicznie
/// </summary>
/// <param name="filePath">Ścieżka do pliku</param>
/// <param name="cancellationToken">Token anulowania operacji</param>
/// <returns>Zadanie reprezentujące operację ładowania</returns>
Task LoadObfOrObzFileAsync(string filePath, CancellationToken cancellationToken = default);

/// <summary>
/// Ładuje plik .obf asynchronicznie
/// </summary>
/// <param name="filePath">Ścieżka do pliku .obf</param>
/// <param name="cancellationToken">Token anulowania operacji</param>
/// <returns>Zadanie reprezentujące operację ładowania</returns>
Task LoadObfFileAsync(string? filePath, CancellationToken cancellationToken = default);

/// <summary>
/// Zapisuje plik .obf asynchronicznie
/// </summary>
/// <param name="filePath">Ścieżka docelowa pliku</param>
/// <param name="obfFile">Obiekt pliku OBF do zapisu</param>
/// <param name="cancellationToken">Token anulowania operacji</param>
/// <returns>Zadanie reprezentujące operację zapisu</returns>
Task SaveObfFileAsync(string filePath, ObfFile obfFile, CancellationToken cancellationToken = default);
}
16 changes: 16 additions & 0 deletions ChatAAC/Abstractions/Services/ICachePathProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ChatAAC.Abstractions.Services;

public interface ICachePathProvider
{
/// <summary>
/// Pobiera ścieżkę do katalogu cache plików OBF
/// </summary>
/// <returns>Ścieżka do katalogu cache OBF</returns>
string GetObfCacheDirectory();

/// <summary>
/// Pobiera ścieżkę do katalogu cache piktogramów
/// </summary>
/// <returns>Ścieżka do katalogu cache piktogramów</returns>
string GetPictogramsCacheDirectory();
}
32 changes: 32 additions & 0 deletions ChatAAC/Abstractions/Services/IFileTypeValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace ChatAAC.Abstractions.Services;

public interface IFileTypeValidator
{
/// <summary>
/// Sprawdza, czy plik jest obrazem
/// </summary>
/// <param name="fileName">Nazwa pliku</param>
/// <returns>True, jeśli plik jest obrazem, False w przeciwnym razie</returns>
bool IsImageFile(string fileName);

/// <summary>
/// Sprawdza, czy plik jest plikiem OBF
/// </summary>
/// <param name="fileName">Nazwa pliku</param>
/// <returns>True, jeśli plik jest OBF, False w przeciwnym razie</returns>
bool IsObfFile(string fileName);

/// <summary>
/// Sprawdza, czy plik jest plikiem manifestu
/// </summary>
/// <param name="fileName">Nazwa pliku</param>
/// <returns>True, jeśli plik jest manifestem, False w przeciwnym razie</returns>
bool IsManifestFile(string fileName);

/// <summary>
/// Sprawdza, czy plik jest plikiem OBZ
/// </summary>
/// <param name="fileName">Nazwa pliku</param>
/// <returns>True, jeśli plik jest OBZ, False w przeciwnym razie</returns>
bool IsObzFile(string fileName);
}
16 changes: 16 additions & 0 deletions ChatAAC/Abstractions/Services/IObfLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading;
using System.Threading.Tasks;
using ChatAAC.Models.Obf;

namespace ChatAAC.Abstractions.Services;

public interface IObfLoader
{
/// <summary>
/// Ładuje plik OBF asynchronicznie
/// </summary>
/// <param name="filePath">Ścieżka do pliku</param>
/// <param name="cancellationToken">Token anulowania operacji</param>
/// <returns>Załadowany obiekt ObfFile lub null</returns>
Task<ObfFile?> LoadObfAsync(string? filePath, CancellationToken cancellationToken = default);
}
50 changes: 48 additions & 2 deletions ChatAAC/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using ChatAAC.Models.Obf;
using ChatAAC.Services;
using ChatAAC.ViewModels;
using ChatAAC.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ReactiveUI;

namespace ChatAAC;
Expand All @@ -27,14 +31,56 @@ public override void OnFrameworkInitializationCompleted()
// Set the current culture to the system's culture
Lang.Resources.Culture = CultureInfo.CurrentCulture;

// Możesz użyć ServiceProvider do utworzenia MainViewModel
var services = new ServiceCollection();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();

// Check if the application is running in Classic Desktop mode
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
DataContext = serviceProvider.GetRequiredService<MainViewModel>()
};

base.OnFrameworkInitializationCompleted();

}
private void ConfigureServices(IServiceCollection services)
{
// Konfiguracja usług
services.AddLogging(configure =>
{
configure.AddConsole();
configure.AddDebug();
configure.SetMinimumLevel(LogLevel.Information);
});

// Dodaj Logger Factory
services.AddSingleton(LoggerFactory.Create(builder =>
{
builder
.AddConsole()
.AddDebug()
.SetMinimumLevel(LogLevel.Information);
}));

// Dodaj logger dla MainViewModel
services.AddSingleton(typeof(ILogger<MainViewModel>),
sp => sp.GetRequiredService<ILoggerFactory>().CreateLogger<MainViewModel>());

// Dodaj MainViewModel jako usługę
services.AddTransient<MainViewModel>();
services.AddTransient<BoardLoaderService>();
services.AddSingleton<HistoryService>();
services.AddSingleton<ObfLoader>();





// Dodaj pozostałe wymagane usługi
// np. services.AddSingleton<IDialogService, DialogService>();
}

/// <summary>
Expand Down Expand Up @@ -104,7 +150,7 @@ private void OnSettingsClick(object? sender, EventArgs e)
/// (e.g., from Polish to English) to reload or rebind localizable
/// text resources in the MainWindow.
/// </remarks>
public static void RefreshMainWindow()
private static void RefreshMainWindow()
{
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: not null } desktop)
// Option A: Force the window to redraw itself
Expand Down
7 changes: 7 additions & 0 deletions ChatAAC/ChatAAC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<PackageReference Include="DotNet.Bundle" Version="0.9.13" />
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.2" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.36.1" />
<PackageReference Include="OllamaSharp" Version="5.0.6" />
<PackageReference Include="ReactiveUI" Version="20.1.63"/>
Expand Down Expand Up @@ -76,4 +79,8 @@
<SubType>Code</SubType>
</Compile>
</ItemGroup>

<ItemGroup>
<Folder Include="Interfaces\" />
</ItemGroup>
</Project>
7 changes: 4 additions & 3 deletions ChatAAC/Models/Obf/ObfLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using ChatAAC.Lang;

namespace ChatAAC.Models.Obf;

public static partial class ObfLoader
public partial class ObfLoader
{
private static readonly HttpClient HttpClient = new();

Expand All @@ -30,9 +31,9 @@ public static partial class ObfLoader
/// An asynchronous task that returns an ObfFile object if successful.
/// Returns null if loading or processing fails.
/// </returns>
public static async Task<ObfFile?> LoadObfAsync(string? filePath)
public static async Task<ObfFile?> LoadObfAsync(string? filePath, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(filePath))
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
{
LogError("The OBF file path is empty.");
return null;
Expand Down
3 changes: 3 additions & 0 deletions ChatAAC/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using Avalonia;
using Avalonia.ReactiveUI;
using ChatAAC.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ChatAAC;

Expand Down
Loading

0 comments on commit af7cda7

Please sign in to comment.