decouple RelayView UI from state mutation

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-07-01 15:09:10 -04:00
parent 10c4ac80a1
commit b41f4c3359
6 changed files with 53 additions and 115 deletions

View File

@@ -315,25 +315,21 @@ impl Accounts {
self.cache.get(pubkey).and_then(|r| r.key.to_full()) self.cache.get(pubkey).and_then(|r| r.key.to_full())
} }
pub fn add_advertised_relay(&mut self, relay_to_add: &str, pool: &mut RelayPool) { pub fn process_relay_action(
&mut self,
ctx: &egui::Context,
pool: &mut RelayPool,
action: RelayAction,
) {
let acc = self.cache.selected_mut(); let acc = self.cache.selected_mut();
modify_advertised_relays( modify_advertised_relays(&acc.key, action, pool, &self.relay_defaults, &mut acc.data);
&acc.key,
RelayAction::Add(relay_to_add.to_owned()),
pool,
&self.relay_defaults,
&mut acc.data,
);
}
pub fn remove_advertised_relay(&mut self, relay_to_remove: &str, pool: &mut RelayPool) { update_relay_configuration(
let acc = self.cache.selected_mut();
modify_advertised_relays(
&acc.key,
RelayAction::Remove(relay_to_remove.to_owned()),
pool, pool,
&self.relay_defaults, &self.relay_defaults,
&mut acc.data, &acc.key.pubkey,
&acc.data,
create_wakeup(ctx),
); );
} }
} }

View File

@@ -34,6 +34,7 @@ mod wallet;
mod zaps; mod zaps;
pub use account::accounts::{AccountData, Accounts}; pub use account::accounts::{AccountData, Accounts};
pub use account::relay::RelayAction;
pub use account::FALLBACK_PUBKEY; pub use account::FALLBACK_PUBKEY;
pub use app::{App, AppAction, Notedeck}; pub use app::{App, AppAction, Notedeck};
pub use args::Args; pub use args::Args;

View File

@@ -20,7 +20,6 @@ mod nav;
mod post; mod post;
mod profile; mod profile;
mod profile_state; mod profile_state;
pub mod relay_pool_manager;
mod route; mod route;
mod search; mod search;
mod subscriptions; mod subscriptions;

View File

