use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use crate::transaction::IosIapTransaction;
use crate::{
IosIapProductsResponse, IosIapPurchaseResponse, IosIapTransactionFinishResponse,
IosIapTransactionResponse,
};
#[derive(Message, Clone, Debug)]
pub enum IosIapResponse {
Products((i64, IosIapProductsResponse)),
Purchase((i64, IosIapPurchaseResponse)),
TransactionFinished((i64, IosIapTransactionFinishResponse)),
AllTransactions((i64, IosIapTransactionResponse)),
CurrentEntitlements((i64, IosIapTransactionResponse)),
}
#[non_exhaustive]
#[derive(Message, Clone, Debug)]
pub enum IosIapEvents {
TransactionUpdate(IosIapTransaction),
}
#[allow(dead_code)]
pub struct IosIapPlugin {
auto_init: bool,
}
impl IosIapPlugin {
pub fn new(auto_init: bool) -> Self {
Self { auto_init }
}
}
impl Plugin for IosIapPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(crate::request::plugin);
#[cfg(not(target_os = "ios"))]
{
app.add_message::<IosIapEvents>();
app.add_message::<IosIapResponse>();
}
#[cfg(target_os = "ios")]
{
use bevy_channel_message::{ChannelMessageApp, ChannelMessageSender};
app.add_channel_message::<IosIapEvents>();
let sender = app
.world()
.get_resource::<ChannelMessageSender<IosIapEvents>>()
.unwrap()
.clone();
crate::native::set_sender_events(sender);
app.add_channel_message::<IosIapResponse>();
let sender = app
.world()
.get_resource::<ChannelMessageSender<IosIapResponse>>()
.unwrap()
.clone();
crate::native::set_sender_response(sender);
if self.auto_init {
crate::native::ios_iap_init();
}
}
}
}