-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(offline-mode): add downloaded data to store
* feat(electron): add downloaded data * chore(data): change soil moisture color map
- Loading branch information
Showing
17 changed files
with
219 additions
and
45 deletions.
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,17 @@ | ||
import {DownloadedData} from '../types/downloaded-data'; | ||
|
||
export const SET_DOWNLOADED_DATA = 'SET_DOWNLOADED_DATA'; | ||
|
||
export interface SetDownloadedDataAction { | ||
type: typeof SET_DOWNLOADED_DATA; | ||
data: DownloadedData; | ||
} | ||
|
||
const setDownloadedDataAction = ( | ||
data: DownloadedData | ||
): SetDownloadedDataAction => ({ | ||
type: SET_DOWNLOADED_DATA, | ||
data | ||
}); | ||
|
||
export default setDownloadedDataAction; |
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
56 changes: 35 additions & 21 deletions
56
src/scripts/components/layer-list-item/layer-list-item.tsx
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 |
---|---|---|
@@ -1,39 +1,53 @@ | ||
import React, {FunctionComponent} from 'react'; | ||
import {FormattedMessage} from 'react-intl'; | ||
|
||
// @ts-ignore | ||
import {isElectron, downloadUrl} from 'electronHelpers'; // this is an webpack alias | ||
import {replaceUrlPlaceholders} from '../../libs/replace-url-placeholders'; | ||
import config from '../../config/main'; | ||
|
||
import {LayerListItem as LayerListItemType} from '../../types/layer-list'; | ||
|
||
import styles from './layer-list-item.styl'; | ||
|
||
interface Props { | ||
layer: LayerListItemType; | ||
isMainSelected: boolean; | ||
isDownloaded: boolean; | ||
onSelect: (id: string, isMain: boolean) => void; | ||
onDownload: null | ((id: string) => void); | ||
} | ||
|
||
const LayerListItem: FunctionComponent<Props> = ({ | ||
layer, | ||
isMainSelected, | ||
onSelect, | ||
onDownload | ||
}) => ( | ||
<div className={styles.layerItem} onClick={() => onSelect(layer.id, true)}> | ||
<span className={styles.layerTitle}>{layer.name}</span> | ||
{isMainSelected && ( | ||
<button | ||
className={styles.compare} | ||
onClick={event => { | ||
onSelect(layer.id, false); | ||
event.stopPropagation(); | ||
}}> | ||
<FormattedMessage id={'layerSelector.compare'} /> | ||
</button> | ||
)} | ||
{onDownload && ( | ||
<button onClick={() => onDownload(layer.id)}>Download</button> | ||
)} | ||
</div> | ||
); | ||
isDownloaded, | ||
onSelect | ||
}) => { | ||
const onDownload = () => | ||
isElectron() && | ||
downloadUrl( | ||
replaceUrlPlaceholders(config.api.layerOfflinePackage, {id: layer.id}) | ||
); | ||
|
||
return ( | ||
<div className={styles.layerItem} onClick={() => onSelect(layer.id, true)}> | ||
<span className={styles.layerTitle}>{layer.name}</span> | ||
{isMainSelected && ( | ||
<button | ||
className={styles.compare} | ||
onClick={event => { | ||
onSelect(layer.id, false); | ||
event.stopPropagation(); | ||
}}> | ||
<FormattedMessage id={'layerSelector.compare'} /> | ||
</button> | ||
)} | ||
{isElectron() && !isDownloaded && ( | ||
<button onClick={onDownload}>Download</button> | ||
)} | ||
{isElectron() && isDownloaded && <button disabled>Ready</button>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default LayerListItem; |
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 |
---|---|---|
@@ -1,15 +1,61 @@ | ||
/** | ||
* Webpack will only include this module when building for electron | ||
*/ | ||
import path from 'path'; | ||
import {remote, ipcRenderer} from 'electron'; | ||
import {Dispatch} from 'redux'; | ||
import setDownloadedDataAction, { | ||
SetDownloadedDataAction | ||
} from '../actions/set-downloaded-data'; | ||
|
||
import {remote} from 'electron'; | ||
console.log('Electron Helpers loaded'); | ||
|
||
const app = remote.app; | ||
const webContents = remote.getCurrentWebContents(); | ||
let isConnected = false; | ||
|
||
// Returns if environment is electron | ||
export function isElectron() { | ||
return true; | ||
} | ||
|
||
// Returns if the machine is offline | ||
export function isOffline() { | ||
return !navigator.onLine; | ||
} | ||
|
||
/** | ||
* Connects this module to the store so that incoming ipc messages can be | ||
* dispatched | ||
*/ | ||
export function connectToStore(dispatch: Dispatch<SetDownloadedDataAction>) { | ||
if (isConnected) { | ||
console.warn('Electron Helpers: Store already connected! Doing nothing...'); | ||
return; | ||
} | ||
|
||
isConnected = true; | ||
ipcRenderer.on('offline-update', (event, message) => { | ||
const data = JSON.parse(message); | ||
dispatch(setDownloadedDataAction(data)); | ||
}); | ||
} | ||
|
||
// Returns the url template for offline usage | ||
export function getOfflineTilesUrl() { | ||
return path.join( | ||
app.getPath('downloads'), | ||
'{id}', | ||
'tiles', | ||
'{timeIndex}', | ||
'{z}', | ||
'{x}', | ||
'{reverseY}.png' | ||
); | ||
} | ||
|
||
// Downloads the content at the given URL | ||
// the download will be handled by the electron 'will-download' handler) | ||
export function downloadUrl(url: string) { | ||
webContents.downloadURL(url); | ||
} |
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,25 @@ | ||
import { | ||
SET_DOWNLOADED_DATA, | ||
SetDownloadedDataAction | ||
} from '../actions/set-downloaded-data'; | ||
|
||
import {DownloadedData} from '../types/downloaded-data'; | ||
|
||
const initialState: DownloadedData = { | ||
layers: [], | ||
stories: [] | ||
}; | ||
|
||
function downloadedDataReducer( | ||
state: DownloadedData = initialState, | ||
action: SetDownloadedDataAction | ||
): DownloadedData { | ||
switch (action.type) { | ||
case SET_DOWNLOADED_DATA: | ||
return action.data; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export default downloadedDataReducer; |
Oops, something went wrong.