@ -24,13 +24,16 @@ export class AppSelectPeers {
private myID = $rootScope . myID ;
private myID = $rootScope . myID ;
private folderID = 0 ;
private offsetIndex = 0 ;
private offsetIndex = 0 ;
private promise : Promise < any > ;
private promise : Promise < any > ;
private query = '' ;
private query = '' ;
private cachedContacts : number [ ] ;
private cachedContacts : number [ ] ;
constructor ( private appendTo : HTMLElement , private onChange ? : ( length : number ) = > void , private peerType : 'contacts' | 'dialogs' = 'dialogs' , onFirstRender ? : ( ) = > void , private renderResultsFunc ? : ( peerIDs : number [ ] ) = > void ) {
private loadedWhat : Partial < { [ k in 'dialogs' | 'archived' | 'contacts' ] : true } > = { } ;
constructor ( private appendTo : HTMLElement , private onChange ? : ( length : number ) = > void , private peerType : 'contacts' | 'dialogs' | 'both' = 'dialogs' , onFirstRender ? : ( ) = > void , private renderResultsFunc ? : ( peerIDs : number [ ] ) = > void ) {
this . container . classList . add ( 'selector' ) ;
this . container . classList . add ( 'selector' ) ;
if ( ! this . renderResultsFunc ) {
if ( ! this . renderResultsFunc ) {
@ -93,9 +96,15 @@ export class AppSelectPeers {
this . input . addEventListener ( 'input' , ( ) = > {
this . input . addEventListener ( 'input' , ( ) = > {
let value = this . input . value ;
let value = this . input . value ;
if ( this . query != value ) {
if ( this . query != value ) {
if ( this . peerType == 'contacts' ) {
if ( this . peerType == 'contacts' || this . peerType == 'both' ) {
delete this . loadedWhat . contacts ;
this . cachedContacts = null ;
this . cachedContacts = null ;
} else {
}
if ( this . peerType == 'dialogs' || this . peerType == 'both' ) {
delete this . loadedWhat . dialogs ;
delete this . loadedWhat . archived ;
this . folderID = 0 ;
this . offsetIndex = 0 ;
this . offsetIndex = 0 ;
}
}
@ -103,6 +112,10 @@ export class AppSelectPeers {
this . list . innerHTML = '' ;
this . list . innerHTML = '' ;
this . query = value ;
this . query = value ;
if ( this . query && 'saved messages' . includes ( this . query . toLowerCase ( ) ) ) {
this . renderResultsFunc ( [ $rootScope . myID ] ) ;
}
//console.log('selectPeers input:', this.query);
//console.log('selectPeers input:', this.query);
this . getMoreResults ( ) ;
this . getMoreResults ( ) ;
}
}
@ -126,24 +139,25 @@ export class AppSelectPeers {
} , 0 ) ;
} , 0 ) ;
}
}
private async getMoreDialogs() {
private async getMoreDialogs ( ) : Promise < any > {
if ( this . promise ) return this . promise ;
if ( this . promise ) return this . promise ;
if ( this . loadedWhat . dialogs && this . loadedWhat . archived ) {
return ;
}
// в десктопе - сначала без группы, потом архивные, потом контакты без сообщений
// в десктопе - сначала без группы, потом архивные, потом контакты без сообщений
const pageCount = appPhotosManager . windowH / 72 * 1.25 | 0 ;
const pageCount = appPhotosManager . windowH / 72 * 1.25 | 0 ;
this . promise = appMessagesManager . getConversations ( this . query , this . offsetIndex , pageCount , 0 ) ;
this . promise = appMessagesManager . getConversations ( this . query , this . offsetIndex , pageCount , this . folderID ) ;
const value = await this . promise ;
const value = await this . promise ;
let dialogs = value . dialogs as Dialog [ ] ;
let dialogs = value . dialogs as Dialog [ ] ;
if ( ! dialogs . length ) {
if ( dialogs . length ) {
return ;
}
const newOffsetIndex = dialogs [ dialogs . length - 1 ] . index || 0 ;
const newOffsetIndex = dialogs [ dialogs . length - 1 ] . index || 0 ;
dialogs = dialogs . filter ( d = > d . peerID != this . myID ) ;
dialogs = dialogs . filter ( d = > d . peerID != this . myID ) ;
if ( ! this . offsetIndex && ! this . query ) {
if ( ! this . offsetIndex && this . folderID == 0 && ! this . query ) {
dialogs . unshift ( {
dialogs . unshift ( {
peerID : this.myID ,
peerID : this.myID ,
pFlags : { }
pFlags : { }
@ -153,6 +167,23 @@ export class AppSelectPeers {
this . offsetIndex = newOffsetIndex ;
this . offsetIndex = newOffsetIndex ;
this . renderResultsFunc ( dialogs . map ( dialog = > dialog . peerID ) ) ;
this . renderResultsFunc ( dialogs . map ( dialog = > dialog . peerID ) ) ;
} else {
if ( ! this . loadedWhat . dialogs ) {
this . loadedWhat . dialogs = true ;
this . offsetIndex = 0 ;
this . folderID = 1 ;
this . promise = null ;
return this . getMoreDialogs ( ) ;
} else {
this . loadedWhat . archived = true ;
if ( ! this . loadedWhat . contacts && this . peerType == 'both' ) {
this . promise = null ;
return this . getMoreContacts ( ) ;
}
}
}
this . promise = null ;
this . promise = null ;
}
}
@ -160,6 +191,10 @@ export class AppSelectPeers {
private async getMoreContacts() {
private async getMoreContacts() {
if ( this . promise ) return this . promise ;
if ( this . promise ) return this . promise ;
if ( this . loadedWhat . contacts ) {
return ;
}
if ( ! this . cachedContacts ) {
if ( ! this . cachedContacts ) {
this . promise = appUsersManager . getContacts ( this . query ) ;
this . promise = appUsersManager . getContacts ( this . query ) ;
this . cachedContacts = ( await this . promise ) . slice ( ) ;
this . cachedContacts = ( await this . promise ) . slice ( ) ;
@ -171,17 +206,28 @@ export class AppSelectPeers {
const pageCount = appPhotosManager . windowH / 72 * 1.25 | 0 ;
const pageCount = appPhotosManager . windowH / 72 * 1.25 | 0 ;
const arr = this . cachedContacts . splice ( 0 , pageCount ) ;
const arr = this . cachedContacts . splice ( 0 , pageCount ) ;
this . renderResultsFunc ( arr ) ;
this . renderResultsFunc ( arr ) ;
} else {
this . loadedWhat . contacts = true ;
}
}
}
}
private getMoreResults() {
private getMoreResults() {
if ( this . peerType == 'dialogs' ) {
const promises : Promise < any > [ ] = [ ] ;
return this . getMoreDialogs ( ) ;
if ( this . peerType == 'dialogs' || this . peerType == 'both' ) {
} else {
promises . push ( this . getMoreDialogs ( ) ) ;
return this . getMoreContacts ( ) ;
if ( ! this . loadedWhat . archived ) {
return Promise . all ( promises ) ;
}
}
}
}
if ( this . peerType == 'contacts' || this . peerType == 'both' ) {
promises . push ( this . getMoreContacts ( ) ) ;
}
return Promise . all ( promises ) ;
}
private renderResults ( peerIDs : number [ ] ) {
private renderResults ( peerIDs : number [ ] ) {
//console.log('will renderResults:', peerIDs);
//console.log('will renderResults:', peerIDs);
peerIDs . forEach ( peerID = > {
peerIDs . forEach ( peerID = > {