From e0910bfb3ea3217c8963b04d4af8fa7107288654 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Wed, 8 Oct 2014 11:42:36 +0400
Subject: [PATCH] fixed dialogs preloading on scroll

---
 Telegram/SourceFiles/config.h          | 2 ++
 Telegram/SourceFiles/dialogswidget.cpp | 4 ++--
 Telegram/SourceFiles/historywidget.cpp | 8 ++++----
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/Telegram/SourceFiles/config.h b/Telegram/SourceFiles/config.h
index 5ce1c37c1..43261f129 100644
--- a/Telegram/SourceFiles/config.h
+++ b/Telegram/SourceFiles/config.h
@@ -82,6 +82,8 @@ enum {
 
 	MediaViewImageSizeLimit = 10 * 1024 * 1024, // show up to 10mb jpg/png docs in mediaview
 	MaxZoomLevel = 7, // x8
+
+	PreloadHeightsCount = 3, // when 3 screens to scroll left make a preload request
 };
 
 #ifdef Q_OS_WIN
diff --git a/Telegram/SourceFiles/dialogswidget.cpp b/Telegram/SourceFiles/dialogswidget.cpp
index 9bde7816a..686179596 100644
--- a/Telegram/SourceFiles/dialogswidget.cpp
+++ b/Telegram/SourceFiles/dialogswidget.cpp
@@ -1283,10 +1283,10 @@ void DialogsWidget::onListScroll() {
 	list.loadPeerPhotos(scroll.scrollTop());
 	if (list.state() == DialogsListWidget::SearchedState) {
 		DialogsListWidget::SearchResults &res(list.searchList());
-		if (scroll.scrollTop() > res.size() * st::dlgHeight - 2 * scroll.height()) {
+		if (scroll.scrollTop() > res.size() * st::dlgHeight - PreloadHeightsCount * scroll.height()) {
 			onSearchMore(list.lastSearchId());
 		}
-	} else if (scroll.scrollTop() > list.dialogsList().list.count * st::dlgHeight - scroll.height()) {
+	} else if (scroll.scrollTop() > list.dialogsList().list.count * st::dlgHeight - PreloadHeightsCount * scroll.height()) {
 		loadDialogs();
 	}
 }
diff --git a/Telegram/SourceFiles/historywidget.cpp b/Telegram/SourceFiles/historywidget.cpp
index 5fef45fc0..260334756 100644
--- a/Telegram/SourceFiles/historywidget.cpp
+++ b/Telegram/SourceFiles/historywidget.cpp
@@ -2094,7 +2094,7 @@ void HistoryWidget::loadMessages() {
 			return;
 		}
 	}
-	if (!histPreloading && (!hist->readyForWork() || _scroll.scrollTop() < 3 * _scroll.height())) {
+	if (!histPreloading && (!hist->readyForWork() || _scroll.scrollTop() < PreloadHeightsCount * _scroll.height())) {
 		MsgId min = hist->minMsgId();
 		int32 offset = 0, loadCount = min ? MessagesPerPage : MessagesFirstLoad;
 		if (!min && hist->activeMsgId) {
@@ -2125,7 +2125,7 @@ void HistoryWidget::loadMessagesDown() {
 			return;
 		}
 	}
-	if (!histPreloadingDown && hist->readyForWork() && (_scroll.scrollTop() + 3 * _scroll.height() > _scroll.scrollTopMax())) {
+	if (!histPreloadingDown && hist->readyForWork() && (_scroll.scrollTop() + PreloadHeightsCount * _scroll.height() > _scroll.scrollTopMax())) {
 		MsgId max = hist->maxMsgId();
 		if (max) {
 			int32 loadCount = MessagesPerPage, offset = -loadCount;
@@ -2156,11 +2156,11 @@ void HistoryWidget::onListScroll() {
 		return;
 	}
 
-	if (hist->readyForWork() && (_scroll.scrollTop() + 3 * _scroll.height() > _scroll.scrollTopMax())) {
+	if (hist->readyForWork() && (_scroll.scrollTop() + PreloadHeightsCount * _scroll.height() > _scroll.scrollTopMax())) {
 		loadMessagesDown();
 	}
 
-	if (!hist->readyForWork() || _scroll.scrollTop() < 3 * _scroll.height()) {
+	if (!hist->readyForWork() || _scroll.scrollTop() < PreloadHeightsCount * _scroll.height()) {
 		loadMessages();
 	} else {
 		checkUnreadLoaded(true);