Revive the feature to share proposals in the bike network tool. #765

Explicitly mark the edits as remote in the URL, and refresh the URL +
panel after sharing.
This commit is contained in:
Dustin Carlino 2021-10-04 15:16:28 -07:00
parent eb9c4df3f2
commit 9f708354d8
3 changed files with 63 additions and 54 deletions

View File

@ -311,6 +311,47 @@ fn continue_app_setup(
// Just apply this here, don't plumb to SimFlags or anything else. We recreate things using
// these flags later, but we don't want to keep applying the same edits.
if let Some(ref edits_name) = setup.start_with_edits {
if let Some(id) = edits_name.strip_prefix("remote/") {
let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
let url = format!("{}/get?id={}", ungap::PROPOSAL_HOST_URL, id);
return vec![FutureLoader::<App, Vec<u8>>::new_state(
ctx,
Box::pin(async move {
let bytes = abstio::http_get(url).await?;
let wrapper: Box<dyn Send + FnOnce(&App) -> Vec<u8>> = Box::new(move |_| bytes);
Ok(wrapper)
}),
outer_progress_rx,
inner_progress_rx,
"Downloading proposal",
Box::new(move |ctx, app, result| {
match result
.and_then(|bytes| MapEdits::load_from_bytes(&app.primary.map, bytes))
{
Ok(edits) => Transition::Clear(finish_app_setup(
ctx,
app,
title,
savestate,
Some(edits),
setup,
)),
Err(err) => {
// TODO Fail more gracefully -- add a popup with the error, but continue
// app setup?
error!("Couldn't load remote proposal: {}", err);
Transition::Replace(PopupMsg::new_state(
ctx,
"Couldn't load remote proposal",
vec![err.to_string()],
))
}
}
}),
)];
}
for path in [
abstio::path_edits(app.primary.map.get_name(), edits_name),
abstio::path(format!("system/proposals/{}.json", edits_name)),
@ -326,43 +367,8 @@ fn continue_app_setup(
}
}
// Try loading from remote
let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
let url = format!("{}/get?id={}", ungap::PROPOSAL_HOST_URL, edits_name);
return vec![FutureLoader::<App, Vec<u8>>::new_state(
ctx,
Box::pin(async move {
let bytes = abstio::http_get(url).await?;
let wrapper: Box<dyn Send + FnOnce(&App) -> Vec<u8>> = Box::new(move |_| bytes);
Ok(wrapper)
}),
outer_progress_rx,
inner_progress_rx,
"Downloading proposal",
Box::new(move |ctx, app, result| {
match result.and_then(|bytes| MapEdits::load_from_bytes(&app.primary.map, bytes)) {
Ok(edits) => Transition::Clear(finish_app_setup(
ctx,
app,
title,
savestate,
Some(edits),
setup,
)),
Err(err) => {
// TODO Fail more gracefully -- add a popup with the error, but continue
// app setup?
error!("Couldn't load remote proposal: {}", err);
Transition::Replace(PopupMsg::new_state(
ctx,
"Couldn't load remote proposal",
vec![err.to_string()],
))
}
}
}),
)];
// TODO Fail more gracefully -- add a popup with the error, but continue app setup?
panic!("Can't start with nonexistent edits {}", edits_name);
}
finish_app_setup(ctx, app, title, savestate, None, setup)

View File

@ -55,10 +55,16 @@ impl State<App> for ExploreMap {
self.map_edit_key = key;
self.top_panel = make_top_panel(ctx, app);
URLManager::update_url_param(
"--edits".to_string(),
app.primary.map.get_edits().edits_name.clone(),
);
let map = &app.primary.map;
let checksum = map.get_edits().get_checksum(map);
if share::UploadedProposals::load().md5sums.contains(&checksum) {
URLManager::update_url_param("--edits".to_string(), format!("remote/{}", checksum));
} else {
URLManager::update_url_param(
"--edits".to_string(),
map.get_edits().edits_name.clone(),
);
}
}
if ctx.canvas_movement() {
@ -113,6 +119,8 @@ impl State<App> for ExploreMap {
));
}
"Share proposal" => {
// After we return from the new state, force recalculation
self.map_edit_key = usize::MAX;
return Transition::Push(share::upload_proposal(ctx, app));
}
"Show more layers" => {
@ -207,16 +215,13 @@ fn make_top_panel(ctx: &mut EventCtx, app: &App) -> Panel {
.disabled(edits.commands.is_empty())
.build_def(ctx),
]));
if false {
// TODO Rethink UI of this, probably fold into save dialog
col.push(
ctx.style()
.btn_outline
.text("Share proposal")
.disabled(!share::UploadedProposals::should_upload_proposal(app))
.build_def(ctx),
);
}
col.push(
ctx.style()
.btn_outline
.text("Share proposal")
.disabled(!share::UploadedProposals::should_upload_proposal(app))
.build_def(ctx),
);
// TODO Should undo/redo, save, share functionality also live here?
col.push(

View File

@ -33,9 +33,7 @@ pub fn upload_proposal(ctx: &mut EventCtx, app: &App) -> Box<dyn State<App>> {
Ok(id) => {
info!("Proposal uploaded! {}/get?id={}", PROPOSAL_HOST_URL, id);
UploadedProposals::proposal_uploaded(id);
// TODO Change URL
// TODO Ahh this doesn't actually remake the top panel and change the share
// button. Grrrr.
// TODO On web, this is fine. On native, copy the URL into the clipboard?
PopupMsg::new_state(ctx, "Success", vec!["You can now share the URL..."])
}
Err(err) => PopupMsg::new_state(