-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Setting to choose coordinate display format #526
Merged
Merged
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2736f57
feat: Add coordinate system page to settings with ability to change b…
luandro cf4be4f
fix: Add latlon to formatCoords switch
luandro 81953d0
feat: Add information on GpsModal based on selected coordinate system
luandro 330c882
feat: Show coordinates on observation screens based on selected system
luandro 32fbe0a
fix: Add value check before setting AsyncStorage
luandro 2c07212
fix: Have Coordinate System before About Mapeo on configuration menu
luandro f853030
fix: Remove Decimal Degrees which is same as Lat Long
luandro 873a6b5
fix: Round decimal degrees to 6 decimal places
luandro 48e815b
chore: Cleanup and add better formating for DMS
luandro 930346b
fix: Add 3 decimal degrees to DMS coordinates
luandro 70b3d1c
fix: Pass lat/lon explicitly within an object on formatting functions
luandro 31c127a
fix: Use Decimal Degrees naming instead of lat/lon
luandro 72fa39e
chore: Extract messages and add DD to naming in config menu
luandro e4ee80d
chore: Add settings context
luandro 4caa862
chore: Update screens and components to use settings context
luandro d4499e8
fixes & cleanup
gmaclennan a41fddf
remove unused message
gmaclennan 9e8eeaa
Update language to coordinate format
gmaclennan 4b15d25
Use Number.toFixed() for rounding coordinates
gmaclennan ecc7245
Merge branch 'develop' into custom-coord-system
gmaclennan 46fe030
Fix flow type error
gmaclennan fa8a738
Add comma to DMS formatting
gmaclennan 7e02d19
Format lat/long DD according to Nat Geo style guide
gmaclennan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// @flow | ||
import * as React from "react"; | ||
import AsyncStorage from "@react-native-community/async-storage"; | ||
|
||
const STORE_KEY = "@MapeoSettings@36"; | ||
|
||
export type SettingsContextType = { | ||
coordinateSystem?: string, | ||
}; | ||
|
||
const initialState = { | ||
coordinateSystem: "utm", | ||
}; | ||
|
||
const defaultContext = [initialState, () => {}]; | ||
const SettingsContext = React.createContext(defaultContext); | ||
|
||
function reducer(state: State, action: Action): State { | ||
switch (action.type) { | ||
case "set": { | ||
return action.value; | ||
} | ||
case "set_coordinate_system": { | ||
try { | ||
return { ...state, coordinateSystem: action.value }; | ||
} catch { | ||
return state; | ||
} | ||
} | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
const getData = async dispatch => { | ||
try { | ||
// AsyncStorage.clear() | ||
const state = await AsyncStorage.getItem(STORE_KEY); | ||
if (state) { | ||
const parsedState = JSON.parse(state); | ||
dispatch({ type: "set", value: parsedState }); | ||
return parsedState; | ||
} else return initialState; | ||
} catch (e) { | ||
console.log("Failed to fetch the data from storage"); | ||
return initialState; | ||
} | ||
}; | ||
|
||
const saveData = async value => { | ||
try { | ||
const stringified = JSON.stringify(value); | ||
await AsyncStorage.setItem(STORE_KEY, stringified); | ||
return value; | ||
} catch (e) { | ||
console.log("Failed to save the data to the storage"); | ||
} | ||
}; | ||
|
||
export const SettingsProvider = ({ children }: { children: React.Node }) => { | ||
const didMountRef = React.useRef(false); | ||
const [state, dispatch] = React.useReducer(reducer, defaultContext[0]); | ||
const contextValue = React.useMemo(() => [state, dispatch], [ | ||
state, | ||
dispatch, | ||
]); | ||
React.useEffect(() => { | ||
if (didMountRef.current) { | ||
saveData(contextValue[0]); | ||
} else { | ||
didMountRef.current = true; | ||
getData(dispatch); | ||
} | ||
}, [contextValue]); | ||
return ( | ||
<SettingsContext.Provider | ||
value={{ settings: contextValue[0], dispatch: contextValue[1] }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a new object on every render |
||
> | ||
{children} | ||
</SettingsContext.Provider> | ||
); | ||
}; | ||
|
||
export default SettingsContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,15 +121,30 @@ export function getLastPhotoAttachment( | |
return filterPhotosFromAttachments(attachments).pop(); | ||
} | ||
|
||
export function formatCoords({ | ||
lon, | ||
lat, | ||
format = "utm", | ||
}: { | ||
lon: number, | ||
lat: number, | ||
format?: "utm", | ||
}): string { | ||
// Coordinates conversions | ||
function toDegreesMinutesAndSeconds(coordinate) { | ||
const absolute = Math.abs(coordinate); | ||
const degrees = Math.floor(absolute); | ||
const minutesNotTruncated = (absolute - degrees) * 60; | ||
const minutes = Math.floor(minutesNotTruncated); | ||
const seconds = (minutesNotTruncated - minutes) * 60; | ||
const decimals = 3; | ||
const formattedSeconds = Number( | ||
Math.round(seconds + "e" + decimals) + "e-" + decimals | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
return `${degrees}° ${minutes}' ${formattedSeconds}"`; | ||
} | ||
|
||
function convertToDMS({ lat, lon }) { | ||
const latitude = toDegreesMinutesAndSeconds(lat); | ||
const latitudeCardinal = lat >= 0 ? "N" : "S"; | ||
|
||
const longitude = toDegreesMinutesAndSeconds(lon); | ||
const longitudeCardinal = lon >= 0 ? "E" : "W"; | ||
return `${latitude} ${latitudeCardinal} ${longitude} ${longitudeCardinal}`; | ||
} | ||
|
||
function convertToUTM({ lat, lon }) { | ||
try { | ||
let { easting, northing, zoneNum, zoneLetter } = fromLatLon(lat, lon); | ||
easting = leftPad(easting.toFixed(), 6, "0"); | ||
|
@@ -143,6 +158,38 @@ export function formatCoords({ | |
} | ||
} | ||
|
||
function formatDD({ lat, lon }) { | ||
const decimals = 6; | ||
const formattedLat = Number( | ||
Math.round(lat + "e" + decimals) + "e-" + decimals | ||
); | ||
const formattedLon = Number( | ||
Math.round(lon + "e" + decimals) + "e-" + decimals | ||
); | ||
return `lat ${formattedLat} lon ${formattedLon}`; | ||
} | ||
|
||
export function formatCoords({ | ||
lon, | ||
lat, | ||
format = "utm", | ||
}: { | ||
lon: number, | ||
lat: number, | ||
format?: "utm" | "dd" | "dms", | ||
}): string { | ||
switch (format) { | ||
case "dd": | ||
return formatDD({ lat, lon }); | ||
case "utm": | ||
return convertToUTM({ lat, lon }); | ||
case "dms": | ||
return convertToDMS({ lat, lon }); | ||
default: | ||
return convertToUTM({ lat, lon }); | ||
} | ||
} | ||
|
||
export function getProp(tags: any, fieldKey: Key, defaultValue: any) { | ||
// TODO: support deeply nested tags. | ||
const shallowKey = Array.isArray(fieldKey) ? fieldKey[0] : fieldKey; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a
usePersistedState
hook, so best use that rather than have two different implementations of this.