//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.Client.Services.Database;
///
/// Class to manage the state of the DbService that others can subscribe to events for.
///
public sealed class DbServiceState
{
private DatabaseState _currentState = new();
///
/// Subscribe to this event to get notified when the state of the database changes.
///
public event EventHandler StateChanged = (sender, e) => { };
///
/// Database status enum.
///
public enum DatabaseStatus
{
///
/// Database not initialized (yet).
///
Uninitialized,
///
/// Database is loading from server.
///
Loading,
///
/// Database is being created.
///
Creating,
///
/// Database failed to decrypt. No data is accessible.
///
DecryptionFailed,
///
/// The loaded vault version is not recognized by the current client (most likely a new version).
///
VaultVersionUnrecognized,
///
/// Database has been decrypted but has pending migrations and needs to be updated.
///
PendingMigrations,
///
/// Database is ready but no task is currently in progress.
///
Ready,
///
/// Database is saving to server.
///
SavingToServer,
///
/// Database has local changes pending background sync to server.
/// Used for non-blocking mutations where UI doesn't wait for server sync.
///
BackgroundSyncPending,
}
///
/// Gets the current state of the database.
///
public DatabaseState CurrentState
{
get => _currentState;
private set
{
if (_currentState != value)
{
_currentState = value;
OnStateChanged(_currentState);
}
}
}
///
/// Update the state of the database.
///
/// New status.
public void UpdateState(DatabaseStatus status)
{
CurrentState = new DatabaseState
{
Status = status,
Message = string.Empty,
LastUpdated = DateTime.Now,
};
}
///
/// Update the state of the database with an additional message.
///
/// New status.
/// Status message.
public void UpdateState(DatabaseStatus status, string message)
{
CurrentState = new DatabaseState
{
Status = status,
Message = message,
LastUpdated = DateTime.Now,
};
}
///
/// OnStateChanged event handler.
///
/// The new state.
private void OnStateChanged(DatabaseState newState)
{
StateChanged?.Invoke(this, newState);
}
///
/// Database state class.
///
public class DatabaseState
{
///
/// Gets or sets the current status of the database.
///
public DatabaseStatus Status { get; set; } = DatabaseStatus.Uninitialized;
///
/// Gets or sets the message associated with the current status.
///
public string Message { get; set; } = string.Empty;
///
/// Gets or sets the last time the state was updated.
///
public DateTime LastUpdated { get; set; } = DateTime.Now;
///
/// Returns true if the database state represents an initialized state.
///
/// Bool.
public bool IsInitialized()
{
return Status is DatabaseStatus.Ready or DatabaseStatus.SavingToServer or DatabaseStatus.BackgroundSyncPending;
}
}
}