From ade1f81420c7254986b00d59067a647981222578 Mon Sep 17 00:00:00 2001 From: Maxim Kadushkin Date: Tue, 12 Jan 2021 23:44:36 +0300 Subject: [PATCH] [mobile] added LocalStorage utility --- apps/common/mobile/utils/LocalStorage.js | 80 +++++++++++++++++++ .../settings/ApplicationSettings.jsx | 11 ++- apps/documenteditor/mobile/src/view/app.jsx | 5 +- 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 apps/common/mobile/utils/LocalStorage.js diff --git a/apps/common/mobile/utils/LocalStorage.js b/apps/common/mobile/utils/LocalStorage.js new file mode 100644 index 000000000..b4b9970b3 --- /dev/null +++ b/apps/common/mobile/utils/LocalStorage.js @@ -0,0 +1,80 @@ + +class LocalStorage { + constructor() { + Common.Gateway.on('internalcommand', data => { + if (data.type == 'localstorage') { + this._store = data.keys; + } + }); + + this._store = {}; + + try { + this._isAllowed = !!window.localStorage; + } catch (e) { + this._isAllowed = false; + } + } + + get id() { + return this._storeName; + } + + set id(name) { + this._storeName = name; + } + + set keysFilter(value) { + this._filter = value; + } + + get keysFilter() { + return this._filter; + } + + sync() { + if ( !this._isAllowed ) + Common.Gateway.internalMessage('localstorage', {cmd:'get', keys:this._filter}); + } + + save() { + if ( !this._isAllowed ) + Common.Gateway.internalMessage('localstorage', {cmd:'set', keys:this._store}); + } + + setItem(name, value, just) { + if ( this._isAllowed ) { + try { + localStorage.setItem(name, value); + } catch (error){} + } else { + this._store[name] = value; + + if ( just===true ) { + Common.Gateway.internalMessage('localstorage', {cmd:'set', keys: {name: value}}); + } + } + } + + getItem(name) { + if ( this._isAllowed ) + return localStorage.getItem(name); + else return this._store[name]===undefined ? null : this._store[name]; + }; + + setBool(name, value, just) { + this.setItem(name, value ? 1 : 0, just); + } + + getBool(name, defValue) { + const value = this.getItem(name); + return (value !== null) ? (parseInt(value) != 0) : !!defValue; + } + + itemExists(name) { + return this.getItem(name) !== null; + } +} + +const instance = new LocalStorage(); +export {instance as LocalStorage}; \ No newline at end of file diff --git a/apps/documenteditor/mobile/src/controller/settings/ApplicationSettings.jsx b/apps/documenteditor/mobile/src/controller/settings/ApplicationSettings.jsx index dda7195ff..b3914a9c6 100644 --- a/apps/documenteditor/mobile/src/controller/settings/ApplicationSettings.jsx +++ b/apps/documenteditor/mobile/src/controller/settings/ApplicationSettings.jsx @@ -1,5 +1,6 @@ import React, { Component } from "react"; import { ApplicationSettings } from "../../view/settings/ApplicationSettings"; +import { LocalStorage } from '../../../../../common/mobile/utils/LocalStorage'; class ApplicationSettingsController extends Component { constructor(props) { @@ -8,10 +9,11 @@ class ApplicationSettingsController extends Component { } setUnitMeasurement(value) { - const api = Common.EditorApi.get(); value = (value !== null) ? parseInt(value) : Common.Utils.Metric.getDefaultMetric(); Common.Utils.Metric.setCurrentMetric(value); - // Common.localStorage.setItem("de-mobile-settings-unit", value); + LocalStorage.setItem("de-mobile-settings-unit", value); + + const api = Common.EditorApi.get(); api.asc_SetDocumentUnits((value == Common.Utils.Metric.c_MetricUnits.inch) ? Asc.c_oAscDocumentUnits.Inch : ((value == Common.Utils.Metric.c_MetricUnits.pt) ? Asc.c_oAscDocumentUnits.Point : Asc.c_oAscDocumentUnits.Millimeter)); } @@ -23,14 +25,15 @@ class ApplicationSettingsController extends Component { } switchNoCharacters(value) { + Common.localStorage.setItem("de-mobile-no-characters", value); + const api = Common.EditorApi.get(); - // Common.localStorage.setItem("de-mobile-no-characters", value); api.put_ShowParaMarks(value); } switchShowTableEmptyLine(value) { + Common.localStorage.setItem("de-mobile-hidden-borders", value); const api = Common.EditorApi.get(); - // Common.localStorage.setItem("de-mobile-hidden-borders", state); api.put_ShowTableEmptyLine(value); } diff --git a/apps/documenteditor/mobile/src/view/app.jsx b/apps/documenteditor/mobile/src/view/app.jsx index da82af4b7..23137afc5 100644 --- a/apps/documenteditor/mobile/src/view/app.jsx +++ b/apps/documenteditor/mobile/src/view/app.jsx @@ -2,10 +2,11 @@ import React from 'react'; import {App,Panel,Views,View,Popup,Page,Navbar,Toolbar,NavRight,Link,Block,BlockTitle,List,ListItem,ListInput,ListButton,BlockFooter} from 'framework7-react'; -import routes from '../js/routes'; - import '../../../../common/Gateway.js'; import '../../../../common/main/lib/util/utils.js'; + +import routes from '../js/routes'; + import Notifications from '../../../../common/mobile/utils/notifications.js' import {MainController} from '../controller/Main'; import {Device} from '../../../../common/mobile/utils/device'