From 3132e4dc175a6e1afd6784e10df16d9bf0e8d1f4 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Fri, 25 Mar 2016 19:39:58 +0300
Subject: [PATCH] Fixed collapse button style (looks like service message now).
 Added some asserts because of unknown crash in switch in channel.

---
 Telegram/Resources/style.txt     |  2 +-
 Telegram/SourceFiles/history.cpp | 24 +++++++++++++++---------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/Telegram/Resources/style.txt b/Telegram/Resources/style.txt
index d8e2e8cbd..5ada019a8 100644
--- a/Telegram/Resources/style.txt
+++ b/Telegram/Resources/style.txt
@@ -1148,7 +1148,7 @@ collapseButton: flatButton(btnDefFlat) {
 	textTop: 3px;
 	overTextTop: 3px;
 	downTextTop: 3px;
-	height: 24px;
+	height: 25px;
 }
 collapseHideDuration: 200;
 collapseShowDuration: 200;
diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp
index 26da94f50..2b1b3173b 100644
--- a/Telegram/SourceFiles/history.cpp
+++ b/Telegram/SourceFiles/history.cpp
@@ -830,6 +830,7 @@ void ChannelHistory::switchMode() {
 			int willAddToBlock = qMin(int(MessagesPerPage), count - i);
 			block->items.reserve(willAddToBlock);
 			for (int till = i + willAddToBlock; i < till; ++i) {
+				t_assert(_otherList.at(i)->detached());
 				addItemToBlock(_otherList.at(i), block);
 			}
 
@@ -2565,6 +2566,8 @@ void History::changeMsgId(MsgId oldId, MsgId newId) {
 void History::removeBlock(HistoryBlock *block) {
 	setPendingResize();
 
+	t_assert(block->items.isEmpty());
+
 	int index = block->indexInHistory();
 	blocks.removeAt(index);
 	for (int i = index, l = blocks.size(); i < l; ++i) {
@@ -2614,7 +2617,7 @@ void HistoryBlock::clear(bool leaveItems) {
 void HistoryBlock::removeItem(HistoryItem *item) {
 	t_assert(item->block() == this);
 
-	int32 itemIndex = item->indexInBlock();
+	int itemIndex = item->indexInBlock();
 	if (history->showFrom == item) {
 		history->getNextShowFrom(this, itemIndex);
 	}
@@ -2625,13 +2628,14 @@ void HistoryBlock::removeItem(HistoryItem *item) {
 		history->getNextScrollTopItem(this, itemIndex);
 	}
 
-	int myIndex = indexInHistory();
-	if (myIndex >= 0) { // fix message groups
+	int blockIndex = indexInHistory();
+	if (blockIndex >= 0) { // fix message groups
 		if (item->isImportant()) { // unite message groups around this important message
-			HistoryGroup *nextGroup = 0, *prevGroup = 0;
-			HistoryCollapse *nextCollapse = 0;
-			HistoryItem *prevItem = 0;
-			for (int32 nextBlock = myIndex, nextIndex = qMin(items.size(), itemIndex + 1); nextBlock < history->blocks.size(); ++nextBlock) {
+			HistoryGroup *nextGroup = nullptr;
+			HistoryGroup *prevGroup = nullptr;
+			HistoryCollapse *nextCollapse = nullptr;
+			HistoryItem *prevItem = nullptr;
+			for (int nextBlock = blockIndex, nextIndex = qMin(items.size(), itemIndex + 1); nextBlock < history->blocks.size(); ++nextBlock) {
 				HistoryBlock *block = history->blocks.at(nextBlock);
 				for (; nextIndex < block->items.size(); ++nextIndex) {
 					HistoryItem *item = block->items.at(nextIndex);
@@ -2651,7 +2655,7 @@ void HistoryBlock::removeItem(HistoryItem *item) {
 					break;
 				}
 			}
-			for (int32 prevBlock = myIndex + 1, prevIndex = qMax(1, itemIndex); prevBlock > 0;) {
+			for (int prevBlock = blockIndex + 1, prevIndex = qMax(1, itemIndex); prevBlock > 0;) {
 				--prevBlock;
 				HistoryBlock *block = history->blocks.at(prevBlock);
 				if (!prevIndex) prevIndex = block->items.size();
@@ -2680,7 +2684,9 @@ void HistoryBlock::removeItem(HistoryItem *item) {
 			}
 		}
 	}
-	// myIndex can be invalid now, because of destroying previous blocks
+	// blockIndex can be invalid now, because of destroying previous blocks
+	blockIndex = indexInHistory();
+	itemIndex = item->indexInBlock();
 
 	items.remove(itemIndex);
 	for (int i = itemIndex, l = items.size(); i < l; ++i) {