From abfd3ad1b9ac4c96743017a09801afb0489bf422 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 20 Apr 2020 13:51:17 +0400
Subject: [PATCH] Use built-in zero-value dice animations.

---
 .../chat_helpers/stickers_dice_pack.cpp       | 30 +++++++++++--------
 .../chat_helpers/stickers_dice_pack.h         |  3 +-
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp b/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp
index d69b76742..d98fd9c85 100644
--- a/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp
+++ b/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp
@@ -35,11 +35,8 @@ DocumentData *DicePack::lookup(int value) {
 	if (!_requestId) {
 		load();
 	}
+	tryGenerateLocalZero();
 	const auto i = _map.find(value);
-	//if (!value) {
-	//	ensureZeroGenerated();
-	//	return _zero;
-	//}
 	return (i != end(_map)) ? i->second.get() : nullptr;
 }
 
@@ -88,16 +85,21 @@ void DicePack::applySet(const MTPDmessages_stickerSet &data) {
 	}
 }
 
-void DicePack::ensureZeroGenerated() {
-	if (_zero) {
+void DicePack::tryGenerateLocalZero() {
+	if (!_map.empty()) {
 		return;
 	}
 
 	static const auto kDiceString = QString::fromUtf8("\xF0\x9F\x8E\xB2");
 	static const auto kDartString = QString::fromUtf8("\xF0\x9F\x8E\xAF");
-	const auto path = QString((_emoji == kDiceString)
-		? ":/gui/art/dice_idle.tgs"
-		: ":/gui/art/dart_idle.tgs");
+	const auto path = (_emoji == kDiceString)
+		? qsl(":/gui/art/dice_idle.tgs")
+		: (_emoji == kDartString)
+		? qsl(":/gui/art/dart_idle.tgs")
+		: QString();
+	if (path.isEmpty()) {
+		return;
+	}
 	auto task = FileLoadTask(
 		path,
 		QByteArray(),
@@ -108,13 +110,15 @@ void DicePack::ensureZeroGenerated() {
 	task.process();
 	const auto result = task.peekResult();
 	Assert(result != nullptr);
-	_zero = _session->data().processDocument(
+	const auto document = _session->data().processDocument(
 		result->document,
 		std::move(result->thumb));
-	_zero->setLocation(FileLocation(path));
+	document->setLocation(FileLocation(path));
 
-	Ensures(_zero->sticker());
-	Ensures(_zero->sticker()->animated);
+	_map.emplace(0, document);
+
+	Ensures(document->sticker());
+	Ensures(document->sticker()->animated);
 }
 
 DicePacks::DicePacks(not_null<Main::Session*> session) : _session(session) {
diff --git a/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.h b/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.h
index 92035bfdc..744a6d65e 100644
--- a/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.h
+++ b/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.h
@@ -26,12 +26,11 @@ public:
 private:
 	void load();
 	void applySet(const MTPDmessages_stickerSet &data);
-	void ensureZeroGenerated();
+	void tryGenerateLocalZero();
 
 	const not_null<Main::Session*> _session;
 	QString _emoji;
 	base::flat_map<int, not_null<DocumentData*>> _map;
-	DocumentData *_zero = nullptr;
 	mtpRequestId _requestId = 0;
 
 };