Save Celsius/Fahrenheit in Settings.

This commit is contained in:
John Preston 2024-07-26 14:44:06 +02:00
parent 6a8a85e395
commit 3eeb01be61
6 changed files with 61 additions and 27 deletions

View file

@ -220,7 +220,7 @@ QByteArray Settings::serialize() const {
+ Serialize::bytearraySize(ivPosition) + Serialize::bytearraySize(ivPosition)
+ Serialize::stringSize(noWarningExtensions) + Serialize::stringSize(noWarningExtensions)
+ Serialize::stringSize(_customFontFamily) + Serialize::stringSize(_customFontFamily)
+ sizeof(qint32) * 2; + sizeof(qint32) * 3;
auto result = QByteArray(); auto result = QByteArray();
result.reserve(size); result.reserve(size);
@ -372,7 +372,8 @@ QByteArray Settings::serialize() const {
qRound(_dialogsNoChatWidthRatio.current() * 1000000), qRound(_dialogsNoChatWidthRatio.current() * 1000000),
0, 0,
1000000)) 1000000))
<< qint32(_systemUnlockEnabled ? 1 : 0); << qint32(_systemUnlockEnabled ? 1 : 0)
<< qint32(!_weatherInCelsius ? 0 : *_weatherInCelsius ? 1 : 2);
} }
Ensures(result.size() == size); Ensures(result.size() == size);
@ -493,6 +494,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
QByteArray ivPosition; QByteArray ivPosition;
QString customFontFamily = _customFontFamily; QString customFontFamily = _customFontFamily;
qint32 systemUnlockEnabled = _systemUnlockEnabled ? 1 : 0; qint32 systemUnlockEnabled = _systemUnlockEnabled ? 1 : 0;
qint32 weatherInCelsius = !_weatherInCelsius ? 0 : *_weatherInCelsius ? 1 : 2;
stream >> themesAccentColors; stream >> themesAccentColors;
if (!stream.atEnd()) { if (!stream.atEnd()) {
@ -793,6 +795,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
if (!stream.atEnd()) { if (!stream.atEnd()) {
stream >> systemUnlockEnabled; stream >> systemUnlockEnabled;
} }
if (!stream.atEnd()) {
stream >> weatherInCelsius;
}
if (stream.status() != QDataStream::Ok) { if (stream.status() != QDataStream::Ok) {
LOG(("App Error: " LOG(("App Error: "
"Bad data for Core::Settings::constructFromSerialized()")); "Bad data for Core::Settings::constructFromSerialized()"));
@ -1001,6 +1006,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
} }
_customFontFamily = customFontFamily; _customFontFamily = customFontFamily;
_systemUnlockEnabled = (systemUnlockEnabled == 1); _systemUnlockEnabled = (systemUnlockEnabled == 1);
_weatherInCelsius = !weatherInCelsius
? std::optional<bool>()
: (weatherInCelsius == 1);
} }
QString Settings::getSoundPath(const QString &key) const { QString Settings::getSoundPath(const QString &key) const {

View file

@ -891,6 +891,13 @@ public:
_systemUnlockEnabled = enabled; _systemUnlockEnabled = enabled;
} }
[[nodiscard]] std::optional<bool> weatherInCelsius() const {
return _weatherInCelsius;
}
void setWeatherInCelsius(bool value) {
_weatherInCelsius = value;
}
[[nodiscard]] static bool ThirdColumnByDefault(); [[nodiscard]] static bool ThirdColumnByDefault();
[[nodiscard]] static float64 DefaultDialogsWidthRatio(); [[nodiscard]] static float64 DefaultDialogsWidthRatio();
@ -1022,6 +1029,7 @@ private:
WindowPosition _ivPosition; WindowPosition _ivPosition;
QString _customFontFamily; QString _customFontFamily;
bool _systemUnlockEnabled = false; bool _systemUnlockEnabled = false;
std::optional<bool> _weatherInCelsius;
bool _tabbedReplacedWithInfo = false; // per-window bool _tabbedReplacedWithInfo = false; // per-window
rpl::event_stream<bool> _tabbedReplacedWithInfoValue; // per-window rpl::event_stream<bool> _tabbedReplacedWithInfoValue; // per-window

