2024-02-20 15:06:01 +00:00
|
|
|
/*
|
|
|
|
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 "settings/business/settings_greeting.h"
|
|
|
|
|
2024-02-23 09:56:18 +00:00
|
|
|
#include "base/event_filter.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "core/application.h"
|
2024-02-23 09:56:18 +00:00
|
|
|
#include "data/business/data_business_info.h"
|
2024-02-23 17:23:15 +00:00
|
|
|
#include "data/business/data_shortcut_messages.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "main/main_session.h"
|
2024-02-23 17:23:15 +00:00
|
|
|
#include "settings/business/settings_shortcut_messages.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "settings/business/settings_recipients_helper.h"
|
2024-03-01 13:28:51 +00:00
|
|
|
#include "ui/boxes/time_picker_box.h"
|
2024-02-23 09:56:18 +00:00
|
|
|
#include "ui/layers/generic_box.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "ui/text/text_utilities.h"
|
2024-03-01 08:29:03 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2024-02-23 09:56:18 +00:00
|
|
|
#include "ui/widgets/box_content_divider.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2024-02-23 09:56:18 +00:00
|
|
|
#include "ui/widgets/vertical_drum_picker.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/vertical_list.h"
|
|
|
|
#include "window/window_session_controller.h"
|
2024-02-23 09:56:18 +00:00
|
|
|
#include "styles/style_layers.h"
|
2024-02-20 15:06:01 +00:00
|
|
|
#include "styles/style_settings.h"
|
|
|
|
|
|
|
|
namespace Settings {
|
|
|
|
namespace {
|
|
|
|
|
2024-02-23 09:56:18 +00:00
|
|
|
constexpr auto kDefaultNoActivityDays = 7;
|
|
|
|
|
2024-02-20 15:06:01 +00:00
|
|
|
class Greeting : public BusinessSection<Greeting> {
|
|
|
|
public:
|
|
|
|
Greeting(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller);
|
|
|
|
~Greeting();
|
|
|
|
|
2024-03-07 13:02:32 +00:00
|
|
|
[[nodiscard]] bool closeByOutsideClick() const override;
|
2024-02-20 15:06:01 +00:00
|
|
|
[[nodiscard]] rpl::producer<QString> title() override;
|
|
|
|
|
2024-03-01 18:18:01 +00:00
|
|
|
const Ui::RoundRect *bottomSkipRounding() const override {
|
2024-02-23 09:56:18 +00:00
|
|
|
return &_bottomSkipRounding;
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:06:01 +00:00
|
|
|
private:
|
|
|
|
void setupContent(not_null<Window::SessionController*> controller);
|
|
|
|
void save();
|
|
|
|
|
2024-02-23 09:56:18 +00:00
|
|
|
Ui::RoundRect _bottomSkipRounding;
|
|
|
|
|
2024-02-20 15:06:01 +00:00
|
|
|
rpl::variable<Data::BusinessRecipients> _recipients;
|
2024-03-01 08:29:03 +00:00
|
|
|
rpl::variable<bool> _canHave;
|
|
|
|
rpl::event_stream<> _deactivateOnAttempt;
|
2024-02-23 09:56:18 +00:00
|
|
|
rpl::variable<int> _noActivityDays;
|
|
|
|
rpl::variable<bool> _enabled;
|
2024-02-20 15:06:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Greeting::Greeting(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
2024-02-23 09:56:18 +00:00
|
|
|
: BusinessSection(parent, controller)
|
|
|
|
, _bottomSkipRounding(st::boxRadius, st::boxDividerBg) {
|
2024-02-20 15:06:01 +00:00
|
|
|
setupContent(controller);
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:56:18 +00:00
|
|
|
void EditPeriodBox(
|
|
|
|
not_null<Ui::GenericBox*> box,
|
|
|
|
int days,
|
|
|
|
Fn<void(int)> save) {
|
2024-03-01 13:28:51 +00:00
|
|
|
auto values = std::vector{ 7, 14, 21, 28 };
|
|
|
|
if (!ranges::contains(values, days)) {
|
|
|
|
values.push_back(days);
|
|
|
|
ranges::sort(values);
|
2024-02-23 09:56:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 13:28:51 +00:00
|
|
|
const auto phrases = ranges::views::all(
|
|
|
|
values
|
|
|
|
) | ranges::views::transform([](int days) {
|
|
|
|
return tr::lng_days(tr::now, lt_count, days);
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
const auto take = TimePickerBox(box, values, phrases, days);
|
2024-02-23 09:56:18 +00:00
|
|
|
|
|
|
|
box->addButton(tr::lng_settings_save(), [=] {
|
|
|
|
const auto weak = Ui::MakeWeak(box);
|
2024-03-01 13:28:51 +00:00
|
|
|
save(take());
|
2024-02-23 09:56:18 +00:00
|
|
|
if (const auto strong = weak.data()) {
|
|
|
|
strong->closeBox();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
box->addButton(tr::lng_cancel(), [=] {
|
|
|
|
box->closeBox();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:06:01 +00:00
|
|
|
Greeting::~Greeting() {
|
|
|
|
if (!Core::Quitting()) {
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:02:32 +00:00
|
|
|
bool Greeting::closeByOutsideClick() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:06:01 +00:00
|
|
|
rpl::producer<QString> Greeting::title() {
|
|
|
|
return tr::lng_greeting_title();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Greeting::setupContent(
|
|
|
|
not_null<Window::SessionController*> controller) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
|
2024-02-23 09:56:18 +00:00
|
|
|
const auto info = &controller->session().data().businessInfo();
|
|
|
|
const auto current = info->greetingSettings();
|
|
|
|
const auto disabled = !current.noActivityDays;
|
2024-02-20 15:06:01 +00:00
|
|
|
|
2024-03-01 13:28:51 +00:00
|
|
|
_recipients = disabled
|
|
|
|
? Data::BusinessRecipients{ .allButExcluded = true }
|
2024-04-09 07:55:38 +00:00
|
|
|
: Data::BusinessRecipients::MakeValid(current.recipients);
|
2024-02-23 09:56:18 +00:00
|
|
|
_noActivityDays = disabled
|
|
|
|
? kDefaultNoActivityDays
|
|
|
|
: current.noActivityDays;
|
2024-02-20 15:06:01 +00:00
|
|
|
|
|
|
|
AddDividerTextWithLottie(content, {
|
|
|
|
.lottie = u"greeting"_q,
|
|
|
|
.lottieSize = st::settingsCloudPasswordIconSize,
|
|
|
|
.lottieMargins = st::peerAppearanceIconPadding,
|
|
|
|
.showFinished = showFinishes(),
|
|
|
|
.about = tr::lng_greeting_about(Ui::Text::WithEntities),
|
|
|
|
.aboutMargins = st::peerAppearanceCoverLabelMargin,
|
|
|
|
});
|
|
|
|
|
2024-03-01 08:29:03 +00:00
|
|
|
const auto session = &controller->session();
|
|
|
|
_canHave = rpl::combine(
|
|
|
|
ShortcutsCountValue(session),
|
|
|
|
ShortcutsLimitValue(session),
|
|
|
|
ShortcutExistsValue(session, u"hello"_q),
|
|
|
|
(_1 < _2) || _3);
|
|
|
|
|
2024-02-20 15:06:01 +00:00
|
|
|
Ui::AddSkip(content);
|
|
|
|
const auto enabled = content->add(object_ptr<Ui::SettingsButton>(
|
|
|
|
content,
|
|
|
|
tr::lng_greeting_enable(),
|
|
|
|
st::settingsButtonNoIcon
|
2024-03-01 08:29:03 +00:00
|
|
|
))->toggleOn(rpl::single(
|
|
|
|
!disabled
|
|
|
|
) | rpl::then(rpl::merge(
|
|
|
|
_canHave.value() | rpl::filter(!_1),
|
|
|
|
_deactivateOnAttempt.events() | rpl::map_to(false)
|
|
|
|
)));
|
2024-02-23 09:56:18 +00:00
|
|
|
|
|
|
|
_enabled = enabled->toggledValue();
|
2024-03-01 08:29:03 +00:00
|
|
|
_enabled.value() | rpl::filter(_1) | rpl::start_with_next([=] {
|
|
|
|
if (!_canHave.current()) {
|
|
|
|
controller->showToast({
|
2024-03-01 18:18:01 +00:00
|
|
|
.text = { tr::lng_greeting_limit_reached(tr::now) },
|
2024-03-01 08:29:03 +00:00
|
|
|
.adaptive = true,
|
|
|
|
});
|
|
|
|
_deactivateOnAttempt.fire({});
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2024-02-23 09:56:18 +00:00
|
|
|
|
|
|
|
Ui::AddSkip(content);
|
|
|
|
|
|
|
|
content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::BoxContentDivider>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::BoxContentDivider>(
|
|
|
|
content,
|
|
|
|
st::boxDividerHeight,
|
|
|
|
st::boxDividerBg,
|
|
|
|
RectPart::Top))
|
|
|
|
)->setDuration(0)->toggleOn(enabled->toggledValue() | rpl::map(!_1));
|
|
|
|
content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::BoxContentDivider>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::BoxContentDivider>(
|
|
|
|
content))
|
|
|
|
)->setDuration(0)->toggleOn(enabled->toggledValue());
|
2024-02-20 15:06:01 +00:00
|
|
|
|
|
|
|
const auto wrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content)));
|
|
|
|
const auto inner = wrap->entity();
|
|
|
|
|
2024-02-23 17:23:15 +00:00
|
|
|
const auto createWrap = inner->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
inner,
|
|
|
|
object_ptr<Ui::VerticalLayout>(inner)));
|
|
|
|
const auto createInner = createWrap->entity();
|
|
|
|
Ui::AddSkip(createInner);
|
2024-03-01 08:29:03 +00:00
|
|
|
const auto create = AddButtonWithLabel(
|
2024-02-23 17:23:15 +00:00
|
|
|
createInner,
|
2024-03-01 08:29:03 +00:00
|
|
|
rpl::conditional(
|
|
|
|
ShortcutExistsValue(session, u"hello"_q),
|
|
|
|
tr::lng_business_edit_messages(),
|
|
|
|
tr::lng_greeting_create()),
|
|
|
|
ShortcutMessagesCountValue(
|
|
|
|
session,
|
|
|
|
u"hello"_q
|
|
|
|
) | rpl::map([=](int count) {
|
|
|
|
return count
|
|
|
|
? tr::lng_forum_messages(tr::now, lt_count, count)
|
|
|
|
: QString();
|
|
|
|
}),
|
|
|
|
st::settingsButtonLightNoIcon);
|
2024-02-23 17:23:15 +00:00
|
|
|
create->setClickedCallback([=] {
|
|
|
|
const auto owner = &controller->session().data();
|
|
|
|
const auto id = owner->shortcutMessages().emplaceShortcut("hello");
|
2024-02-28 10:55:28 +00:00
|
|
|
showOther(ShortcutMessagesId(id));
|
2024-02-23 17:23:15 +00:00
|
|
|
});
|
|
|
|
Ui::AddSkip(createInner);
|
|
|
|
Ui::AddDivider(createInner);
|
|
|
|
|
|
|
|
createWrap->toggleOn(rpl::single(true));
|
|
|
|
|
|
|
|
Ui::AddSkip(inner);
|
2024-02-20 15:06:01 +00:00
|
|
|
AddBusinessRecipientsSelector(inner, {
|
|
|
|
.controller = controller,
|
|
|
|
.title = tr::lng_greeting_recipients(),
|
|
|
|
.data = &_recipients,
|
2024-03-18 11:02:12 +00:00
|
|
|
.type = Data::BusinessRecipientsType::Messages,
|
2024-02-20 15:06:01 +00:00
|
|
|
});
|
|
|
|
|
2024-02-23 09:56:18 +00:00
|
|
|
Ui::AddSkip(inner);
|
|
|
|
Ui::AddDivider(inner);
|
|
|
|
Ui::AddSkip(inner);
|
|
|
|
|
|
|
|
AddButtonWithLabel(
|
|
|
|
inner,
|
|
|
|
tr::lng_greeting_period_title(),
|
|
|
|
_noActivityDays.value(
|
|
|
|
) | rpl::map(
|
|
|
|
[](int days) { return tr::lng_days(tr::now, lt_count, days); }
|
|
|
|
),
|
|
|
|
st::settingsButtonNoIcon
|
|
|
|
)->setClickedCallback([=] {
|
|
|
|
controller->show(Box(
|
|
|
|
EditPeriodBox,
|
|
|
|
_noActivityDays.current(),
|
|
|
|
[=](int days) { _noActivityDays = days; }));
|
|
|
|
});
|
|
|
|
|
|
|
|
Ui::AddSkip(inner);
|
|
|
|
Ui::AddDividerText(
|
|
|
|
inner,
|
|
|
|
tr::lng_greeting_period_about(),
|
|
|
|
st::settingsChatbotsBottomTextMargin,
|
|
|
|
RectPart::Top);
|
|
|
|
|
|
|
|
wrap->toggleOn(enabled->toggledValue());
|
|
|
|
wrap->finishAnimating();
|
2024-02-20 15:06:01 +00:00
|
|
|
|
|
|
|
Ui::ResizeFitChild(this, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Greeting::save() {
|
2024-03-01 13:28:51 +00:00
|
|
|
const auto show = controller()->uiShow();
|
2024-03-01 09:17:41 +00:00
|
|
|
const auto session = &controller()->session();
|
2024-03-01 13:28:51 +00:00
|
|
|
const auto fail = [=](QString error) {
|
|
|
|
if (error == u"BUSINESS_RECIPIENTS_EMPTY"_q) {
|
2024-03-05 16:52:14 +00:00
|
|
|
show->showToast(tr::lng_greeting_recipients_empty(tr::now));
|
2024-03-01 13:28:51 +00:00
|
|
|
} else if (error != u"SHORTCUT_INVALID"_q) {
|
|
|
|
show->showToast(error);
|
|
|
|
}
|
|
|
|
};
|
2024-03-01 09:17:41 +00:00
|
|
|
session->data().businessInfo().saveGreetingSettings(
|
2024-02-23 09:56:18 +00:00
|
|
|
_enabled.current() ? Data::GreetingSettings{
|
|
|
|
.recipients = _recipients.current(),
|
|
|
|
.noActivityDays = _noActivityDays.current(),
|
2024-03-01 09:17:41 +00:00
|
|
|
.shortcutId = LookupShortcutId(session, u"hello"_q),
|
2024-03-01 13:28:51 +00:00
|
|
|
} : Data::GreetingSettings(),
|
|
|
|
fail);
|
2024-02-20 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Type GreetingId() {
|
|
|
|
return Greeting::Id();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|