Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions crates/engine/src/cmd/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub fn is_field_package(name: &str) -> bool {

/// A field package of the active map, as (name, bytes).
///
/// An offline area's map (`<area> (offline)` of one of this project's areas, or the map of a
/// project opened from a field package) is packaged as it is, to bring its edits back. Any other map is first made an offline area of this project (`extent`
/// The map of a home offline area or of an opened field package goes as it is, so its edits
/// can come back. Any other map is first made an offline area of this project (`extent`
/// and `name` as for `offline.download`, the view by default), whose map is packaged.
///
/// Never silent: the raster layers an offline area leaves behind, the tile layers that stay
Expand Down Expand Up @@ -109,15 +109,12 @@ fn build_package(s: &mut Session, p: &Value, notes: &mut Vec<String>) -> Result<
if opened_field && s.project.maps.len() != 1 {
return Err("a returning field project must hold only its original field map: remove added maps before sending it back".into());
}
// An offline area's map: named for one of this project's areas, or opened from a field
// package (the phone's project holds no areas). A map only named so is packaged as any other.
let returning = s
.map()
.name
.strip_suffix(OFFLINE)
.map(str::to_string)
.filter(|area| opened_field || s.project.offline_areas.iter().any(|a| a.name.eq_ignore_ascii_case(area)))
.or_else(|| opened_field.then(|| s.map().name.clone()));
let home_area = if opened_field { None } else { super::workspace::map_area_index(&s.project, s.project.active_map) };
let returning = if opened_field {
Some(s.map().name.strip_suffix(OFFLINE).unwrap_or(&s.map().name).to_string())
} else {
home_area.and_then(|i| s.project.offline_areas.get(i)).map(|area| area.name.clone())
};
// Only a map coming back from the field carries its photos. Its persisted provenance
// survives an ordinary Save As; going out, the photos named are at home.
let coming_back = returning.is_some() && opened_field;
Expand Down Expand Up @@ -168,9 +165,7 @@ fn build_package(s: &mut Session, p: &Value, notes: &mut Vec<String>) -> Result<
false,
);
}
let mut m = data;
m.name = format!("{name}{OFFLINE}");
(name, m)
(name, data)
}
};
let mut entries: Vec<(String, Vec<u8>)> = Vec::new();
Expand Down Expand Up @@ -272,7 +267,9 @@ fn build_package(s: &mut Session, p: &Value, notes: &mut Vec<String>) -> Result<
}
s.project.field_origin.clone()
} else {
let ai = s.project.offline_areas.iter().position(|a| a.name.eq_ignore_ascii_case(&area)).ok_or("the offline area was not found")?;
let ai = home_area
.or_else(|| s.project.offline_areas.iter().position(|a| a.name.eq_ignore_ascii_case(&area)))
.ok_or("the offline area was not found")?;
let new_home = s.project.origin_id.is_empty().then(crate::project::field_id).transpose()?;
let new_area = s.project.offline_areas.get(ai).is_some_and(|a| a.id.is_empty()).then(crate::project::field_id).transpose()?;
// The first package of an area gives it the state a return is checked against; later ones
Expand Down Expand Up @@ -317,7 +314,12 @@ fn build_package(s: &mut Session, p: &Value, notes: &mut Vec<String>) -> Result<
}
let mut project = crate::project::Project::new(area.clone());
project.field_origin = origin;
// Rasters were taken out above, so the map goes as it is.
// The package has no home offline areas and carries its association in field_origin.
map.offline_area_id = None;
if !coming_back {
// Older phones also look at the outgoing title to show Send Field Package.
map.name = format!("{area}{OFFLINE}");
}
project.maps = vec![map];
let home = &s.project;
let stayed: Vec<String> = [
Expand Down
119 changes: 119 additions & 0 deletions crates/engine/src/cmd/field_audit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,125 @@ fn with_label(s: &Session, id: u32, field: &str, label: &str) -> usize {

const AREA: [f64; 4] = [-20.0, -40.0, 60.0, 40.0];

/// A map imported from another project under an offline title is renamed, so an area of the
/// same name here doesn't take it for its offline copy (and sync it as the area's edits).
#[test]
fn an_imported_offline_titled_map_is_not_taken_for_an_area_copy() {
let temp = Temp::new("import-offline-title");
let mut home = home(&temp);
package(&mut home, &json!({"name": "Crew", "extent": AREA})).unwrap();
let maps = home.project.maps.len();
let mut other = crate::Project::default();
// Another project's offline copy, saved before maps carried their area's ID.
other.maps[0].name = "Crew (offline)".into();
home.run("map.import", &json!({"text": serde_json::to_string(&other).unwrap()})).unwrap();
assert!(home.project.maps.iter().any(|m| m.name == "Crew (imported)" && m.offline_area_id.is_none()));
assert!(home.notices.iter().any(|n| n.text.contains("renamed")), "{:?}", home.notices);
home.run("offline.open", &json!({"name": "Crew"})).unwrap();
assert_eq!(home.map().name, "Crew (offline)");
assert_eq!(home.project.maps.len(), maps + 2);
}

/// A map imported with its area's ID loses it, and says so; an ordinary map imports with neither
/// notice.
#[test]
fn importing_maps_says_when_an_area_association_is_dropped() {
let temp = Temp::new("import-detach");
let mut home = home(&temp);
package(&mut home, &json!({"name": "Crew", "extent": AREA})).unwrap();
home.run("offline.open", &json!({"name": "Crew"})).unwrap();
let path = temp.0.join("with-area.rgis");
home.run("project.save", &json!({"path": path})).unwrap();

let mut other = Session::new(crate::Project::default());
let mark = other.notice_mark();
other.run("map.import", &json!({"path": path})).unwrap();
let texts: Vec<_> = other.notices_since(mark).iter().map(|n| n.text.clone()).collect();
assert!(texts.iter().any(|t| t.starts_with("1 imported map(s) were detached")), "{texts:?}");
assert!(other.project.maps.iter().all(|m| m.offline_area_id.is_none()));

let mark = other.notice_mark();
other.run("map.import", &json!({"text": serde_json::to_string(&crate::Project::default()).unwrap()})).unwrap();
let texts: Vec<_> = other.notices_since(mark).iter().map(|n| n.text.clone()).collect();
assert!(!texts.iter().any(|t| t.contains("detached") || t.contains("renamed")), "{texts:?}");
}

/// Removing an area whose copy is the only map keeps the map but not its association, so the
/// project still saves and reopens.
#[test]
fn removing_an_area_clears_the_association_of_the_copy_it_keeps() {
let temp = Temp::new("remove-only-copy");
let mut home = home(&temp);
package(&mut home, &json!({"name": "Crew", "extent": AREA})).unwrap();
home.run("offline.open", &json!({"name": "Crew"})).unwrap();
home.project.maps.retain(|m| m.offline_area_id.is_some());
home.project.active_map = 0;
home.run("offline.remove", &json!({"name": "Crew"})).unwrap();
assert_eq!(home.project.maps.len(), 1);
assert!(home.project.maps[0].offline_area_id.is_none());
// Nor does its title still name the area's copy.
assert_eq!(home.project.maps[0].name, "Crew (kept)");
assert!(home.notices.iter().any(|n| n.text.contains("kept as 'Crew (kept)'")), "{:?}", home.notices);
let path = temp.0.join("removed.rgis");
home.run("project.save", &json!({"path": path})).unwrap();
home.run("project.open", &json!({"path": path})).unwrap();
}

/// A legacy home tab gains its association on rename, so callers still find it after save and reopen.
#[test]
fn bring_back_refreshes_a_renamed_home_offline_map() {
let temp = Temp::new("rename-home-offline");
let mut home = home(&temp);
let source = home.map().name.clone();
let (_, out) = package(&mut home, &json!({"name": "Crew", "extent": AREA})).unwrap();
home.run("offline.open", &json!({"name": "Crew"})).unwrap();
// A home project saved before maps carried their area ID.
home.map_mut().offline_area_id = None;
home.run("map.rename", &json!({"name": "Renamed at home"})).unwrap();
let path = temp.0.join("home.rgis");
home.run("project.save", &json!({"path": path})).unwrap();
let saved: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert!(saved["requires"].as_array().unwrap().contains(&json!("offline-map-area-id")));
home.run("project.open", &json!({"path": path})).unwrap();
assert_eq!(home.map().name, "Renamed at home");

let mut field = phone(&temp, &out, "phone");
let cities = layer(&field, "Cities");
let name = text_field(&field, cities);
let count = |s: &Session, label| with_label(s, layer(s, "Cities"), &name, label);
add_point(&mut field, cities, &name, "Captured after rename", [10.0, 5.0]);
let (done, _) = bring_back(&temp, &mut field, &mut home, "phone");
assert_eq!(done["area"], "Crew");
assert_eq!(done["added"], 1);
assert_eq!(count(&home, "Captured after rename"), 1);

home.run("map.activate", &json!({"name": source})).unwrap();
assert_eq!(count(&home, "Captured after rename"), 1);
home.run("offline.open", &json!({"name": "Crew"})).unwrap();
assert_eq!(home.map().name, "Renamed at home");
assert_eq!(home.project.maps.len(), 2);

// Reusing the old title for another map must not redirect Sync, Package or Remove.
home.run("map.new", &json!({"name": "Crew (offline)"})).unwrap();
home.run("offline.open", &json!({"name": "Crew"})).unwrap();
assert_eq!(home.map().name, "Renamed at home");
add_point(&mut home, cities, &name, "Edited at home", [11.0, 5.0]);
let (area, bytes) = package(&mut home, &Value::Null).unwrap();
assert_eq!(area, "Crew");
assert_eq!(home.project.offline_areas.len(), 1);
let repacked = phone(&temp, &bytes, "repacked");
assert_eq!(repacked.map().name, "Crew (offline)");
assert_eq!(count(&repacked, "Edited at home"), 1);
assert_eq!(repacked.project.field_origin.as_ref().unwrap().area, home.project.offline_areas[0].id);

home.run("offline.sync", &json!({"name": "Crew"})).unwrap();
home.run("map.activate", &json!({"name": source})).unwrap();
assert_eq!(count(&home, "Edited at home"), 1);
home.run("offline.remove", &json!({"name": "Crew"})).unwrap();
assert!(home.project.maps.iter().all(|map| map.name != "Renamed at home"));
assert!(home.project.maps.iter().any(|map| map.name == "Crew (offline)"));
}

/// One package, copied to two phones (emailed to a crew, AirDropped to a second device). Each fixes
/// a city and captures a point. A is brought back first. B's copy is of a state the area has left:
/// bringing it back would undo A's fix and delete A's point (B's copy lacks it), so it is refused
Expand Down
27 changes: 27 additions & 0 deletions crates/engine/src/cmd/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,21 @@ fn import_maps(s: &mut Session, p: &Value) -> CmdResult {
};
let mut names = Vec::new();
let first_map = s.project.maps.len();
let mut detached_offline = 0;
let mut renamed_offline = 0;
for mut m in maps.into_iter().take(MAX_IMPORTED_MAPS) {
// A map imported without its home area becomes an independent copy.
detached_offline += usize::from(m.offline_area_id.take().is_some());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug (medium confidence, about 0.6): detaching the association does not make the imported copy independent. An imported map keeps its <area> (offline) title, and offline_map_index still matches any map with offline_area_id == None and that title. So if the destination project has an area with the same name and no map with that title, the imported map is adopted as the area's offline copy. offline.open then reuses it and unbinds its table links, and offline.sync treats its contents as the area's edits, which can delete source features the copy lacks.

Suggested direction: when a detached map's title carries the offline suffix, rename it so title matching can't claim it, and say so in the same notice. Add a test: an import of such a map gives the notice and does not make offline.open reuse it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 22aaf398e: an imported map titled <area> (offline) is renamed <area> (imported) with a notice, so title matching can't adopt it. an_imported_offline_titled_map_is_not_taken_for_an_area_copy imports such a map into a project with a Crew area and checks that offline.open makes its own copy.

// Nor may its title make it one: an older project matches its offline copy by the name
// "<area> (offline)", and syncing an unrelated map would delete what it lacks.
let suffix = super::field::OFFLINE;
if m.name.len() > suffix.len()
&& m.name.is_char_boundary(m.name.len() - suffix.len())
&& m.name[m.name.len() - suffix.len()..].eq_ignore_ascii_case(suffix)
{
m.name = format!("{} (imported)", &m.name[..m.name.len() - suffix.len()]);
renamed_offline += 1;
}
// A binding in a file is no authority to use a login here.
crate::s3_connections::clear_bindings(m.addresses_mut());
crate::project::clear_login_bindings(m.addresses_mut());
Expand All @@ -549,6 +563,18 @@ fn import_maps(s: &mut Session, p: &Value) -> CmdResult {
m.renew_instance_id();
s.project.maps.push(m);
}
if detached_offline > 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AGENTS.md (Never silent, "Test both ways"): the detach notice added here has no test. an_imported_offline_titled_map_is_not_taken_for_an_area_copy only checks the rename notice, and only by contains("renamed"). Please add:

  • a lossy case: importing a map that carries an offline_area_id gives the "detached" notice with the count;
  • a clean case: importing a map with no association and an ordinary title gives neither notice.

Confidence: high that the gap exists (grep finds no test referencing "detached from").

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 45e4fe565: importing_maps_says_when_an_area_association_is_dropped imports a saved project whose map carries its area's ID and checks the "1 imported map(s) were detached" notice, then imports an ordinary map and checks that neither the detach nor the rename notice appears.

s.notify(
format!("{detached_offline} imported map(s) were detached from their offline areas, which aren't imported with maps"),
false,
);
}
if renamed_offline > 0 {
s.notify(
format!("{renamed_offline} imported map(s) titled as offline copies were renamed \"… (imported)\", so they aren't taken for this project's offline maps"),
false,
);
}
if let Some(message) = super::design::display_area_messages(s.project.maps.iter().skip(first_map).flat_map(|map| map.layers.iter())) {
s.notify(message, false);
}
Expand Down Expand Up @@ -747,6 +773,7 @@ fn template(p: &Project) -> (Project, TemplateLosses) {
})
.saturating_add(t.tables.iter().filter(|table| table.source.is_some()).count());
for m in &mut t.maps {
m.offline_area_id = None;
for l in &mut m.layers {
l.dataset.features.clear();
l.selection.clear();
Expand Down
4 changes: 3 additions & 1 deletion crates/engine/src/cmd/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub fn commands() -> Vec<C> {
C::new("map.rename", "Rename Map", "Layers", |s, p| {
let (i, name) = rename_choice(&super::InputSource::input_context(&*s), p)?;
let old = s.project.maps.get(i).map(|m| m.name.clone()).ok_or("no map")?;
super::workspace::bind_offline_map_before_rename(&mut s.project, i)?;
if let Some(m) = s.project.maps.get_mut(i) {
m.name = name.clone();
}
Expand Down Expand Up @@ -263,6 +264,7 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult {
let i = map_at(s, p)?;
let mut m = s.project.maps.get(i).ok_or("no map")?.clone();
m.name = super::copy_name(ps(p, "name"), &m.name, |n| s.project.maps.iter().any(|x| x.name.eq_ignore_ascii_case(n)));
m.offline_area_id = None;
m.label_lock = None;
m.label_lock_line_anchors.clear();
m.renew_instance_id();
Expand All @@ -284,7 +286,7 @@ fn duplicate_map(s: &mut Session, p: &Value) -> CmdResult {

/// Points layout map frames, presentation map pages and offline areas at a renamed map (#287),
/// so they keep finding it.
fn follow_renamed_map(s: &mut Session, old: &str, new: &str) {
pub(crate) fn follow_renamed_map(s: &mut Session, old: &str, new: &str) {
let renamed = |m: &mut String| {
if m.eq_ignore_ascii_case(old) {
*m = new.to_string();
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ fn legacy_field_package(project: &Project) -> bool {
/// Unlinks the maps of the project just opened that hold only some of a table's rows from the
/// GeoPackage or PostGIS tables they were loaded from, as offline copies made now are, and says so:
/// every map of a field project (`field`, opened from a field package, or one with a field
/// origin), else a home project's offline maps (`<area> (offline)`). Saved before offline copies
/// origin), else a home project's offline maps. Saved before offline copies
/// were unlinked, they would delete from the table every row they lack.
fn unlink_partial_copies(s: &mut Session, field: bool) {
if field || s.project.field_origin.is_some() {
Expand Down
Loading
Loading