Skip to content

Commit a2eddd1

Browse files
committed
Expose parent and menu_order on PostMetadataCollectionItem
Add wp_mobile_metadata_item! macro that generates both the state enum and collection item struct with metadata fields (parent, menu_order). This enables the Android hierarchical post list to build the page tree immediately from list metadata, without waiting for full post data.
1 parent 2e2048b commit a2eddd1

File tree

2 files changed

+89
-16
lines changed

2 files changed

+89
-16
lines changed

wp_mobile/src/collection/mod.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,80 @@ macro_rules! wp_mobile_item_state {
7272
};
7373
}
7474

75+
/// Macro to create UniFFI-compatible metadata collection item types.
76+
///
77+
/// This macro generates both the state enum and the collection item struct for
78+
/// metadata-driven collections. The generated types are suitable for use across
79+
/// language boundaries via UniFFI.
80+
///
81+
/// # Parameters
82+
/// - `$item_name`: Name for the collection item struct (e.g., `PostMetadataCollectionItem`)
83+
/// - `$state_name`: Name for the state enum (e.g., `PostItemState`)
84+
/// - `$full_entity_type`: The FullEntity wrapper type (e.g., `FullEntityAnyPostWithEditContext`)
85+
///
86+
/// # Generated Types
87+
///
88+
/// ## State Enum (`$state_name`)
89+
/// - `Missing`: No cached data, needs fetch
90+
/// - `Fetching`: Fetch in progress, no cached data
91+
/// - `FetchingWithData { data }`: Fetch in progress, showing cached data
92+
/// - `Cached { data }`: Fresh cached data
93+
/// - `Stale { data }`: Outdated cached data
94+
/// - `Failed { error }`: Fetch failed, no cached data
95+
/// - `FailedWithData { error, data }`: Fetch failed, showing cached data
96+
///
97+
/// ## Collection Item Struct (`$item_name`)
98+
/// - `id: i64`: The entity ID
99+
/// - `parent: Option<i64>`: Parent entity ID (from list metadata, for hierarchical types)
100+
/// - `menu_order: Option<i64>`: Menu order (from list metadata, for hierarchical types)
101+
/// - `state: $state_name`: The combined state and data
102+
///
103+
/// # Usage
104+
/// ```ignore
105+
/// wp_mobile_metadata_item!(
106+
/// PostMetadataCollectionItem,
107+
/// PostItemState,
108+
/// FullEntityAnyPostWithEditContext
109+
/// );
110+
/// ```
111+
#[macro_export]
112+
macro_rules! wp_mobile_metadata_item {
113+
($item_name:ident, $state_name:ident, $full_entity_type:ty) => {
114+
// Generate the state enum using the existing macro
115+
$crate::wp_mobile_item_state!($state_name, $full_entity_type);
116+
117+
/// Item in a metadata collection with type-safe state representation.
118+
///
119+
/// The `state` enum encodes both the sync status and data availability,
120+
/// making it impossible to have inconsistent combinations.
121+
///
122+
/// The `parent` and `menu_order` fields come from the list metadata store,
123+
/// making them available immediately without waiting for full entity data
124+
/// to be fetched. This enables building hierarchical views (like page trees)
125+
/// as soon as the list structure is known.
126+
#[derive(uniffi::Record)]
127+
pub struct $item_name {
128+
/// The entity ID
129+
pub id: i64,
130+
131+
/// Parent entity ID (from list metadata, for hierarchical post types like pages)
132+
///
133+
/// This value comes from the list metadata, so it's available immediately
134+
/// without waiting for the full post data to be fetched.
135+
pub parent: Option<i64>,
136+
137+
/// Menu order (from list metadata, for hierarchical post types)
138+
///
139+
/// This value comes from the list metadata, so it's available immediately
140+
/// without waiting for the full post data to be fetched.
141+
pub menu_order: Option<i64>,
142+
143+
/// Combined state and data - see the state enum for variants
144+
pub state: $state_name,
145+
}
146+
};
147+
}
148+
75149
/// Macro to create UniFFI-compatible post collection wrappers
76150
///
77151
/// This macro generates a wrapper type for `PostCollection<T>` that can be used

wp_mobile/src/collection/post_metadata_collection.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,12 @@ use crate::{
1414
},
1515
};
1616

17-
// Generate PostItemState enum using the macro
18-
crate::wp_mobile_item_state!(PostItemState, crate::FullEntityAnyPostWithEditContext);
19-
20-
/// Item in a metadata collection with type-safe state representation.
21-
///
22-
/// The `state` enum encodes both the sync status and data availability,
23-
/// making it impossible to have inconsistent combinations.
24-
#[derive(uniffi::Record)]
25-
pub struct PostMetadataCollectionItem {
26-
/// The post ID
27-
pub id: i64,
28-
29-
/// Combined state and data - see [`PostItemState`] for variants
30-
pub state: PostItemState,
31-
}
17+
// Generate PostItemState enum and PostMetadataCollectionItem struct using the macro
18+
crate::wp_mobile_metadata_item!(
19+
PostMetadataCollectionItem,
20+
PostItemState,
21+
crate::FullEntityAnyPostWithEditContext
22+
);
3223

3324
/// Metadata-first collection for posts with edit context.
3425
///
@@ -136,6 +127,9 @@ impl PostMetadataCollectionWithEditContext {
136127
.into_iter()
137128
.map(|item| {
138129
let id = item.id();
130+
// Extract parent and menu_order from metadata (available immediately)
131+
let parent = item.metadata.parent;
132+
let menu_order = item.metadata.menu_order;
139133
let cached_data = cached_map.remove(&id).map(|e| e.into());
140134
let state = match (item.state, cached_data) {
141135
// Missing state
@@ -161,7 +155,12 @@ impl PostMetadataCollectionWithEditContext {
161155
}
162156
};
163157

164-
PostMetadataCollectionItem { id, state }
158+
PostMetadataCollectionItem {
159+
id,
160+
parent,
161+
menu_order,
162+
state,
163+
}
165164
})
166165
.collect();
167166

0 commit comments

Comments
 (0)