recents_category_drag_drop

Add the cell drag and drop.
This commit is contained in:
yannick 2015-12-11 14:59:55 +01:00
parent 7d790e4a2f
commit 922607d521
3 changed files with 119 additions and 13 deletions

View file

@ -31,6 +31,17 @@
*/
@property (nonatomic, copy) void (^onRoomInvitationAccept)(MXRoom*);
/**
There is a pending drag and drop cell.
It defines its path.
*/
@property (nonatomic, copy) NSIndexPath* movingCellIndexPath;
/**
The movingCellBackgroundImage;
*/
@property (nonatomic) UIImageView* movingCellBackGroundView;
/**
Return the header height from the section.
*/

View file

@ -41,6 +41,7 @@
@implementation RecentsDataSource
@synthesize onRoomInvitationReject, onRoomInvitationAccept;
@synthesize movingCellIndexPath, movingCellBackGroundView;
- (instancetype)init
{
@ -166,6 +167,10 @@
count = invitesCellDataArray.count;
}
if (self.movingCellIndexPath && (self.movingCellIndexPath.section == section))
{
count++;
}
return count;
}
@ -207,10 +212,44 @@
return [super viewForHeaderInSection:section withFrame:frame];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)anIndexPath
{
UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
NSIndexPath* indexPath = anIndexPath;
if (self.movingCellIndexPath && (self.movingCellIndexPath.section == indexPath.section))
{
if ([anIndexPath isEqual:self.movingCellIndexPath])
{
static NSString* cellIdentifier = @"VectorRecentsMovingCell";
UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"VectorRecentsMovingCell"];
UIImageView* imageView = [cell viewWithTag:[cellIdentifier hash]];
if (!imageView || (imageView != self.movingCellBackGroundView))
{
if (imageView)
{
[imageView removeFromSuperview];
}
self.movingCellBackGroundView.tag = [cellIdentifier hash];
[cell.contentView addSubview:self.movingCellBackGroundView];
}
self.movingCellBackGroundView.frame = self.movingCellBackGroundView.frame;
cell.contentView.backgroundColor = [UIColor greenColor];
cell.backgroundColor = [UIColor redColor];
return cell;
}
if (anIndexPath.row > self.movingCellIndexPath.row)
{
indexPath = [NSIndexPath indexPathForRow:anIndexPath.row-1 inSection:anIndexPath.section];
}
}
UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// on invite cell, add listeners on accept / reject buttons
if (cell && [cell isKindOfClass:[InviteRecentTableViewCell class]])
@ -236,25 +275,35 @@
return cell;
}
- (id<MXKRecentCellDataStoring>)cellDataAtIndexPath:(NSIndexPath *)indexPath
- (id<MXKRecentCellDataStoring>)cellDataAtIndexPath:(NSIndexPath *)anIndexPath
{
id<MXKRecentCellDataStoring> cellData = nil;
NSInteger row = anIndexPath.row;
NSInteger section = anIndexPath.section;
if (indexPath.section == favoritesSection)
if (self.movingCellIndexPath && (self.movingCellIndexPath.section == section))
{
cellData = [favoriteCellDataArray objectAtIndex:indexPath.row];
if (anIndexPath.row > self.movingCellIndexPath.row)
{
row = anIndexPath.row - 1;
}
}
else if (indexPath.section == conversationSection)
if (section == favoritesSection)
{
cellData = [conversationCellDataArray objectAtIndex:indexPath.row];
cellData = [favoriteCellDataArray objectAtIndex:row];
}
else if (indexPath.section == lowPrioritySection)
else if (section== conversationSection)
{
cellData = [lowPriorityCellDataArray objectAtIndex:indexPath.row];
cellData = [conversationCellDataArray objectAtIndex:row];
}
else if (indexPath.section == invitesSection)
else if (section == lowPrioritySection)
{
cellData = [invitesCellDataArray objectAtIndex:indexPath.row];
cellData = [lowPriorityCellDataArray objectAtIndex:row];
}
else if (section == invitesSection)
{
cellData = [invitesCellDataArray objectAtIndex:row];
}
return cellData;
@ -262,6 +311,11 @@
- (CGFloat)cellHeightAtIndexPath:(NSIndexPath *)indexPath
{
if (self.movingCellIndexPath && [indexPath isEqual:self.movingCellIndexPath])
{
return self.movingCellBackGroundView.frame.size.height;
}
// Override this method here to use our own cellDataAtIndexPath
id<MXKRecentCellDataStoring> cellData = [self cellDataAtIndexPath:indexPath];

View file

@ -38,8 +38,10 @@
HomeViewController *homeViewController;
// recents drag and drop management
UIView *cellSnapshot;
UIImageView *cellSnapshot;
NSIndexPath* movingCellPath;
NSIndexPath* lastPotentialCellPath;
}
@end
@ -465,6 +467,8 @@ static NSMutableDictionary* backgroundByImageNameDict;
#pragma mark - recents drag & drop management
static UITableViewCell* cell = nil;
- (IBAction) onRecentsLongPress:(id)sender
{
RecentsDataSource* recentsDataSource = nil;
@ -510,6 +514,7 @@ static NSMutableDictionary* backgroundByImageNameDict;
UIGraphicsEndImageContext();
cellSnapshot = [[UIImageView alloc] initWithImage:image];
recentsDataSource.movingCellBackGroundView = [[UIImageView alloc] initWithImage:image];
// display the selected cell over the tableview
CGPoint center = cell.center;
@ -518,7 +523,19 @@ static NSMutableDictionary* backgroundByImageNameDict;
cellSnapshot.alpha = 0.5f;
[self.recentsTableView addSubview:cellSnapshot];
cell = [[UITableViewCell alloc] init];
cell.frame = CGRectMake(0, 0, 100, 80);
cell.backgroundColor = [UIColor redColor];
[self.recentsTableView beginUpdates];
lastPotentialCellPath = indexPath;
recentsDataSource.movingCellIndexPath = indexPath;
[self.recentsTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.recentsTableView endUpdates];
movingCellPath = indexPath;
}
break;
}
@ -578,6 +595,18 @@ static NSMutableDictionary* backgroundByImageNameDict;
[self.recentsTableView setContentOffset:contentOffset animated:NO];
}
NSIndexPath *indexPath = [self.recentsTableView indexPathForRowAtPoint:location];
if (![indexPath isEqual:lastPotentialCellPath])
{
[self.recentsTableView beginUpdates];
[self.recentsTableView moveRowAtIndexPath:lastPotentialCellPath toIndexPath:indexPath];
lastPotentialCellPath = indexPath;
recentsDataSource.movingCellIndexPath = indexPath;
[self.recentsTableView endUpdates];
}
break;
}
@ -596,12 +625,18 @@ static NSMutableDictionary* backgroundByImageNameDict;
// the destinated row is retrieved from the moving cell (center comparison)
int row = (location.y > cell.center.y) ? indexPath.row + 1 : indexPath.row;
[recentsDataSource moveCellFrom:movingCellPath to: [NSIndexPath indexPathForRow:row inSection:section]];
[recentsDataSource moveCellFrom:movingCellPath to: lastPotentialCellPath];
[cellSnapshot removeFromSuperview];
cellSnapshot = nil;
movingCellPath = nil;
[self.recentsTableView beginUpdates];
[self.recentsTableView deleteRowsAtIndexPaths:@[lastPotentialCellPath] withRowAnimation:UITableViewRowAnimationNone];
lastPotentialCellPath = nil;
recentsDataSource.movingCellIndexPath = nil;
[self.recentsTableView endUpdates];
break;
}
@ -612,6 +647,12 @@ static NSMutableDictionary* backgroundByImageNameDict;
[cellSnapshot removeFromSuperview];
cellSnapshot = nil;
movingCellPath = nil;
[self.recentsTableView beginUpdates];
[self.recentsTableView deleteRowsAtIndexPaths:@[lastPotentialCellPath] withRowAnimation:UITableViewRowAnimationNone];
lastPotentialCellPath = nil;
recentsDataSource.movingCellIndexPath = nil;
[self.recentsTableView endUpdates];
break;
}
}