Slightly improved appearing of usernames list.

This commit is contained in:
23rd 2022-10-12 21:16:04 +03:00 committed by John Preston
parent d55ff7aa4a
commit 17623640b3
5 changed files with 52 additions and 2 deletions

View File

@ -199,5 +199,28 @@ Data::Usernames Usernames::FromTL(const MTPVector<MTPUsername> &usernames) {
) | ranges::views::transform(UsernameFromTL) | ranges::to_vector; ) | ranges::views::transform(UsernameFromTL) | ranges::to_vector;
} }
void Usernames::requestToCache(not_null<PeerData*> peer) {
_tinyCache = {};
if (const auto user = peer->asUser()) {
if (user->usernames().empty()) {
return;
}
} else if (const auto channel = peer->asChannel()) {
if (channel->usernames().empty()) {
return;
}
}
const auto lifetime = std::make_shared<rpl::lifetime>();
*lifetime = loadUsernames(
peer
) | rpl::start_with_next([=, id = peer->id](Data::Usernames usernames) {
_tinyCache = std::make_pair(id, std::move(usernames));
lifetime->destroy();
});
}
Data::Usernames Usernames::cacheFor(PeerId id) {
return (_tinyCache.first == id) ? _tinyCache.second : Data::Usernames();
}
} // namespace Api } // namespace Api

View File

@ -33,6 +33,9 @@ public:
not_null<PeerData*> peer, not_null<PeerData*> peer,
const std::vector<QString> &usernames); const std::vector<QString> &usernames);
void requestToCache(not_null<PeerData*> peer);
[[nodiscard]] Data::Usernames cacheFor(PeerId id);
static Data::Usernames FromTL(const MTPVector<MTPUsername> &usernames); static Data::Usernames FromTL(const MTPVector<MTPUsername> &usernames);
private: private:
@ -47,6 +50,8 @@ private:
}; };
base::flat_map<Key, Entry> _toggleRequests; base::flat_map<Key, Entry> _toggleRequests;
base::flat_map<Key, mtpRequestId> _reorderRequests; base::flat_map<Key, mtpRequestId> _reorderRequests;
// Used for a seamless display of usernames list.
std::pair<Key, Data::Usernames> _tinyCache;
}; };

View File

@ -635,7 +635,7 @@ void Controller::showEditPeerTypeBox(
}); });
_typeDataSavedValue->hasLinkedChat _typeDataSavedValue->hasLinkedChat
= (_linkedChatSavedValue.value_or(nullptr) != nullptr); = (_linkedChatSavedValue.value_or(nullptr) != nullptr);
_navigation->parentController()->show( const auto box = _navigation->parentController()->show(
Box<EditPeerTypeBox>( Box<EditPeerTypeBox>(
_navigation, _navigation,
_peer, _peer,
@ -644,6 +644,10 @@ void Controller::showEditPeerTypeBox(
_typeDataSavedValue, _typeDataSavedValue,
error), error),
Ui::LayerOption::KeepOther); Ui::LayerOption::KeepOther);
box->boxClosing(
) | rpl::start_with_next([peer = _peer] {
peer->session().api().usernames().requestToCache(peer);
}, box->lifetime());
} }
void Controller::showEditLinkedChatBox() { void Controller::showEditLinkedChatBox() {
@ -740,6 +744,9 @@ void Controller::fillPrivacyTypeButton() {
: tr::lng_manage_peer_channel_type)(), : tr::lng_manage_peer_channel_type)(),
_privacyTypeUpdates.events( _privacyTypeUpdates.events(
) | rpl::map([=](Privacy flag) { ) | rpl::map([=](Privacy flag) {
if (flag == Privacy::HasUsername) {
_peer->session().api().usernames().requestToCache(_peer);
}
return (flag == Privacy::HasUsername) return (flag == Privacy::HasUsername)
? (hasLocation ? (hasLocation
? tr::lng_manage_peer_link_permanent ? tr::lng_manage_peer_link_permanent

View File

@ -113,6 +113,13 @@ UsernamesList::UsernamesList(
: RpWidget(parent) : RpWidget(parent)
, _show(show) , _show(show)
, _peer(peer) { , _peer(peer) {
{
auto &api = _peer->session().api();
const auto usernames = api.usernames().cacheFor(_peer->id);
if (!usernames.empty()) {
rebuild(usernames);
}
}
load(); load();
} }

View File

@ -48,6 +48,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_peer_menu.h" #include "window/window_peer_menu.h"
#include "apiwrap.h" #include "apiwrap.h"
#include "api/api_peer_photo.h" #include "api/api_peer_photo.h"
#include "api/api_user_names.h"
#include "core/file_utilities.h" #include "core/file_utilities.h"
#include "base/call_delayed.h" #include "base/call_delayed.h"
#include "base/unixtime.h" #include "base/unixtime.h"
@ -445,12 +446,19 @@ void SetupRows(
"internal:edit_username" }); "internal:edit_username" });
return result; return result;
}); });
session->api().usernames().requestToCache(session->user());
AddRow( AddRow(
container, container,
std::move(label), std::move(label),
std::move(value), std::move(value),
tr::lng_context_copy_mention(tr::now), tr::lng_context_copy_mention(tr::now),
[=] { controller->show(Box(UsernamesBox, session)); }, [=] {
const auto box = controller->show(Box(UsernamesBox, session));
box->boxClosing(
) | rpl::start_with_next([=] {
session->api().usernames().requestToCache(session->user());
}, box->lifetime());
},
{ &st::settingsIconMention, kIconLightOrange }); { &st::settingsIconMention, kIconLightOrange });
AddSkip(container); AddSkip(container);