Skip to main content

qualia_core_db/hypermedia_authoring/
interactive.rs

1//! Interactive hypermedia / HbbTV — interactive, second screen, social, packaging.
2//!
3//! ~25 required functions.
4
5use std::collections::BTreeMap;
6
7/// An HbbTV / interactive application.
8#[derive(Debug, Clone)]
9pub struct HbbTVApp {
10    pub id: String,
11    pub name: String,
12    pub pages: BTreeMap<String, InteractivePage>,
13    pub current_page: Option<String>,
14    pub state: BTreeMap<String, String>,
15}
16
17#[derive(Debug, Clone)]
18pub struct InteractivePage {
19    pub id: String,
20    pub title: String,
21    pub elements: Vec<InteractiveElement>,
22    pub triggers: Vec<Trigger>,
23}
24
25#[derive(Debug, Clone)]
26pub struct InteractiveElement {
27    pub id: String,
28    pub element_type: ElementType,
29    pub visible: bool,
30    pub action: Option<String>,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum ElementType {
35    Button,
36    Text,
37    Image,
38    Video,
39    Input,
40    Overlay,
41}
42
43#[derive(Debug, Clone)]
44pub struct Trigger {
45    pub id: String,
46    pub event: TriggerEvent,
47    pub action: String,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum TriggerEvent {
52    RedButton,
53    GreenButton,
54    YellowButton,
55    BlueButton,
56    OkButton,
57    BackButton,
58    Timer,
59    StreamEvent,
60}
61
62impl HbbTVApp {
63    /// Creates an HbbTV app shell with no pages.
64    pub fn new(id: &str, name: &str) -> Self {
65        Self {
66            id: id.to_string(),
67            name: name.to_string(),
68            pages: BTreeMap::new(),
69            current_page: None,
70            state: BTreeMap::new(),
71        }
72    }
73
74    /// Appends an interactive page.
75    pub fn add_page(&mut self, page: InteractivePage) {
76        if self.current_page.is_none() {
77            self.current_page = Some(page.id.clone());
78        }
79        self.pages.insert(page.id.clone(), page);
80    }
81
82    /// Switches the active page to `page_id`; `false` when unknown.
83    pub fn navigate_to(&mut self, page_id: &str) -> bool {
84        if self.pages.contains_key(page_id) {
85            self.current_page = Some(page_id.to_string());
86            true
87        } else {
88            false
89        }
90    }
91
92    /// Sets an app-level `key`/`value` state pair.
93    pub fn set_state(&mut self, key: &str, value: &str) {
94        self.state.insert(key.to_string(), value.to_string());
95    }
96
97    /// Reads an app-level state value.
98    pub fn get_state(&self, key: &str) -> Option<&String> {
99        self.state.get(key)
100    }
101
102    /// Number of pages.
103    pub fn page_count(&self) -> usize {
104        self.pages.len()
105    }
106}
107
108/// Second screen companion app.
109#[derive(Debug, Clone)]
110pub struct SecondScreen {
111    pub id: String,
112    pub device_id: String,
113    pub synced_content: String,
114    pub sync_offset: f64,
115    pub interactive_layer: BTreeMap<String, String>,
116}
117
118impl SecondScreen {
119    /// Creates a second-screen companion for `device_id`.
120    pub fn new(id: &str, device_id: &str) -> Self {
121        Self {
122            id: id.to_string(),
123            device_id: device_id.to_string(),
124            synced_content: String::new(),
125            sync_offset: 0.0,
126            interactive_layer: BTreeMap::new(),
127        }
128    }
129
130    /// Syncs the companion to `content_id` at `offset` seconds.
131    pub fn sync_to_content(&mut self, content_id: &str, offset: f64) {
132        self.synced_content = content_id.to_string();
133        self.sync_offset = offset;
134    }
135
136    /// Adds a key/value interactive layer.
137    pub fn add_interactive_layer(&mut self, key: &str, value: &str) {
138        self.interactive_layer
139            .insert(key.to_string(), value.to_string());
140    }
141}
142
143/// Interactive stream with synchronized triggers.
144#[derive(Debug, Clone)]
145pub struct InteractiveStream {
146    pub id: String,
147    pub content_id: String,
148    pub triggers: Vec<StreamTrigger>,
149    pub social_feed: Vec<SocialPost>,
150}
151
152#[derive(Debug, Clone)]
153pub struct StreamTrigger {
154    pub timestamp: f64,
155    pub trigger_type: TriggerEvent,
156    pub payload: String,
157}
158
159#[derive(Debug, Clone)]
160pub struct SocialPost {
161    pub id: String,
162    pub author: String,
163    pub content: String,
164    pub timestamp: f64,
165}
166
167impl InteractiveStream {
168    /// Creates a stream bound to `content_id`.
169    pub fn new(id: &str, content_id: &str) -> Self {
170        Self {
171            id: id.to_string(),
172            content_id: content_id.to_string(),
173            triggers: Vec::new(),
174            social_feed: Vec::new(),
175        }
176    }
177
178    /// Appends a timed `StreamTrigger`.
179    pub fn add_trigger(&mut self, trigger: StreamTrigger) {
180        self.triggers.push(trigger);
181    }
182
183    /// Appends a social post annotation.
184    pub fn add_social_post(&mut self, post: SocialPost) {
185        self.social_feed.push(post);
186    }
187
188    /// Triggers whose time falls within `time` ± `window` seconds.
189    pub fn triggers_at(&self, time: f64, window: f64) -> Vec<&StreamTrigger> {
190        self.triggers
191            .iter()
192            .filter(|t| (t.timestamp - time).abs() < window)
193            .collect()
194    }
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200
201    #[test]
202    fn hbbtv_app_creation() {
203        let app = HbbTVApp::new("app1", "My App");
204        assert_eq!(app.name, "My App");
205        assert_eq!(app.page_count(), 0);
206    }
207
208    #[test]
209    fn hbbtv_add_page() {
210        let mut app = HbbTVApp::new("app1", "My App");
211        app.add_page(InteractivePage {
212            id: "p1".into(),
213            title: "Home".into(),
214            elements: vec![],
215            triggers: vec![],
216        });
217        assert_eq!(app.page_count(), 1);
218        assert_eq!(app.current_page, Some("p1".to_string()));
219    }
220
221    #[test]
222    fn hbbtv_navigate() {
223        let mut app = HbbTVApp::new("app1", "App");
224        app.add_page(InteractivePage {
225            id: "p1".into(),
226            title: "Home".into(),
227            elements: vec![],
228            triggers: vec![],
229        });
230        app.add_page(InteractivePage {
231            id: "p2".into(),
232            title: "Info".into(),
233            elements: vec![],
234            triggers: vec![],
235        });
236        assert!(app.navigate_to("p2"));
237        assert_eq!(app.current_page, Some("p2".to_string()));
238        assert!(!app.navigate_to("nonexistent"));
239    }
240
241    #[test]
242    fn hbbtv_state() {
243        let mut app = HbbTVApp::new("app1", "App");
244        app.set_state("score", "100");
245        assert_eq!(app.get_state("score"), Some(&"100".to_string()));
246    }
247
248    #[test]
249    fn second_screen_sync() {
250        let mut ss = SecondScreen::new("ss1", "device1");
251        ss.sync_to_content("content_a", 1.5);
252        assert_eq!(ss.synced_content, "content_a");
253        assert!((ss.sync_offset - 1.5).abs() < 0.01);
254    }
255
256    #[test]
257    fn interactive_stream_triggers() {
258        let mut stream = InteractiveStream::new("s1", "content_a");
259        stream.add_trigger(StreamTrigger {
260            timestamp: 5.0,
261            trigger_type: TriggerEvent::RedButton,
262            payload: "show_menu".into(),
263        });
264        stream.add_trigger(StreamTrigger {
265            timestamp: 10.0,
266            trigger_type: TriggerEvent::Timer,
267            payload: "show_ad".into(),
268        });
269        let triggers = stream.triggers_at(5.0, 0.5);
270        assert_eq!(triggers.len(), 1);
271    }
272
273    #[test]
274    fn interactive_stream_social() {
275        let mut stream = InteractiveStream::new("s1", "content_a");
276        stream.add_social_post(SocialPost {
277            id: "p1".into(),
278            author: "user1".into(),
279            content: "Great show!".into(),
280            timestamp: 1.0,
281        });
282        assert_eq!(stream.social_feed.len(), 1);
283    }
284}