@@ -6,7 +6,6 @@ use crate::{
decks::{Deck, DecksAction, DecksCache}, decks::{Deck, DecksAction, DecksCache},
profile::{ProfileAction, SaveProfileChanges}, profile::{ProfileAction, SaveProfileChanges},
profile_state::ProfileState, profile_state::ProfileState,
relay_pool_manager::RelayPoolManager,
route::{Route, Router, SingletonRouter}, route::{Route, Router, SingletonRouter},
timeline::{ timeline::{
route::{render_thread_route, render_timeline_route}, route::{render_thread_route, render_timeline_route},
@@ -31,9 +30,8 @@ use crate::{
use egui_nav::{Nav, NavAction, NavResponse, NavUiType, Percent, PopupResponse, PopupSheet}; use egui_nav::{Nav, NavAction, NavResponse, NavUiType, Percent, PopupResponse, PopupSheet};
use nostrdb::Transaction; use nostrdb::Transaction;
use notedeck::{ use notedeck::{
get_current_default_msats, get_current_wallet, AppContext, NoteAction, NoteContext, get_current_default_msats, get_current_wallet, AppContext, NoteAction, NoteContext, RelayAction,
}; };
use notedeck_ui::View;
use tracing::error; use tracing::error;
/// The result of processing a nav response /// The result of processing a nav response
@@ -59,6 +57,7 @@ pub enum RenderNavAction {
ProfileAction(ProfileAction), ProfileAction(ProfileAction),
SwitchingAction(SwitchingAction), SwitchingAction(SwitchingAction),
WalletAction(WalletAction), WalletAction(WalletAction),
RelayAction(RelayAction),
} }
pub enum SwitchingAction { pub enum SwitchingAction {
@@ -334,7 +333,6 @@ fn process_render_nav_action(
let router_action = match action { let router_action = match action {
RenderNavAction::Back => Some(RouterAction::GoBack), RenderNavAction::Back => Some(RouterAction::GoBack),
RenderNavAction::PfpClicked => Some(RouterAction::PfpClicked), RenderNavAction::PfpClicked => Some(RouterAction::PfpClicked),
RenderNavAction::RemoveColumn => { RenderNavAction::RemoveColumn => {
let kinds_to_pop = app.columns_mut(ctx.accounts).delete_column(col); let kinds_to_pop = app.columns_mut(ctx.accounts).delete_column(col);
@@ -346,7 +344,6 @@ fn process_render_nav_action(
return Some(ProcessNavResult::SwitchOccurred); return Some(ProcessNavResult::SwitchOccurred);
} }
RenderNavAction::PostAction(new_post_action) => { RenderNavAction::PostAction(new_post_action) => {
let txn = Transaction::new(ctx.ndb).expect("txn"); let txn = Transaction::new(ctx.ndb).expect("txn");
match new_post_action.execute(ctx.ndb, &txn, ctx.pool, &mut app.drafts) { match new_post_action.execute(ctx.ndb, &txn, ctx.pool, &mut app.drafts) {
@@ -356,7 +353,6 @@ fn process_render_nav_action(
Some(RouterAction::GoBack) Some(RouterAction::GoBack)
} }
RenderNavAction::NoteAction(note_action) => { RenderNavAction::NoteAction(note_action) => {
let txn = Transaction::new(ctx.ndb).expect("txn"); let txn = Transaction::new(ctx.ndb).expect("txn");
@@ -378,7 +374,6 @@ fn process_render_nav_action(
ui, ui,
) )
} }
RenderNavAction::SwitchingAction(switching_action) => { RenderNavAction::SwitchingAction(switching_action) => {
if switching_action.process( if switching_action.process(
&mut app.timeline_cache, &mut app.timeline_cache,
@@ -399,6 +394,11 @@ fn process_render_nav_action(
RenderNavAction::WalletAction(wallet_action) => { RenderNavAction::WalletAction(wallet_action) => {
wallet_action.process(ctx.accounts, ctx.global_wallet) wallet_action.process(ctx.accounts, ctx.global_wallet)
} }
RenderNavAction::RelayAction(action) => {
ctx.accounts
.process_relay_action(ui.ctx(), ctx.pool, action);
None
}
}; };
if let Some(action) = router_action { if let Some(action) = router_action {
@@ -471,11 +471,9 @@ fn render_nav_body(
.accounts_action .accounts_action
.map(|f| RenderNavAction::SwitchingAction(SwitchingAction::Accounts(f))) .map(|f| RenderNavAction::SwitchingAction(SwitchingAction::Accounts(f)))
} }
Route::Relays => { Route::Relays => RelayView::new(ctx.pool, &mut app.view_state.id_string_map)
let manager = RelayPoolManager::new(ctx.pool); .ui(ui)
RelayView::new(ctx.accounts, manager, &mut app.view_state.id_string_map).ui(ui); .map(RenderNavAction::RelayAction),
None
}
Route::Reply(id) => { Route::Reply(id) => {
let txn = if let Ok(txn) = Transaction::new(ctx.ndb) { let txn = if let Ok(txn) = Transaction::new(ctx.ndb) {
txn txn

View File

@@ -1,60 +0,0 @@
use enostr::RelayPool;
pub use enostr::RelayStatus;
/// The interface to a RelayPool for UI components.
/// Represents all user-facing operations that can be performed for a user's relays
pub struct RelayPoolManager<'a> {
pub pool: &'a mut RelayPool,
}
pub struct RelayInfo<'a> {
pub relay_url: &'a str,
pub status: RelayStatus,
}
impl<'a> RelayPoolManager<'a> {
pub fn new(pool: &'a mut RelayPool) -> Self {
RelayPoolManager { pool }
}
pub fn get_relay_infos(&self) -> Vec<RelayInfo> {
self.pool
.relays
.iter()
.map(|relay| RelayInfo {
relay_url: relay.url(),
status: relay.status(),
})
.collect()
}
/// index of the Vec<RelayInfo> from get_relay_infos
pub fn remove_relay(&mut self, index: usize) {
if index < self.pool.relays.len() {
self.pool.relays.remove(index);
}
}
/// removes all specified relay indicies shown in get_relay_infos
pub fn remove_relays(&mut self, mut indices: Vec<usize>) {
indices.sort_unstable_by(|a, b| b.cmp(a));
indices.iter().for_each(|index| self.remove_relay(*index));
}
// FIXME - this is not ever called?
pub fn add_relay(&mut self, ctx: &egui::Context, relay_url: String) {
let _ = self.pool.add_url(relay_url, create_wakeup(ctx));
}
/// check whether a relay url is valid
pub fn is_valid_relay(&self, url: &str) -> bool {
self.pool.is_valid_url(url)
}
}
pub fn create_wakeup(ctx: &egui::Context) -> impl Fn() + Send + Sync + Clone + 'static {
let ctx = ctx.clone();
move || {
ctx.request_repaint();
}
}

View File

@@ -1,24 +1,23 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::relay_pool_manager::{RelayPoolManager, RelayStatus};
use crate::ui::{Preview, PreviewConfig}; use crate::ui::{Preview, PreviewConfig};
use egui::{Align, Button, CornerRadius, Frame, Id, Layout, Margin, Rgba, RichText, Ui, Vec2}; use egui::{Align, Button, CornerRadius, Frame, Id, Layout, Margin, Rgba, RichText, Ui, Vec2};
use enostr::RelayPool; use enostr::{RelayPool, RelayStatus};
use notedeck::{Accounts, NotedeckTextStyle}; use notedeck::{NotedeckTextStyle, RelayAction};
use notedeck_ui::app_images; use notedeck_ui::app_images;
use notedeck_ui::{colors::PINK, padding, View}; use notedeck_ui::{colors::PINK, padding};
use tracing::debug; use tracing::debug;
use super::widgets::styled_button; use super::widgets::styled_button;
pub struct RelayView<'a> { pub struct RelayView<'a> {
accounts: &'a mut Accounts, pool: &'a RelayPool,
manager: RelayPoolManager<'a>,
id_string_map: &'a mut HashMap<Id, String>, id_string_map: &'a mut HashMap<Id, String>,
} }
impl View for RelayView<'_> { impl RelayView<'_> {
fn ui(&mut self, ui: &mut egui::Ui) { pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<RelayAction> {
let mut action = None;
Frame::new() Frame::new()
.inner_margin(Margin::symmetric(10, 0)) .inner_margin(Margin::symmetric(10, 0))
.show(ui, |ui| { .show(ui, |ui| {
@@ -40,28 +39,23 @@ impl View for RelayView<'_> {
.auto_shrink([false; 2]) .auto_shrink([false; 2])
.show(ui, |ui| { .show(ui, |ui| {
if let Some(relay_to_remove) = self.show_relays(ui) { if let Some(relay_to_remove) = self.show_relays(ui) {
self.accounts action = Some(RelayAction::Remove(relay_to_remove));
.remove_advertised_relay(&relay_to_remove, self.manager.pool);
} }
ui.add_space(8.0); ui.add_space(8.0);
if let Some(relay_to_add) = self.show_add_relay_ui(ui) { if let Some(relay_to_add) = self.show_add_relay_ui(ui) {
self.accounts action = Some(RelayAction::Add(relay_to_add));
.add_advertised_relay(&relay_to_add, self.manager.pool);
} }
}); });
}); });
action
} }
} }
impl<'a> RelayView<'a> { impl<'a> RelayView<'a> {
pub fn new( pub fn new(pool: &'a RelayPool, id_string_map: &'a mut HashMap<Id, String>) -> Self {
accounts: &'a mut Accounts,
manager: RelayPoolManager<'a>,
id_string_map: &'a mut HashMap<Id, String>,
) -> Self {
RelayView { RelayView {
accounts, pool,
manager,
id_string_map, id_string_map,
} }
} }
@@ -73,7 +67,7 @@ impl<'a> RelayView<'a> {
/// Show the current relays and return a relay the user selected to delete /// Show the current relays and return a relay the user selected to delete
fn show_relays(&'a self, ui: &mut Ui) -> Option<String> { fn show_relays(&'a self, ui: &mut Ui) -> Option<String> {
let mut relay_to_remove = None; let mut relay_to_remove = None;
for (index, relay_info) in self.manager.get_relay_infos().iter().enumerate() { for (index, relay_info) in get_relay_infos(self.pool).iter().enumerate() {
ui.add_space(8.0); ui.add_space(8.0);
ui.vertical_centered_justified(|ui| { ui.vertical_centered_justified(|ui| {
relay_frame(ui).show(ui, |ui| { relay_frame(ui).show(ui, |ui| {
@@ -153,7 +147,7 @@ impl<'a> RelayView<'a> {
.id_string_map .id_string_map
.entry(id) .entry(id)
.or_insert_with(|| Self::RELAY_PREFILL.to_string()); .or_insert_with(|| Self::RELAY_PREFILL.to_string());
let is_enabled = self.manager.is_valid_relay(text_buffer); let is_enabled = self.pool.is_valid_url(text_buffer);
let text_edit = egui::TextEdit::singleline(text_buffer) let text_edit = egui::TextEdit::singleline(text_buffer)
.hint_text( .hint_text(
RichText::new("Enter the relay here") RichText::new("Enter the relay here")
@@ -254,6 +248,21 @@ fn get_connection_icon(status: RelayStatus) -> egui::Image<'static> {
} }
} }
struct RelayInfo<'a> {
pub relay_url: &'a str,
pub status: RelayStatus,
}
fn get_relay_infos(pool: &RelayPool) -> Vec<RelayInfo> {
pool.relays
.iter()
.map(|relay| RelayInfo {
relay_url: relay.url(),
status: relay.status(),
})
.collect()
}
// PREVIEWS // PREVIEWS
mod preview { mod preview {
@@ -277,12 +286,7 @@ mod preview {
fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) -> Option<AppAction> { fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) -> Option<AppAction> {
self.pool.try_recv(); self.pool.try_recv();
let mut id_string_map = HashMap::new(); let mut id_string_map = HashMap::new();
RelayView::new( RelayView::new(app.pool, &mut id_string_map).ui(ui);
app.accounts,
RelayPoolManager::new(&mut self.pool),
&mut id_string_map,
)
.ui(ui);
None None
} }
} }