View file

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/ */
#include "media/stories/media_stories_controller.h" #include "media/stories/media_stories_controller.h"
#include "base/platform/base_platform_info.h"
#include "base/power_save_blocker.h" #include "base/power_save_blocker.h"
#include "base/qt_signal_producer.h" #include "base/qt_signal_producer.h"
#include "base/unixtime.h" #include "base/unixtime.h"
@ -128,6 +129,13 @@ struct SameDayRange {
int(base::SafeRound(asin * point.x() + acos * point.y()))); int(base::SafeRound(asin * point.x() + acos * point.y())));
} }
[[nodiscard]] bool ResolveWeatherInCelsius() {
const auto saved = Core::App().settings().weatherInCelsius();
return saved.value_or(!ranges::contains(
std::array{ u"US"_q, u"BS"_q, u"KY"_q, u"LR"_q, u"BZ"_q },
Platform::SystemCountry().toUpper()));
}
} // namespace } // namespace
class Controller::PhotoPlayback final { class Controller::PhotoPlayback final {
@ -284,7 +292,8 @@ Controller::Controller(not_null<Delegate*> delegate)
, _slider(std::make_unique<Slider>(this)) , _slider(std::make_unique<Slider>(this))
, _replyArea(std::make_unique<ReplyArea>(this)) , _replyArea(std::make_unique<ReplyArea>(this))
, _reactions(std::make_unique<Reactions>(this)) , _reactions(std::make_unique<Reactions>(this))
, _recentViews(std::make_unique<RecentViews>(this)) { , _recentViews(std::make_unique<RecentViews>(this))
, _weatherInCelsius(ResolveWeatherInCelsius()){
initLayout(); initLayout();
using namespace rpl::mappers; using namespace rpl::mappers;
@ -1272,16 +1281,16 @@ ClickHandlerPtr Controller::lookupAreaHandler(QPoint point) const {
}); });
} }
for (const auto &weather : _weatherAreas) { for (const auto &weather : _weatherAreas) {
auto widget = _reactions->makeWeatherAreaWidget(weather);
const auto raw = widget.get();
_areas.push_back({ _areas.push_back({
.original = weather.area.geometry, .original = weather.area.geometry,
.radiusOriginal = weather.area.radius, .radiusOriginal = weather.area.radius,
.rotation = weather.area.rotation, .rotation = weather.area.rotation,
.handler = std::make_shared<LambdaClickHandler>([=] { .handler = std::make_shared<LambdaClickHandler>([=] {
raw->toggleMode(); toggleWeatherMode();
}), }),
.view = std::move(widget), .view = _reactions->makeWeatherAreaWidget(
weather,
_weatherInCelsius.value()),
}); });
} }
rebuildActiveAreas(*layout); rebuildActiveAreas(*layout);
@ -1300,6 +1309,13 @@ ClickHandlerPtr Controller::lookupAreaHandler(QPoint point) const {
return nullptr; return nullptr;
} }
void Controller::toggleWeatherMode() const {
const auto now = !_weatherInCelsius.current();
Core::App().settings().setWeatherInCelsius(now);
Core::App().saveSettingsDelayed();
_weatherInCelsius = now;
}
void Controller::maybeMarkAsRead(const Player::TrackState &state) { void Controller::maybeMarkAsRead(const Player::TrackState &state) {
const auto length = state.length; const auto length = state.length;
const auto position = Player::IsStoppedAtEnd(state.state) const auto position = Player::IsStoppedAtEnd(state.state)

View file

@ -229,6 +229,7 @@ private:
void updatePlayingAllowed(); void updatePlayingAllowed();
void setPlayingAllowed(bool allowed); void setPlayingAllowed(bool allowed);
void rebuildActiveAreas(const Layout &layout) const; void rebuildActiveAreas(const Layout &layout) const;
void toggleWeatherMode() const;
void hideSiblings(); void hideSiblings();
void showSiblings(not_null<Main::Session*> session); void showSiblings(not_null<Main::Session*> session);
@ -307,6 +308,7 @@ private:
std::vector<Data::UrlArea> _urlAreas; std::vector<Data::UrlArea> _urlAreas;
std::vector<Data::WeatherArea> _weatherAreas; std::vector<Data::WeatherArea> _weatherAreas;
mutable std::vector<ActiveArea> _areas; mutable std::vector<ActiveArea> _areas;
mutable rpl::variable<bool> _weatherInCelsius;
std::vector<CachedSource> _cachedSourcesList; std::vector<CachedSource> _cachedSourcesList;
int _cachedSourceIndex = -1; int _cachedSourceIndex = -1;

View file

@ -75,7 +75,6 @@ public:
void setAreaGeometry(QRect geometry, float64 radius) override; void setAreaGeometry(QRect geometry, float64 radius) override;
void updateReactionsCount(int count) override; void updateReactionsCount(int count) override;
void playEffect() override; void playEffect() override;
void toggleMode() override;
bool contains(QPoint point) override; bool contains(QPoint point) override;
private: private:
@ -137,12 +136,12 @@ public:
WeatherView( WeatherView(
QWidget *parent, QWidget *parent,
not_null<Main::Session*> session, not_null<Main::Session*> session,
const Data::WeatherArea &data); const Data::WeatherArea &data,
rpl::producer<bool> weatherInCelsius);
void setAreaGeometry(QRect geometry, float64 radius) override; void setAreaGeometry(QRect geometry, float64 radius) override;
void updateReactionsCount(int count) override; void updateReactionsCount(int count) override;
void playEffect() override; void playEffect() override;
void toggleMode() override;
bool contains(QPoint point) override; bool contains(QPoint point) override;
private: private:
@ -349,10 +348,6 @@ void ReactionView::playEffect() {
} }
} }
void ReactionView::toggleMode() {
Unexpected("ReactionView::toggleMode.");
}
bool ReactionView::contains(QPoint point) { bool ReactionView::contains(QPoint point) {
const auto circle = _apiGeometry; const auto circle = _apiGeometry;
const auto radius = std::min(circle.width(), circle.height()) / 2; const auto radius = std::min(circle.width(), circle.height()) / 2;
@ -537,7 +532,8 @@ void ReactionView::cacheBackground() {
WeatherView::WeatherView( WeatherView::WeatherView(
QWidget *parent, QWidget *parent,
not_null<Main::Session*> session, not_null<Main::Session*> session,
const Data::WeatherArea &data) const Data::WeatherArea &data,
rpl::producer<bool> weatherInCelsius)
: RpWidget(parent) : RpWidget(parent)
, _session(session) , _session(session)
, _data(data) , _data(data)
@ -546,6 +542,12 @@ WeatherView::WeatherView(
watchForSticker(); watchForSticker();
setAttribute(Qt::WA_TransparentForMouseEvents); setAttribute(Qt::WA_TransparentForMouseEvents);
show(); show();
std::move(weatherInCelsius) | rpl::start_with_next([=](bool celsius) {
_celsius = celsius;
_background = {};
update();
}, lifetime());
} }
void WeatherView::watchForSticker() { void WeatherView::watchForSticker() {
@ -565,7 +567,7 @@ void WeatherView::watchForSticker() {
) | rpl::start_with_next([=](not_null<DocumentData*> document) { ) | rpl::start_with_next([=](not_null<DocumentData*> document) {
setStickerFrom(document); setStickerFrom(document);
update(); update();
}, _lifetime); }, lifetime());
} }
} }
@ -598,12 +600,6 @@ void WeatherView::playEffect() {
Unexpected("WeatherView::playEffect."); Unexpected("WeatherView::playEffect.");
} }
void WeatherView::toggleMode() {
_celsius = !_celsius;
_background = {};
update();
}
bool WeatherView::contains(QPoint point) { bool WeatherView::contains(QPoint point) {
const auto geometry = _rect.translated(pos()).toRect(); const auto geometry = _rect.translated(pos()).toRect();
const auto angle = -_data.area.rotation; const auto angle = -_data.area.rotation;
@ -683,7 +679,7 @@ void WeatherView::setStickerFrom(not_null<DocumentData*> document) {
} }
_sticker->setRepaintCallback([=] { update(); }); _sticker->setRepaintCallback([=] { update(); });
update(); update();
}, _lifetime); }, lifetime());
} }
void WeatherView::cacheBackground() { void WeatherView::cacheBackground() {
@ -1087,12 +1083,15 @@ auto Reactions::makeSuggestedReactionWidget(
reaction); reaction);
} }
auto Reactions::makeWeatherAreaWidget(const Data::WeatherArea &data) auto Reactions::makeWeatherAreaWidget(
const Data::WeatherArea &data,
rpl::producer<bool> weatherInCelsius)
-> std::unique_ptr<StoryAreaView> { -> std::unique_ptr<StoryAreaView> {
return std::make_unique<WeatherView>( return std::make_unique<WeatherView>(
_controller->wrap(), _controller->wrap(),
&_controller->uiShow()->session(), &_controller->uiShow()->session(),
data); data,
std::move(weatherInCelsius));
} }
void Reactions::setReplyFieldState( void Reactions::setReplyFieldState(

View file

@ -49,7 +49,6 @@ public:
virtual void setAreaGeometry(QRect geometry, float64 radius) = 0; virtual void setAreaGeometry(QRect geometry, float64 radius) = 0;
virtual void updateReactionsCount(int count) = 0; virtual void updateReactionsCount(int count) = 0;
virtual void playEffect() = 0; virtual void playEffect() = 0;
virtual void toggleMode() = 0;
virtual bool contains(QPoint point) = 0; virtual bool contains(QPoint point) = 0;
}; };
@ -83,7 +82,9 @@ public:
[[nodiscard]] auto makeSuggestedReactionWidget( [[nodiscard]] auto makeSuggestedReactionWidget(
const Data::SuggestedReaction &reaction) const Data::SuggestedReaction &reaction)
-> std::unique_ptr<StoryAreaView>; -> std::unique_ptr<StoryAreaView>;
[[nodiscard]] auto makeWeatherAreaWidget(const Data::WeatherArea &data) [[nodiscard]] auto makeWeatherAreaWidget(
const Data::WeatherArea &data,
rpl::producer<bool> weatherInCelsius)
-> std::unique_ptr<StoryAreaView>; -> std::unique_ptr<StoryAreaView>;
void setReplyFieldState( void setReplyFieldState(