Browse Source

Positioning to unread messages in chat

master
Igor Zhukov 11 years ago
parent
commit
f1b1e0ea96
  1. 7
      app/css/app.css
  2. 15
      app/js/controllers.js
  3. 12
      app/js/directives.js
  4. 91
      app/js/services.js
  5. 6
      app/partials/message.html

7
app/css/app.css

@ -896,6 +896,13 @@ a.im_dialog:hover .im_dialog_date { @@ -896,6 +896,13 @@ a.im_dialog:hover .im_dialog_date {
opacity: 0;
}*/
.im_message_unread_split {
background: #E9EBED;
text-align: center;
padding: 4px 10px;
margin: 10px 0;
}
.im_message_author {

15
app/js/controllers.js

@ -447,12 +447,12 @@ angular.module('myApp.controllers', []) @@ -447,12 +447,12 @@ angular.module('myApp.controllers', [])
inputMediaFilter = $scope.mediaType && {_: inputMediaFilters[$scope.mediaType]},
getMessagesPromise = inputMediaFilter
? AppMessagesManager.getSearch($scope.curDialog.inputPeer, '', inputMediaFilter, maxID, startLimit)
: AppMessagesManager.getHistory($scope.curDialog.inputPeer, maxID, startLimit);
: AppMessagesManager.getHistory($scope.curDialog.inputPeer, maxID);
getMessagesPromise.then(function (historyResult) {
if (curJump != jump) return;
offset += startLimit;
offset += historyResult.history.length;
hasMore = offset < historyResult.count;
maxID = historyResult.history[historyResult.history.length - 1];
@ -462,6 +462,15 @@ angular.module('myApp.controllers', []) @@ -462,6 +462,15 @@ angular.module('myApp.controllers', [])
});
$scope.history.reverse();
if (historyResult.unreadLimit) {
$scope.historyUnread = {
beforeID: historyResult.history[historyResult.unreadLimit - 1],
count: historyResult.unreadLimit
};
} else {
$scope.historyUnread = {};
}
safeReplaceObject($scope.state, {loaded: true});
$scope.$broadcast('ui_history_change');
@ -589,6 +598,8 @@ angular.module('myApp.controllers', []) @@ -589,6 +598,8 @@ angular.module('myApp.controllers', [])
$scope.history.push(AppMessagesManager.wrapForHistory(addedMessage.messageID));
$scope.typing = {};
$scope.$broadcast('ui_history_append', {my: addedMessage.my});
$scope.historyUnread = {};
offset++;
// console.log('append check', $rootScope.idle.isIDLE, addedMessage.peerID, $scope.curDialog.peerID);

12
app/js/directives.js

@ -215,8 +215,16 @@ angular.module('myApp.directives', ['myApp.filters']) @@ -215,8 +215,16 @@ angular.module('myApp.directives', ['myApp.filters'])
onContentLoaded(function () {
$(scrollableWrap).removeClass('im_history_to_bottom');
$(scrollable).css({bottom: ''});
updateSizes();
scrollableWrap.scrollTop = scrollableWrap.scrollHeight;
updateSizes(true);
var unreadSplit = $('.im_message_unread_split', scrollableWrap);
if (unreadSplit[0]) {
scrollableWrap.scrollTop = unreadSplit[0].offsetTop;
atBottom = false;
} else {
scrollableWrap.scrollTop = scrollableWrap.scrollHeight;
}
updateScroller();
moreNotified = false;
});

91
app/js/services.js

@ -748,6 +748,46 @@ angular.module('myApp.services', []) @@ -748,6 +748,46 @@ angular.module('myApp.services', [])
return deferred.promise;
}
function fillHistoryStorage (inputPeer, maxID, fullLimit, historyStorage) {
return MtpApiManager.invokeApi('messages.getHistory', {
peer: inputPeer,
offset: 0,
limit: fullLimit,
max_id: maxID || 0
}).then(function (historyResult) {
AppUsersManager.saveApiUsers(historyResult.users);
AppChatsManager.saveApiChats(historyResult.chats);
saveMessages(historyResult.messages);
historyStorage.count = historyResult._ == 'messages.messagesSlice'
? historyResult.count
: historyResult.messages.length;
var offset = 0;
if (maxID > 0) {
for (offset = 0; offset < historyStorage.history.length; offset++) {
if (maxID > historyStorage.history[offset]) {
break;
}
}
}
historyStorage.history.splice(offset, historyStorage.history.length - offset);
angular.forEach(historyResult.messages, function (message) {
historyStorage.history.push(message.id);
});
fullLimit -= historyResult.messages.length;
if (fullLimit > 0 && historyStorage.history.length < historyStorage.count) {
maxID = historyStorage.history[historyStorage.history.length - 1];
return fillHistoryStorage(inputPeer, maxID, fullLimit, historyStorage);
}
return true;
});
};
function getHistory (inputPeer, maxID, limit) {
var peerID = AppPeersManager.getPeerID(inputPeer),
@ -762,6 +802,17 @@ angular.module('myApp.services', []) @@ -762,6 +802,17 @@ angular.module('myApp.services', [])
resultPending = historyStorage.pending.slice();
}
var unreadLimit = false;
if (!limit && !maxID) {
var foundDialog = getDialogByPeerID(peerID);
if (foundDialog && foundDialog[0] && foundDialog[0].unread_count > 0) {
unreadLimit = foundDialog[0].unread_count;
limit = Math.max(20, unreadLimit + 2);
}
}
if (!limit) {
limit = 20;
}
if (maxID > 0) {
for (offset = 0; offset < historyStorage.history.length; offset++) {
@ -770,7 +821,6 @@ angular.module('myApp.services', []) @@ -770,7 +821,6 @@ angular.module('myApp.services', [])
}
}
}
// console.log('history storage', angular.copy(historyStorage.history), maxID, offset);
if (historyStorage.count !== null && (
historyStorage.history.length >= offset + limit ||
@ -778,26 +828,12 @@ angular.module('myApp.services', []) @@ -778,26 +828,12 @@ angular.module('myApp.services', [])
)) {
return $q.when({
count: historyStorage.count,
history: resultPending.concat(historyStorage.history.slice(offset, offset + limit))
history: resultPending.concat(historyStorage.history.slice(offset, offset + limit)),
unreadLimit: unreadLimit
});
}
var deferred = $q.defer();
MtpApiManager.invokeApi('messages.getHistory', {
peer: inputPeer,
offset: offset,
limit: limit,
max_id: maxID || 0
}).then(function (historyResult) {
AppUsersManager.saveApiUsers(historyResult.users);
AppChatsManager.saveApiChats(historyResult.chats);
saveMessages(historyResult.messages);
historyStorage.count = historyResult._ == 'messages.messagesSlice'
? historyResult.count
: historyResult.messages.length;
return fillHistoryStorage(inputPeer, maxID, limit, historyStorage).then(function () {
offset = 0;
if (maxID > 0) {
for (offset = 0; offset < historyStorage.history.length; offset++) {
@ -807,23 +843,12 @@ angular.module('myApp.services', []) @@ -807,23 +843,12 @@ angular.module('myApp.services', [])
}
}
// console.log('history storage after', angular.copy(historyStorage.history), historyResult.messages, maxID, offset);
historyStorage.history.splice(offset, historyStorage.history.length - offset);
angular.forEach(historyResult.messages, function (message) {
historyStorage.history.push(message.id);
});
// console.log('history storage final', angular.copy(historyStorage.history), historyResult.messages, maxID, offset);
deferred.resolve({
return {
count: historyStorage.count,
history: resultPending.concat(historyStorage.history.slice(offset, offset + limit))
});
}, function (error) {
deferred.reject(error);
history: resultPending.concat(historyStorage.history.slice(offset, offset + limit)),
unreadLimit: unreadLimit
};
});
return deferred.promise;
}
function getSearch (inputPeer, query, inputFilter, maxID, limit) {

6
app/partials/message.html

@ -1,3 +1,9 @@ @@ -1,3 +1,9 @@
<div class="im_message_unread_split" ng-if="historyUnread &amp;&amp; historyUnread.beforeID == historyMessage.id">
<ng-pluralize count="historyUnread.count"
when="{'one': '1 unread message', 'other': '{} unread messages'}">
</ng-pluralize>
</div>
<div class="im_message_outer_wrap" ng-class="{im_message_selected: selectedMsgs[historyMessage.id]}" ng-click="toggleMessage(historyMessage.id, $event.target)">

Loading…
Cancel
Save