Files
WorkFlowCheck/src/WorkFlowCheck.MAUI/MauiProgram.cs
T
2025-02-06 16:06:09 +01:00

61 lines
2.5 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using WorkFlowCheck.MAUI.Services;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.DependencyInjection;
using CommunityToolkit.Maui;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.Mapper;
namespace WorkFlowCheck.MAUI
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.Services.AddAutoMapper(typeof(MapperProfile));
// appsettings.json beállítása
string jsonFilePath = Path.Combine(FileSystem.AppDataDirectory, "appsettings.json");
// Másold a fájlt a Raw erőforrásokból az AppDataDirectory-ba, ha még nincs ott
if (!File.Exists(jsonFilePath))
{
using var stream = FileSystem.OpenAppPackageFileAsync("appsettings.json").Result;
using var reader = new StreamReader(stream);
File.WriteAllText(jsonFilePath, reader.ReadToEnd());
}
var configuration = new ConfigurationBuilder().SetBasePath(FileSystem.AppDataDirectory) // A konfigurációs fájl betöltése az alkalmazás adat könyvtárából
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
builder.Services.AddHttpClient<UserService>(client =>
{
var apiUrl = configuration["ApiBaseUrl"]; // API-cím betöltése a konfigurációból
client.BaseAddress = new Uri(apiUrl);
});
builder.Services.AddDbContext<AppDbContext>();
builder.Services.AddSingleton<IConfiguration>(configuration);
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<ISyncService, SyncService>();
builder.Services.AddTransient<MainPage>();
builder.UseMauiApp<App>().ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
}).UseMauiCommunityToolkit();
#if DEBUG
builder.Logging.AddDebug();
#endif
// HttpClient regisztrálása az alap API-címmel
var app = builder.Build();
// Szolgáltató tárolása globálisan
ServiceProvider = app.Services;
return app;
}
public static IServiceProvider ServiceProvider { get; private set; }
}
}