use std::cell::Cell;
use objc2::{
define_class, msg_send, rc::Retained, DeclaredClass, MainThreadMarker, MainThreadOnly,
};
use objc2_core_location::{
CLAuthorizationStatus, CLError, CLLocation, CLLocationManager, CLLocationManagerDelegate,
};
use objc2_foundation::{NSArray, NSError, NSObject, NSObjectProtocol};
use super::Location;
use crate::{Error, Handler};
type InnerHandler = dyn Handler;
#[derive(Clone, Copy, Default)]
struct Pending {
update_once: bool,
start_updates: bool,
}
pub(super) struct Ivars {
handler: Box<InnerHandler>,
pending: Cell<Pending>,
one_shot: Cell<bool>,
last_delivered: Cell<f64>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[ivars = Ivars]
pub(super) struct RobiusLocationDelegate;
unsafe impl NSObjectProtocol for RobiusLocationDelegate {}
unsafe impl CLLocationManagerDelegate for RobiusLocationDelegate {
#[unsafe(method(locationManager:didUpdateLocations:))]
#[allow(non_snake_case)]
unsafe fn locationManager_didUpdateLocations(
&self,
_: &CLLocationManager,
locations: &NSArray<CLLocation>,
) {
let one_shot = self.ivars().one_shot.get();
for location in locations.iter() {
if one_shot {
self.deliver_if_newer(&location);
} else {
self.deliver(&location); }
}
}
#[unsafe(method(locationManager:didFailWithError:))]
#[allow(non_snake_case)]
unsafe fn locationManager_didFailWithError(&self, _: &CLLocationManager, error: &NSError) {
if self.ivars().one_shot.get() && self.ivars().last_delivered.get().is_finite() {
return;
}
self.ivars().handler.error(match CLError(error.code()) {
CLError::LocationUnknown => Error::TemporarilyUnavailable,
CLError::Denied => Error::AuthorizationDenied,
CLError::Network => Error::Network,
_ => Error::Unknown,
})
}
#[unsafe(method(locationManagerDidChangeAuthorization:))]
#[allow(non_snake_case)]
unsafe fn locationManagerDidChangeAuthorization(&self, manager: &CLLocationManager) {
let status = unsafe { manager.authorizationStatus() };
self.authorization_changed(manager, status);
}
}
);
impl RobiusLocationDelegate {
pub(super) fn new<T: Handler>(mtm: MainThreadMarker, handler: T) -> Retained<Self> {
let this = Self::alloc(mtm)
.set_ivars(Ivars {
handler: Box::new(handler),
pending: Cell::new(Pending::default()),
one_shot: Cell::new(false),
last_delivered: Cell::new(f64::NEG_INFINITY),
});
unsafe { msg_send![super(this), init] }
}
fn deliver(&self, location: &CLLocation) {
self.ivars().handler.handle(crate::Location { inner: Location { inner: location } });
}
fn deliver_if_newer(&self, location: &CLLocation) {
let ts = unsafe { location.timestamp().timeIntervalSince1970() };
if ts <= self.ivars().last_delivered.get() {
return;
}
self.ivars().last_delivered.set(ts);
self.deliver(location);
}
pub(super) fn begin_one_shot(&self, manager: &CLLocationManager) {
self.ivars().last_delivered.set(f64::NEG_INFINITY);
self.ivars().one_shot.set(true);
if let Some(location) = unsafe { manager.location() } {
self.deliver_if_newer(&location);
}
}
pub(super) fn begin_continuous(&self) {
self.ivars().one_shot.set(false);
}
pub(super) fn defer_update_once(&self) {
let mut pending = self.ivars().pending.get();
pending.update_once = true;
self.ivars().pending.set(pending);
}
pub(super) fn defer_start_updates(&self) {
let mut pending = self.ivars().pending.get();
pending.start_updates = true;
self.ivars().pending.set(pending);
}
pub(super) fn cancel_deferred_start_updates(&self) {
let mut pending = self.ivars().pending.get();
pending.start_updates = false;
self.ivars().pending.set(pending);
}
fn authorization_changed(&self, manager: &CLLocationManager, status: CLAuthorizationStatus) {
match status {
CLAuthorizationStatus::AuthorizedWhenInUse | CLAuthorizationStatus::AuthorizedAlways => {
let pending = self.ivars().pending.replace(Pending::default());
if pending.update_once {
self.begin_one_shot(manager);
unsafe { manager.requestLocation() };
}
if pending.start_updates {
self.begin_continuous();
unsafe { manager.startUpdatingLocation() };
}
}
CLAuthorizationStatus::Denied | CLAuthorizationStatus::Restricted => {
let pending = self.ivars().pending.replace(Pending::default());
if pending.update_once || pending.start_updates {
self.ivars().handler.error(Error::AuthorizationDenied);
}
}
_ => {}
}
}
}