From 22e67c95ccb7e378b506b48b502116f3cd18d4e0 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 18 Nov 2024 18:03:27 -0800 Subject: [PATCH] refactor: rename AccountsManager to Accounts plz stop with the managers --- src/{account_manager.rs => accounts/mod.rs} | 32 +++++++------------ src/accounts/route.rs | 13 ++++++++ src/app.rs | 19 ++++++----- src/lib.rs | 2 +- src/nav.rs | 2 +- src/route.rs | 2 +- src/timeline/route.rs | 4 +-- src/ui/{account_management.rs => accounts.rs} | 26 +++++++-------- src/ui/mod.rs | 4 +-- src/ui/side_panel.rs | 2 +- src/ui_preview/main.rs | 6 ++-- 11 files changed, 59 insertions(+), 53 deletions(-) rename src/{account_manager.rs => accounts/mod.rs} (91%) create mode 100644 src/accounts/route.rs rename src/ui/{account_management.rs => accounts.rs} (91%) diff --git a/src/account_manager.rs b/src/accounts/mod.rs similarity index 91% rename from src/account_manager.rs rename to src/accounts/mod.rs index 40d8dc7..d0e81f2 100644 --- a/src/account_manager.rs +++ b/src/accounts/mod.rs @@ -2,7 +2,6 @@ use std::cmp::Ordering; use enostr::{FilledKeypair, FullKeypair, Keypair}; use nostrdb::Ndb; -use serde::{Deserialize, Serialize}; use crate::{ column::Columns, @@ -12,34 +11,25 @@ use crate::{ storage::{KeyStorageResponse, KeyStorageType}, ui::{ account_login_view::{AccountLoginResponse, AccountLoginView}, - account_management::{AccountsView, AccountsViewResponse}, + accounts::{AccountsView, AccountsViewResponse}, }, unknowns::SingleUnkIdAction, + user_account::UserAccount, }; use tracing::{error, info}; -pub use crate::user_account::UserAccount; +mod route; + +pub use route::{AccountsRoute, AccountsRouteResponse}; /// The interface for managing the user's accounts. /// Represents all user-facing operations related to account management. -pub struct AccountManager { +pub struct Accounts { currently_selected_account: Option, accounts: Vec, key_store: KeyStorageType, } -// TODO(jb55): move to accounts/route.rs -pub enum AccountsRouteResponse { - Accounts(AccountsViewResponse), - AddAccount(AccountLoginResponse), -} - -#[derive(Debug, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)] -pub enum AccountsRoute { - Accounts, - AddAccount, -} - /// Render account management views from a route #[allow(clippy::too_many_arguments)] pub fn render_accounts_route( @@ -48,7 +38,7 @@ pub fn render_accounts_route( col: usize, columns: &mut Columns, img_cache: &mut ImageCache, - accounts: &mut AccountManager, + accounts: &mut Accounts, login_state: &mut AcquireKeyState, route: AccountsRoute, ) -> SingleUnkIdAction { @@ -84,7 +74,7 @@ pub fn render_accounts_route( } pub fn process_accounts_view_response( - manager: &mut AccountManager, + manager: &mut Accounts, response: AccountsViewResponse, router: &mut Router, ) { @@ -101,7 +91,7 @@ pub fn process_accounts_view_response( } } -impl AccountManager { +impl Accounts { pub fn new(key_store: KeyStorageType) -> Self { let accounts = if let KeyStorageResponse::ReceivedResult(res) = key_store.get_keys() { res.unwrap_or_default() @@ -110,7 +100,7 @@ impl AccountManager { }; let currently_selected_account = get_selected_index(&accounts, &key_store); - AccountManager { + Accounts { currently_selected_account, accounts, key_store, @@ -224,7 +214,7 @@ fn get_selected_index(accounts: &[UserAccount], keystore: &KeyStorageType) -> Op } pub fn process_login_view_response( - manager: &mut AccountManager, + manager: &mut Accounts, response: AccountLoginResponse, ) -> SingleUnkIdAction { let r = match response { diff --git a/src/accounts/route.rs b/src/accounts/route.rs new file mode 100644 index 0000000..69ce127 --- /dev/null +++ b/src/accounts/route.rs @@ -0,0 +1,13 @@ +use super::{AccountLoginResponse, AccountsViewResponse}; +use serde::{Deserialize, Serialize}; + +pub enum AccountsRouteResponse { + Accounts(AccountsViewResponse), + AddAccount(AccountLoginResponse), +} + +#[derive(Debug, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)] +pub enum AccountsRoute { + Accounts, + AddAccount, +} diff --git a/src/app.rs b/src/app.rs index 7f0493d..7cab222 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,10 +1,10 @@ use crate::{ - account_manager::AccountManager, + accounts::{Accounts, AccountsRoute}, app_creation::setup_cc, app_size_handler::AppSizeHandler, app_style::user_requested_visuals_change, args::Args, - column::Columns, + column::{Column, Columns}, draft::Drafts, filter::FilterState, frame_history::FrameHistory, @@ -13,6 +13,7 @@ use crate::{ notecache::NoteCache, notes_holder::NotesHolderStorage, profile::Profile, + route::Route, storage::{self, DataPath, DataPathType, Directory, FileKeyStorage, KeyStorageType}, subscriptions::{SubKind, Subscriptions}, support::Support, @@ -57,7 +58,7 @@ pub struct Damus { pub threads: NotesHolderStorage, pub profiles: NotesHolderStorage, pub img_cache: ImageCache, - pub accounts: AccountManager, + pub accounts: Accounts, pub subscriptions: Subscriptions, pub app_rect_handler: AppSizeHandler, pub support: Support, @@ -415,7 +416,7 @@ impl Damus { KeyStorageType::None }; - let mut accounts = AccountManager::new(keystore); + let mut accounts = Accounts::new(keystore); let num_keys = parsed_args.keys.len(); @@ -489,7 +490,9 @@ impl Damus { let debug = parsed_args.debug; if columns.columns().is_empty() { - columns.new_column_picker(); + columns.add_column(Column::new(vec![Route::Accounts( + AccountsRoute::AddAccount, + )])); } let app_rect_handler = AppSizeHandler::new(&path); @@ -535,11 +538,11 @@ impl Damus { &mut self.img_cache } - pub fn accounts(&self) -> &AccountManager { + pub fn accounts(&self) -> &Accounts { &self.accounts } - pub fn accounts_mut(&mut self) -> &mut AccountManager { + pub fn accounts_mut(&mut self) -> &mut Accounts { &mut self.accounts } @@ -603,7 +606,7 @@ impl Damus { &config, ) .expect("ndb"), - accounts: AccountManager::new(KeyStorageType::None), + accounts: Accounts::new(KeyStorageType::None), frame_history: FrameHistory::default(), view_state: ViewState::default(), diff --git a/src/lib.rs b/src/lib.rs index e93d35d..7771b8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ mod error; //mod note; //mod block; mod abbrev; -pub mod account_manager; +pub mod accounts; mod actionbar; pub mod app_creation; mod app_size_handler; diff --git a/src/nav.rs b/src/nav.rs index 0421d92..b6f8338 100644 --- a/src/nav.rs +++ b/src/nav.rs @@ -1,5 +1,5 @@ use crate::{ - account_manager::render_accounts_route, + accounts::render_accounts_route, app_style::{get_font_size, NotedeckTextStyle}, column::Columns, fonts::NamedFontFamily, diff --git a/src/route.rs b/src/route.rs index ed5e51f..9aec2bb 100644 --- a/src/route.rs +++ b/src/route.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use std::fmt::{self}; use crate::{ - account_manager::AccountsRoute, + accounts::AccountsRoute, column::Columns, timeline::{TimelineId, TimelineRoute}, ui::{ diff --git a/src/timeline/route.rs b/src/timeline/route.rs index a922af6..61fc132 100644 --- a/src/timeline/route.rs +++ b/src/timeline/route.rs @@ -1,5 +1,5 @@ use crate::{ - account_manager::AccountManager, + accounts::Accounts, column::Columns, draft::Drafts, imgcache::ImageCache, @@ -51,7 +51,7 @@ pub fn render_timeline_route( unknown_ids: &mut UnknownIds, note_cache: &mut NoteCache, threads: &mut NotesHolderStorage, - accounts: &mut AccountManager, + accounts: &mut Accounts, route: TimelineRoute, col: usize, textmode: bool, diff --git a/src/ui/account_management.rs b/src/ui/accounts.rs similarity index 91% rename from src/ui/account_management.rs rename to src/ui/accounts.rs index b68d4c5..0e0d71a 100644 --- a/src/ui/account_management.rs +++ b/src/ui/accounts.rs @@ -1,7 +1,7 @@ use crate::colors::PINK; use crate::imgcache::ImageCache; use crate::{ - account_manager::AccountManager, + accounts::Accounts, route::{Route, Router}, ui::{Preview, PreviewConfig, View}, Damus, @@ -13,7 +13,7 @@ use super::profile::preview::SimpleProfilePreview; pub struct AccountsView<'a> { ndb: &'a Ndb, - accounts: &'a AccountManager, + accounts: &'a Accounts, img_cache: &'a mut ImageCache, } @@ -31,7 +31,7 @@ enum ProfilePreviewOp { } impl<'a> AccountsView<'a> { - pub fn new(ndb: &'a Ndb, accounts: &'a AccountManager, img_cache: &'a mut ImageCache) -> Self { + pub fn new(ndb: &'a Ndb, accounts: &'a Accounts, img_cache: &'a mut ImageCache) -> Self { AccountsView { ndb, accounts, @@ -56,7 +56,7 @@ impl<'a> AccountsView<'a> { fn show_accounts( ui: &mut Ui, - account_manager: &AccountManager, + accounts: &Accounts, ndb: &Ndb, img_cache: &mut ImageCache, ) -> Option { @@ -71,8 +71,8 @@ impl<'a> AccountsView<'a> { return; }; - for i in 0..account_manager.num_accounts() { - let account_pubkey = account_manager + for i in 0..accounts.num_accounts() { + let account_pubkey = accounts .get_account(i) .map(|account| account.pubkey.bytes()); @@ -83,12 +83,12 @@ impl<'a> AccountsView<'a> { }; let profile = ndb.get_profile_by_pubkey(&txn, account_pubkey).ok(); - let is_selected = - if let Some(selected) = account_manager.get_selected_account_index() { - i == selected - } else { - false - }; + let is_selected = if let Some(selected) = accounts.get_selected_account_index() + { + i == selected + } else { + false + }; let profile_peview_view = { let width = ui.available_width(); @@ -217,7 +217,7 @@ fn selected_widget() -> impl egui::Widget { mod preview { use super::*; - use crate::{account_manager::process_accounts_view_response, test_data}; + use crate::{accounts::process_accounts_view_response, test_data}; pub struct AccountsPreview { app: Damus, diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 109df61..95e4afc 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,5 +1,5 @@ pub mod account_login_view; -pub mod account_management; +pub mod accounts; pub mod add_column; pub mod anim; pub mod mention; @@ -13,7 +13,7 @@ pub mod thread; pub mod timeline; pub mod username; -pub use account_management::AccountsView; +pub use accounts::AccountsView; pub use mention::Mention; pub use note::{NoteResponse, NoteView, PostReplyView, PostView}; pub use preview::{Preview, PreviewApp, PreviewConfig}; diff --git a/src/ui/side_panel.rs b/src/ui/side_panel.rs index 9f31142..ca3ee3a 100644 --- a/src/ui/side_panel.rs +++ b/src/ui/side_panel.rs @@ -2,7 +2,7 @@ use egui::{vec2, Color32, InnerResponse, Layout, Margin, Separator, Stroke, Widg use tracing::info; use crate::{ - account_manager::AccountsRoute, + accounts::AccountsRoute, colors, column::{Column, Columns}, imgcache::ImageCache, diff --git a/src/ui_preview/main.rs b/src/ui_preview/main.rs index 8be13a0..835d012 100644 --- a/src/ui_preview/main.rs +++ b/src/ui_preview/main.rs @@ -1,7 +1,7 @@ use notedeck::ui::{ - account_login_view::AccountLoginView, account_management::AccountsView, - add_column::AddColumnView, DesktopSidePanel, PostView, Preview, PreviewApp, PreviewConfig, - ProfilePic, ProfilePreview, RelayView, + account_login_view::AccountLoginView, accounts::AccountsView, add_column::AddColumnView, + DesktopSidePanel, PostView, Preview, PreviewApp, PreviewConfig, ProfilePic, ProfilePreview, + RelayView, }; use notedeck::{ app_creation::{generate_mobile_emulator_native_options, generate_native_options, setup_cc},