settings_blocked_peers.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "settings/settings_blocked_peers.h"
  8. #include "api/api_blocked_peers.h"
  9. #include "apiwrap.h"
  10. #include "data/data_changes.h"
  11. #include "data/data_peer.h"
  12. #include "lang/lang_keys.h"
  13. #include "lottie/lottie_icon.h"
  14. #include "main/main_session.h"
  15. #include "settings/settings_privacy_controllers.h"
  16. #include "ui/widgets/buttons.h"
  17. #include "ui/wrap/padding_wrap.h"
  18. #include "ui/wrap/slide_wrap.h"
  19. #include "ui/wrap/vertical_layout.h"
  20. #include "ui/vertical_list.h"
  21. #include "window/window_session_controller.h"
  22. #include "styles/style_settings.h"
  23. #include "styles/style_boxes.h"
  24. #include "styles/style_layers.h"
  25. #include "styles/style_menu_icons.h"
  26. namespace Settings {
  27. Blocked::Blocked(
  28. QWidget *parent,
  29. not_null<Window::SessionController*> controller)
  30. : Section(parent)
  31. , _controller(controller)
  32. , _container(Ui::CreateChild<Ui::VerticalLayout>(this)) {
  33. setupContent();
  34. {
  35. auto padding = st::changePhoneIconPadding;
  36. padding.setBottom(padding.top());
  37. _loading = base::make_unique_q<Ui::CenterWrap<>>(
  38. this,
  39. object_ptr<Ui::PaddingWrap<>>(
  40. this,
  41. object_ptr<Ui::FlatLabel>(
  42. this,
  43. tr::lng_contacts_loading(),
  44. st::changePhoneDescription),
  45. std::move(padding)));
  46. Ui::ResizeFitChild(
  47. this,
  48. _loading.get(),
  49. st::settingsBlockedHeightMin);
  50. }
  51. _controller->session().api().blockedPeers().slice(
  52. ) | rpl::start_with_next([=](const Api::BlockedPeers::Slice &slice) {
  53. checkTotal(slice.total);
  54. }, lifetime());
  55. _controller->session().changes().peerUpdates(
  56. Data::PeerUpdate::Flag::IsBlocked
  57. ) | rpl::start_with_next([=](const Data::PeerUpdate &update) {
  58. if (update.peer->isBlocked()) {
  59. checkTotal(1);
  60. }
  61. }, lifetime());
  62. }
  63. rpl::producer<QString> Blocked::title() {
  64. return tr::lng_settings_blocked_users();
  65. }
  66. QPointer<Ui::RpWidget> Blocked::createPinnedToTop(not_null<QWidget*> parent) {
  67. const auto content = Ui::CreateChild<Ui::VerticalLayout>(parent.get());
  68. Ui::AddSkip(content);
  69. AddButtonWithIcon(
  70. content,
  71. tr::lng_blocked_list_add(),
  72. st::settingsButtonActive,
  73. { &st::menuIconBlockSettings }
  74. )->addClickHandler([=] {
  75. BlockedBoxController::BlockNewPeer(_controller);
  76. });
  77. Ui::AddSkip(content);
  78. Ui::AddDividerText(content, tr::lng_blocked_list_about());
  79. {
  80. const auto subtitle = content->add(
  81. object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  82. content,
  83. object_ptr<Ui::VerticalLayout>(content)))->setDuration(0);
  84. Ui::AddSkip(subtitle->entity());
  85. auto subtitleText = _countBlocked.value(
  86. ) | rpl::map([=](int count) {
  87. return tr::lng_blocked_list_subtitle(tr::now, lt_count, count);
  88. });
  89. Ui::AddSubsectionTitle(
  90. subtitle->entity(),
  91. rpl::duplicate(subtitleText),
  92. st::settingsBlockedListSubtitleAddPadding);
  93. subtitle->toggleOn(
  94. rpl::merge(
  95. _emptinessChanges.events() | rpl::map(!rpl::mappers::_1),
  96. _countBlocked.value() | rpl::map(rpl::mappers::_1 > 0)
  97. ) | rpl::distinct_until_changed());
  98. // Workaround.
  99. std::move(
  100. subtitleText
  101. ) | rpl::start_with_next([=] {
  102. subtitle->entity()->resizeToWidth(content->width());
  103. }, subtitle->lifetime());
  104. }
  105. return Ui::MakeWeak(not_null<Ui::RpWidget*>{ content });
  106. }
  107. void Blocked::setupContent() {
  108. using namespace rpl::mappers;
  109. const auto listWrap = _container->add(
  110. object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  111. _container,
  112. object_ptr<Ui::VerticalLayout>(_container)));
  113. listWrap->toggleOn(
  114. _emptinessChanges.events_starting_with(true) | rpl::map(!_1),
  115. anim::type::instant);
  116. {
  117. struct State {
  118. std::unique_ptr<BlockedBoxController> controller;
  119. std::unique_ptr<PeerListContentDelegateSimple> delegate;
  120. };
  121. auto controller = std::make_unique<BlockedBoxController>(_controller);
  122. controller->setStyleOverrides(&st::settingsBlockedList);
  123. const auto content = listWrap->entity()->add(
  124. object_ptr<PeerListContent>(this, controller.get()));
  125. const auto state = content->lifetime().make_state<State>();
  126. state->controller = std::move(controller);
  127. state->delegate = std::make_unique<PeerListContentDelegateSimple>();
  128. state->delegate->setContent(content);
  129. state->controller->setDelegate(state->delegate.get());
  130. state->controller->rowsCountChanges(
  131. ) | rpl::start_with_next([=](int total) {
  132. _countBlocked = total;
  133. checkTotal(total);
  134. }, content->lifetime());
  135. _countBlocked = content->fullRowsCount();
  136. }
  137. const auto emptyWrap = _container->add(
  138. object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
  139. _container,
  140. object_ptr<Ui::VerticalLayout>(_container)));
  141. emptyWrap->toggleOn(
  142. _emptinessChanges.events_starting_with(false),
  143. anim::type::instant);
  144. {
  145. const auto content = emptyWrap->entity();
  146. auto icon = CreateLottieIcon(
  147. content,
  148. {
  149. .name = u"blocked_peers_empty"_q,
  150. .sizeOverride = {
  151. st::changePhoneIconSize,
  152. st::changePhoneIconSize,
  153. },
  154. },
  155. st::settingsBlockedListIconPadding);
  156. content->add(std::move(icon.widget));
  157. _showFinished.events(
  158. ) | rpl::start_with_next([animate = std::move(icon.animate)] {
  159. animate(anim::repeat::once);
  160. }, content->lifetime());
  161. content->add(
  162. object_ptr<Ui::CenterWrap<>>(
  163. content,
  164. object_ptr<Ui::FlatLabel>(
  165. content,
  166. tr::lng_blocked_list_empty_title(),
  167. st::changePhoneTitle)),
  168. st::changePhoneTitlePadding);
  169. content->add(
  170. object_ptr<Ui::CenterWrap<>>(
  171. content,
  172. object_ptr<Ui::FlatLabel>(
  173. content,
  174. tr::lng_blocked_list_empty_description(),
  175. st::changePhoneDescription)),
  176. st::changePhoneDescriptionPadding);
  177. Ui::AddSkip(content, st::settingsBlockedListIconPadding.top());
  178. }
  179. // We want minimal height to be the same no matter if subtitle
  180. // is visible or not, so minimal height isn't a constant here.
  181. // Ui::ResizeFitChild(this, _container, st::settingsBlockedHeightMin);
  182. widthValue(
  183. ) | rpl::start_with_next([=](int width) {
  184. _container->resizeToWidth(width);
  185. }, _container->lifetime());
  186. rpl::combine(
  187. _container->heightValue(),
  188. _emptinessChanges.events_starting_with(true)
  189. ) | rpl::start_with_next([=](int height, bool empty) {
  190. const auto subtitled = !empty || (_countBlocked.current() > 0);
  191. const auto total = st::settingsBlockedHeightMin;
  192. const auto padding = st::defaultSubsectionTitlePadding
  193. + st::settingsBlockedListSubtitleAddPadding;
  194. const auto subtitle = st::defaultVerticalListSkip
  195. + padding.top()
  196. + st::defaultSubsectionTitle.style.font->height
  197. + padding.bottom();
  198. const auto min = total - (subtitled ? subtitle : 0);
  199. resize(width(), std::max(height, min));
  200. }, _container->lifetime());
  201. }
  202. void Blocked::checkTotal(int total) {
  203. _loading = nullptr;
  204. _emptinessChanges.fire(total <= 0);
  205. }
  206. void Blocked::visibleTopBottomUpdated(int visibleTop, int visibleBottom) {
  207. setChildVisibleTopBottom(_container, visibleTop, visibleBottom);
  208. }
  209. void Blocked::showFinished() {
  210. _showFinished.fire({});
  211. }
  212. } // namespace Settings