From f9f5974267922c5bd2cb2e6736a858ef4204f86f Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 28 Nov 2017 18:02:54 +0000 Subject: [PATCH] All: Added option to set date and time format --- ElectronClient/app/gui/PromptDialog.jsx | 5 +- ReactNativeClient/lib/BaseApplication.js | 6 +++ ReactNativeClient/lib/models/setting.js | 27 ++++++++++ .../lib/services/AlarmService.js | 3 -- ReactNativeClient/lib/time-utils.js | 49 ++++++++++++++----- ReactNativeClient/root.js | 6 +++ 6 files changed, 80 insertions(+), 16 deletions(-) diff --git a/ElectronClient/app/gui/PromptDialog.jsx b/ElectronClient/app/gui/PromptDialog.jsx index 8e81bb600..55f5d9190 100644 --- a/ElectronClient/app/gui/PromptDialog.jsx +++ b/ElectronClient/app/gui/PromptDialog.jsx @@ -2,6 +2,7 @@ const React = require('react'); const { connect } = require('react-redux'); const { _ } = require('lib/locale.js'); const { themeStyle } = require('../theme.js'); +const { time } = require('lib/time-utils.js'); const Datetime = require('react-datetime'); class PromptDialog extends React.Component { @@ -106,8 +107,8 @@ class PromptDialog extends React.Component { if (this.props.inputType === 'datetime') { inputComp = onDateTimeChange(momentObject)} /> } else { diff --git a/ReactNativeClient/lib/BaseApplication.js b/ReactNativeClient/lib/BaseApplication.js index e8c34b714..165ac3fb2 100644 --- a/ReactNativeClient/lib/BaseApplication.js +++ b/ReactNativeClient/lib/BaseApplication.js @@ -14,6 +14,7 @@ const { Logger } = require('lib/logger.js'); const { splitCommandString } = require('lib/string-utils.js'); const { sprintf } = require('sprintf-js'); const { reg } = require('lib/registry.js'); +const { time } = require('lib/time-utils.js'); const BaseSyncTarget = require('lib/BaseSyncTarget.js'); const { fileExtension } = require('lib/path-utils.js'); const { shim } = require('lib/shim.js'); @@ -250,6 +251,11 @@ class BaseApplication { await this.refreshNotes(newState); } + if ((action.type == 'SETTING_UPDATE_ONE' && (action.key == 'dateFormat' || action.key == 'timeFormat')) || (action.type == 'SETTING_UPDATE_ALL')) { + time.setDateFormat(Setting.value('dateFormat')); + time.setTimeFormat(Setting.value('timeFormat')); + } + if (action.type == 'TAG_SELECT' || action.type === 'TAG_DELETE') { await this.refreshNotes(newState); } diff --git a/ReactNativeClient/lib/models/setting.js b/ReactNativeClient/lib/models/setting.js index c26dbefc9..d12b4ece0 100644 --- a/ReactNativeClient/lib/models/setting.js +++ b/ReactNativeClient/lib/models/setting.js @@ -2,6 +2,7 @@ const { BaseModel } = require('lib/base-model.js'); const { Database } = require('lib/database.js'); const { Logger } = require('lib/logger.js'); const SyncTargetRegistry = require('lib/SyncTargetRegistry.js'); +const { time } = require('lib/time-utils.js'); const { sprintf } = require('sprintf-js'); const { _, supportedLocalesToLanguages, defaultLocale } = require('lib/locale.js'); @@ -34,6 +35,23 @@ class Setting extends BaseModel { 'locale': { value: defaultLocale(), type: Setting.TYPE_STRING, isEnum: true, public: true, label: () => _('Language'), options: () => { return supportedLocalesToLanguages(); }}, + 'dateFormat': { value: Setting.DATE_FORMAT_1, type: Setting.TYPE_STRING, isEnum: true, public: true, label: () => _('Date format'), options: () => { + let options = {} + const now = (new Date('2017-01-30T12:00:00')).getTime(); + options[Setting.DATE_FORMAT_1] = time.formatMsToLocal(now, Setting.DATE_FORMAT_1); + options[Setting.DATE_FORMAT_2] = time.formatMsToLocal(now, Setting.DATE_FORMAT_2); + options[Setting.DATE_FORMAT_3] = time.formatMsToLocal(now, Setting.DATE_FORMAT_3); + options[Setting.DATE_FORMAT_4] = time.formatMsToLocal(now, Setting.DATE_FORMAT_4); + options[Setting.DATE_FORMAT_5] = time.formatMsToLocal(now, Setting.DATE_FORMAT_5); + return options; + }}, + 'timeFormat': { value: Setting.TIME_FORMAT_1, type: Setting.TYPE_STRING, isEnum: true, public: true, label: () => _('Time format'), options: () => { + let options = {} + const now = (new Date('2017-01-30T20:30:00')).getTime(); + options[Setting.TIME_FORMAT_1] = time.formatMsToLocal(now, Setting.TIME_FORMAT_1); + options[Setting.TIME_FORMAT_2] = time.formatMsToLocal(now, Setting.TIME_FORMAT_2); + return options; + }}, 'theme': { value: Setting.THEME_LIGHT, type: Setting.TYPE_INT, public: true, appTypes: ['mobile'], isEnum: true, label: () => _('Theme'), options: () => { let output = {}; output[Setting.THEME_LIGHT] = _('Light'); @@ -398,6 +416,15 @@ Setting.TYPE_OBJECT = 5; Setting.THEME_LIGHT = 1; Setting.THEME_DARK = 2; +Setting.DATE_FORMAT_1 = 'DD/MM/YYYY' +Setting.DATE_FORMAT_2 = 'DD/MM/YY'; +Setting.DATE_FORMAT_3 = 'MM/DD/YYYY'; +Setting.DATE_FORMAT_4 = 'MM/DD/YY'; +Setting.DATE_FORMAT_5 = 'YYYY-MM-DD'; + +Setting.TIME_FORMAT_1 = 'HH:mm'; +Setting.TIME_FORMAT_2 = 'h:mm A'; + // Contains constants that are set by the application and // cannot be modified by the user: Setting.constants_ = { diff --git a/ReactNativeClient/lib/services/AlarmService.js b/ReactNativeClient/lib/services/AlarmService.js index 87d878983..cff8c1024 100644 --- a/ReactNativeClient/lib/services/AlarmService.js +++ b/ReactNativeClient/lib/services/AlarmService.js @@ -64,8 +64,6 @@ class AlarmService { } } - console.info('NOTE', note, Note.needAlarm(note)); - if (isDeleted || !Note.needAlarm(note) || (alarm && alarm.trigger_time !== note.todo_due)) @@ -124,7 +122,6 @@ class AlarmService { } // TODO: inner notifications (when app is active) - // TODO: locale-dependent format // TODO: status to view active notifications } diff --git a/ReactNativeClient/lib/time-utils.js b/ReactNativeClient/lib/time-utils.js index b5d16f751..83678fc0d 100644 --- a/ReactNativeClient/lib/time-utils.js +++ b/ReactNativeClient/lib/time-utils.js @@ -1,38 +1,63 @@ const moment = require('moment'); -let time = { +class Time { + + constructor() { + this.dateFormat_ = 'DD/MM/YYYY'; + this.timeFormat_ = 'HH:mm'; + } + + dateFormat() { + return this.dateFormat_; + } + + setDateFormat(v) { + this.dateFormat_ = v; + } + + timeFormat() { + return this.timeFormat_; + } + + setTimeFormat(v) { + this.timeFormat_ = v; + } + + dateTimeFormat() { + return this.dateFormat() + ' ' + this.timeFormat(); + } unix() { return Math.floor(Date.now() / 1000); - }, + } unixMs() { return Date.now(); - }, + } unixMsToObject(ms) { return new Date(ms); - }, + } unixMsToS(ms) { return Math.floor(ms / 1000); - }, + } unixMsToIso(ms) { return moment.unix(ms / 1000).utc().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z'; - }, + } unixMsToIsoSec(ms) { return moment.unix(ms / 1000).utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z'; - }, + } unixMsToLocalDateTime(ms) { return moment.unix(ms / 1000).format('DD/MM/YYYY HH:mm'); - }, + } formatMsToLocal(ms, format) { return moment(ms).format(format); - }, + } msleep(ms) { return new Promise((resolve, reject) => { @@ -40,12 +65,14 @@ let time = { resolve(); }, ms); }); - }, + } sleep(seconds) { return this.msleep(seconds * 1000); - }, + } } +const time = new Time(); + module.exports = { time }; \ No newline at end of file diff --git a/ReactNativeClient/root.js b/ReactNativeClient/root.js index 7cb4b4b7a..4c955a403 100644 --- a/ReactNativeClient/root.js +++ b/ReactNativeClient/root.js @@ -7,6 +7,7 @@ const AlarmServiceDriver = require('lib/services/AlarmServiceDriver'); const { createStore, applyMiddleware } = require('redux'); const { shimInit } = require('lib/shim-init-react.js'); const { Log } = require('lib/log.js'); +const { time } = require('lib/time-utils.js'); const { AppNav } = require('lib/components/app-nav.js'); const { Logger } = require('lib/logger.js'); const { Note } = require('lib/models/note.js'); @@ -63,6 +64,11 @@ const generalMiddleware = store => next => async (action) => { reg.setupRecurrentSync(); } + if ((action.type == 'SETTING_UPDATE_ONE' && (action.key == 'dateFormat' || action.key == 'timeFormat')) || (action.type == 'SETTING_UPDATE_ALL')) { + time.setDateFormat(Setting.value('dateFormat')); + time.setTimeFormat(Setting.value('timeFormat')); + } + if (action.type == 'SETTING_UPDATE_ONE' && action.key == 'locale' || action.type == 'SETTING_UPDATE_ALL') { setLocale(Setting.value('locale')); }