QML UI: allow edit of dive date and time

This is a bit more complicated because we are asking the user to edit the text
field instead of giving them a date and time picker. This is not a great
choice, but let's run with it for now.

One downside is that the user is likely going to edit the date "Oct 29" -> "Oct
25" without adjusting the day of the week. And if we then try to parse that Qt
correctly complains about an invalid date. So we hack around this by removing
the day of the week from both the format and the date entered (which of course
now will break things if the user did, in fact, adjust the day of the week). As
I said, not a great solution.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-01-05 22:53:32 -08:00
parent 415536aba9
commit e774c8077b
3 changed files with 23 additions and 3 deletions

View file

@ -52,9 +52,10 @@ MobileComponents.Page {
iconName: checked ? "view-readermode" : "document-edit"
onTriggered: {
if (diveDetailsWindow.state == "edit") {
manager.commitChanges(dive_id, detailsEdit.locationText, detailsEdit.gpsText, detailsEdit.durationText,
manager.commitChanges(dive_id, detailsEdit.dateText, detailsEdit.locationText, detailsEdit.gpsText, detailsEdit.durationText,
detailsEdit.depthText, detailsEdit.airtempText, detailsEdit.watertempText, detailsEdit.suitText,
detailsEdit.buddyText, detailsEdit.divemasterText, detailsEdit.notesText)
date = detailsEdit.dateText
location = detailsEdit.locationText
// gps = detailsEdit.gps
duration = detailsEdit.durationText

View file

@ -303,7 +303,7 @@ void QMLManager::loadDivesWithValidCredentials()
setLoadFromCloud(true);
}
void QMLManager::commitChanges(QString diveId, QString location, QString gps, QString duration, QString depth,
void QMLManager::commitChanges(QString diveId, QString date, QString location, QString gps, QString duration, QString depth,
QString airtemp, QString watertemp, QString suit, QString buddy, QString diveMaster, QString notes)
{
struct dive *d = get_dive_by_uniq_id(diveId.toInt());
@ -315,6 +315,25 @@ void QMLManager::commitChanges(QString diveId, QString location, QString gps, QS
}
bool diveChanged = false;
if (date != get_dive_date_string(d->when)) {
diveChanged = true;
QDateTime newDate;
// what a pain - Qt will not parse dates if the day of the week is incorrect
// so if the user changed the date but didn't update the day of the week (most likely behavior, actually),
// we need to make sure we don't try to parse that
QString format(QString(prefs.date_format) + " " + prefs.time_format);
if (format.contains("ddd") || format.contains("dddd")) {
QString dateFormatToDrop = format.contains("ddd") ? "ddd" : "dddd";
QDateTime ts;
QLocale loc = getLocale();
ts.setMSecsSinceEpoch(d->when * 1000L);
QString drop = loc.toString(ts.toUTC(), dateFormatToDrop);
format.replace(dateFormatToDrop, "");
date.replace(drop, "");
}
newDate = QDateTime::fromString(date, format);
d->when = newDate.toMSecsSinceEpoch() / 1000 + gettimezoneoffset(newDate.toMSecsSinceEpoch() / 1000);
}
struct dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid);
char *locationtext = NULL;
if (ds)

View file

@ -71,7 +71,7 @@ public slots:
void loadDivesWithValidCredentials();
void loadDiveProgress(int percent);
void provideAuth(QNetworkReply *reply, QAuthenticator *auth);
void commitChanges(QString diveId, QString location, QString gps, QString duration, QString depth,
void commitChanges(QString diveId, QString date, QString location, QString gps, QString duration, QString depth,
QString airtemp, QString watertemp, QString suit, QString buddy, QString diveMaster, QString notes);
void saveChanges();
QString addDive();