From 5e187e4f1aee4375b50d238ba837e97c152f1e43 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 22 May 2017 20:22:50 +0000 Subject: [PATCH] Handle lat/long --- .../android/app/src/main/AndroidManifest.xml | 1 + .../src/components/screens/note.js | 20 +++++------ ReactNativeClient/src/database.js | 2 -- ReactNativeClient/src/geolocation.js | 34 +++++++++++++++++++ ReactNativeClient/src/models/note.js | 33 ++++++++++++++++++ ReactNativeClient/src/root.js | 2 +- 6 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 ReactNativeClient/src/geolocation.js diff --git a/ReactNativeClient/android/app/src/main/AndroidManifest.xml b/ReactNativeClient/android/app/src/main/AndroidManifest.xml index f6e45faa5f..b32d72138b 100644 --- a/ReactNativeClient/android/app/src/main/AndroidManifest.xml +++ b/ReactNativeClient/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + { + this.setState({ note: note }); + }); + } } noteComponent_change = (propName, propValue) => { @@ -37,14 +43,7 @@ class NoteScreenComponent extends React.Component { } saveNoteButton_press = () => { - Note.save(this.state.note).then((note) => { - this.props.dispatch({ - type: 'NOTES_UPDATE_ONE', - note: note, - }); - }).catch((error) => { - Log.warn('Cannot save note', error); - }); + Note.save(this.state.note); } render() { @@ -63,7 +62,8 @@ class NoteScreenComponent extends React.Component { const NoteScreen = connect( (state) => { return { - note: state.selectedNoteId ? Note.byId(state.notes, state.selectedNoteId) : Note.new(state.selectedFolderId), + noteId: state.selectedNoteId, + folderId: state.selectedFolderId, }; } )(NoteScreenComponent) diff --git a/ReactNativeClient/src/database.js b/ReactNativeClient/src/database.js index 1fdf10e9fc..8de61b542b 100644 --- a/ReactNativeClient/src/database.js +++ b/ReactNativeClient/src/database.js @@ -330,8 +330,6 @@ class Database { default: Database.formatValue(row.field_type, row.field_default), }); } - - Log.info(this.tableFields_); }); diff --git a/ReactNativeClient/src/geolocation.js b/ReactNativeClient/src/geolocation.js new file mode 100644 index 0000000000..f5b325a767 --- /dev/null +++ b/ReactNativeClient/src/geolocation.js @@ -0,0 +1,34 @@ +class Geolocation { + + static currentPosition_testResponse() { + return { + mocked: false, + timestamp: (new Date()).getTime(), + coords: { + speed: 0, + heading: 0, + accuracy: 20, + longitude: -3.4596633911132812, + altitude: 0, + latitude: 48.73219093634444 + } + } + } + + static currentPosition(options = null) { + if (!options) options = {}; + if (!('enableHighAccuracy' in options)) options.enableHighAccuracy = true; + if (!('timeout' in options)) options.timeout = 10000; + + return new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition((data) => { + resolve(data); + }, (error) => { + rejec(error); + }, options); + }); + } + +} + +export { Geolocation }; \ No newline at end of file diff --git a/ReactNativeClient/src/models/note.js b/ReactNativeClient/src/models/note.js index 257bb52426..b8db02c8ac 100644 --- a/ReactNativeClient/src/models/note.js +++ b/ReactNativeClient/src/models/note.js @@ -1,5 +1,6 @@ import { BaseModel } from 'src/base-model.js'; import { Log } from 'src/log.js'; +import { Geolocation } from 'src/geolocation.js'; class Note extends BaseModel { @@ -39,6 +40,38 @@ class Note extends BaseModel { } + static save(o, options = null) { + if (!options) options = {}; + if (!('updateLatLong' in options)) options.updateLatLong = true; + + return super.save(o, options).then((note) => { + this.dispatch({ + type: 'NOTES_UPDATE_ONE', + note: note, + }); + + if (options.updateLatLong && !note.latitude && !note.longitude) { + Log.info('Updating lat/long of note...'); + let geoData = null; + Geolocation.currentPosition().then((data) => { + Log.info('Got lat/long'); + geoData = data; + return Note.load(note.id); + }).then((note) => { + if (!note) return; // Has been deleted in the meantime + note.longitude = geoData.coords.longitude; + note.latitude = geoData.coords.latitude; + note.altitude = geoData.coords.altitude; + Note.save(note, { updateLatLong: false }); + }).catch((error) => { + Log.info('Cannot get location:', error); + }); + } + + return note; + }); + } + } export { Note }; \ No newline at end of file diff --git a/ReactNativeClient/src/root.js b/ReactNativeClient/src/root.js index 95721a25ae..f1275b1eff 100644 --- a/ReactNativeClient/src/root.js +++ b/ReactNativeClient/src/root.js @@ -164,7 +164,7 @@ class AppComponent extends React.Component { componentDidMount() { let db = new Database(); //db.setDebugEnabled(Registry.debugMode()); - db.setDebugEnabled(true); + db.setDebugEnabled(false); BaseModel.dispatch = this.props.dispatch; BaseModel.db_ = db;