2017-09-13 17:01:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-13 17:01:23 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-13 17:01:23 +00:00
|
|
|
*/
|
2017-11-03 15:52:53 +00:00
|
|
|
#include "info/common_groups/info_common_groups_inner_widget.h"
|
2017-09-13 17:01:23 +00:00
|
|
|
|
2017-11-03 15:52:53 +00:00
|
|
|
#include "info/common_groups/info_common_groups_widget.h"
|
2017-10-31 18:25:22 +00:00
|
|
|
#include "info/info_controller.h"
|
2017-09-13 17:01:23 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-10-10 20:39:44 +00:00
|
|
|
#include "mtproto/sender.h"
|
2019-11-27 08:02:56 +00:00
|
|
|
#include "main/main_session.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2017-10-10 20:39:44 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2017-11-03 18:26:14 +00:00
|
|
|
#include "ui/search_field_controller.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "styles/style_info.h"
|
|
|
|
#include "styles/style_widgets.h"
|
2017-09-13 17:01:23 +00:00
|
|
|
|
|
|
|
namespace Info {
|
|
|
|
namespace CommonGroups {
|
2017-10-10 20:39:44 +00:00
|
|
|
namespace {
|
2017-09-13 17:01:23 +00:00
|
|
|
|
2017-11-03 18:26:14 +00:00
|
|
|
constexpr auto kCommonGroupsPerPage = 40;
|
|
|
|
constexpr auto kCommonGroupsSearchAfter = 20;
|
2017-10-10 20:39:44 +00:00
|
|
|
|
2021-07-15 11:12:38 +00:00
|
|
|
class ListController final : public PeerListController {
|
2017-10-10 20:39:44 +00:00
|
|
|
public:
|
2017-10-31 18:25:22 +00:00
|
|
|
ListController(
|
|
|
|
not_null<Controller*> controller,
|
2017-10-10 20:39:44 +00:00
|
|
|
not_null<UserData*> user);
|
|
|
|
|
2019-07-25 18:55:11 +00:00
|
|
|
Main::Session &session() const override;
|
2017-10-10 20:39:44 +00:00
|
|
|
void prepare() override;
|
|
|
|
void rowClicked(not_null<PeerListRow*> row) override;
|
|
|
|
void loadMoreRows() override;
|
|
|
|
|
2017-11-03 18:26:14 +00:00
|
|
|
std::unique_ptr<PeerListRow> createRestoredRow(
|
|
|
|
not_null<PeerData*> peer) override {
|
|
|
|
return createRow(peer);
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:08:43 +00:00
|
|
|
std::unique_ptr<PeerListState> saveState() const override;
|
2017-11-03 18:26:14 +00:00
|
|
|
void restoreState(std::unique_ptr<PeerListState> state) override;
|
|
|
|
|
2017-10-10 20:39:44 +00:00
|
|
|
private:
|
2017-11-03 18:26:14 +00:00
|
|
|
std::unique_ptr<PeerListRow> createRow(not_null<PeerData*> peer);
|
|
|
|
|
|
|
|
struct SavedState : SavedStateBase {
|
2021-04-01 21:04:10 +00:00
|
|
|
PeerId preloadGroupId = 0;
|
2017-11-03 18:26:14 +00:00
|
|
|
bool allLoaded = false;
|
|
|
|
bool wasLoading = false;
|
|
|
|
};
|
2017-10-31 18:25:22 +00:00
|
|
|
const not_null<Controller*> _controller;
|
2019-11-27 08:02:56 +00:00
|
|
|
MTP::Sender _api;
|
2017-10-10 20:39:44 +00:00
|
|
|
not_null<UserData*> _user;
|
|
|
|
mtpRequestId _preloadRequestId = 0;
|
|
|
|
bool _allLoaded = false;
|
2021-04-01 21:04:10 +00:00
|
|
|
PeerId _preloadGroupId = 0;
|
2017-10-10 20:39:44 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
ListController::ListController(
|
|
|
|
not_null<Controller*> controller,
|
2017-10-10 20:39:44 +00:00
|
|
|
not_null<UserData*> user)
|
|
|
|
: PeerListController()
|
2017-10-31 18:25:22 +00:00
|
|
|
, _controller(controller)
|
2020-06-17 09:36:25 +00:00
|
|
|
, _api(&_controller->session().mtp())
|
2017-09-13 17:01:23 +00:00
|
|
|
, _user(user) {
|
2017-11-03 18:26:14 +00:00
|
|
|
_controller->setSearchEnabledByContent(false);
|
|
|
|
}
|
|
|
|
|
2019-07-25 18:55:11 +00:00
|
|
|
Main::Session &ListController::session() const {
|
|
|
|
return _user->session();
|
|
|
|
}
|
|
|
|
|
2017-11-03 18:26:14 +00:00
|
|
|
std::unique_ptr<PeerListRow> ListController::createRow(
|
|
|
|
not_null<PeerData*> peer) {
|
|
|
|
auto result = std::make_unique<PeerListRow>(peer);
|
|
|
|
result->setCustomStatus(QString());
|
|
|
|
return result;
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
void ListController::prepare() {
|
2019-06-19 15:09:03 +00:00
|
|
|
setSearchNoResultsText(tr::lng_bot_groups_not_found(tr::now));
|
2017-10-10 20:39:44 +00:00
|
|
|
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
|
2019-06-18 15:00:55 +00:00
|
|
|
delegate()->peerListSetTitle(tr::lng_profile_common_groups_section());
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
void ListController::loadMoreRows() {
|
2017-10-10 20:39:44 +00:00
|
|
|
if (_preloadRequestId || _allLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-27 08:02:56 +00:00
|
|
|
_preloadRequestId = _api.request(MTPmessages_GetCommonChats(
|
2017-10-10 20:39:44 +00:00
|
|
|
_user->inputUser,
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(peerIsChat(_preloadGroupId)
|
2021-04-01 21:04:10 +00:00
|
|
|
? peerToChat(_preloadGroupId).bare
|
2021-08-25 08:15:05 +00:00
|
|
|
: peerToChannel(_preloadGroupId).bare),
|
2017-10-10 20:39:44 +00:00
|
|
|
MTP_int(kCommonGroupsPerPage)
|
|
|
|
)).done([this](const MTPmessages_Chats &result) {
|
|
|
|
_preloadRequestId = 0;
|
|
|
|
_preloadGroupId = 0;
|
|
|
|
_allLoaded = true;
|
2019-01-13 08:03:34 +00:00
|
|
|
const auto &chats = result.match([](const auto &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
return data.vchats().v;
|
2019-01-13 08:03:34 +00:00
|
|
|
});
|
|
|
|
if (!chats.empty()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
for (const auto &chat : chats) {
|
|
|
|
if (const auto peer = _user->owner().processChat(chat)) {
|
|
|
|
if (!peer->migrateTo()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
delegate()->peerListAppendRow(
|
2019-01-18 12:27:37 +00:00
|
|
|
createRow(peer));
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
2021-04-01 21:04:10 +00:00
|
|
|
_preloadGroupId = peer->id;
|
2019-01-13 08:03:34 +00:00
|
|
|
_allLoaded = false;
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 08:03:34 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
2017-11-03 18:26:14 +00:00
|
|
|
auto fullCount = delegate()->peerListFullRowsCount();
|
|
|
|
if (fullCount > kCommonGroupsSearchAfter) {
|
|
|
|
_controller->setSearchEnabledByContent(true);
|
|
|
|
}
|
2017-10-10 20:39:44 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:08:43 +00:00
|
|
|
std::unique_ptr<PeerListState> ListController::saveState() const {
|
2017-11-03 18:26:14 +00:00
|
|
|
auto result = PeerListController::saveState();
|
|
|
|
auto my = std::make_unique<SavedState>();
|
|
|
|
my->preloadGroupId = _preloadGroupId;
|
|
|
|
my->allLoaded = _allLoaded;
|
2017-12-04 12:08:43 +00:00
|
|
|
my->wasLoading = (_preloadRequestId != 0);
|
2017-11-03 18:26:14 +00:00
|
|
|
result->controllerState = std::move(my);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListController::restoreState(
|
|
|
|
std::unique_ptr<PeerListState> state) {
|
|
|
|
auto typeErasedState = state
|
|
|
|
? state->controllerState.get()
|
|
|
|
: nullptr;
|
|
|
|
if (auto my = dynamic_cast<SavedState*>(typeErasedState)) {
|
|
|
|
if (auto requestId = base::take(_preloadRequestId)) {
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(requestId).cancel();
|
2017-11-03 18:26:14 +00:00
|
|
|
}
|
|
|
|
_allLoaded = my->allLoaded;
|
|
|
|
_preloadGroupId = my->preloadGroupId;
|
|
|
|
if (my->wasLoading) {
|
|
|
|
loadMoreRows();
|
|
|
|
}
|
|
|
|
PeerListController::restoreState(std::move(state));
|
|
|
|
auto fullCount = delegate()->peerListFullRowsCount();
|
|
|
|
if (fullCount > kCommonGroupsSearchAfter) {
|
|
|
|
_controller->setSearchEnabledByContent(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:25:22 +00:00
|
|
|
void ListController::rowClicked(not_null<PeerListRow*> row) {
|
2017-12-09 15:13:06 +00:00
|
|
|
_controller->parentController()->showPeerHistory(
|
2017-10-10 20:39:44 +00:00
|
|
|
row->peer(),
|
|
|
|
Window::SectionShow::Way::Forward);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
InnerWidget::InnerWidget(
|
|
|
|
QWidget *parent,
|
2017-10-31 18:25:22 +00:00
|
|
|
not_null<Controller*> controller,
|
2017-10-10 20:39:44 +00:00
|
|
|
not_null<UserData*> user)
|
|
|
|
: RpWidget(parent)
|
2017-11-03 18:26:14 +00:00
|
|
|
, _controller(controller)
|
2017-10-10 20:39:44 +00:00
|
|
|
, _user(user)
|
2017-10-31 18:25:22 +00:00
|
|
|
, _listController(std::make_unique<ListController>(controller, _user))
|
2017-10-10 20:39:44 +00:00
|
|
|
, _list(setupList(this, _listController.get())) {
|
|
|
|
setContent(_list.data());
|
|
|
|
_listController->setDelegate(static_cast<PeerListDelegate*>(this));
|
2017-11-17 13:23:36 +00:00
|
|
|
|
2017-12-22 07:05:20 +00:00
|
|
|
_controller->searchFieldController()->queryValue(
|
|
|
|
) | rpl::start_with_next([this](QString &&query) {
|
|
|
|
peerListScrollToTop();
|
|
|
|
content()->searchQueryChanged(std::move(query));
|
|
|
|
}, lifetime());
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:01:23 +00:00
|
|
|
void InnerWidget::visibleTopBottomUpdated(
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
2017-10-10 20:39:44 +00:00
|
|
|
setChildVisibleTopBottom(_list, visibleTop, visibleBottom);
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InnerWidget::saveState(not_null<Memento*> memento) {
|
2017-11-03 18:26:14 +00:00
|
|
|
memento->setListState(_listController->saveState());
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InnerWidget::restoreState(not_null<Memento*> memento) {
|
2017-11-05 09:56:43 +00:00
|
|
|
_listController->restoreState(memento->listState());
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<Ui::ScrollToRequest> InnerWidget::scrollToRequests() const {
|
|
|
|
return _scrollToRequests.events();
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:39:44 +00:00
|
|
|
int InnerWidget::desiredHeight() const {
|
|
|
|
auto desired = 0;
|
|
|
|
auto count = qMax(_user->commonChatsCount(), 1);
|
|
|
|
desired += qMax(count, _list->fullRowsCount())
|
|
|
|
* st::infoCommonGroupsList.item.height;
|
|
|
|
return qMax(height(), desired);
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<InnerWidget::ListWidget> InnerWidget::setupList(
|
|
|
|
RpWidget *parent,
|
|
|
|
not_null<PeerListController*> controller) const {
|
2020-11-29 18:26:49 +00:00
|
|
|
controller->setStyleOverrides(&st::infoCommonGroupsList);
|
2017-10-10 20:39:44 +00:00
|
|
|
auto result = object_ptr<ListWidget>(
|
|
|
|
parent,
|
2020-11-29 18:26:49 +00:00
|
|
|
controller);
|
2017-12-22 07:05:20 +00:00
|
|
|
result->scrollToRequests(
|
|
|
|
) | rpl::start_with_next([this](Ui::ScrollToRequest request) {
|
|
|
|
auto addmin = (request.ymin < 0)
|
|
|
|
? 0
|
|
|
|
: st::infoCommonGroupsMargin.top();
|
|
|
|
auto addmax = (request.ymax < 0)
|
|
|
|
? 0
|
|
|
|
: st::infoCommonGroupsMargin.top();
|
|
|
|
_scrollToRequests.fire({
|
|
|
|
request.ymin + addmin,
|
|
|
|
request.ymax + addmax });
|
|
|
|
}, result->lifetime());
|
2017-10-10 20:39:44 +00:00
|
|
|
result->moveToLeft(0, st::infoCommonGroupsMargin.top());
|
2017-12-22 07:05:20 +00:00
|
|
|
parent->widthValue(
|
|
|
|
) | rpl::start_with_next([list = result.data()](int newWidth) {
|
|
|
|
list->resizeToWidth(newWidth);
|
|
|
|
}, result->lifetime());
|
|
|
|
result->heightValue(
|
|
|
|
) | rpl::start_with_next([parent](int listHeight) {
|
|
|
|
auto newHeight = st::infoCommonGroupsMargin.top()
|
|
|
|
+ listHeight
|
|
|
|
+ st::infoCommonGroupsMargin.bottom();
|
|
|
|
parent->resize(parent->width(), newHeight);
|
|
|
|
}, result->lifetime());
|
2017-10-10 20:39:44 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-18 15:00:55 +00:00
|
|
|
void InnerWidget::peerListSetTitle(rpl::producer<QString> title) {
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
void InnerWidget::peerListSetAdditionalTitle(rpl::producer<QString> title) {
|
2017-10-10 20:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 13:05:21 +00:00
|
|
|
bool InnerWidget::peerListIsRowChecked(not_null<PeerListRow*> row) {
|
2017-10-10 20:39:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int InnerWidget::peerListSelectedRowsCount() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerWidget::peerListScrollToTop() {
|
|
|
|
_scrollToRequests.fire({ -1, -1 });
|
|
|
|
}
|
|
|
|
|
2020-03-13 13:05:21 +00:00
|
|
|
void InnerWidget::peerListAddSelectedPeerInBunch(not_null<PeerData*> peer) {
|
|
|
|
Unexpected("Item selection in Info::Profile::Members.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerWidget::peerListAddSelectedRowInBunch(not_null<PeerListRow*> row) {
|
2017-10-10 20:39:44 +00:00
|
|
|
Unexpected("Item selection in Info::Profile::Members.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerWidget::peerListFinishSelectedRowsBunch() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerWidget::peerListSetDescription(
|
|
|
|
object_ptr<Ui::FlatLabel> description) {
|
|
|
|
description.destroy();
|
2017-09-13 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace CommonGroups
|
|
|
|
} // namespace Info
|
|
|
|
|