Handle back pagination in room details

This commit is contained in:
giomfo
2014-10-14 22:50:24 +02:00
parent 182846eb57
commit 39938a76c4
2 changed files with 46 additions and 1 deletions

View File

@@ -182,6 +182,9 @@
<constraint firstAttribute="centerY" secondItem="cfF-YG-Cvg" secondAttribute="centerY" id="ugG-2r-MsO"/>
</constraints>
</view>
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" hidesWhenStopped="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="dvT-c5-Ymf">
<rect key="frame" x="290" y="100" width="20" height="20"/>
</activityIndicatorView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
@@ -189,13 +192,16 @@
<constraint firstItem="6fM-aJ-d0M" firstAttribute="leading" secondItem="1BF-nV-MTf" secondAttribute="leading" id="7eH-ST-BbY"/>
<constraint firstItem="nNf-bE-8dQ" firstAttribute="top" secondItem="6fM-aJ-d0M" secondAttribute="bottom" id="C5t-bm-3s8"/>
<constraint firstAttribute="bottom" secondItem="meV-kn-sxo" secondAttribute="bottom" constant="44" id="Cos-M5-WHG"/>
<constraint firstItem="dvT-c5-Ymf" firstAttribute="top" secondItem="it4-Tc-q7z" secondAttribute="bottom" constant="36" id="FV3-P0-ekh"/>
<constraint firstItem="meV-kn-sxo" firstAttribute="top" secondItem="1BF-nV-MTf" secondAttribute="top" id="OWt-4M-1DF"/>
<constraint firstItem="meV-kn-sxo" firstAttribute="leading" secondItem="1BF-nV-MTf" secondAttribute="leading" id="ext-xr-eNx"/>
<constraint firstAttribute="centerX" secondItem="dvT-c5-Ymf" secondAttribute="centerX" id="u9d-Bd-6Ci"/>
<constraint firstAttribute="trailing" secondItem="meV-kn-sxo" secondAttribute="trailing" id="vBl-Ri-4Hb"/>
</constraints>
</view>
<navigationItem key="navigationItem" title="Room title" id="3Zt-Wl-J6o"/>
<connections>
<outlet property="activityIndicator" destination="dvT-c5-Ymf" id="F6h-Al-Vw6"/>
<outlet property="controlView" destination="6fM-aJ-d0M" id="13g-Wl-z5n"/>
<outlet property="controlViewBottomConstraint" destination="C5t-bm-3s8" id="Ks1-Z5-mzO"/>
<outlet property="messageTextField" destination="k2m-aY-U73" id="fSA-Eg-duj"/>

View File

@@ -49,6 +49,7 @@
@property (weak, nonatomic) IBOutlet UITextField *messageTextField;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *controlViewBottomConstraint;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (strong, nonatomic) MXRoomData *mxRoomData;
@@ -135,6 +136,7 @@
[self scrollToBottomAnimated:YES];
// Move up control view
// Don't forget the offset related to tabBar
_controlViewBottomConstraint.constant = insets.bottom - [AppDelegate theDelegate].masterTabBarController.tabBar.frame.size.height;
}
@@ -203,6 +205,43 @@
[self dismissKeyboard];
}
// Detect vertical bounce at the top of the tableview to trigger pagination
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (scrollView == self.tableView) {
// paginate ?
if ((scrollView.contentOffset.y < -64) && (_activityIndicator.isAnimating == NO))
{
if (self.mxRoomData.canPaginate)
{
[_activityIndicator startAnimating];
[self.mxRoomData paginateBackMessages:20 success:^(NSArray *messages) {
// Update room data
self.mxRoomData = [[[MatrixHandler sharedHandler] mxData] getRoomData:self.roomId];
// Refresh display
[self.tableView beginUpdates];
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:messages.count];
for (NSUInteger index = 0; index < messages.count; index++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// Maintain the current message in visible area
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(messages.count - 1) inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[_activityIndicator stopAnimating];
} failure:^(NSError *error) {
[_activityIndicator stopAnimating];
NSLog(@"Failed to paginate back: %@", error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
}
}
}
}
#pragma mark - UITextField delegate
- (void)onTextFieldChange:(NSNotification *)notif {
@@ -240,7 +279,7 @@
}];
} else if (sender == _optionBtn) {
[self dismissKeyboard];
//TODO
//TODO: display option menu (Attachments...)
}
}
@end