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
31 changes: 22 additions & 9 deletions apps/rustgis-mobile/java/org/rustgis/mobile/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
* picked purpose name path a chosen file, copied to the app's storage
* ended purpose a request whose every file failed to be copied
* cancelled a picker closed without a choice
* saved name a file written where the user chose
* saved name an export written where the user chose
* project-saved name a project written where the user chose
* project-save-failed reason a project save that failed or was cancelled
* position lon lat accuracy time elapsed where the device is (accuracy in metres, or
* empty; time in milliseconds since 1970; elapsed in
* nanoseconds since boot, from getElapsedRealtimeNanos)
Expand Down Expand Up @@ -82,6 +84,7 @@ public class MainActivity extends NativeActivity {
private File photo;
private String saveName = "";
private byte[] saveBytes;
private boolean saveProject;

@Override
protected void onCreate(android.os.Bundle saved) {
Expand Down Expand Up @@ -443,15 +446,16 @@ public void pick(final String purpose) {
});
}

/** Asks the user where to write a file, then writes it; a "saved" or "failed" event follows. */
public void save(final String name, final byte[] bytes) {
/** Asks where to write a file. Project saves return a distinct result to the save prompt. */
public void save(final String name, final byte[] bytes, final boolean project) {
runOnUiThread(() -> {
if (saveBytes != null) {
add("failed\t" + name + " wasn't saved: another file is waiting for its place to be chosen.");
if (saveBytes != null || picking) {
saveFailed(project, name, "another file is being chosen.");
return;
}
saveName = name;
saveBytes = bytes;
saveProject = project;
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// The file's own type where Android knows its extension: some providers name the
Expand All @@ -467,6 +471,10 @@ public void save(final String name, final byte[] bytes) {
});
}

private void saveFailed(boolean project, String name, String reason) {
add((project ? "project-save-failed\t" : "failed\t") + name + " wasn't saved: " + reason);
}

/** What happened since the last call, oldest first, one event to a line. */
public String takeEvents() {
String why = refusal.getAndSet(null);
Expand Down Expand Up @@ -543,7 +551,11 @@ private boolean start(Intent intent, int request) {
}
return true;
} catch (RuntimeException e) {
add("failed\tThis device has no file picker: " + e.getMessage());
if (request == SAVE) {
saveFailed(saveProject, saveName, "the file picker couldn't be shown: " + e.getMessage());
} else {
add("failed\tThis device has no file picker: " + e.getMessage());
}
return false;
}
}
Expand Down Expand Up @@ -667,11 +679,12 @@ protected void onActivityResult(int request, int result, Intent data) {
} else if (request == SAVE) {
final byte[] bytes = saveBytes;
final String name = saveName;
final boolean project = saveProject;
saveBytes = null;
final Uri uri = result == RESULT_OK && data != null ? data.getData() : null;
if (uri == null || bytes == null) {
// Said, as the app has already told the user to choose a place for it.
add("failed\t" + name + " wasn't saved: no place was chosen for it.");
saveFailed(project, name, "no place was chosen for it.");
answered();
return;
}
Expand All @@ -684,9 +697,9 @@ protected void onActivityResult(int request, int result, Intent data) {
out.write(bytes);
}
// Said once the file is closed: a failure to close is a failure to save.
add("saved\t" + name);
add((project ? "project-saved\t" : "saved\t") + name);
} catch (Throwable e) {
add("failed\t" + name + " wasn't saved: " + e.getMessage());
saveFailed(project, name, e.getMessage());
} finally {
answered();
}
Expand Down
40 changes: 27 additions & 13 deletions apps/rustgis-mobile/src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ impl Shared {
self.add(Event::Failed(text));
}

/// Project picker outcomes also release the front end's pending save.
fn save_result(&self, project: bool, result: Result<String, String>) {
self.add(if project {
Event::ProjectSave(result)
} else {
match result {
Ok(name) => Event::Saved { name },
Err(error) => Event::Failed(error),
}
});
}

fn showing(&self) -> bool {
*self.showing.lock().unwrap_or_else(|e| e.into_inner()) > 0
}
Expand All @@ -108,7 +120,7 @@ enum Asked {
Open { purpose: String },
/// A file to save: offered under `name`, written to `temporary` until the picker has put it
/// where the user chose.
Save { name: String, temporary: PathBuf },
Save { name: String, temporary: PathBuf, project: bool },
}

struct PickerIvars {
Expand Down Expand Up @@ -162,13 +174,14 @@ impl PickerDelegate {
wake(self.mtm());
}
None => {}
Some(Asked::Save { name, temporary }) => {
if urls.is_empty() {
shared.failed(i18n::fmt("{name} wasn't saved: no place was chosen for it.", &[("name", &name)]));
Some(Asked::Save { name, temporary, project }) => {
let result = if urls.is_empty() {
Err(i18n::fmt("{name} wasn't saved: no place was chosen for it.", &[("name", &name)]))
} else {
// The picker has copied the file where the user chose.
shared.add(Event::Saved { name });
}
Ok(name)
};
shared.save_result(project, result);
remove_with_folder(&temporary);
count(&shared.showing, false);
wake(self.mtm());
Expand Down Expand Up @@ -847,6 +860,7 @@ struct Saving {
name: String,
temporary: PathBuf,
done: Written,
project: bool,
}

impl Drop for Ios {
Expand Down Expand Up @@ -894,21 +908,21 @@ impl Ios {
None => return,
}
};
let Some(Saving { name, temporary, .. }) = self.saving.borrow_mut().take() else { return };
let Some(Saving { name, temporary, project, .. }) = self.saving.borrow_mut().take() else { return };
let presenter = written.and_then(|()| self.presenter());
let presenter = match presenter {
Ok(presenter) => presenter,
Err(error) => {
remove_with_folder(&temporary);
self.shared.failed(i18n::fmt("{name} wasn't saved: {error}", &[("name", &name), ("error", &error)]));
self.shared.save_result(project, Err(i18n::fmt("{name} wasn't saved: {error}", &[("name", &name), ("error", &error)])));
return;
}
};
let url = NSURL::fileURLWithPath(&NSString::from_str(&temporary.to_string_lossy()));
let urls = NSArray::from_retained_slice(&[url]);
let picker =
UIDocumentPickerViewController::initForExportingURLs_asCopy(UIDocumentPickerViewController::alloc(self.mtm), &urls, true);
let delegate = PickerDelegate::new(self.mtm, self.shared.clone(), self.folders.clone(), Asked::Save { name, temporary });
let delegate = PickerDelegate::new(self.mtm, self.shared.clone(), self.folders.clone(), Asked::Save { name, temporary, project });
picker.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
self.picker.replace(Some(delegate));
// Declined, it is seen on a later frame ([`Ios::check_presented`]).
Expand Down Expand Up @@ -954,9 +968,9 @@ impl Ios {
return;
}
match self.picker.borrow().as_ref().and_then(|delegate| delegate.ivars().asked.borrow_mut().take()) {
Some(Asked::Save { name, temporary }) => {
Some(Asked::Save { name, temporary, project }) => {
remove_with_folder(&temporary);
self.shared.failed(i18n::fmt("{name} wasn't saved: {error}", &[("name", &name), ("error", &error)]));
self.shared.save_result(project, Err(i18n::fmt("{name} wasn't saved: {error}", &[("name", &name), ("error", &error)])));
}
// A request for files that will get none ends by name.
Some(Asked::Open { purpose }) => {
Expand Down Expand Up @@ -1017,7 +1031,7 @@ impl Platform for Ios {
self.present(&presenter, &picker, false)
}

fn save_file(&self, name: &str, bytes: Vec<u8>) -> Result<(), String> {
fn save_file(&self, name: &str, bytes: Vec<u8>, project: bool) -> Result<(), String> {
// Refused, not dropped: the caller is told the file wasn't saved.
self.not_already_asking(i18n::fmt("{name} wasn't saved: another file is waiting for its place to be chosen.", &[("name", name)]))?;
// The export picker copies a file: it is written to the app's tmp folder first, under
Expand All @@ -1035,7 +1049,7 @@ impl Platform for Ios {
return Err(i18n::fmt("{name} wasn't saved: {error}", &[("name", name), ("error", &e.to_string())]));
}
// Waiting from now: another save is refused until this one's picker has answered.
self.saving.replace(Some(Saving { name: name.to_string(), temporary, done }));
self.saving.replace(Some(Saving { name: name.to_string(), temporary, done, project }));
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions apps/rustgis-mobile/src/phone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ impl Platform for Phone {
})
}

fn save_file(&self, name: &str, bytes: Vec<u8>) -> Result<(), String> {
fn save_file(&self, name: &str, bytes: Vec<u8>, project: bool) -> Result<(), String> {
self.java(|env, activity| {
let name = env.new_string(name)?;
let bytes = env.byte_array_from_slice(&bytes)?;
env.call_method(
activity,
jni_str!("save"),
jni_sig!((name: JString, bytes: [jbyte]) -> void),
&[JValue::Object(&name), JValue::Object(&bytes)],
jni_sig!((name: JString, bytes: [jbyte], project: boolean) -> void),
&[JValue::Object(&name), JValue::Object(&bytes), JValue::Bool(project)],
)?
.v()
})
Expand Down
7 changes: 5 additions & 2 deletions apps/rustgis-mobile/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl<P: Phone> App<P> {
self.app.photos.push(path);
}
Event::Saved { name } => self.app.session.notify(i18n::fmt("Saved {name}.", &[("name", &name)]), false),
Event::ProjectSave(result) => self.app.complete_project_save(result),
Event::Position(fix) if self.locating => self.app.report_position(fix),
// A fix that was on its way when positioning was turned off.
Event::Position(_) => {}
Expand Down Expand Up @@ -451,7 +452,9 @@ fn use_picker<P: Phone + 'static>(
}
}));
let saver = phone.clone();
app.services.download = Some(Box::new(move |name: &str, bytes: Vec<u8>| saver.save_file(name, bytes)));
app.services.download = Some(Box::new(move |name: &str, bytes: Vec<u8>| saver.save_file(name, bytes, false)));
let saver = phone.clone();
app.services.save_project = Some(Box::new(move |name: &str, bytes: Vec<u8>| saver.save_file(name, bytes, true)));
app.services.inbox = Some(inbox.clone());
let (camera, photographing) = (phone.clone(), asked.clone());
app.services.take_photo = Some(Box::new(move || {
Expand Down Expand Up @@ -722,7 +725,7 @@ mod tests {
self.0.set(true);
Ok(())
}
fn save_file(&self, _: &str, _: Vec<u8>) -> Result<(), String> {
fn save_file(&self, _: &str, _: Vec<u8>, _: bool) -> Result<(), String> {
Ok(())
}
fn locate(&self, _: bool) -> Result<(), String> {
Expand Down
1 change: 1 addition & 0 deletions apps/rustgis-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e
let copernicus_ctx = ctx.clone();
let clipboard_ctx = ctx.clone();
Services {
save_project: None,
// The browser's camera isn't asked yet (a phone's shell asks its own).
take_photo: None,
// A text field's Paste: the page reads the clipboard (Firefox and Safari show a Paste
Expand Down
5 changes: 5 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@ pub struct Session {
/// The state last saved, or opened from its file; `None` when no state of the open project is
/// (recovered work). The project has unsaved changes while another is open ([`Session::dirty`]).
saved: Option<u64>,
/// How many times [`Session::mark_unsaved`] has run, so a save that finishes later can tell
/// whether a change outside undo came after the state it wrote.
unsaved_marks: u64,
/// Favourite, recent and saved data sources (kept with the app's preferences, not the
/// project).
pub sources: sources::Sources,
Expand Down Expand Up @@ -1295,6 +1298,7 @@ impl Session {
state: 0,
states: 0,
saved: Some(0),
unsaved_marks: 0,
sources: sources::Sources::default(),
extensions: extensions::State::default(),
connections: connections::Connections::default(),
Expand Down Expand Up @@ -3288,6 +3292,7 @@ impl Session {
/// crash: no state of it is in a file).
pub fn mark_unsaved(&mut self) {
self.saved = None;
self.unsaved_marks = self.unsaved_marks.wrapping_add(1);
}

/// A state of the project not given before in this session, for a change just made.
Expand Down
Loading
Loading