2020-01-09 04:34:58 +01:00
|
|
|
import ObservableStore from 'obs-store'
|
2019-05-13 18:16:09 +02:00
|
|
|
|
|
|
|
class AppStateController {
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param opts
|
|
|
|
*/
|
|
|
|
constructor (opts = {}) {
|
2019-12-03 21:50:55 +01:00
|
|
|
const { initState, onInactiveTimeout, preferencesStore } = opts
|
|
|
|
const { preferences } = preferencesStore.getState()
|
2019-05-13 18:16:09 +02:00
|
|
|
|
|
|
|
this.onInactiveTimeout = onInactiveTimeout || (() => {})
|
2020-01-13 19:59:36 +01:00
|
|
|
this.store = new ObservableStore(Object.assign({
|
2019-05-13 18:16:09 +02:00
|
|
|
timeoutMinutes: 0,
|
2019-11-19 20:34:33 +01:00
|
|
|
mkrMigrationReminderTimestamp: null,
|
2019-05-13 18:16:09 +02:00
|
|
|
}, initState))
|
|
|
|
this.timer = null
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
preferencesStore.subscribe((state) => {
|
2020-01-21 23:09:53 +01:00
|
|
|
this._setInactiveTimeout(state.preferences.autoLockTimeLimit)
|
2019-05-13 18:16:09 +02:00
|
|
|
})
|
|
|
|
|
2020-01-21 23:09:53 +01:00
|
|
|
this._setInactiveTimeout(preferences.autoLockTimeLimit)
|
2019-05-13 18:16:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 20:34:33 +01:00
|
|
|
setMkrMigrationReminderTimestamp (timestamp) {
|
|
|
|
this.store.updateState({
|
|
|
|
mkrMigrationReminderTimestamp: timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-13 18:16:09 +02:00
|
|
|
/**
|
|
|
|
* Sets the last active time to the current time
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {void}
|
2019-05-13 18:16:09 +02:00
|
|
|
*/
|
|
|
|
setLastActiveTime () {
|
|
|
|
this._resetTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the inactive timeout for the app
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {number} timeoutMinutes - the inactive timeout in minutes
|
|
|
|
* @returns {void}
|
2019-05-13 18:16:09 +02:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_setInactiveTimeout (timeoutMinutes) {
|
2019-11-26 21:44:29 +01:00
|
|
|
this.store.updateState({
|
2019-05-13 18:16:09 +02:00
|
|
|
timeoutMinutes,
|
|
|
|
})
|
|
|
|
|
|
|
|
this._resetTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the internal inactive timer
|
|
|
|
*
|
|
|
|
* If the {@code timeoutMinutes} state is falsy (i.e., zero) then a new
|
|
|
|
* timer will not be created.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {void}
|
2019-05-13 18:16:09 +02:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_resetTimer () {
|
2019-12-03 21:50:55 +01:00
|
|
|
const { timeoutMinutes } = this.store.getState()
|
2019-05-13 18:16:09 +02:00
|
|
|
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!timeoutMinutes) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.timer = setTimeout(() => this.onInactiveTimeout(), timeoutMinutes * 60 * 1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default AppStateController
|
2019-05-13 18:16:09 +02:00
|
|
|
|