2023-06-14 00:26:43 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import { notify } from '/imports/ui/services/notification';
|
|
|
|
import Settings from '/imports/ui/services/settings';
|
|
|
|
import Styled from './styles';
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
wakeLockOfferTitle: {
|
|
|
|
id: 'app.toast.wakeLock.offerTitle',
|
|
|
|
},
|
|
|
|
wakeLockAcquireSuccess: {
|
|
|
|
id: 'app.toast.wakeLock.acquireSuccess',
|
|
|
|
},
|
|
|
|
wakeLockAcquireFailed: {
|
|
|
|
id: 'app.toast.wakeLock.acquireFailed',
|
|
|
|
},
|
2023-08-12 03:57:11 +08:00
|
|
|
wakeLockNotSupported: {
|
|
|
|
id: 'app.toast.wakeLock.notSupported',
|
|
|
|
},
|
|
|
|
wakeLockDisclaimer: {
|
|
|
|
id: 'app.toast.wakeLock.disclaimer',
|
|
|
|
}
|
2023-06-14 00:26:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
intl: PropTypes.objectOf(Object).isRequired,
|
|
|
|
request: PropTypes.func.isRequired,
|
|
|
|
release: PropTypes.func.isRequired,
|
|
|
|
wakeLockSettings: PropTypes.bool.isRequired,
|
2024-01-30 21:03:11 +08:00
|
|
|
setLocalSettings: PropTypes.func.isRequired,
|
2023-06-14 00:26:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class WakeLock extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2023-08-12 03:57:11 +08:00
|
|
|
const { wakeLockSettings } = this.props;
|
2023-06-14 00:26:43 +08:00
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
if (wakeLockSettings) {
|
|
|
|
this.requestWakeLock();
|
|
|
|
}
|
2023-06-14 00:26:43 +08:00
|
|
|
}
|
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { wakeLockSettings, release } = this.props;
|
|
|
|
if (wakeLockSettings !== prevProps.wakeLockSettings) {
|
|
|
|
if (wakeLockSettings) {
|
|
|
|
this.requestWakeLock();
|
|
|
|
} else {
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 00:26:43 +08:00
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
getToast(id, message) {
|
2023-06-14 00:26:43 +08:00
|
|
|
return (
|
|
|
|
<div id={id}>
|
|
|
|
<Styled.Title>
|
2023-08-12 03:57:11 +08:00
|
|
|
{ message }
|
2023-06-14 00:26:43 +08:00
|
|
|
</Styled.Title>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
feedbackToast(result) {
|
|
|
|
const { intl } = this.props;
|
|
|
|
|
2023-06-14 00:26:43 +08:00
|
|
|
const feedbackToastProps = {
|
|
|
|
closeOnClick: true,
|
|
|
|
autoClose: true,
|
|
|
|
closeButton: false,
|
|
|
|
};
|
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
const toastType = result.error ? 'error' : 'success';
|
|
|
|
const message = result.error
|
|
|
|
? intl.formatMessage(intlMessages.wakeLockDisclaimer,{
|
|
|
|
0: intl.formatMessage(intlMessages[result.locale])
|
|
|
|
} )
|
|
|
|
: intl.formatMessage(intlMessages.wakeLockAcquireSuccess);
|
|
|
|
const feedbackToast = this.getToast('wakeLockToast', message);
|
|
|
|
notify(feedbackToast, toastType, 'lock', feedbackToastProps, null, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
requestWakeLock () {
|
2024-01-30 21:03:11 +08:00
|
|
|
const { request, setLocalSettings } = this.props;
|
2023-08-12 03:57:11 +08:00
|
|
|
request().then((result) => {
|
|
|
|
if (result && result.error) {
|
|
|
|
Settings.application.wakeLock = false;
|
2024-01-30 21:03:11 +08:00
|
|
|
Settings.save(setLocalSettings);
|
2023-08-23 21:06:33 +08:00
|
|
|
this.feedbackToast(result);
|
2023-08-12 03:57:11 +08:00
|
|
|
}
|
|
|
|
});
|
2023-06-14 00:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WakeLock.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default injectIntl(WakeLock);
|