Remember "current trip" when switching tabs

This commit is contained in:
Michael Kirk 2021-10-11 18:05:08 -07:00 committed by Dustin Carlino
parent 56c5bf31bd
commit f1f895eab2
4 changed files with 56 additions and 15 deletions

View File

@ -734,6 +734,7 @@ pub struct SessionState {
// Specific to the ungap tool
pub elevation_contours: Cached<MapName, (FindClosest<Distance>, ToggleZoomed)>,
pub routing_preferences: crate::ungap::RoutingPreferences,
pub ungap_current_trip_name: Option<String>,
// Map and edit change key
pub mode_shift: Cached<(MapName, usize), crate::ungap::ModeShiftData>,
}
@ -756,6 +757,7 @@ impl SessionState {
elevation_contours: Cached::new(),
routing_preferences: crate::ungap::RoutingPreferences::default(),
ungap_current_trip_name: None,
mode_shift: Cached::new(),
}
}

View File

@ -133,10 +133,10 @@ impl Tab {
Transition::Pop,
Transition::Replace(match self {
Tab::Explore => ExploreMap::new_state(ctx, app, layers),
Tab::Trip => trip::TripPlanner::new_state(ctx, app, layers),
Tab::AddLanes => {
quick_sketch::QuickSketch::new_state(ctx, app, layers)
}
Tab::Trip => trip::TripPlanner::new_state(ctx, app, layers),
Tab::PredictImpact => {
predict::ShowGaps::new_state(ctx, app, layers)
}

View File

@ -86,10 +86,15 @@ impl SavedTrips {
impl TripManagement {
pub fn new(app: &App) -> TripManagement {
let all = SavedTrips::load(app);
let current = NamedTrip {
name: all.new_name(),
waypoints: Vec::new(),
};
let current = all
.trips
.iter()
.next()
.map(|(_k, v)| v.clone())
.unwrap_or(NamedTrip {
name: all.new_name(),
waypoints: Vec::new(),
});
TripManagement { all, current }
}
@ -129,7 +134,7 @@ impl TripManagement {
}
/// saves iff current trip is changed.
pub fn autosave(&mut self, app: &App) {
pub fn autosave(&mut self, app: &mut App) {
match self.all.trips.get(&self.current.name) {
None if self.current.waypoints.len() == 0 => return,
Some(existing) if existing == &self.current => return,
@ -140,18 +145,37 @@ impl TripManagement {
.trips
.insert(self.current.name.clone(), self.current.clone());
self.all.save(app);
self.save_current_trip_to_session(app);
}
pub fn on_click(&mut self, ctx: &mut EventCtx, app: &App, action: &str) -> Option<Transition> {
pub fn set_current(&mut self, name: &str) {
if self.all.trips.contains_key(name) {
self.current = self.all.trips[name].clone();
}
}
pub fn on_click(
&mut self,
ctx: &mut EventCtx,
app: &mut App,
action: &str,
) -> Option<Transition> {
match action {
"Delete" => {
if self.all.trips.remove(&self.current.name).is_some() {
self.all.save(app);
}
self.current = NamedTrip {
name: self.all.new_name(),
waypoints: Vec::new(),
};
self.current = self
.all
.trips
.iter()
.next()
.map(|(_k, v)| v.clone())
.unwrap_or_else(|| NamedTrip {
name: self.all.new_name(),
waypoints: Vec::new(),
});
self.save_current_trip_to_session(app);
Some(Transition::Keep)
}
"Start new trip" => {
@ -159,6 +183,7 @@ impl TripManagement {
name: self.all.new_name(),
waypoints: Vec::new(),
};
app.session.ungap_current_trip_name = None;
Some(Transition::Keep)
}
"Load another trip" => Some(Transition::Push(ChooseSomething::new_state(
@ -171,6 +196,7 @@ impl TripManagement {
Transition::ModifyState(Box::new(move |state, ctx, app| {
let state = state.downcast_mut::<TripPlanner>().unwrap();
state.files.current = state.files.all.trips[&choice].clone();
state.files.save_current_trip_to_session(app);
state.sync_from_file_management(ctx, app);
})),
])
@ -178,10 +204,12 @@ impl TripManagement {
))),
"previous trip" => {
self.current = self.all.prev(&self.current.name).unwrap().clone();
self.save_current_trip_to_session(app);
Some(Transition::Keep)
}
"next trip" => {
self.current = self.all.next(&self.current.name).unwrap().clone();
self.save_current_trip_to_session(app);
Some(Transition::Keep)
}
"rename trip" => Some(Transition::Push(RenameTrip::new_state(
@ -192,6 +220,12 @@ impl TripManagement {
_ => None,
}
}
fn save_current_trip_to_session(&self, app: &mut App) {
if app.session.ungap_current_trip_name.as_ref() != Some(&self.current.name) {
app.session.ungap_current_trip_name = Some(self.current.name.clone());
}
}
}
struct RenameTrip {
@ -241,6 +275,7 @@ impl SimpleState<App> for RenameTrip {
let state = state.downcast_mut::<TripPlanner>().unwrap();
state.files.all.trips.remove(&old_name);
state.files.current.name = new_name.clone();
app.session.ungap_current_trip_name = Some(new_name.clone());
state
.files
.all

View File

@ -38,7 +38,7 @@ enum ID {
impl ObjectID for ID {}
impl TripPlanner {
pub fn new_state(ctx: &mut EventCtx, app: &App, layers: Layers) -> Box<dyn State<App>> {
pub fn new_state(ctx: &mut EventCtx, app: &mut App, layers: Layers) -> Box<dyn State<App>> {
let mut rp = TripPlanner {
layers,
once: true,
@ -50,12 +50,16 @@ impl TripPlanner {
alt_routes: Vec::new(),
world: World::bounded(app.primary.map.get_bounds()),
};
rp.recalculate_routes(ctx, app);
if let Some(current_name) = &app.session.ungap_current_trip_name {
rp.files.set_current(current_name);
}
rp.sync_from_file_management(ctx, app);
Box::new(rp)
}
// Use the current session settings to determine "main" and alts
fn recalculate_routes(&mut self, ctx: &mut EventCtx, app: &App) {
fn recalculate_routes(&mut self, ctx: &mut EventCtx, app: &mut App) {
let mut world = World::bounded(app.primary.map.get_bounds());
let main_route = RouteDetails::main_route(ctx, app, self.waypoints.get_waypoints());
@ -153,7 +157,7 @@ impl TripPlanner {
self.input_panel = new_panel;
}
fn sync_from_file_management(&mut self, ctx: &mut EventCtx, app: &App) {
fn sync_from_file_management(&mut self, ctx: &mut EventCtx, app: &mut App) {
self.waypoints
.overwrite(app, self.files.current.waypoints.clone());
self.recalculate_routes(ctx, app);