Improve non-authed language switch.

This commit is contained in:
John Preston 2019-06-19 13:22:25 +02:00
parent a7c8feaecb
commit 52e40cbc6e
9 changed files with 61 additions and 10 deletions

View File

@ -55,6 +55,7 @@ public:
void activateSelected();
rpl::producer<Language> activations() const;
void changeChosen(const QString &chosen);
Ui::ScrollToRequest rowScrollRequest(int index) const;
@ -170,6 +171,7 @@ public:
Ui::ScrollToRequest jump(int rows);
void filter(const QString &query);
rpl::producer<Language> activations() const;
void changeChosen(const QString &chosen);
void activateBySubmit();
private:
@ -180,6 +182,7 @@ private:
Fn<Ui::ScrollToRequest(int rows)> _jump;
Fn<void(const QString &query)> _filter;
Fn<rpl::producer<Language>()> _activations;
Fn<void(const QString &chosen)> _changeChosen;
Fn<void()> _activateBySubmit;
};
@ -613,6 +616,12 @@ rpl::producer<Language> Rows::activations() const {
return _activations.events();
}
void Rows::changeChosen(const QString &chosen) {
for (const auto &row : _rows) {
row.check->setChecked(row.data.id == chosen, anim::type::normal);
}
}
void Rows::setSelected(int selected) {
_mouseSelection = false;
const auto limit = count();
@ -1003,6 +1012,14 @@ void Content::setupContent(
other->activations()
) | rpl::type_erased();
};
_changeChosen = [=](const QString &chosen) {
if (main) {
main->changeChosen(chosen);
}
if (other) {
other->changeChosen(chosen);
}
};
_activateBySubmit = [=] {
if (selectedIndex() < 0) {
_jump(1);
@ -1024,6 +1041,10 @@ rpl::producer<Language> Content::activations() const {
return _activations();
}
void Content::changeChosen(const QString &chosen) {
_changeChosen(chosen);
}
void Content::activateBySubmit() {
_activateBySubmit();
}
@ -1076,6 +1097,9 @@ void LanguageBox::prepare() {
// So we check that the language really has changed.
if (language.id != Lang::Current().id()) {
Lang::CurrentCloudManager().switchToLanguage(language);
if (inner) {
inner->changeChosen(Lang::Current().id());
}
}
}, inner->lifetime());

View File

@ -94,7 +94,7 @@ bool PasscodeBox::onlyCheckCurrent() const {
void PasscodeBox::prepare() {
addButton(
(_cloudFields.customSubmitButton
? rpl::single(*_cloudFields.customSubmitButton)
? std::move(_cloudFields.customSubmitButton)
: _turningOff
? tr::lng_passcode_remove_button()
: tr::lng_settings_save()),
@ -111,7 +111,7 @@ void PasscodeBox::prepare() {
if (onlyCheck) {
_oldPasscode->show();
setTitle(_cloudFields.customTitle
? rpl::single(*_cloudFields.customTitle)
? std::move(_cloudFields.customTitle)
: _cloudPwd
? tr::lng_cloud_password_remove()
: tr::lng_passcode_remove());

View File

@ -38,9 +38,9 @@ public:
// Check cloud password for some action.
Fn<void(const Core::CloudPasswordResult &)> customCheckCallback;
std::optional<QString> customTitle;
rpl::producer<QString> customTitle;
std::optional<QString> customDescription;
std::optional<QString> customSubmitButton;
rpl::producer<QString> customSubmitButton;
};
PasscodeBox(QWidget*, const CloudFields &fields);

View File

@ -490,10 +490,10 @@ void EditAdminBox::requestTransferPassword(not_null<ChannelData*> channel) {
) | rpl::start_with_next([=](const Core::CloudPasswordState &state) {
const auto box = std::make_shared<QPointer<PasscodeBox>>();
auto fields = PasscodeBox::CloudFields::From(state);
fields.customTitle = lang(lng_rights_transfer_password_title);
fields.customTitle = tr::lng_rights_transfer_password_title();
fields.customDescription
= lang(lng_rights_transfer_password_description);
fields.customSubmitButton = lang(lng_passcode_submit);
fields.customSubmitButton = tr::lng_passcode_submit();
fields.customCheckCallback = crl::guard(this, [=](
const Core::CloudPasswordResult &result) {
sendTransferRequestFrom(*box, channel, result);

View File

@ -422,7 +422,11 @@ void CloudManager::requestLanguageAndSwitch(
_switchingToLanguageRequest = 0;
const auto language = Lang::ParseLanguage(result);
const auto finalize = [=] {
performSwitchAndRestart(language);
if (canApplyWithoutRestart(language.id)) {
performSwitchAndAddToRecent(language);
} else {
performSwitchAndRestart(language);
}
};
if (!warning) {
finalize();
@ -452,7 +456,7 @@ void CloudManager::switchToLanguage(const Language &data) {
if (data.id == qstr("#custom")) {
performSwitchToCustom();
} else if (canApplyWithoutRestart(data.id)) {
performSwitch(data);
performSwitchAndAddToRecent(data);
} else {
QVector<MTPstring> keys;
keys.reserve(3);
@ -545,9 +549,13 @@ void CloudManager::performSwitch(const Language &data) {
requestLangPackDifference(Pack::Base);
}
void CloudManager::performSwitchAndRestart(const Language &data) {
void CloudManager::performSwitchAndAddToRecent(const Language &data) {
Local::pushRecentLanguage(data);
performSwitch(data);
}
void CloudManager::performSwitchAndRestart(const Language &data) {
performSwitchAndAddToRecent(data);
restartAfterSwitch();
}

View File

@ -60,6 +60,7 @@ private:
bool canApplyWithoutRestart(const QString &id) const;
void performSwitchToCustom();
void performSwitch(const Language &data);
void performSwitchAndAddToRecent(const Language &data);
void performSwitchAndRestart(const Language &data);
void restartAfterSwitch();
void offerSwitchLangPack();

View File

@ -301,6 +301,8 @@ void Instance::reset(const Language &data) {
_values[i] = GetOriginalValue(LangKey(i));
}
ranges::fill(_nonDefaultSet, 0);
_idChanges.fire_copy(_id);
}
QString Instance::systemLangCode() const {
@ -329,6 +331,10 @@ QString Instance::id() const {
return id(Pack::Current);
}
rpl::producer<QString> Instance::idChanges() const {
return _idChanges.events();
}
QString Instance::baseId() const {
return id(Pack::Base);
}
@ -538,6 +544,8 @@ void Instance::fillFromSerialized(
applyValue(nonDefaultStrings[i], nonDefaultStrings[i + 1]);
}
updatePluralRules();
_idChanges.fire_copy(_id);
}
void Instance::loadFromContent(const QByteArray &content) {
@ -560,6 +568,8 @@ void Instance::fillFromCustomContent(
_pluralId = PluralCodeForCustom(absolutePath, relativePath);
_name = _nativeName = QString();
loadFromCustomContent(absolutePath, relativePath, content);
_idChanges.fire_copy(_id);
}
void Instance::loadFromCustomContent(
@ -624,6 +634,8 @@ void Instance::fillFromLegacy(int legacyId, const QString &legacyPath) {
_name = _nativeName = QString();
_base = nullptr;
updatePluralRules();
_idChanges.fire_copy(_id);
}
// SetCallback takes two QByteArrays: key, value.

View File

@ -81,6 +81,7 @@ public:
QString langPackName() const;
QString cloudLangCode(Pack pack) const;
QString id() const;
rpl::producer<QString> idChanges() const;
QString baseId() const;
QString name() const;
QString nativeName() const;
@ -141,6 +142,7 @@ private:
Instance *_derived = nullptr;
QString _id, _pluralId;
rpl::event_stream<QString> _idChanges;
QString _name, _nativeName;
int _legacyId = kLegacyLanguageNone;
QString _customFilePathAbsolute;

View File

@ -35,7 +35,11 @@ void SetupLanguageButton(
const auto button = AddButtonWithLabel(
container,
tr::lng_settings_language(),
rpl::single(Lang::Current().nativeName()),
rpl::single(
Lang::Current().id()
) | rpl::then(
Lang::Current().idChanges()
) | rpl::map([] { return Lang::Current().nativeName(); }),
icon ? st::settingsSectionButton : st::settingsButton,
icon ? &st::settingsIconLanguage : nullptr);
const auto guard = Ui::CreateChild<base::binary_guard>(button.get());