recents_category_drag_drop

the drag and drop is implemented : the cell can be move in the recents sections.
This commit is contained in:
yannick 2015-12-11 11:50:13 +01:00
parent 5e8e81d019
commit 27a72ae7db
3 changed files with 136 additions and 6 deletions

View file

@ -36,4 +36,15 @@
*/
- (CGFloat)heightForHeaderInSection:(NSInteger)section;
/**
Return true of the cell can be moved from a section to another one.
*/
- (BOOL)isDraggableCellAt:(NSIndexPath*)path;
/**
Move a cell from a path to another one.
It is based on room Tag.
*/
- (void)moveCellFrom:(NSIndexPath*)oldPath to:(NSIndexPath*)newPath;
@end

View file

@ -482,4 +482,76 @@
[super destroy];
}
#pragma mark - drag and drop managemenent
- (BOOL)isDraggableCellAt:(NSIndexPath*)path
{
return (path && ((path.section == favoritesSection) || (path.section == lowPrioritySection) || (path.section == conversationSection)));
}
- (BOOL)canCellMoveFrom:(NSIndexPath*)oldPath to:(NSIndexPath*)newPath
{
BOOL res = [self isDraggableCellAt:oldPath] && [self isDraggableCellAt:newPath];
// the both index pathes are movable
if (res)
{
// cannot move conversation rooms in the same section
res &= !((oldPath.section == conversationSection) && (newPath.section == conversationSection));
// other cases ?
}
return res;
}
- (NSString*)roomTagAt:(NSIndexPath*)path
{
if (path.section == favoritesSection)
{
return kMXRoomTagFavourite;
}
else if (path.section == lowPrioritySection)
{
return kMXRoomTagLowPriority;
}
return nil;
}
- (void)moveCellFrom:(NSIndexPath*)oldPath to:(NSIndexPath*)newPath
{
if ([self canCellMoveFrom:oldPath to:newPath] && ![newPath isEqual:oldPath])
{
NSString* oldRoomTag = [self roomTagAt:oldPath];
NSString* dstRoomTag = [self roomTagAt:newPath];
MXRoom* room = [self getRoomAtIndexPath:oldPath];
NSString* tagOrder = [room.mxSession tagOrderToBeAtIndex:newPath.row withTag:dstRoomTag];
NSLog(@"[MXKRecentsDataSource] Update the room %@ tag from %@ to %@ with tag order %@", room.state.roomId, oldRoomTag, dstRoomTag, tagOrder);
[room replaceTag:oldRoomTag
byTag:dstRoomTag
withOrder:tagOrder
success: ^{
// Refresh table display
if (self.delegate)
{
[self.delegate dataSource:self didCellChange:nil];
}
} failure:^(NSError *error) {
NSLog(@"[MXKRecentsDataSource] Failed to update the tag %@ of room (%@) failed: %@", dstRoomTag, room.state.roomId, error);
// Notify MatrixKit user
[[NSNotificationCenter defaultCenter] postNotificationName:kMXKErrorNotification object:error];
}];
}
}
@end

View file

@ -39,6 +39,7 @@
// recents drag and drop management
UIView *cellSnapshot;
NSIndexPath* movingCellPath;
}
@end
@ -323,7 +324,6 @@ static NSMutableDictionary* backgroundByImageNameDict;
{
NSString* title = @" ";
// pushes settings
BOOL isMuted = ![self.dataSource isRoomNotifiedAtIndexPath:indexPath];
@ -416,9 +416,6 @@ static NSMutableDictionary* backgroundByImageNameDict;
[self.recentsTableView setEditing:NO];
}
#pragma mark - Override UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
@ -467,20 +464,42 @@ static NSMutableDictionary* backgroundByImageNameDict;
}
#pragma mark - recents drag & drop management
- (IBAction) onRecentsLongPress:(id)sender
{
RecentsDataSource* recentsDataSource = nil;
if ([self.dataSource isKindOfClass:[RecentsDataSource class]])
{
recentsDataSource = (RecentsDataSource*)self.dataSource;
}
// only support RecentsDataSource
if (!recentsDataSource)
{
return;
}
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
// check if there is a moving cell during the long press managemnt
if ((state != UIGestureRecognizerStateBegan) && !movingCellPath)
{
return;
}
CGPoint location = [longPress locationInView:self.recentsTableView];
NSIndexPath *indexPath = [self.recentsTableView indexPathForRowAtPoint:location];
switch (state)
{
// step 1 : display the selected cell
case UIGestureRecognizerStateBegan:
{
if (indexPath)
NSIndexPath *indexPath = [self.recentsTableView indexPathForRowAtPoint:location];
// check if the cell can be moved
if (indexPath && [recentsDataSource isDraggableCellAt:indexPath])
{
UITableViewCell *cell = [self.recentsTableView cellForRowAtIndexPath:indexPath];
@ -498,6 +517,8 @@ static NSMutableDictionary* backgroundByImageNameDict;
cellSnapshot.center = center;
cellSnapshot.alpha = 0.5f;
[self.recentsTableView addSubview:cellSnapshot];
movingCellPath = indexPath;
}
break;
}
@ -565,6 +586,32 @@ static NSMutableDictionary* backgroundByImageNameDict;
case UIGestureRecognizerStateEnded:
{
[cellSnapshot removeFromSuperview];
cellSnapshot = nil;
NSIndexPath *indexPath = [self.recentsTableView indexPathForRowAtPoint:location];
NSInteger section = indexPath.section;
UITableViewCell* cell = [self.recentsTableView cellForRowAtIndexPath:indexPath];
// 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]];
[cellSnapshot removeFromSuperview];
cellSnapshot = nil;
movingCellPath = nil;
break;
}
// default behaviour
// remove the cell and cancel the insertion
default:
{
[cellSnapshot removeFromSuperview];
cellSnapshot = nil;
movingCellPath = nil;
break;
}
}