From 3710d61a09699eebd65fc18c610abb70361745df Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 16 Jan 2024 16:53:46 +0400
Subject: [PATCH] Show "Hide read time" only on non-trivial privacy.

---
 Telegram/Resources/langs/lang.strings         |  2 +-
 .../SourceFiles/boxes/edit_privacy_box.cpp    |  8 +++--
 Telegram/SourceFiles/boxes/edit_privacy_box.h |  3 +-
 .../settings/settings_privacy_controllers.cpp | 34 ++++++++++++++++---
 .../settings/settings_privacy_controllers.h   | 12 +++++--
 5 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings
index 13a234755..3e4f9da5e 100644
--- a/Telegram/Resources/langs/lang.strings
+++ b/Telegram/Resources/langs/lang.strings
@@ -1108,7 +1108,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 "lng_edit_privacy_lastseen_always_title" = "Always share with";
 "lng_edit_privacy_lastseen_never_title" = "Never share with";
 "lng_edit_lastseen_hide_read_time" = "Hide read time";
-"lng_edit_lastseen_hide_read_time_about" = "Do not show the time when you read a message to people you hid your last seen from. If you turn this on, their read time will also be hidden from you. This does not affect group chats.";
+"lng_edit_lastseen_hide_read_time_about" = "Hide the time when you read messages from people who can't see your last seen. If you turn this on, their read time will also be hidden from you. This setting does not affect group chats.";
 "lng_edit_lastseen_subscribe" = "Subscribe to Telegram Premium";
 "lng_edit_lastseen_subscribe_about" = "If you subscribe to Premium, you will see other users' last seen and read time even if you hid yours from them (unless they specifically restricted it).";
 
diff --git a/Telegram/SourceFiles/boxes/edit_privacy_box.cpp b/Telegram/SourceFiles/boxes/edit_privacy_box.cpp
index 0a8bc866d..e1cbe8e10 100644
--- a/Telegram/SourceFiles/boxes/edit_privacy_box.cpp
+++ b/Telegram/SourceFiles/boxes/edit_privacy_box.cpp
@@ -379,7 +379,7 @@ void EditPrivacyBox::setupContent() {
 	auto middle = _controller->setupMiddleWidget(
 		_window,
 		content,
-		std::move(optionValue));
+		rpl::duplicate(optionValue));
 	if (middle) {
 		content->add(std::move(middle));
 	}
@@ -396,7 +396,11 @@ void EditPrivacyBox::setupContent() {
 		_controller->exceptionsDescription() | Ui::Text::ToWithEntities(),
 		st::defaultVerticalListSkip);
 
-	if (auto below = _controller->setupBelowWidget(_window, content)) {
+	auto below = _controller->setupBelowWidget(
+		_window,
+		content,
+		rpl::duplicate(optionValue));
+	if (below) {
 		content->add(std::move(below));
 	}
 
diff --git a/Telegram/SourceFiles/boxes/edit_privacy_box.h b/Telegram/SourceFiles/boxes/edit_privacy_box.h
index 66cd75948..c715ef473 100644
--- a/Telegram/SourceFiles/boxes/edit_privacy_box.h
+++ b/Telegram/SourceFiles/boxes/edit_privacy_box.h
@@ -75,7 +75,8 @@ public:
 	}
 	[[nodiscard]] virtual object_ptr<Ui::RpWidget> setupBelowWidget(
 			not_null<Window::SessionController*> controller,
-			not_null<QWidget*> parent) {
+			not_null<QWidget*> parent,
+			rpl::producer<Option> option) {
 		return { nullptr };
 	}
 
diff --git a/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp b/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp
index 34e98bd42..a8466de5e 100644
--- a/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp
+++ b/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp
@@ -669,9 +669,17 @@ auto LastSeenPrivacyController::exceptionsDescription() const
 
 object_ptr<Ui::RpWidget> LastSeenPrivacyController::setupBelowWidget(
 		not_null<Window::SessionController*> controller,
-		not_null<QWidget*> parent) {
-	auto result = object_ptr<Ui::VerticalLayout>(parent);
-	const auto content = result.data();
+		not_null<QWidget*> parent,
+		rpl::producer<Option> option) {
+	using namespace rpl::mappers;
+
+	auto result = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
+		parent,
+		object_ptr<Ui::VerticalLayout>(parent));
+
+	_option = std::move(option);
+
+	const auto content = result->entity();
 
 	Ui::AddSkip(content);
 
@@ -704,9 +712,22 @@ object_ptr<Ui::RpWidget> LastSeenPrivacyController::setupBelowWidget(
 			tr::lng_edit_lastseen_subscribe_about());
 	}
 
+	result->toggleOn(rpl::combine(
+		_option.value(),
+		_exceptionsNever.value(),
+		(_1 != Option::Everyone) || (_2 > 0)));
+
 	return result;
 }
 
+void LastSeenPrivacyController::handleExceptionsChange(
+		Exception exception,
+		rpl::producer<int> value) {
+	if (exception == Exception::Never) {
+		_exceptionsNever = std::move(value);
+	}
+}
+
 void LastSeenPrivacyController::confirmSave(
 		bool someAreDisallowed,
 		Fn<void()> saveCallback) {
@@ -732,6 +753,10 @@ void LastSeenPrivacyController::confirmSave(
 }
 
 void LastSeenPrivacyController::saveAdditional() {
+	if (_option.current() == Option::Everyone
+		&& !_exceptionsNever.current()) {
+		return;
+	}
 	const auto privacy = &_session->api().globalPrivacy();
 	if (privacy->hideReadTimeCurrent() != _hideReadTime) {
 		privacy->updateHideReadTime(_hideReadTime);
@@ -811,7 +836,8 @@ auto CallsPrivacyController::exceptionsDescription() const
 
 object_ptr<Ui::RpWidget> CallsPrivacyController::setupBelowWidget(
 		not_null<Window::SessionController*> controller,
-		not_null<QWidget*> parent) {
+		not_null<QWidget*> parent,
+		rpl::producer<Option> option) {
 	auto result = object_ptr<Ui::VerticalLayout>(parent);
 	const auto content = result.data();
 
diff --git a/Telegram/SourceFiles/settings/settings_privacy_controllers.h b/Telegram/SourceFiles/settings/settings_privacy_controllers.h
index 7b097ec44..2c214bfec 100644
--- a/Telegram/SourceFiles/settings/settings_privacy_controllers.h
+++ b/Telegram/SourceFiles/settings/settings_privacy_controllers.h
@@ -109,9 +109,14 @@ public:
 		Exception exception) const override;
 	rpl::producer<QString> exceptionsDescription() const override;
 
+	void handleExceptionsChange(
+		Exception exception,
+		rpl::producer<int> value) override;
+
 	object_ptr<Ui::RpWidget> setupBelowWidget(
 		not_null<Window::SessionController*> controller,
-		not_null<QWidget*> parent) override;
+		not_null<QWidget*> parent,
+		rpl::producer<Option> option) override;
 
 	void confirmSave(
 		bool someAreDisallowed,
@@ -121,6 +126,8 @@ public:
 
 private:
 	const not_null<::Main::Session*> _session;
+	rpl::variable<Option> _option;
+	rpl::variable<int> _exceptionsNever;
 	bool _hideReadTime = false;
 
 };
@@ -159,7 +166,8 @@ public:
 
 	object_ptr<Ui::RpWidget> setupBelowWidget(
 		not_null<Window::SessionController*> controller,
-		not_null<QWidget*> parent) override;
+		not_null<QWidget*> parent,
+		rpl::producer<Option> option) override;
 
 };