Rewrite using Relm4
Yes, this is a monster commit but at this stage I'm the only one working on this project anyway. Further commits will follow Best Practises™ again.
This commit is contained in:
parent
d25fdd0a32
commit
4d4b7eb1c7
43 changed files with 2159 additions and 1248 deletions
82
src/persist/data_manager.rs
Normal file
82
src/persist/data_manager.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
use std::env::var_os;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use gtk4::gdk::Texture;
|
||||
|
||||
use crate::persist::common::concat_os_str;
|
||||
use crate::persist::file_system_manager::FileSystemManager;
|
||||
use crate::persist::sqlite_manager::SqliteManager;
|
||||
use crate::views::overview::{FilmOverview, SeriesOverview};
|
||||
|
||||
|
||||
|
||||
static SQLITE_MANAGER: OnceLock<SqliteManager> = OnceLock::new();
|
||||
static FILE_SYSTEM_MANAGER: OnceLock<FileSystemManager> = OnceLock::new();
|
||||
|
||||
macro_rules! sqlite_manager {
|
||||
() => {
|
||||
SQLITE_MANAGER
|
||||
.get()
|
||||
.expect("SQLite manager should be initialized")
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! fs_manager {
|
||||
() => {
|
||||
FILE_SYSTEM_MANAGER
|
||||
.get()
|
||||
.expect("File system manager should be initialized")
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub struct DataManager {}
|
||||
|
||||
impl DataManager {
|
||||
pub async fn init() -> Result<(), DataManagerError> {
|
||||
let data_dir = match var_os("XDG_DATA_HOME") {
|
||||
Some(xdg_home_data_dir) => concat_os_str!(xdg_home_data_dir, "/zoodex"),
|
||||
None => {
|
||||
let home_dir = var_os("HOME").ok_or(DataManagerError::NoHomeDir)?;
|
||||
concat_os_str!(home_dir, "/.local/share/zoodex")
|
||||
}
|
||||
};
|
||||
|
||||
let sqlite_manager = SqliteManager::new(data_dir.as_os_str()).await?;
|
||||
SQLITE_MANAGER
|
||||
.set(sqlite_manager)
|
||||
.expect("SQLite manager should not already be initialized");
|
||||
|
||||
let fs_manager = FileSystemManager::new(data_dir.as_os_str());
|
||||
FILE_SYSTEM_MANAGER
|
||||
.set(fs_manager)
|
||||
.expect("File system manager should not already be initialized");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn films_overview() -> Result<Vec<FilmOverview>, DataManagerError> {
|
||||
sqlite_manager!().films_overview().await
|
||||
}
|
||||
|
||||
pub async fn series_overview() -> Result<Vec<SeriesOverview>, DataManagerError> {
|
||||
sqlite_manager!().series_overview().await
|
||||
}
|
||||
|
||||
pub async fn poster(uuid: &str) -> Result<Option<Texture>, DataManagerError> {
|
||||
fs_manager!().poster(uuid).await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum DataManagerError {
|
||||
NoHomeDir,
|
||||
CannotOpenSharedDB,
|
||||
UnknownSharedDBError,
|
||||
CannotOpenLocalDB,
|
||||
UnknownLocalDBError,
|
||||
UnknownTextureError,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue