From e64d102efdee0ce4b4bbb86a6e679a8c47372355 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 28 Jun 2018 20:01:26 +0100 Subject: [PATCH] Better display of first_name + last_name. --- .../passport_form_view_controller.cpp | 61 ++++++++++++++++--- .../passport/passport_panel_controller.cpp | 1 + .../passport/passport_panel_edit_document.h | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Telegram/SourceFiles/passport/passport_form_view_controller.cpp b/Telegram/SourceFiles/passport/passport_form_view_controller.cpp index 44e61dda4..d0eed7e7c 100644 --- a/Telegram/SourceFiles/passport/passport_form_view_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_view_controller.cpp @@ -101,14 +101,56 @@ std::vector ComputeScopes( return result; } +QString JoinScopeRowReadyString( + std::vector> &&values) { + using Pair = std::pair; + + if (values.empty()) { + return QString(); + } + auto result = QString(); + auto size = ranges::accumulate( + values, + 0, + ranges::plus(), + [](const Pair &v) { return v.second.size(); }); + result.reserve(size + (values.size() - 1) * 2); + for (const auto &pair : values) { + if (pair.second.isEmpty()) { + continue; + } + if (!result.isEmpty()) { + result.append(", "); + } + result.append(pair.second); + } + return result; +} + QString ComputeScopeRowReadyString(const Scope &scope) { switch (scope.type) { case Scope::Type::Identity: case Scope::Type::Address: { - auto list = QStringList(); - const auto pushListValue = [&](const QString &value) { - if (const auto trimmed = value.trimmed(); !trimmed.isEmpty()) { - list.push_back(trimmed); + auto list = std::vector>(); + const auto pushListValue = [&]( + const QString &key, + const QString &value, + const QString &keyForAttachmentTo = QString()) { + if (keyForAttachmentTo.isEmpty()) { + list.push_back({ key, value.trimmed() }); + } else { + const auto i = ranges::find( + list, + keyForAttachmentTo, + [](const std::pair &value) { + return value.first; + }); + Assert(i != end(list)); + if (i->second.isEmpty()) { + i->second = value.trimmed(); + } else { + i->second += ' ' + value.trimmed(); + } } }; const auto &fields = scope.fields->data.parsed.fields; @@ -121,7 +163,7 @@ QString ComputeScopeRowReadyString(const Scope &scope) { return nullptr; }(); if (document && scope.documents.size() > 1) { - pushListValue([&] { + pushListValue("_type", [&] { using Type = Value::Type; switch (document->type) { case Type::Passport: @@ -161,7 +203,10 @@ QString ComputeScopeRowReadyString(const Scope &scope) { if (row.error && row.error(text).has_value()) { return QString(); } - pushListValue(format ? format(text) : text); + pushListValue( + row.key, + format ? format(text) : text, + row.keyForAttachmentTo); } else if (scope.documents.empty()) { continue; } else { @@ -173,10 +218,10 @@ QString ComputeScopeRowReadyString(const Scope &scope) { if (row.error && row.error(text).has_value()) { return QString(); } - pushListValue(text); + pushListValue(row.key, text, row.keyForAttachmentTo); } } - return list.join(", "); + return JoinScopeRowReadyString(std::move(list)); } break; case Scope::Type::Phone: case Scope::Type::Email: { diff --git a/Telegram/SourceFiles/passport/passport_panel_controller.cpp b/Telegram/SourceFiles/passport/passport_panel_controller.cpp index 38c5bb0d5..441f681e4 100644 --- a/Telegram/SourceFiles/passport/passport_panel_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_controller.cpp @@ -138,6 +138,7 @@ EditDocumentScheme GetDocumentScheme( NameValidate, DontFormat, kMaxNameSize, + qsl("first_name") }, { ValueClass::Fields, diff --git a/Telegram/SourceFiles/passport/passport_panel_edit_document.h b/Telegram/SourceFiles/passport/passport_panel_edit_document.h index 3edbd2812..ed9e89a06 100644 --- a/Telegram/SourceFiles/passport/passport_panel_edit_document.h +++ b/Telegram/SourceFiles/passport/passport_panel_edit_document.h @@ -46,6 +46,7 @@ struct EditDocumentScheme { Fn(const QString &value)> error; Fn format; int lengthLimit = 0; + QString keyForAttachmentTo; // attach last_name to first_name }; std::vector rows; QString rowsHeader;