Farbpalette vorladen lassen
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich cleanup and rework PDF switch to HTML export fix Colorsystem Farben für knöpfe hinzugefügt als anpassung button color button color primary and secondary change
This commit is contained in:
+60
-16
@@ -1,30 +1,74 @@
|
||||
using SQLite;
|
||||
using cfs_tracker.Models;
|
||||
using Microsoft.Maui.Storage;
|
||||
using cfs_tracker.Services;
|
||||
using SQLite;
|
||||
using System.Threading;
|
||||
|
||||
namespace cfs_tracker.Data;
|
||||
|
||||
public class AppDatabase
|
||||
public sealed class AppDatabase
|
||||
{
|
||||
private SQLiteAsyncConnection? _database;
|
||||
private readonly DatabaseLocationService _databaseLocationService;
|
||||
private readonly SemaphoreSlim _initLock = new(1, 1);
|
||||
|
||||
private SQLiteAsyncConnection? _connection;
|
||||
private bool _initialized;
|
||||
|
||||
public AppDatabase(DatabaseLocationService databaseLocationService)
|
||||
{
|
||||
_databaseLocationService = databaseLocationService;
|
||||
}
|
||||
|
||||
public async Task InitAsync()
|
||||
{
|
||||
if (_initialized) return;
|
||||
if (_initialized)
|
||||
return;
|
||||
|
||||
var dbPath = Path.Combine(FileSystem.Current.AppDataDirectory, "cfs_tracker.db3");
|
||||
_database = new SQLiteAsyncConnection(dbPath);
|
||||
await _initLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_initialized)
|
||||
return;
|
||||
|
||||
await _database.CreateTableAsync<ActivityItem>();
|
||||
await _database.CreateTableAsync<SymptomItem>();
|
||||
await _database.CreateTableAsync<HealthEntry>();
|
||||
await _database.CreateTableAsync<HealthEntrySymptom>();
|
||||
await _database.CreateTableAsync<ThemeSettingItem>();
|
||||
await _database.CreateTableAsync<AppearanceSetting>();
|
||||
var dbPath = _databaseLocationService.ResolveDatabasePath();
|
||||
var directory = Path.GetDirectoryName(dbPath);
|
||||
|
||||
_initialized = true;
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
_connection = new SQLiteAsyncConnection(dbPath);
|
||||
|
||||
await _connection.ExecuteAsync("PRAGMA foreign_keys = ON;");
|
||||
|
||||
await _connection.CreateTableAsync<ActivityItem>();
|
||||
await _connection.CreateTableAsync<SymptomItem>();
|
||||
await _connection.CreateTableAsync<HealthEntry>();
|
||||
await _connection.CreateTableAsync<HealthEntrySymptom>();
|
||||
await _connection.CreateTableAsync<AppearanceSetting>();
|
||||
await _connection.CreateTableAsync<NotificationSchedule>();
|
||||
|
||||
await _connection.ExecuteAsync(@"
|
||||
CREATE TABLE IF NOT EXISTS HealthEntrySymptom (
|
||||
Id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
HealthEntryId INTEGER NOT NULL,
|
||||
SymptomItemId INTEGER NOT NULL
|
||||
);");
|
||||
|
||||
await _connection.ExecuteAsync(
|
||||
"CREATE INDEX IF NOT EXISTS IX_HealthEntrySymptom_HealthEntryId ON HealthEntrySymptom(HealthEntryId);");
|
||||
|
||||
await _connection.ExecuteAsync(
|
||||
"CREATE INDEX IF NOT EXISTS IX_HealthEntrySymptom_SymptomItemId ON HealthEntrySymptom(SymptomItemId);");
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_initLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public SQLiteAsyncConnection Connection => _database ?? throw new InvalidOperationException("Database not initialized.");
|
||||
}
|
||||
public string CurrentPath => _databaseLocationService.ResolveDatabasePath();
|
||||
|
||||
public SQLiteAsyncConnection Connection =>
|
||||
_connection ?? throw new InvalidOperationException("Database has not been initialized yet.");
|
||||
}
|
||||
Reference in New Issue
Block a user