diff --git a/crates/bitwarden-uniffi/src/vault/collections.rs b/crates/bitwarden-uniffi/src/vault/collections.rs index 26e307ffc..3c7c3af3c 100644 --- a/crates/bitwarden-uniffi/src/vault/collections.rs +++ b/crates/bitwarden-uniffi/src/vault/collections.rs @@ -1,10 +1,10 @@ -use std::{collections::HashMap, sync::Arc}; +use std::sync::Arc; use bitwarden_collections::{ - collection::{Collection, CollectionView}, + collection::{Collection, CollectionId, CollectionView}, tree::{NodeItem, Tree}, }; -use uuid::Uuid; +use bitwarden_vault::collection_client::AncestorMap; use crate::Result; @@ -40,25 +40,34 @@ pub struct CollectionViewTree { } #[derive(uniffi::Object)] -#[allow(unused)] pub struct CollectionViewNodeItem { node_item: NodeItem, } #[uniffi::export] impl CollectionViewTree { - pub fn get_item_by_id(&self, collection_id: Uuid) -> Option> { + pub fn get_item_for_view( + &self, + collection_view: CollectionView, + ) -> Option> { self.tree - .get_item_by_id(collection_id) + .get_item_by_id(collection_view.id.unwrap_or_default().into()) .map(|n| Arc::new(CollectionViewNodeItem { node_item: n })) } pub fn get_root_items(&self) -> Vec> { self.tree - .nodes - .iter() - .filter(|n| n.parent_idx.is_none()) - .filter_map(|n| self.get_item_by_id(n.item_id)) + .get_root_items() + .into_iter() + .map(|n| Arc::new(CollectionViewNodeItem { node_item: n })) + .collect() + } + + pub fn get_flat_items(&self) -> Vec> { + self.tree + .get_flat_items() + .into_iter() + .map(|n| Arc::new(CollectionViewNodeItem { node_item: n })) .collect() } } @@ -77,7 +86,14 @@ impl CollectionViewNodeItem { self.node_item.children.clone() } - pub fn get_ancestors(&self) -> HashMap { - self.node_item.ancestors.clone() + pub fn get_ancestors(&self) -> AncestorMap { + AncestorMap { + ancestors: self + .node_item + .ancestors + .iter() + .map(|(&uuid, name)| (CollectionId::new(uuid), name.clone())) + .collect(), + } } } diff --git a/crates/bitwarden-vault/src/collection_client.rs b/crates/bitwarden-vault/src/collection_client.rs index 8698a53e9..f74cf7be2 100644 --- a/crates/bitwarden-vault/src/collection_client.rs +++ b/crates/bitwarden-vault/src/collection_client.rs @@ -1,10 +1,13 @@ +use std::collections::HashMap; + use bitwarden_collections::{ - collection::{Collection, CollectionView}, + collection::{Collection, CollectionId, CollectionView}, tree::{NodeItem, Tree}, }; use bitwarden_core::Client; +use serde::{Deserialize, Serialize}; #[cfg(feature = "wasm")] -use bitwarden_error::js_sys::Map; +use tsify::Tsify; #[cfg(feature = "wasm")] use wasm_bindgen::prelude::wasm_bindgen; @@ -56,6 +59,16 @@ pub struct CollectionViewNodeItem { node_item: NodeItem, } +#[cfg_attr( + feature = "wasm", + derive(Tsify, Serialize, Deserialize), + tsify(into_wasm_abi, from_wasm_abi) +)] +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] +pub struct AncestorMap { + pub ancestors: HashMap, +} + #[cfg_attr(feature = "wasm", wasm_bindgen)] impl CollectionViewNodeItem { pub fn get_item(&self) -> CollectionView { @@ -70,21 +83,21 @@ impl CollectionViewNodeItem { self.node_item.children.clone() } - #[cfg(feature = "wasm")] - pub fn get_ancestors(&self) -> Map { - self.node_item - .ancestors - .iter() - .fold(Map::new(), |map, (id, name)| { - map.set(&id.to_string().into(), &name.into()); - map - }) + pub fn get_ancestors(&self) -> AncestorMap { + AncestorMap { + ancestors: self + .node_item + .ancestors + .iter() + .map(|(&uuid, name)| (CollectionId::new(uuid), name.clone())) + .collect(), + } } } #[cfg_attr(feature = "wasm", wasm_bindgen)] impl CollectionViewTree { - pub fn get_item_by_id( + pub fn get_item_for_view( &self, collection_view: CollectionView, ) -> Option {