diff options
author | Stefan Malzner <stefan@adlk.io> | 2018-11-24 20:20:31 +0100 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2018-11-24 20:20:31 +0100 |
commit | df4d195ec7135c76f8f5de54c3939d2e74e80d4a (patch) | |
tree | 9a669462fa769d47e7be055b2026be7b58dc14a7 /src/features/delayApp | |
parent | feat(App): Lay groundwork for general themeing support (diff) | |
download | ferdium-app-df4d195ec7135c76f8f5de54c3939d2e74e80d4a.tar.gz ferdium-app-df4d195ec7135c76f8f5de54c3939d2e74e80d4a.tar.zst ferdium-app-df4d195ec7135c76f8f5de54c3939d2e74e80d4a.zip |
Delay app based on featureAPI settings
Diffstat (limited to 'src/features/delayApp')
-rw-r--r-- | src/features/delayApp/Component.js | 86 | ||||
-rw-r--r-- | src/features/delayApp/index.js | 67 | ||||
-rw-r--r-- | src/features/delayApp/styles.js | 26 |
3 files changed, 179 insertions, 0 deletions
diff --git a/src/features/delayApp/Component.js b/src/features/delayApp/Component.js new file mode 100644 index 000000000..2bfa1162e --- /dev/null +++ b/src/features/delayApp/Component.js | |||
@@ -0,0 +1,86 @@ | |||
1 | import React, { Component } from 'react'; | ||
2 | import PropTypes from 'prop-types'; | ||
3 | import { inject, observer } from 'mobx-react'; | ||
4 | import { defineMessages, intlShape } from 'react-intl'; | ||
5 | import injectSheet from 'react-jss'; | ||
6 | |||
7 | import Button from '../../components/ui/Button'; | ||
8 | |||
9 | import { config } from './'; | ||
10 | import styles from './styles'; | ||
11 | |||
12 | const messages = defineMessages({ | ||
13 | headline: { | ||
14 | id: 'feature.delayApp.headline', | ||
15 | defaultMessage: '!!!Please purchase license to skip waiting', | ||
16 | }, | ||
17 | action: { | ||
18 | id: 'feature.delayApp.action', | ||
19 | defaultMessage: '!!!Get a Franz Supporter License', | ||
20 | }, | ||
21 | text: { | ||
22 | id: 'feature.delayApp.text', | ||
23 | defaultMessage: '!!!Franz will continue in {seconds} seconds.', | ||
24 | }, | ||
25 | }); | ||
26 | |||
27 | export default @inject('actions') @observer @injectSheet(styles) class DelayApp extends Component { | ||
28 | static propTypes = { | ||
29 | // eslint-disable-next-line | ||
30 | classes: PropTypes.object.isRequired, | ||
31 | }; | ||
32 | |||
33 | static contextTypes = { | ||
34 | intl: intlShape, | ||
35 | }; | ||
36 | |||
37 | state = { | ||
38 | countdown: config.delayDuration, | ||
39 | } | ||
40 | |||
41 | componentDidMount() { | ||
42 | // const { reload } = this.props; | ||
43 | |||
44 | this.countdownInterval = setInterval(() => { | ||
45 | this.setState({ | ||
46 | countdown: this.state.countdown - this.countdownIntervalTimeout, | ||
47 | }); | ||
48 | |||
49 | if (this.state.countdown <= 0) { | ||
50 | // reload(); | ||
51 | clearInterval(this.countdownInterval); | ||
52 | } | ||
53 | }, this.countdownIntervalTimeout); | ||
54 | } | ||
55 | |||
56 | countdownInterval = null; | ||
57 | countdownIntervalTimeout = 1000; | ||
58 | |||
59 | render() { | ||
60 | const { classes, actions } = this.props; | ||
61 | const { intl } = this.context; | ||
62 | |||
63 | return ( | ||
64 | <div className={`${classes.container}`}> | ||
65 | <h1 className={classes.headline}>{intl.formatMessage(messages.headline)}</h1> | ||
66 | <Button | ||
67 | label={intl.formatMessage(messages.action)} | ||
68 | className={classes.button} | ||
69 | buttonType="inverted" | ||
70 | onClick={() => actions.ui.openSettings({ path: 'user' })} | ||
71 | /> | ||
72 | <p className="footnote">{intl.formatMessage(messages.text, { | ||
73 | seconds: this.state.countdown / 1000, | ||
74 | })}</p> | ||
75 | </div> | ||
76 | ); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | DelayApp.wrappedComponent.propTypes = { | ||
81 | actions: PropTypes.shape({ | ||
82 | ui: PropTypes.shape({ | ||
83 | openSettings: PropTypes.func.isRequired, | ||
84 | }).isRequired, | ||
85 | }).isRequired, | ||
86 | }; | ||
diff --git a/src/features/delayApp/index.js b/src/features/delayApp/index.js new file mode 100644 index 000000000..a3cce03ee --- /dev/null +++ b/src/features/delayApp/index.js | |||
@@ -0,0 +1,67 @@ | |||
1 | import { autorun, observable, reaction } from 'mobx'; | ||
2 | import moment from 'moment'; | ||
3 | import DelayAppComponent from './Component'; | ||
4 | |||
5 | const debug = require('debug')('Franz:feature:delayApp'); | ||
6 | |||
7 | const DEFAULT_DELAY_DURATION = 10000; // 10 seconds | ||
8 | const DEFAULT_DELAY_OFFSET = 3600000; // 1 hour | ||
9 | const DEFAULT_VISIBILITY = false; | ||
10 | |||
11 | export const config = { | ||
12 | delayOffset: DEFAULT_DELAY_OFFSET, | ||
13 | delayDuration: DEFAULT_DELAY_DURATION, | ||
14 | }; | ||
15 | |||
16 | export const state = observable({ | ||
17 | isDelayAppScreenVisible: DEFAULT_VISIBILITY, | ||
18 | }); | ||
19 | |||
20 | function setVisibility(value) { | ||
21 | Object.assign(state, { | ||
22 | isDelayAppScreenVisible: value, | ||
23 | }); | ||
24 | } | ||
25 | |||
26 | export default function init(stores) { | ||
27 | reaction( | ||
28 | () => stores.features.features.needToWaitToProceed, | ||
29 | (enabled, r) => { | ||
30 | if (enabled) { | ||
31 | debug('Initializing `delayApp` feature'); | ||
32 | |||
33 | // Dispose the reaction to run this only once | ||
34 | r.dispose(); | ||
35 | |||
36 | const { needToWaitToProceedConfig: globalConfig } = stores.features.features; | ||
37 | |||
38 | let shownAfterLaunch = false; | ||
39 | let timeLastDelay = moment(); | ||
40 | |||
41 | config.delayOffset = globalConfig.delayOffset || DEFAULT_DELAY_OFFSET; | ||
42 | config.delayDuration = globalConfig.wait || DEFAULT_DELAY_DURATION; | ||
43 | |||
44 | autorun(() => { | ||
45 | const diff = moment().diff(timeLastDelay); | ||
46 | if ((stores.app.isFocused && diff >= config.delayOffset) || !shownAfterLaunch) { | ||
47 | debug(`App will be delayed for ${config.delayDuration / 1000}s`); | ||
48 | |||
49 | setVisibility(true); | ||
50 | |||
51 | timeLastDelay = moment(); | ||
52 | shownAfterLaunch = true; | ||
53 | |||
54 | setTimeout(() => { | ||
55 | debug('Resetting app delay'); | ||
56 | |||
57 | setVisibility(false); | ||
58 | }, DEFAULT_DELAY_DURATION + 1000); // timer needs to be able to hit 0 | ||
59 | } | ||
60 | }); | ||
61 | } | ||
62 | }, | ||
63 | ); | ||
64 | } | ||
65 | |||
66 | export const component = DelayAppComponent; | ||
67 | |||
diff --git a/src/features/delayApp/styles.js b/src/features/delayApp/styles.js new file mode 100644 index 000000000..097368d9a --- /dev/null +++ b/src/features/delayApp/styles.js | |||
@@ -0,0 +1,26 @@ | |||
1 | export default (theme) => { | ||
2 | console.log(theme); | ||
3 | return ({ | ||
4 | container: { | ||
5 | background: theme.colorBackground, | ||
6 | position: 'absolute', | ||
7 | top: 0, | ||
8 | width: '100%', | ||
9 | display: 'flex', | ||
10 | 'flex-direction': 'column', | ||
11 | 'align-items': 'center', | ||
12 | 'justify-content': 'center', | ||
13 | 'z-index': 150, | ||
14 | }, | ||
15 | headline: { | ||
16 | color: theme.colorHeadline, | ||
17 | margin: [25, 0, 40], | ||
18 | 'max-width': 500, | ||
19 | 'text-align': 'center', | ||
20 | 'line-height': '1.3em', | ||
21 | }, | ||
22 | button: { | ||
23 | margin: [40, 0, 20], | ||
24 | }, | ||
25 | }); | ||
26 | }; | ||