Skip to content

Commit

Permalink
nuke use_rw and fix use_geolocation
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Mar 19, 2024
1 parent 33f6fce commit fd64567
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 63 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
- [x] Notifications - (Desktop)
- [x] Color Scheme - (any)
- [x] Utility Hooks
- [x] use_rw - (any)
- [x] use_channel - (any)
- [ ] use_interval (any)
- [x] i18n - (any)
Expand Down
20 changes: 11 additions & 9 deletions std/src/geolocation/use_geolocation.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
//! Provides an initialization and use_geolocation hook.

use super::core::{Error, Event, Geocoordinates, Geolocator, PowerMode, Status};
use dioxus::prelude::{provide_context, try_consume_context, use_coroutine, UnboundedReceiver};
use dioxus::{
prelude::{
provide_context, try_consume_context, use_coroutine, use_signal, Signal, UnboundedReceiver,
},
signals::{Readable, Writable},
};
use futures_util::stream::StreamExt;
use std::{rc::Rc, sync::Once};

use crate::utils::rw::{use_rw, UseRw};

static INIT: Once = Once::new();

/// Provides the latest geocoordinates. Good for navigation-type apps.
pub fn use_geolocation() -> Result<Geocoordinates, Error> {
// Store the coords
let coords: UseRw<Result<Geocoordinates, Error>> = use_rw(|| Err(Error::NotInitialized));
let mut coords: Signal<Result<Geocoordinates, Error>> =
use_signal(|| Err(Error::NotInitialized));

// Get geolocator
let geolocator = match try_consume_context::<Rc<Geolocator>>() {
Some(v) => v,
None => return Err(Error::NotInitialized),
};

let coords_cloned = coords.clone();

// Initialize the handler of events
let listener = use_coroutine(|mut rx: UnboundedReceiver<Event>| async move {
while let Some(event) = rx.next().await {
match event {
Event::NewGeocoordinates(new_coords) => {
let _ = coords_cloned.write(Ok(new_coords));
*coords.write() = Ok(new_coords);
}
Event::StatusChanged(Status::Disabled) => {
let _ = coords_cloned.write(Err(Error::AccessDenied));
*coords.write() = Err(Error::AccessDenied);
}
_ => {}
}
Expand All @@ -43,7 +45,7 @@ pub fn use_geolocation() -> Result<Geocoordinates, Error> {
});

// Get the result and return a clone
coords.read().map_err(|_| Error::Poisoned)?.clone()
coords.read_unchecked().clone()
}

/// Must be called before any use of the geolocation abstraction.
Expand Down
1 change: 0 additions & 1 deletion std/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod channel;
pub mod rw;
3 changes: 0 additions & 3 deletions std/src/utils/rw/mod.rs

This file was deleted.

49 changes: 0 additions & 49 deletions std/src/utils/rw/use_rw.rs

This file was deleted.

0 comments on commit fd64567

Please sign in to comment.