FLEX-34854 Externalized the complex field watching functionality from ListCollectionView into ComplexFieldChangeWatcher. The same unit tests that passed before still pass now.
Project: http://git-wip-us.apache.org/repos/asf/flex-sdk/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-sdk/commit/849d90a8 Tree: http://git-wip-us.apache.org/repos/asf/flex-sdk/tree/849d90a8 Diff: http://git-wip-us.apache.org/repos/asf/flex-sdk/diff/849d90a8 Branch: refs/heads/develop Commit: 849d90a829602b72e06a21770bf70b97ba8d2abb Parents: cf65784 Author: Mihai Chira <[email protected]> Authored: Mon Jun 8 17:05:03 2015 +0200 Committer: Mihai Chira <[email protected]> Committed: Mon Jun 8 17:05:03 2015 +0200 ---------------------------------------------------------------------- .../mx/collections/ComplexFieldChangeWatcher.as | 92 +++++++++++++++++++ .../src/mx/collections/ListCollectionView.as | 94 ++++++++------------ .../framework/tests/FLEX_34854_Tests.as | 5 ++ 3 files changed, 134 insertions(+), 57 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/849d90a8/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 new file mode 100644 index 0000000..faef198 --- /dev/null +++ b/frameworks/projects/framework/src/mx/collections/ComplexFieldChangeWatcher.as @@ -0,0 +1,92 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +package mx.collections { + import flash.events.EventDispatcher; + + import mx.binding.utils.BindingUtils; + import mx.binding.utils.ChangeWatcher; + import mx.events.PropertyChangeEvent; + + public class ComplexFieldChangeWatcher extends EventDispatcher { + + private var _complexFieldWatchers:Vector.<ChangeWatcher> = new Vector.<ChangeWatcher>(); + private var _list:IList; + + public function stopWatchingForComplexFieldChanges():void + { + for each(var watcher:ChangeWatcher in _complexFieldWatchers) + { + watcher.unwatch(); + } + + _complexFieldWatchers.length = 0; + } + + public function startWatchingForComplexFieldChanges(list:IList, fields:Array):void + { + _list = list; + + for(var i:int = 0; i < fields.length; i++) + { + var sortField:IComplexSortField = fields[i] as IComplexSortField; + if(sortField && sortField.nameParts) + { + for(var j:int = 0; j < _list.length; j++) + { + 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); + } + } + } + } + } + } + + private function complexValueChanged(item:Object):void + { + dispatchEvent(PropertyChangeEvent.createUpdateEvent(item, null, null, null)); + } + } +} + +import flash.events.Event; + +class Closure +{ + private var _object:Object; + private var _function:Function; + + public function Closure(cachedObject:Object, cachedFunction:Function) + { + _object = cachedObject; + _function = cachedFunction; + } + + public function callFunctionOnObject(event:Event):void + { + _function.apply(null, [_object]); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/849d90a8/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 f322139..0f76081 100644 --- a/frameworks/projects/framework/src/mx/collections/ListCollectionView.as +++ b/frameworks/projects/framework/src/mx/collections/ListCollectionView.as @@ -100,7 +100,11 @@ public class ListCollectionView extends Proxy // //-------------------------------------------------------------------------- - private var _complexFieldWatchers:Vector.<ChangeWatcher> = new Vector.<ChangeWatcher>(); + /** + * @private + * Change watcher for complex sort fields. + */ + private var _complexFieldWatcher:ComplexFieldChangeWatcher; /** * @private @@ -377,56 +381,24 @@ public class ListCollectionView extends Proxy */ public function set sort(s:ISort):void { - if(_sort && _sort != s) - stopWatchingForComplexFieldChanges(); + if(_sort && _sort != s && complexFieldWatcher) + complexFieldWatcher.stopWatchingForComplexFieldChanges(); _sort = s; - if(_sort && _sort.fields) - startWatchingForComplexFieldChanges(_sort.fields); + if(_sort && _sort.fields && complexFieldWatcher) + complexFieldWatcher.startWatchingForComplexFieldChanges(this, _sort.fields); dispatchEvent(new Event("sortChanged")); } - private function stopWatchingForComplexFieldChanges():void - { - for each(var watcher:ChangeWatcher in _complexFieldWatchers) - { - watcher.unwatch(); - } - _complexFieldWatchers.length = 0; - } - private function startWatchingForComplexFieldChanges(fields:Array):void + private function onComplexFieldValueChanged(changeEvent:PropertyChangeEvent):void { - for(var i:int = 0; i < fields.length; i++) + if(sort) { - var sortField:IComplexSortField = fields[i] as IComplexSortField; - if(sortField && sortField.nameParts) - { - for(var j:int = 0; j < this.length; j++) - { - var item:Object = this.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); - } - } - } - } - } - } - - private function complexValueChanged(item:Object):void - { - if(filterFunction != null || sort) - { - moveItemInView(item); + moveItemInView(changeEvent.source); } } @@ -1885,6 +1857,31 @@ public class ListCollectionView extends Proxy } } + public function get complexFieldWatcher():ComplexFieldChangeWatcher + { + return _complexFieldWatcher; + } + + public function set complexFieldWatcher(value:ComplexFieldChangeWatcher):void + { + if(_complexFieldWatcher != value) + { + if(_complexFieldWatcher) + { + _complexFieldWatcher.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onComplexFieldValueChanged); + _complexFieldWatcher.stopWatchingForComplexFieldChanges(); + } + + _complexFieldWatcher = value; + + if(_complexFieldWatcher) + { + _complexFieldWatcher.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onComplexFieldValueChanged, false, 0, true); + if(sort) + _complexFieldWatcher.startWatchingForComplexFieldChanges(this, sort.fields); + } + } + } } } @@ -2753,21 +2750,4 @@ class ListCollectionViewBookmark extends CursorBookmark { return view.getBookmarkIndex(this); } -} - -class Closure -{ - private var _object:Object; - private var _function:Function; - - public function Closure(cachedObject:Object, cachedFunction:Function) - { - _object = cachedObject; - _function = cachedFunction; - } - - public function callFunctionOnObject(event:Event):void - { - _function.apply(null, [_object]); - } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/849d90a8/frameworks/projects/framework/tests/FLEX_34854_Tests.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/framework/tests/FLEX_34854_Tests.as b/frameworks/projects/framework/tests/FLEX_34854_Tests.as index e60fe22..9b579f8 100644 --- a/frameworks/projects/framework/tests/FLEX_34854_Tests.as +++ b/frameworks/projects/framework/tests/FLEX_34854_Tests.as @@ -19,6 +19,7 @@ package { import mx.collections.ArrayList; + import mx.collections.ComplexFieldChangeWatcher; import mx.collections.ComplexSortField; import mx.collections.IList; import mx.collections.ListCollectionView; @@ -54,6 +55,8 @@ package { _sut.sort = sortByNameAscending; _sut.refresh(); //values: Object1, Object2, Object3, Object4 + _sut.complexFieldWatcher = new ComplexFieldChangeWatcher(); + //when const first:ListCollectionView_FLEX_34854_VO = _sut.getItemAt(0) as ListCollectionView_FLEX_34854_VO; first.address.street = "Street9"; //this should immediately place the newItem at the end @@ -76,6 +79,8 @@ package { _sut.sort = sortByNameAscending; _sut.refresh(); //values: Object1, Object2, Object3, Object4 + _sut.complexFieldWatcher = new ComplexFieldChangeWatcher(); + //when const newItem:ListCollectionView_FLEX_34854_VO = generateOneObject(5); _sut.addItem(newItem); //values: Object1, Object2, Object3, Object4, Object5
