dioxus_std/geolocation/
core.rs1use super::platform;
4use core::fmt;
5use dioxus::prelude::Coroutine;
6use std::sync::Arc;
7
8#[derive(Debug, Clone)]
10pub struct Geocoordinates {
11 pub latitude: f64,
12 pub longitude: f64,
13}
14
15#[derive(Debug)]
17pub enum PowerMode {
18 High,
20 Low,
22}
23
24#[derive(Debug)]
26pub enum Event {
27 StatusChanged(Status),
29 NewGeocoordinates(Geocoordinates),
31}
32
33#[derive(Debug)]
35pub enum Access {
36 Allowed,
37 Denied,
38 Unspecified,
40}
41
42#[derive(Debug, PartialEq)]
44pub enum Status {
45 Ready,
47 Disabled,
49 NotAvailable,
51 Initializing,
53 Unknown,
55}
56
57pub struct Geolocator {
59 device_geolocator: platform::Geolocator,
60}
61
62impl Geolocator {
63 pub fn new(power_mode: PowerMode) -> Result<Self, Error> {
65 let mut device_geolocator = platform::Geolocator::new()?;
66 platform::set_power_mode(&mut device_geolocator, power_mode)?;
67
68 Ok(Self { device_geolocator })
69 }
70
71 pub async fn get_coordinates(&self) -> Result<Geocoordinates, Error> {
73 platform::get_coordinates(&self.device_geolocator).await
74 }
75
76 pub fn listen(&self, listener: Coroutine<Event>) -> Result<(), Error> {
78 let tx = listener.tx();
79 platform::listen(
80 &self.device_geolocator,
81 Arc::new(move |event: Event| {
82 tx.unbounded_send(event).ok();
83 }),
84 )
85 }
86}
87
88#[derive(Debug, Clone)]
90pub enum Error {
91 NotInitialized,
92 AccessDenied,
93 Poisoned,
94 DeviceError(String),
95}
96
97impl std::error::Error for Error {}
98impl fmt::Display for Error {
99 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100 match self {
101 Error::NotInitialized => write!(f, "not initialized"),
102 Error::AccessDenied => {
103 write!(f, "access denied (access may have been revoked during use)")
104 }
105 Error::Poisoned => write!(f, "the internal read/write lock has been poisioned"),
106 Error::DeviceError(e) => write!(f, "a device error has occurred: {}", e),
107 }
108 }
109}