1use std::sync::Once;
12
13pub struct Service {
15 pub name: &'static str,
16 pub initialize: fn(),
17}
18
19#[cfg(not(target_arch = "wasm32"))]
20#[linkme::distributed_slice]
21pub static SERVICES: [Service];
22
23pub fn initialize() {
25 static ONCE: Once = Once::new();
26 ONCE.call_once(|| {
27 #[cfg(not(target_arch = "wasm32"))]
28 for service in SERVICES {
29 (service.initialize)();
30 }
31 });
32}
33
34#[derive(Clone)]
37pub enum NotificationEvent {
38 Registered(Vec<u8>),
39 RegistrationFailed(String),
40 Received { payload: String, opened: bool },
41}
42
43impl std::fmt::Debug for NotificationEvent {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match self {
46 Self::Registered(_) => f.debug_tuple("Registered").finish_non_exhaustive(),
47 Self::RegistrationFailed(error) => {
48 f.debug_tuple("RegistrationFailed").field(error).finish()
49 }
50 Self::Received { opened, .. } => f
51 .debug_struct("Received")
52 .field("opened", opened)
53 .finish_non_exhaustive(),
54 }
55 }
56}
57
58#[cfg(not(target_arch = "wasm32"))]
59#[linkme::distributed_slice]
60pub static NOTIFICATION_HANDLERS: [fn(&NotificationEvent)];
61
62#[expect(
64 clippy::needless_pass_by_value,
65 reason = "public signature; toolkits hand over the event they built"
66)]
67pub fn notification(event: NotificationEvent) {
68 #[cfg(not(target_arch = "wasm32"))]
69 for handler in NOTIFICATION_HANDLERS {
70 handler(&event);
71 }
72 #[cfg(target_arch = "wasm32")]
73 let _ = event;
74}
75
76#[cfg(target_os = "android")]
78#[unsafe(no_mangle)]
79pub extern "system" fn Java_dev_daybrite_day_bridge_DayRuntime_initializeServices(
80 mut env: day_platform_android::jni::EnvUnowned,
81 class: day_platform_android::jni::objects::JClass,
82) {
83 env.with_env(|env| -> day_platform_android::jni::errors::Result<()> {
84 day_platform_android::init(env, &class)?;
85 initialize();
86 Ok(())
87 })
88 .resolve::<day_platform_android::jni::errors::ThrowRuntimeExAndDefault>();
89}