FLEX-34854 Refactored ComplexFieldChangeWatcher to allow for monitoring of additions to the sorted list.
NOTES -Now all the functions in FLEX_34854_Tests pass. -Now we're using ChangeWatcher.watch instead of BindingUtils.bindSetter, to avoid unnecessary steps and memory allocation. Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/017ab1f0 Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/017ab1f0 Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/017ab1f0 Branch: refs/heads/develop Commit: 017ab1f0e48b6ecc2dd1bf0857365b55d614ed74 Parents: 849d90a Author: Mihai Chira <[email protected]> Authored: Mon Jun 8 17:59:01 2015 +0200 Committer: Mihai Chira <[email protected]> Committed: Mon Jun 8 17:59:01 2015 +0200 ---------------------------------------------------------------------- .../mx/collections/ComplexFieldChangeWatcher.as | 112 ++++++++++++++++--- .../src/mx/collections/ListCollectionView.as | 63 ++++++----- 2 files changed, 131 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/017ab1f0/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as b/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as index faef198..7190bd5 100644 --- a/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as +++ b/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as @@ -20,17 +20,22 @@ package mx.collections { import flash.events.EventDispatcher; - import mx.binding.utils.BindingUtils; import mx.binding.utils.ChangeWatcher; + import mx.core.mx_internal; + import mx.events.CollectionEvent; + import mx.events.CollectionEventKind; import mx.events.PropertyChangeEvent; public class ComplexFieldChangeWatcher extends EventDispatcher { private var _complexFieldWatchers:Vector.<ChangeWatcher> = new Vector.<ChangeWatcher>(); private var _list:IList; + private var _listCollection:ICollectionView; public function stopWatchingForComplexFieldChanges():void { + unwatchListForChanges(); + for each(var watcher:ChangeWatcher in _complexFieldWatchers) { watcher.unwatch(); @@ -39,36 +44,107 @@ package mx.collections { _complexFieldWatchers.length = 0; } - public function startWatchingForComplexFieldChanges(list:IList, fields:Array):void + public function startWatchingForComplexFieldChanges():void + { + watchListForChanges(); + + watchItems(list); + } + + private function watchItems(items:IList):void + { + if(sortFields) + { + for(var i:int = 0; i < items.length; i++) + { + watchItem(items.getItemAt(i), sortFields); + } + } + } + + private function watchArrayOfItems(items:Array):void { - _list = list; + if(sortFields) + { + for(var i:int = 0; i < items.length; i++) + { + watchItem(items[i], sortFields); + } + } + } - for(var i:int = 0; i < fields.length; i++) + private function watchItem(item:Object, sortFields:Array):void + { + if(item) { - var sortField:IComplexSortField = fields[i] as IComplexSortField; - if(sortField && sortField.nameParts) + for(var i:int = 0; i < sortFields.length; i++) { - for(var j:int = 0; j < _list.length; j++) + var sortField:IComplexSortField = sortFields[i] as IComplexSortField; + if(sortField && sortField.nameParts) { - var item:Object = _list.getItemAt(j); - if(item) - { - var watcher:ChangeWatcher = BindingUtils.bindSetter(function(value:Object):void {}, item, sortField.nameParts); - if(watcher) - { - watcher.setHandler(new Closure(item, complexValueChanged).callFunctionOnObject); - _complexFieldWatchers.push(watcher); - } - } + watchItemForField(item, sortField.nameParts); } } } } - private function complexValueChanged(item:Object):void + private function watchItemForField(item:Object, chain:Array):void + { + var watcher:ChangeWatcher = ChangeWatcher.watch(item, chain, new Closure(item, onComplexValueChanged).callFunctionOnObject, false, true); + if(watcher) + { + _complexFieldWatchers.push(watcher); + } + } + + private function onCollectionChanged(event:CollectionEvent):void + { + switch(event.kind) + { + case CollectionEventKind.ADD: { + watchArrayOfItems(event.items); + break; + } + } + } + + private function onComplexValueChanged(item:Object):void { dispatchEvent(PropertyChangeEvent.createUpdateEvent(item, null, null, null)); } + + private function get sortFields():Array + { + return _listCollection && _listCollection.sort ? _listCollection.sort.fields : null; + } + + mx_internal function set list(value:IList):void + { + if(_list != value) + { + stopWatchingForComplexFieldChanges(); + + _list = value; + _listCollection = value as ICollectionView; + } + } + + protected function get list():IList + { + return _list; + } + + private function watchListForChanges():void + { + if(list) + list.addEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChanged, false, 0, true); + } + + private function unwatchListForChanges():void + { + if(list) + list.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChanged); + } } } http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/017ab1f0/frameworks/projects/framework/src/mx/collections/ListCollectionView.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/src/mx/collections/ListCollectionView.as b/frameworks/projects/framework/src/mx/collections/ListCollectionView.as index 0f76081..f5bef43 100644 --- a/frameworks/projects/framework/src/mx/collections/ListCollectionView.as +++ b/frameworks/projects/framework/src/mx/collections/ListCollectionView.as @@ -379,29 +379,22 @@ public class ListCollectionView extends Proxy /** * @private */ - public function set sort(s:ISort):void + public function set sort(value:ISort):void { - if(_sort && _sort != s && complexFieldWatcher) - complexFieldWatcher.stopWatchingForComplexFieldChanges(); + if(_sort != value) + { + stopWatchingForComplexFieldsChanges(); - _sort = s; + _sort = value; - if(_sort && _sort.fields && complexFieldWatcher) - complexFieldWatcher.startWatchingForComplexFieldChanges(this, _sort.fields); + startWatchingForComplexFieldsChanges(); - dispatchEvent(new Event("sortChanged")); + dispatchEvent(new Event("sortChanged")); + } } - private function onComplexFieldValueChanged(changeEvent:PropertyChangeEvent):void - { - if(sort) - { - moveItemInView(changeEvent.source); - } - } - //-------------------------------------------------------------------------- // // ICollectionView Methods @@ -1866,20 +1859,38 @@ public class ListCollectionView extends Proxy { if(_complexFieldWatcher != value) { - if(_complexFieldWatcher) - { - _complexFieldWatcher.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onComplexFieldValueChanged); - _complexFieldWatcher.stopWatchingForComplexFieldChanges(); - } + stopWatchingForComplexFieldsChanges(); _complexFieldWatcher = value; + _complexFieldWatcher.mx_internal::list = this; - if(_complexFieldWatcher) - { - _complexFieldWatcher.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onComplexFieldValueChanged, false, 0, true); - if(sort) - _complexFieldWatcher.startWatchingForComplexFieldChanges(this, sort.fields); - } + startWatchingForComplexFieldsChanges(); + } + } + + private function startWatchingForComplexFieldsChanges():void + { + if(complexFieldWatcher && sort && sort.fields) + { + _complexFieldWatcher.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onComplexFieldValueChanged, false, 0, true); + _complexFieldWatcher.startWatchingForComplexFieldChanges(); + } + } + + private function stopWatchingForComplexFieldsChanges():void + { + if(complexFieldWatcher) + { + _complexFieldWatcher.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onComplexFieldValueChanged); + _complexFieldWatcher.stopWatchingForComplexFieldChanges(); + } + } + + private function onComplexFieldValueChanged(changeEvent:PropertyChangeEvent):void + { + if(sort) + { + moveItemInView(changeEvent.source); } } }
