feat: Out-of-Box Experience (#12)

* refactor app view

* feat: onboarding

* add back buttons in onboarding
This commit is contained in:
reya
2025-03-25 12:34:39 +07:00
committed by GitHub
parent e15cbcc22c
commit 00cf7792e5
34 changed files with 1680 additions and 1920 deletions

View File

@@ -1,6 +1,7 @@
pub const APP_NAME: &str = "Coop";
pub const APP_ID: &str = "su.reya.coop";
pub const KEYRING_SERVICE: &str = "Coop Safe Storage";
pub const CLIENT_KEYRING: &str = "Coop Client Keys";
pub const MASTER_KEYRING: &str = "Coop Master Keys";
@@ -25,9 +26,3 @@ pub const IMAGE_SERVICE: &str = "https://wsrv.nl";
/// NIP96 Media Server
pub const NIP96_SERVER: &str = "https://nostrmedia.com";
/// Updater Public Key
pub const UPDATER_PUBKEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDkxM0EzQTQyRTBBMENENTYKUldSV3phRGdRam82a1dtU0JqYll4VnBaVUpSWUxCWlVQbnRkUnNERS96MzFMWDhqNW5zOXplMEwK";
/// Updater Server URL
pub const UPDATER_URL: &str =
"https://cdn.crabnebula.app/update/lume/coop/{{target}}-{{arch}}/{{current_version}}";

View File

@@ -1,37 +1,20 @@
use constants::{ALL_MESSAGES_SUB_ID, APP_ID};
use dirs::config_dir;
use nostr_sdk::prelude::*;
use smol::lock::Mutex;
use paths::nostr_file;
use std::{
fs,
sync::{Arc, OnceLock},
time::Duration,
};
use std::{sync::OnceLock, time::Duration};
pub mod constants;
pub mod paths;
/// Nostr Client
static CLIENT: OnceLock<Client> = OnceLock::new();
/// Current App Name
static APP_NAME: OnceLock<Arc<str>> = OnceLock::new();
/// NIP-4e: Device Keys, used for encryption
static DEVICE_KEYS: Mutex<Option<Arc<dyn NostrSigner>>> = Mutex::new(None);
/// NIP-4e: Device Name, used for display purposes
static DEVICE_NAME: Mutex<Option<Arc<String>>> = Mutex::new(None);
static CLIENT_KEYS: OnceLock<Keys> = OnceLock::new();
/// Nostr Client instance
pub fn get_client() -> &'static Client {
CLIENT.get_or_init(|| {
// Setup app data folder
let config_dir = config_dir().expect("Config directory not found");
let app_dir = config_dir.join(APP_ID);
// Create app directory if it doesn't exist
_ = fs::create_dir_all(&app_dir);
// Setup database
let lmdb = NostrLMDB::open(app_dir.join("nostr")).expect("Database is NOT initialized");
let db_path = nostr_file();
let lmdb = NostrLMDB::open(db_path).expect("Database is NOT initialized");
// Client options
let opts = Options::new()
@@ -45,60 +28,7 @@ pub fn get_client() -> &'static Client {
})
}
/// Get app name
pub fn get_app_name() -> &'static str {
APP_NAME.get_or_init(|| {
Arc::from(format!(
"Coop on {} ({})",
whoami::distro(),
whoami::devicename()
))
})
}
/// Get device keys
pub async fn get_device_keys() -> Option<Arc<dyn NostrSigner>> {
let guard = DEVICE_KEYS.lock().await;
guard.clone()
}
/// Set device keys
pub async fn set_device_keys<T>(signer: Arc<T>)
where
T: NostrSigner + 'static,
{
DEVICE_KEYS.lock().await.replace(signer);
// Re-subscribe to all messages
smol::spawn(async move {
let client = get_client();
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
if let Ok(signer) = client.signer().await {
let public_key = signer.get_public_key().await.unwrap();
// Create a filter for getting all gift wrapped events send to current user
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let id = SubscriptionId::new(ALL_MESSAGES_SUB_ID);
_ = client.unsubscribe(&id);
_ = client.subscribe_with_id(id, filter, Some(opts)).await;
}
})
.await;
}
/// Set master's device name
pub async fn set_device_name(name: &str) {
let mut guard = DEVICE_NAME.lock().await;
if guard.is_none() {
guard.replace(Arc::new(name.to_owned()));
}
}
/// Get master's device name
pub fn get_device_name() -> Arc<String> {
let guard = DEVICE_NAME.lock_blocking();
guard.clone().unwrap_or(Arc::new("Main Device".into()))
/// Client Keys
pub fn get_client_keys() -> &'static Keys {
CLIENT_KEYS.get_or_init(Keys::generate)
}

View File

@@ -0,0 +1,76 @@
use std::path::PathBuf;
use std::sync::OnceLock;
/// Returns the path to the user's home directory.
pub fn home_dir() -> &'static PathBuf {
static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
HOME_DIR.get_or_init(|| dirs::home_dir().expect("failed to determine home directory"))
}
/// Returns the path to the configuration directory used by Coop.
pub fn config_dir() -> &'static PathBuf {
static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
CONFIG_DIR.get_or_init(|| {
if cfg!(target_os = "windows") {
return dirs::config_dir()
.expect("failed to determine RoamingAppData directory")
.join("Coop");
}
if cfg!(any(target_os = "linux", target_os = "freebsd")) {
return if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
flatpak_xdg_config.into()
} else {
dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
}
.join("coop");
}
home_dir().join(".config").join("coop")
})
}
/// Returns the path to the support directory used by Coop.
pub fn support_dir() -> &'static PathBuf {
static SUPPORT_DIR: OnceLock<PathBuf> = OnceLock::new();
SUPPORT_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
return home_dir().join("Library/Application Support/Coop");
}
if cfg!(any(target_os = "linux", target_os = "freebsd")) {
return if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
flatpak_xdg_data.into()
} else {
dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
}
.join("coop");
}
if cfg!(target_os = "windows") {
return dirs::data_local_dir()
.expect("failed to determine LocalAppData directory")
.join("coop");
}
config_dir().clone()
})
}
/// Returns the path to the `nostr` file.
pub fn nostr_file() -> &'static PathBuf {
static NOSTR_FILE: OnceLock<PathBuf> = OnceLock::new();
NOSTR_FILE.get_or_init(|| support_dir().join("nostr"))
}
/// Returns the path to the `client.dat` file.
pub fn client_file() -> &'static PathBuf {
static CLIENT_FILE: OnceLock<PathBuf> = OnceLock::new();
CLIENT_FILE.get_or_init(|| support_dir().join("client.dat"))
}
/// Returns the path to the `device.dat` file.
pub fn device_file() -> &'static PathBuf {
static DEVICE_FILE: OnceLock<PathBuf> = OnceLock::new();
DEVICE_FILE.get_or_init(|| support_dir().join("device.dat"))
}