From 29b568a63f21a89c5c17e88cc7d0427f40b8a36e Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 8 Aug 2023 15:07:42 +0800 Subject: [PATCH] Vastly simplify NCInputDateField by using the DateFieldBackend Signed-off-by: Claudio Cambra --- src/gui/filedetails/NCInputDateField.qml | 98 ++++++------------------ 1 file changed, 22 insertions(+), 76 deletions(-) diff --git a/src/gui/filedetails/NCInputDateField.qml b/src/gui/filedetails/NCInputDateField.qml index 73a0a369d..63d979e35 100644 --- a/src/gui/filedetails/NCInputDateField.qml +++ b/src/gui/filedetails/NCInputDateField.qml @@ -12,91 +12,37 @@ * for more details. */ +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import com.nextcloud.desktopclient 1.0 + NCInputTextField { id: root - function updateText() { - text = _textFromValue(_value, root.locale); - } - - // Taken from Kalendar 22.08 - // https://invent.kde.org/pim/kalendar/-/blob/release/22.08/src/contents/ui/KalendarUtils/dateutils.js - function _parseDateString(dateString) { - function defaultParse() { - const defaultParsedDate = Date.fromLocaleDateString(root.locale, dateString, Locale.NarrowFormat); - // JS always generates date in system locale, eliminate timezone difference to UTC - const msecsSinceEpoch = defaultParsedDate.getTime() - (defaultParsedDate.getTimezoneOffset() * 60 * 1000); - return new Date(msecsSinceEpoch); - } - - const dateStringDelimiterMatches = dateString.match(/\D/); - if(dateStringDelimiterMatches.length === 0) { - // Let the date method figure out this weirdness - return defaultParse(); - } - - const dateStringDelimiter = dateStringDelimiterMatches[0]; - - const localisedDateFormatSplit = root.locale.dateFormat(Locale.NarrowFormat).split(dateStringDelimiter); - const localisedDateDayPosition = localisedDateFormatSplit.findIndex((x) => /d/gi.test(x)); - const localisedDateMonthPosition = localisedDateFormatSplit.findIndex((x) => /m/gi.test(x)); - const localisedDateYearPosition = localisedDateFormatSplit.findIndex((x) => /y/gi.test(x)); - - let splitDateString = dateString.split(dateStringDelimiter); - let userProvidedYear = splitDateString[localisedDateYearPosition] - - const dateNow = new Date(); - const stringifiedCurrentYear = dateNow.getFullYear().toString(); - - // If we have any input weirdness, or if we have a fully-written year - // (e.g. 2022 instead of 22) then use default parse - if(splitDateString.length === 0 || - splitDateString.length > 3 || - userProvidedYear.length >= stringifiedCurrentYear.length) { + signal userAcceptedDate - return defaultParse(); - } - - let fullyWrittenYear = userProvidedYear.split(""); - const digitsToAdd = stringifiedCurrentYear.length - fullyWrittenYear.length; - for(let i = 0; i < digitsToAdd; i++) { - fullyWrittenYear.splice(i, 0, stringifiedCurrentYear[i]) - } - fullyWrittenYear = fullyWrittenYear.join(""); - - const fixedYearNum = Number(fullyWrittenYear); - const monthIndexNum = Number(splitDateString[localisedDateMonthPosition]) - 1; - const dayNum = Number(splitDateString[localisedDateDayPosition]); - - console.log(dayNum, monthIndexNum, fixedYearNum); - - // Modification: return date in UTC - return new Date(Date.UTC(fixedYearNum, monthIndexNum, dayNum)); - } - - function _textFromValue(value, locale) { - const dateFromValue = new Date(value * dayInMSecs); - return dateFromValue.toLocaleDateString(root.locale, Locale.NarrowFormat); + function updateText() { + text = backend.dateTimeString; } - function _valueFromText(text, locale) { - const dateFromText = _parseDateString(text); - return Math.floor(dateFromText.getTime() / dayInMSecs); + property DateFieldBackend backend: DateFieldBackend { + id: backend + onDateTimeChanged: if (!root.activeFocus) root.updateText() } - property var date: new Date().getTime() * 1000 // QDateTime msecsFromEpoch - onDateChanged: updateText() + property alias date: backend.dateTime + property alias dateInMs: backend.dateTimeMsecs + property alias minimumDate: backend.minimumDateTime + property alias minimumDateMs: backend.minimumDateTimeMsecs + property alias maximumDate: backend.maximumDateTime + property alias maximumDateMs: backend.maximumDateTimeMsecs - property var minimumDate: 0 - property var maximumDate: Number.MAX_SAFE_INTEGER + validInput: backend.validDateTime + text: backend.dateTimeString + onTextChanged: backend.dateTimeString = text - validInput: { - const value = valueFromText(text); - return value >= minimumDate && value <= maximumDate; + onAccepted: { + backend.dateTimeString = text; + root.userAcceptedDate(); } - - text: _textFromValue(_value, locale) - inputMethodHints: Qt.ImhDate - - onAccepted: root.date = _valueFromText(text, locale); } \ No newline at end of file -- 2.30.2