From 8bb14f4565686740bbd743867bd3911368ccabae Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Wed, 18 Jan 2017 13:26:33 +0300
Subject: [PATCH] Small layout search field hidden. Crash fix in common groups
 section.

Also clearing cloud drafts in deactivated chats after migrating them.
---
 Telegram/SourceFiles/dialogswidget.cpp        |  6 ++-
 Telegram/SourceFiles/history.cpp              |  2 +
 Telegram/SourceFiles/historywidget.cpp        | 37 ++++++++++++-------
 Telegram/SourceFiles/localstorage.cpp         |  5 +++
 .../profile/profile_common_groups_section.cpp |  8 +++-
 5 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/Telegram/SourceFiles/dialogswidget.cpp b/Telegram/SourceFiles/dialogswidget.cpp
index ab9776adf..b4a02dd6f 100644
--- a/Telegram/SourceFiles/dialogswidget.cpp
+++ b/Telegram/SourceFiles/dialogswidget.cpp
@@ -2692,13 +2692,17 @@ void DialogsWidget::updateControlsGeometry() {
 		_forwardCancel->moveToLeft(0, filterAreaTop);
 		filterAreaTop += st::dialogsForwardHeight;
 	}
+	auto smallLayoutWidth = (st::dialogsPadding.x() + st::dialogsPhotoSize + st::dialogsPadding.x());
+	auto smallLayoutRatio = (width() < st::dialogsWidthMin) ? (st::dialogsWidthMin - width()) / float64(st::dialogsWidthMin - smallLayoutWidth) : 0.;
 	auto filterLeft = st::dialogsFilterPadding.x() + _mainMenuToggle->width() + st::dialogsFilterPadding.x();
 	auto filterRight = (Global::LocalPasscode() ? (st::dialogsFilterPadding.x() + _lockUnlock->width()) : st::dialogsFilterSkip) + st::dialogsFilterPadding.x();
 	auto filterWidth = qMax(width(), st::dialogsWidthMin) - filterLeft - filterRight;
 	auto filterAreaHeight = st::dialogsFilterPadding.y() + _mainMenuToggle->height() + st::dialogsFilterPadding.y();
 	auto filterTop = filterAreaTop + (filterAreaHeight - _filter->height()) / 2;
+	filterLeft = anim::interpolate(filterLeft, smallLayoutWidth, smallLayoutRatio);
 	_filter->setGeometryToLeft(filterLeft, filterTop, filterWidth, _filter->height());
-	_mainMenuToggle->moveToLeft(st::dialogsFilterPadding.x(), filterAreaTop + st::dialogsFilterPadding.y());
+	auto mainMenuLeft = anim::interpolate(st::dialogsFilterPadding.x(), (smallLayoutWidth - _mainMenuToggle->width()) / 2, smallLayoutRatio);
+	_mainMenuToggle->moveToLeft(mainMenuLeft, filterAreaTop + st::dialogsFilterPadding.y());
 	_lockUnlock->moveToLeft(filterLeft + filterWidth + st::dialogsFilterPadding.x(), filterAreaTop + st::dialogsFilterPadding.y());
 	_cancelSearch->moveToLeft(filterLeft + filterWidth - _cancelSearch->width(), _filter->y());
 
diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp
index 5a0bd029b..660c55cb9 100644
--- a/Telegram/SourceFiles/history.cpp
+++ b/Telegram/SourceFiles/history.cpp
@@ -26,6 +26,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
 #include "styles/style_dialogs.h"
 #include "data/data_drafts.h"
 #include "lang.h"
+#include "apiwrap.h"
 #include "mainwidget.h"
 #include "mainwindow.h"
 #include "localstorage.h"
@@ -119,6 +120,7 @@ void History::takeLocalDraft(History *from) {
 			_localDraft->msgId = 0;
 		}
 		from->clearLocalDraft();
+		App::api()->saveDraftToCloudDelayed(from);
 	}
 }
 
