diff --git a/TNKit.j b/TNKit.j index 07d527d..c531ae5 100644 --- a/TNKit.j +++ b/TNKit.j @@ -25,6 +25,7 @@ @import "TNLocalizationCenter.j"; @import "TNMessageBoard.j" @import "TNMessageView.j" +@import "TNMultiViewDataSource.j" @import "TNOutlineViewDataSource.j" @import "TNStackView.j" @import "TNSwipeView.j" diff --git a/TNMultiViewDataSource.j b/TNMultiViewDataSource.j new file mode 100644 index 0000000..8c958d5 --- /dev/null +++ b/TNMultiViewDataSource.j @@ -0,0 +1,107 @@ +/* + * TNMultiViewDataSource.j + * + * Copyright (C) 2012 Luc Vauvillier + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +@import +@import + +@import "TNTableViewDataSource.j" + +/*! @ingroup tnkit + Synchronize and share datasource between a table view and a collection view + Designed to display a collection of items with grid mode / list mode switch possibility +*/ +@implementation TNMultiViewDataSource: TNTableViewDataSource +{ + CPCollectionView _collectionView @accessors(property=collectionView); +} + +#pragma mark - +#pragma mark Setters and getters + +/*! used to set the collection view + @param aCollectionView the collection view +*/ +- (void)setCollectionView:(CPCollectionView)aCollectionView +{ + if (_collectionView == aCollectionView) + return; + + _collectionView = aCollectionView; + [_collectionView setContent:[self filteredContent]]; + [self synchronizeSelectionFromView:[self table]]; +} + +/*! used to set the table view + @param aTableView the table view +*/ +- (void)setTableView:(CPTableView)aTableView +{ + [super setTable:aTableView]; + [self synchronizeSelectionFromView:_collectionView]; +} + +#pragma mark - +#pragma mark data synchronization + +/*! this action should be bound to a CPSearchField + it will filter the content of the datasource according to the sender value + @param sender the sender of the action +*/ +- (IBAction)filterObjects:(id)sender +{ + [super filterObjects:sender]; + [_collectionView setContent:[self filteredContent]]; +} + +/*! set the given array of object as the content of the datasource + @param aContent CPArray containing the datas of the datasource +*/ +- (void)setContent:(CPArray)aContent +{ + [super setContent:aContent]; + [_collectionView setContent:[self filteredContent]]; +} + +- (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors +{ + [super tableView:aTableView sortDescriptorsDidChange:oldDescriptors]; + [_collectionView reloadContent]; +} + +#pragma mark - +#pragma mark selection synchronization + +/*! synchronize selection between the two components + @param aView The master view +*/ +- (void)synchronizeSelectionFromView:(CPView)aView +{ + if (!aView) + return; + + if (aView == _collectionView) + { + [[self table] selectRowIndexes:[_collectionView selectionIndexes] byExtendingSelection:NO]; + } + else if (aView == [self table]) + { + [_collectionView setSelectionIndexes:[[self table] selectedRowIndexes]]; + } +} + +@end \ No newline at end of file diff --git a/TNTableViewDataSource.j b/TNTableViewDataSource.j index c567eb1..aab234e 100644 --- a/TNTableViewDataSource.j +++ b/TNTableViewDataSource.j @@ -65,7 +65,7 @@ CPTableView _table @accessors(property=table); CPPredicate _displayFilter @accessors(property=displayFilter); - CPArray _filteredContent; + CPArray _filteredContent @accessors(property=filteredContent, readonly); CPSearchField _searchField; CPString _filter; BOOL _needsFilter; @@ -294,6 +294,18 @@ return [_filteredContent count]; } +#pragma mark - +#pragma mark Helpers + +/*! expose selected objects + @return array of selected objects or nil if _table is not setted +*/ +- (CPArray)selectedObjects +{ + if (!_table) + return nil; + return [_filteredContent objectsAtIndexes:[_table selectedRowIndexes]]; +} #pragma mark - #pragma mark Datasource implementation