import { ObservableArray } from "tns-core-modules/data/observable-array" ; import { ListViewEventData , RadListView , LoadOnDemandListViewEventData , ListViewLoadOnDemandMode } from "nativescript-ui-listview" ; import { Observable } from "tns-core-modules/data/observable" ; import { android as androidApplication } from "tns-core-modules/application" ; const serverPeopleCollection = require ( "../people.json" ) ; export class ViewModel extends Observable { private _dataItems : ObservableArray < DataItem > ; private _sourceDataItems : ObservableArray < DataItem > ; private _itemsLoading : boolean ; private _allItems : Array < { name : string ; title : string ; text : string ; image : string ; } > ; constructor ( ) { super ( ) ; this . _allItems = serverPeopleCollection . people . slice ( ) ; this . _sourceDataItems = new ObservableArray < DataItem > ( ) ; this . initSourceDataItems ( 1 ) ; this . _dataItems = new ObservableArray < DataItem > ( ) ; this . addMoreItemsFromSource ( 1 ) ; } get dataItems ( ) : ObservableArray < DataItem > { return this . get ( "_dataItems" ) ; } set dataItems ( value ) { this . set ( "_dataItems" , value ) ; } public onLoadMoreDataRequested ( args ) { const that = new WeakRef ( this ) ; const listView = args . object ; if ( ! this . _itemsLoading ) { if ( this . _sourceDataItems . length !== 0 ) { console . log ( "Load More Data Requested WILL LOAD" ) ; this . _itemsLoading = true ; setTimeout ( function ( ) { let thatInstance = that . get ( ) ; thatInstance . addMoreItemsFromSource ( 1 ) ; thatInstance . _itemsLoading = false ; listView . notifyLoadOnDemandFinished ( ) ; } , 1000 ) ; } else { console . log ( "Load More Data Requested CANNOT LOAD" ) ; args . returnValue = false ; listView . notifyLoadOnDemandFinished ( true ) ; } } } public onPullToRefreshInitiated ( args ) { console . log ( "Pull To Refresh Initiated" ) ; const listView = args . object ; const that = new WeakRef ( this ) ; if ( this . _allItems . length !== 0 ) { this . addItemsToSourceDataItems ( 1 ) ; setTimeout ( function ( ) { let thisInstance = that . get ( ) ; let numberOfItemsToAdd = 1 ; for ( let i = 0 ; i < numberOfItemsToAdd ; i ++ ) { if ( thisInstance . _allItems . length !== 0 ) { thisInstance . _dataItems . splice ( 0 , 0 , thisInstance . getNextItemFromServer ( ) ) ; } } listView . notifyPullToRefreshFinished ( thisInstance . isLoadOnDemandModeNeeded ( ) ) ; } , 1000 ) ; } else { args . returnValue = false ; listView . notifyPullToRefreshFinished ( this . isLoadOnDemandModeNeeded ( ) ) ; } } private isLoadOnDemandModeNeeded ( ) { if ( this . _allItems . length !== 0 ) { return true ; } return false ; } private initSourceDataItems ( size ) { this . addItemsToSourceDataItems ( size ) ; } private addItemsToSourceDataItems ( size ) { for ( let i = 0 ; i < size ; i ++ ) { this . _sourceDataItems . push ( this . getNextItemFromServer ( ) ) ; } } private addMoreItemsFromSource ( chunkSize ) { let newItems = this . _sourceDataItems . splice ( 0 , chunkSize ) ; this . dataItems . push ( newItems ) ; } private getNextItemFromServer ( ) { let item = this . _allItems . splice ( 0 , 1 ) [ 0 ] ; let imagePath ; if ( androidApplication ) { imagePath = item . image . toLowerCase ( ) ; } else { imagePath = item . image ; } return new DataItem ( this . _sourceDataItems . length , item . name , "This is item description" , item . title , item . text , imagePath ) ; } } export class DataItem { constructor ( public id ? , public name ? , public description ? , public title ? , public text ? , public image ? , public selected ? , public type ? , public category ? , public size ? , public color ? ) { } }