Skip to content

Commit

Permalink
wip flexible routing
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Sep 12, 2024
1 parent b4a8cdd commit 0c0a26d
Show file tree
Hide file tree
Showing 16 changed files with 550 additions and 490 deletions.
81 changes: 66 additions & 15 deletions src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,71 @@ pub struct AccountManager {
key_store: KeyStorageType,
}

// TODO(jb55): move to accounts/route.rs
pub enum AccountsRouteResponse {
Accounts(AccountsViewResponse),
AddAccount(AccountLoginResponse),
}


#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum AccountsRoute {
#[default]
Accounts,
AddAccount,
}

/// Render account management views from a route
pub fn render_accounts_route(
ndb: &Ndb,
img_cache: &mut ImageCache,
accounts: &AccountManager,
route: AccountsRoute,
) -> Option<NavResponse<AccountsViewResponse>> {
let resp = match route {
AccountsRoute::AccountManagement => {
AccountsView::new(ui, accounts, ndb, img_cache).ui()
.inner
.map(ManageAcountRouteResponse::AccountManagement)
}
AccountsRoute::AddAccount => AccountLoginView::new(login_state)
.ui(ui)
.inner
.map(ManageAcountRouteResponse::AddAccount),
};

if let Some(resp) = resp {
match resp {
ManageAcountRouteResponse::AccountManagement(response) => {
process_accounts_view_response(response, accounts, router);
}
ManageAcountRouteResponse::AddAccount(response) => {
process_login_view_response(accounts, response);
*login_state = Default::default();
router.go_back();
}
}
}
}

pub pub fn process_accounts_view_response(

Check failure on line 69 in src/account_manager.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `safe`, `unsafe`, or `use`, found keyword `pub`

Check failure on line 69 in src/account_manager.rs

View workflow job for this annotation

GitHub Actions / Clippy

expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `safe`, `unsafe`, or `use`, found keyword `pub`

Check failure on line 69 in src/account_manager.rs

View workflow job for this annotation

GitHub Actions / Test Suite

expected one of `(`, `async`, `const`, `default`, `extern`, `fn`, `safe`, `unsafe`, or `use`, found keyword `pub`
manager: &mut AccountManager,
response: AccountsViewResponse,
router: &mut Router<Route>,
) {
match response {
AccountManagementViewResponse::RemoveAccount(index) => {
manager.remove_account(index);
}
AccountManagementViewResponse::SelectAccount(index) => {
manager.select_account(index);
}
AccountManagementViewResponse::RouteToLogin => {
router.route_to(Route::add_account());
}
}
}

impl AccountManager {
pub fn new(currently_selected_account: Option<usize>, key_store: KeyStorageType) -> Self {
let accounts = if let KeyStorageResponse::ReceivedResult(res) = key_store.get_keys() {
Expand Down Expand Up @@ -122,21 +187,6 @@ impl AccountManager {
}
}

pub fn process_management_view_response_stateless(
manager: &mut AccountManager,
response: AccountManagementViewResponse,
) {
match response {
AccountManagementViewResponse::RemoveAccount(index) => {
manager.remove_account(index);
}
AccountManagementViewResponse::SelectAccount(index) => {
manager.select_account(index);
}
AccountManagementViewResponse::RouteToLogin => {}
}
}

pub fn process_login_view_response(manager: &mut AccountManager, response: AccountLoginResponse) {
match response {
AccountLoginResponse::CreateNew => {
Expand All @@ -147,3 +197,4 @@ pub fn process_login_view_response(manager: &mut AccountManager, response: Accou
}
}
}

20 changes: 8 additions & 12 deletions src/actionbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
column::Column,

Check warning on line 2 in src/actionbar.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `column::Column`
note::NoteRef,
notecache::NoteCache,
route::Route,
route::{Route, Router},
thread::{Thread, ThreadResult, Threads},
};
use enostr::{NoteId, RelayPool};
Expand Down Expand Up @@ -33,17 +33,15 @@ pub enum BarResult {
fn open_thread(
ndb: &Ndb,
txn: &Transaction,
column: &mut Column,
router: &mut Router<Route>,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
threads: &mut Threads,
selected_note: &[u8; 32],
) -> Option<BarResult> {
{
column
.routes_mut()
.push(Route::Thread(NoteId::new(selected_note.to_owned())));
column.navigating = true;
router.route_to(Route::thread(NoteId::new(selected_note.to_owned())));
router.navigating = true;
}

let root_id = crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note);
Expand Down Expand Up @@ -120,7 +118,7 @@ impl BarAction {
pub fn execute(
self,
ndb: &Ndb,
column: &mut Column,
router: &mut Router<Route>,
threads: &mut Threads,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
Expand All @@ -129,15 +127,13 @@ impl BarAction {
) -> Option<BarResult> {
match self {
BarAction::Reply => {
column
.routes_mut()
.push(Route::Reply(NoteId::new(replying_to.to_owned())));
column.navigating = true;
router.route_to(Route::reply(NoteId::new(replying_to.to_owned())));
router.navigating = true;
None
}

BarAction::OpenThread => {
open_thread(ndb, txn, column, note_cache, pool, threads, replying_to)
open_thread(ndb, txn, router, note_cache, pool, threads, replying_to)
}
}
}
Expand Down
Loading

0 comments on commit 0c0a26d

Please sign in to comment.