diff --git a/Telegram/SourceFiles/historywidget.cpp b/Telegram/SourceFiles/historywidget.cpp
index 18f872e1c..52c9addd8 100644
--- a/Telegram/SourceFiles/historywidget.cpp
+++ b/Telegram/SourceFiles/historywidget.cpp
@@ -236,19 +236,30 @@ void HistoryInner::enumerateItemsInHistory(History *history, int historytop, Met
 			if (TopToBottom) {
 				if (itembottom <= _visibleAreaTop && (cAlphaVersion() || cBetaVersion())) {
 					// Debugging a crash
-					auto fields = QStringList();
-					fields.append(QString::number(historytop));
-					fields.append(QString::number(history->height));
-					fields.append(QString::number(_visibleAreaTop));
-					fields.append(QString::number(_visibleAreaBottom));
-					fields.append(QString::number(blocktop));
-					fields.append(QString::number(block->height));
-					fields.append(QString::number(itemtop));
-					fields.append(QString::number(item->height()));
-					fields.append(QString::number(itembottom));
-					fields.append(QString::number(history->blocks.size()));
-					fields.append(QString::number(block->items.size()));
-					SignalHandlers::setCrashAnnotation("ItemInfo1", fields.join(','));
+					auto debugInfo = QStringList();
+					auto debugValue = [&debugInfo](const QString &name, int value) {
+						debugInfo.append(name + ":" + QString::number(value));
+					};
+					debugValue("historytop", historytop);
+					debugValue("history->height", history->height);
+					debugValue("blockIndex", blockIndex);
+					debugValue("history->blocks.size()", history->blocks.size());
+					debugValue("blocktop", blocktop);
+					debugValue("block->height", block->height);
+					debugValue("itemIndex", itemIndex);
+					debugValue("block->items.size()", block->items.size());
+					debugValue("itemtop", itemtop);
+					debugValue("item->height()", item->height());
+					debugValue("itembottom", itembottom);
+					debugValue("_visibleAreaTop", _visibleAreaTop);
+					debugValue("_visibleAreaBottom", _visibleAreaBottom);
+					for (int i = 0; i != qMin(history->blocks.size(), 10); ++i) {
+						for (int j = 0; j != qMin(history->blocks[i]->items.size(), 10); ++j) {
+							debugValue("y[" + QString::number(i) + "][" + QString::number(j) + "]", history->blocks[i]->items[j]->y);
+							debugValue("h[" + QString::number(i) + "][" + QString::number(j) + "]", history->blocks[i]->items[j]->height());
+						}
+					}
+					SignalHandlers::setCrashAnnotation("DebugInfo", debugInfo.join(','));
 				}
 				t_assert(itembottom > _visibleAreaTop);
 			} else {
diff --git a/Telegram/SourceFiles/localstorage.cpp b/Telegram/SourceFiles/localstorage.cpp
index 718dc5b05..cb8440508 100644
--- a/Telegram/SourceFiles/localstorage.cpp
+++ b/Telegram/SourceFiles/localstorage.cpp
@@ -3626,6 +3626,11 @@ void readSavedGifs() {
 void writeBackground(int32 id, const QImage &img) {
 	if (!_working() || !_backgroundCanWrite) return;
 
+	if (!_localKey.created()) {
+		LOG(("App Error: localkey not created in writeBackground()"));
+		return;
+	}
+
 	QByteArray bmp;
 	if (!img.isNull()) {
 		QBuffer buf(&bmp);
diff --git a/Telegram/SourceFiles/profile/profile_common_groups_section.cpp b/Telegram/SourceFiles/profile/profile_common_groups_section.cpp
index a327bfb00..1a192ca26 100644
--- a/Telegram/SourceFiles/profile/profile_common_groups_section.cpp
+++ b/Telegram/SourceFiles/profile/profile_common_groups_section.cpp
@@ -140,7 +140,9 @@ void InnerWidget::restoreState(const SectionMemento *memento) {
 
 void InnerWidget::showInitial(const QList<PeerData*> &list) {
 	for_const (auto group, list) {
-		_items.push_back(computeItem(group));
+		if (auto item = computeItem(group)) {
+			_items.push_back(item);
+		}
 		_preloadGroupId = group->bareId();
 	}
 	updateSize();
@@ -171,7 +173,9 @@ void InnerWidget::preloadMore() {
 							}
 						}
 						if (!found) {
-							_items.push_back(computeItem(chat));
+							if (auto item = computeItem(chat)) {
+								_items.push_back(item);
+							}
 						}
 						_preloadGroupId = chat->bareId();
 						_allLoaded = false;