use std::{
sync::{Arc, RwLock},
time::Duration,
};
use matrix_sdk_base::crypto::{RoomKeyImportResult, store::types::RoomKeyCounts};
use tokio::sync::broadcast;
use crate::utils::ChannelObservable;
#[cfg(doc)]
use crate::{
Client,
encryption::{backups::Backups, secret_storage::SecretStore},
};
#[derive(Clone, Debug)]
pub enum UploadState {
Idle,
Uploading(RoomKeyCounts),
Error,
Done,
}
pub(crate) struct BackupClientState {
pub(super) upload_delay: Arc<RwLock<Duration>>,
pub(crate) upload_progress: ChannelObservable<UploadState>,
pub(super) global_state: ChannelObservable<BackupState>,
pub(super) room_keys_broadcaster: broadcast::Sender<RoomKeyImportResult>,
pub(super) backup_exists_on_server: RwLock<Option<bool>>,
}
impl BackupClientState {
pub(crate) fn set_backup_exists_on_server(&self, exists_on_server: bool) {
*self.backup_exists_on_server.write().unwrap() = Some(exists_on_server);
}
pub(crate) fn backup_exists_on_server(&self) -> Option<bool> {
*self.backup_exists_on_server.read().unwrap()
}
pub(crate) fn clear_backup_exists_on_server(&self) {
*self.backup_exists_on_server.write().unwrap() = None;
}
}
const DEFAULT_BACKUP_UPLOAD_DELAY: Duration = Duration::from_millis(100);
impl Default for BackupClientState {
fn default() -> Self {
Self {
upload_delay: RwLock::new(DEFAULT_BACKUP_UPLOAD_DELAY).into(),
upload_progress: ChannelObservable::new(UploadState::Idle),
global_state: Default::default(),
room_keys_broadcaster: broadcast::Sender::new(100),
backup_exists_on_server: RwLock::new(None),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BackupState {
#[default]
Unknown,
Creating,
Enabling,
Resuming,
Enabled,
Downloading,
Disabling,
}