|
| 1 | +//! The `LocationPreview` is a small view that shows the current location |
| 2 | +//! and allows the user to send their location to a room. |
| 3 | +//! |
| 4 | +//! This view is not visible by default, only when the user requests it |
| 5 | +//! by clicking on the location button in the message input bar. |
| 6 | +//! The `RoomScreen` widget then shows this view above the message input bar. |
| 7 | +
|
| 8 | +use std::time::SystemTime; |
| 9 | + |
| 10 | +use makepad_widgets::*; |
| 11 | +use robius_location::Coordinates; |
| 12 | + |
| 13 | +use crate::location::{get_latest_location, request_location_update, LocationAction, LocationRequest, LocationUpdate}; |
| 14 | + |
| 15 | +live_design! { |
| 16 | + use link::theme::*; |
| 17 | + use link::shaders::*; |
| 18 | + use link::widgets::*; |
| 19 | + |
| 20 | + use crate::shared::helpers::*; |
| 21 | + use crate::shared::styles::*; |
| 22 | + use crate::shared::avatar::*; |
| 23 | + use crate::shared::icon_button::*; |
| 24 | + |
| 25 | + pub LocationPreview = {{LocationPreview}} { |
| 26 | + visible: false |
| 27 | + width: Fill |
| 28 | + height: Fit |
| 29 | + flow: Down |
| 30 | + padding: {left: 12.0, top: 12.0, bottom: 12.0, right: 10.0} |
| 31 | + spacing: 15 |
| 32 | + |
| 33 | + show_bg: true, |
| 34 | + draw_bg: { |
| 35 | + color: #xF0F5FF, |
| 36 | + } |
| 37 | + |
| 38 | + <Label> { |
| 39 | + width: Fill, |
| 40 | + height: Fit, |
| 41 | + draw_text: { |
| 42 | + wrap: Word, |
| 43 | + color: (MESSAGE_TEXT_COLOR), |
| 44 | + text_style: <MESSAGE_TEXT_STYLE>{ font_size: 10.0 }, |
| 45 | + } |
| 46 | + text: "Send your location to this room?" |
| 47 | + } |
| 48 | + |
| 49 | + location_label = <Label> { |
| 50 | + width: Fill, |
| 51 | + height: Fit, |
| 52 | + align: {x: 0.0, y: 0.5}, |
| 53 | + padding: {left: 5.0} |
| 54 | + draw_text: { |
| 55 | + wrap: Word, |
| 56 | + color: (MESSAGE_TEXT_COLOR), |
| 57 | + text_style: <MESSAGE_TEXT_STYLE>{}, |
| 58 | + } |
| 59 | + text: "Fetching current location..." |
| 60 | + } |
| 61 | + |
| 62 | + <View> { |
| 63 | + width: Fill, height: Fit |
| 64 | + flow: Right, |
| 65 | + align: {x: 0.0, y: 0.5} |
| 66 | + spacing: 15 |
| 67 | + |
| 68 | + cancel_location_button = <RobrixIconButton> { |
| 69 | + align: {x: 0.5, y: 0.5} |
| 70 | + padding: 15, |
| 71 | + draw_icon: { |
| 72 | + svg_file: (ICON_BLOCK_USER) |
| 73 | + color: (COLOR_DANGER_RED), |
| 74 | + } |
| 75 | + icon_walk: {width: 16, height: 16, margin: {left: -2, right: -1, top: -1} } |
| 76 | + |
| 77 | + draw_bg: { |
| 78 | + border_color: (COLOR_DANGER_RED), |
| 79 | + color: #fff0f0 // light red |
| 80 | + } |
| 81 | + text: "Cancel" |
| 82 | + draw_text:{ |
| 83 | + color: (COLOR_DANGER_RED), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + send_location_button = <RobrixIconButton> { |
| 88 | + // disabled by default; will be enabled upon receiving valid location update. |
| 89 | + enabled: false, |
| 90 | + align: {x: 0.5, y: 0.5} |
| 91 | + padding: 15, |
| 92 | + draw_icon: { |
| 93 | + svg_file: (ICON_SEND) |
| 94 | + color: (COLOR_ACCEPT_GREEN), |
| 95 | + } |
| 96 | + icon_walk: {width: 16, height: 16, margin: {left: -2, right: -1} } |
| 97 | + |
| 98 | + draw_bg: { |
| 99 | + border_color: (COLOR_ACCEPT_GREEN), |
| 100 | + color: #f0fff0 // light green |
| 101 | + } |
| 102 | + text: "Yes" |
| 103 | + draw_text:{ |
| 104 | + color: (COLOR_ACCEPT_GREEN), |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +#[derive(Live, LiveHook, Widget)] |
| 113 | +struct LocationPreview { |
| 114 | + #[deref] view: View, |
| 115 | + #[rust] coords: Option<Result<Coordinates, robius_location::Error>>, |
| 116 | + #[rust] timestamp: Option<SystemTime>, |
| 117 | +} |
| 118 | +impl Widget for LocationPreview { |
| 119 | + fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) { |
| 120 | + let mut needs_redraw = false; |
| 121 | + if let Event::Actions(actions) = event { |
| 122 | + for action in actions { |
| 123 | + match action.downcast_ref() { |
| 124 | + Some(LocationAction::Update(LocationUpdate { coordinates, time })) => { |
| 125 | + self.coords = Some(Ok(*coordinates)); |
| 126 | + self.timestamp = *time; |
| 127 | + self.button(id!(send_location_button)).set_enabled(cx, true); |
| 128 | + needs_redraw = true; |
| 129 | + } |
| 130 | + Some(LocationAction::Error(e)) => { |
| 131 | + self.coords = Some(Err(*e)); |
| 132 | + self.timestamp = None; |
| 133 | + self.button(id!(send_location_button)).set_enabled(cx, false); |
| 134 | + needs_redraw = true; |
| 135 | + } |
| 136 | + _ => { } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // NOTE: the send location button click event is handled |
| 141 | + // in the RoomScreen handle_event function. |
| 142 | + |
| 143 | + // Handle the cancel location button being clicked. |
| 144 | + if self.button(id!(cancel_location_button)).clicked(actions) { |
| 145 | + self.clear(); |
| 146 | + needs_redraw = true; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if needs_redraw { |
| 151 | + self.redraw(cx); |
| 152 | + } |
| 153 | + |
| 154 | + self.view.handle_event(cx, event, scope); |
| 155 | + } |
| 156 | + |
| 157 | + fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep { |
| 158 | + let text = match self.coords { |
| 159 | + Some(Ok(c)) => { |
| 160 | + // if let Some(st) = self.timestamp { |
| 161 | + // format!("Current location: {:.6},{:.6}\n Timestamp: {:?}", c.latitude, c.longitude, st) |
| 162 | + // } else { |
| 163 | + format!("Current location: {:.6},{:.6}", c.latitude, c.longitude) |
| 164 | + // } |
| 165 | + } |
| 166 | + Some(Err(e)) => format!("Error getting location: {e:?}"), |
| 167 | + None => String::from("Current location is not yet available."), |
| 168 | + }; |
| 169 | + self.label(id!(location_label)).set_text(cx, &text); |
| 170 | + self.view.draw_walk(cx, scope, walk) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +impl LocationPreview { |
| 176 | + fn show(&mut self) { |
| 177 | + request_location_update(LocationRequest::UpdateOnce); |
| 178 | + if let Some(loc) = get_latest_location() { |
| 179 | + self.coords = Some(Ok(loc.coordinates)); |
| 180 | + self.timestamp = loc.time; |
| 181 | + } |
| 182 | + self.visible = true; |
| 183 | + } |
| 184 | + |
| 185 | + fn clear(&mut self) { |
| 186 | + self.coords = None; |
| 187 | + self.timestamp = None; |
| 188 | + self.visible = false; |
| 189 | + } |
| 190 | + |
| 191 | + pub fn get_current_data(&self) -> Option<(Coordinates, Option<SystemTime>)> { |
| 192 | + self.coords |
| 193 | + .as_ref() |
| 194 | + .and_then(|res| res.ok()) |
| 195 | + .map(|c| (c, self.timestamp)) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +impl LocationPreviewRef { |
| 200 | + pub fn show(&self) { |
| 201 | + if let Some(mut inner) = self.borrow_mut() { |
| 202 | + inner.show(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + pub fn clear(&self) { |
| 207 | + if let Some(mut inner) = self.borrow_mut() { |
| 208 | + inner.clear(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + pub fn get_current_data(&self) -> Option<(Coordinates, Option<SystemTime>)> { |
| 213 | + self.borrow().and_then(|inner| inner.get_current_data()) |
| 214 | + } |
| 215 | +} |
0 commit comments