Implement a new AddToContactBox.

This commit is contained in:
John Preston 2019-06-10 17:47:22 +02:00
parent 213a8d888a
commit 08d4715ad6
9 changed files with 277 additions and 12 deletions

View File

@ -77,6 +77,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_month_year" = "{month} {year}";
"lng_box_ok" = "OK";
"lng_box_done" = "Done";
"lng_cancel" = "Cancel";
"lng_continue" = "Continue";
@ -1409,6 +1410,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_contact_phone" = "Phone Number";
"lng_enter_contact_data" = "New Contact";
"lng_contact_mobile_hidden" = "Mobile hidden";
"lng_contact_phone_after" = "Phone number will be visible once {user} adds you as a contact. Your phone number will become visible to {name}.";
"lng_contact_phone_show" = "When you click {button}, your phone number will become visible to {user}.";
"lng_edit_contact_title" = "Edit contact name";
"lng_edit_channel_title" = "Edit channel";
"lng_edit_sign_messages" = "Sign messages";

View File

@ -977,3 +977,6 @@ urlAuthCheckbox: Checkbox(defaultBoxCheckbox) {
linkedChatAbout: membersAbout;
linkedChatAboutPadding: margins(20px, 20px, 20px, 20px);
addContactFieldMargin: margins(19px, 0px, 19px, 10px);
addContactWarningMargin: margins(19px, 10px, 19px, 5px);

View File

@ -0,0 +1,191 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "boxes/peers/add_to_contacts_box.h"
#include "data/data_user.h"
#include "data/data_session.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/input_fields.h"
#include "info/profile/info_profile_cover.h"
#include "lang/lang_keys.h"
#include "window/window_controller.h"
#include "auth_session.h"
#include "apiwrap.h"
#include "styles/style_boxes.h"
#include "styles/style_info.h"
namespace {
QString UserPhone(not_null<UserData*> user) {
const auto phone = user->phone();
return phone.isEmpty()
? user->owner().findContactPhone(user->bareId())
: phone;
}
TextWithEntities BoldText(const QString &text) {
auto result = TextWithEntities{ text };
result.entities.push_back({ EntityType::Bold, 0, text.size() });
return result;
}
} // namespace
AddToContactsBox::AddToContactsBox(
QWidget*,
not_null<Window::Controller*> window,
not_null<UserData*> user)
: _window(window)
, _user(user)
, _phone(UserPhone(user)) {
}
void AddToContactsBox::prepare() {
setupContent();
setTitle(langFactory(lng_enter_contact_data));
addButton(langFactory(lng_box_done), [=] { _save(); });
addButton(langFactory(lng_cancel), [=] { closeBox(); });
}
void AddToContactsBox::setInnerFocus() {
_focus();
}
void AddToContactsBox::setupContent() {
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
setupCover(content);
setupNameFields(content);
setupWarning(content);
setDimensionsToContent(st::boxWidth, content);
}
void AddToContactsBox::setupCover(not_null<Ui::VerticalLayout*> container) {
container->add(object_ptr<Info::Profile::Cover>(
container,
_user,
_window->sessionController(),
(_phone.isEmpty()
? Lang::Viewer(lng_contact_mobile_hidden)
: rpl::single(App::formatPhone(_phone))))
)->setAttribute(Qt::WA_TransparentForMouseEvents);
}
void AddToContactsBox::setupNameFields(
not_null<Ui::VerticalLayout*> container) {
const auto inverted = langFirstNameGoesSecond();
const auto first = container->add(
object_ptr<Ui::InputField>(
container,
st::defaultInputField,
langFactory(lng_signup_firstname),
_user->firstName),
st::addContactFieldMargin);
auto preparedLast = object_ptr<Ui::InputField>(
container,
st::defaultInputField,
langFactory(lng_signup_lastname),
_user->lastName);
const auto last = inverted
? container->insert(
container->count() - 1,
std::move(preparedLast),
st::addContactFieldMargin)
: container->add(std::move(preparedLast), st::addContactFieldMargin);
initNameFields(first, last, inverted);
}
void AddToContactsBox::initNameFields(
not_null<Ui::InputField*> first,
not_null<Ui::InputField*> last,
bool inverted) {
if (inverted) {
setTabOrder(last, first);
}
const auto submit = [=] {
const auto firstValue = first->getLastText().trimmed();
const auto lastValue = last->getLastText().trimmed();
const auto empty = firstValue.isEmpty() && lastValue.isEmpty();
if (inverted ? last->hasFocus() : empty) {
first->setFocus();
} else if (inverted ? empty : first->hasFocus()) {
last->setFocus();
} else {
_save();
}
};
connect(first, &Ui::InputField::submitted, [=] { submit(); });
connect(last, &Ui::InputField::submitted, [=] { submit(); });
_focus = [=] {
const auto firstValue = first->getLastText().trimmed();
const auto lastValue = last->getLastText().trimmed();
const auto empty = firstValue.isEmpty() && lastValue.isEmpty();
const auto focusFirst = (inverted != empty);
(focusFirst ? first : last)->setFocusFast();
};
_save = [=] {
const auto firstValue = first->getLastText().trimmed();
const auto lastValue = last->getLastText().trimmed();
const auto empty = firstValue.isEmpty() && lastValue.isEmpty();
if (empty) {
_focus();
(inverted ? last : first)->showError();
return;
}
const auto user = _user;
const auto box = make_weak(this);
user->session().api().request(MTPcontacts_AddContact(
MTP_flags(0),
user->inputUser,
MTP_string(firstValue),
MTP_string(lastValue),
MTP_string(_phone)
)).done([=](const MTPUpdates &result) {
user->session().api().applyUpdates(result);
if (const auto settings = user->settings()) {
using Flag = MTPDpeerSettings::Flag;
const auto flags = Flag::f_add_contact
| Flag::f_block_contact
| Flag::f_report_spam;
user->setSettings(*settings & ~flags);
}
if (box) {
box->closeBox();
}
}).fail([=](const RPCError &error) {
}).send();
};
}
void AddToContactsBox::setupWarning(
not_null<Ui::VerticalLayout*> container) {
const auto name = _user->firstName.isEmpty()
? _user->lastName
: _user->firstName;
const auto text = _phone.isEmpty()
? TextWithEntities{
lng_contact_phone_after(lt_user, name, lt_name, name)
}
: lng_contact_phone_show__generic<TextWithEntities>(
lt_button,
BoldText(lang(lng_box_done).toUpper()),
lt_user,
TextWithEntities{ name });
container->add(
object_ptr<Ui::FlatLabel>(
container,
rpl::single(text),
st::changePhoneLabel),
st::addContactWarningMargin);
}

View File

@ -0,0 +1,50 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "boxes/abstract_box.h"
class UserData;
namespace Window {
class Controller;
} // namespace Window
namespace Ui {
class VerticalLayout;
} // namespace Ui
class AddToContactsBox : public BoxContent {
public:
AddToContactsBox(
QWidget*,
not_null<Window::Controller*> window,
not_null<UserData*> user);
protected:
void prepare() override;
void setInnerFocus() override;
private:
void setupContent();
void setupCover(not_null<Ui::VerticalLayout*> container);
void setupNameFields(not_null<Ui::VerticalLayout*> container);
void setupWarning(not_null<Ui::VerticalLayout*> container);
void initNameFields(
not_null<Ui::InputField*> first,
not_null<Ui::InputField*> last,
bool inverted);
not_null<Window::Controller*> _window;
not_null<UserData*> _user;
QString _phone;
Fn<void()> _focus;
Fn<void()> _save;
};

View File

@ -484,7 +484,7 @@ void ActionsFiller::addEditContactAction(not_null<UserData*> user) {
_wrap,
Lang::Viewer(lng_info_edit_contact),
IsContactValue(user),
[user] { Ui::show(Box<AddContactBox>(user)); });
[=] { Ui::show(Box<AddContactBox>(user)); });
}
void ActionsFiller::addDeleteContactAction(

View File

@ -212,6 +212,18 @@ Cover::Cover(
QWidget *parent,
not_null<PeerData*> peer,
not_null<Window::SessionController*> controller)
: Cover(parent, peer, controller, NameValue(
peer
) | rpl::map([=](const TextWithEntities &name) {
return name.text;
})) {
}
Cover::Cover(
QWidget *parent,
not_null<PeerData*> peer,
not_null<Window::SessionController*> controller,
rpl::producer<QString> title)
: SectionWithToggle(
parent,
st::infoProfilePhotoTop
@ -240,7 +252,7 @@ Cover::Cover(
_status->setAttribute(Qt::WA_TransparentForMouseEvents);
}
initViewers();
initViewers(std::move(title));
setupChildGeometry();
}
@ -270,12 +282,12 @@ Cover *Cover::setOnlineCount(rpl::producer<int> &&count) {
return this;
}
void Cover::initViewers() {
void Cover::initViewers(rpl::producer<QString> title) {
using Flag = Notify::PeerUpdate::Flag;
NameValue(
_peer
) | rpl::start_with_next([=](const TextWithEntities &name) {
_name->setText(name.text);
std::move(
title
) | rpl::start_with_next([=](const QString &title) {
_name->setText(title);
refreshNameGeometry(width());
}, lifetime());

View File

@ -59,6 +59,11 @@ public:
QWidget *parent,
not_null<PeerData*> peer,
not_null<Window::SessionController*> controller);
Cover(
QWidget *parent,
not_null<PeerData*> peer,
not_null<Window::SessionController*> controller,
rpl::producer<QString> title);
Cover *setOnlineCount(rpl::producer<int> &&count);
@ -75,7 +80,7 @@ public:
private:
void setupChildGeometry();
void initViewers();
void initViewers(rpl::producer<QString> title);
void refreshStatusText();
void refreshNameGeometry(int newWidth);
void refreshStatusGeometry(int newWidth);

View File

@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/report_box.h"
#include "boxes/create_poll_box.h"
#include "boxes/peers/add_participants_box.h"
#include "boxes/peers/add_to_contacts_box.h"
#include "ui/toast/toast.h"
#include "auth_session.h"
#include "apiwrap.h"
@ -628,10 +629,7 @@ void PeerMenuDeleteContact(not_null<UserData*> user) {
}
void PeerMenuAddContact(not_null<UserData*> user) {
Ui::show(Box<AddContactBox>(
user->firstName,
user->lastName,
Auth().data().findContactPhone(user)));
Ui::show(Box<AddToContactsBox>(&App::wnd()->controller(), user));
}
void PeerMenuShareContactBox(not_null<UserData*> user) {

View File

@ -1,5 +1,7 @@
<(src_loc)/boxes/peers/add_participants_box.cpp
<(src_loc)/boxes/peers/add_participants_box.h
<(src_loc)/boxes/peers/add_to_contacts_box.cpp
<(src_loc)/boxes/peers/add_to_contacts_box.h
<(src_loc)/boxes/peers/edit_linked_chat_box.cpp
<(src_loc)/boxes/peers/edit_linked_chat_box.h
<(src_loc)/boxes/peers/edit_participant_box.cpp