Skip to content

Commit

Permalink
Fix problems with date cannot be displayed, remove debugging code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuozhuo committed Sep 2, 2016
1 parent 8804ec6 commit 4f6afa6
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 115 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -28,7 +28,7 @@ Easy integrated chat feature.

pod 'ZHChat', '~> 0.1.4'

### 复制文件夹ZHCMessagesViewController至你的工程中
### Copy the folder ZHCMessagesViewController to your project

## Getting Started
*Getting started guide for ZHChat*
Expand Down
110 changes: 18 additions & 92 deletions ZHCMessagesViewController/Controllers/ZHCMessagesViewController.m
Expand Up @@ -22,78 +22,9 @@
#import "UIColor+ZHCMessages.h"
#import "UIView+ZHCMessages.h"



#import <MobileCoreServices/UTCoreTypes.h>
#import <objc/runtime.h>


static IMP ZHCReplaceMethodWithBlock(Class c, SEL origSEL, id block) {
NSCParameterAssert(block);

// get original method
Method origMethod = class_getInstanceMethod(c, origSEL);
NSCParameterAssert(origMethod);

// convert block to IMP trampoline and replace method implementation
IMP newIMP = imp_implementationWithBlock(block);

// Try adding the method if not yet in the current class
if (!class_addMethod(c, origSEL, newIMP, method_getTypeEncoding(origMethod))) {
return method_setImplementation(origMethod, newIMP);
} else {
return method_getImplementation(origMethod);
}
}

static void ZHCInstallWorkaroundForSheetPresentationIssue26295020(void) {
__block void (^removeWorkaround)(void) = ^{};
const void (^installWorkaround)(void) = ^{
const SEL presentSEL = @selector(presentViewController:animated:completion:);
__block IMP origIMP = ZHCReplaceMethodWithBlock(UIViewController.class, presentSEL, ^(UIViewController *self, id vC, BOOL animated, id completion) {
UIViewController *targetVC = self;
while (targetVC.presentedViewController) {
targetVC = targetVC.presentedViewController;
}
((void (*)(id, SEL, id, BOOL, id))origIMP)(targetVC, presentSEL, vC, animated, completion);
});
removeWorkaround = ^{
Method origMethod = class_getInstanceMethod(UIViewController.class, presentSEL);
NSCParameterAssert(origMethod);
class_replaceMethod(UIViewController.class,
presentSEL,
origIMP,
method_getTypeEncoding(origMethod));
};
};

const SEL presentSheetSEL = NSSelectorFromString(@"presentSheetFromRect:");
const void (^swizzleOnClass)(Class k) = ^(Class klass) {
const __block IMP origIMP = ZHCReplaceMethodWithBlock(klass, presentSheetSEL, ^(id self, CGRect rect) {
// Before calling the original implementation, we swizzle the presentation logic on UIViewController
installWorkaround();
// UIKit later presents the sheet on [view.window rootViewController];
// See https://github.com/WebKit/webkit/blob/1aceb9ed7a42d0a5ed11558c72bcd57068b642e7/Source/WebKit2/UIProcess/ios/WKActionSheet.mm#L102
// Our workaround forwards this to the topmost presentedViewController instead.
((void (*)(id, SEL, CGRect))origIMP)(self, presentSheetSEL, rect);
// Cleaning up again - this workaround would swallow bugs if we let it be there.
removeWorkaround();
});
};

// _UIRotatingAlertController
Class alertClass = NSClassFromString([NSString stringWithFormat:@"%@%@%@", @"_U", @"IRotat", @"ingAlertController"]);
if (alertClass) {
swizzleOnClass(alertClass);
}

// WKActionSheet
Class actionSheetClass = NSClassFromString([NSString stringWithFormat:@"%@%@%@", @"W", @"KActio", @"nSheet"]);
if (actionSheetClass) {
swizzleOnClass(actionSheetClass);
}
}

@interface ZHCMessagesViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet ZHCMessagesTableView *messageTableView;
@property (strong, nonatomic) IBOutlet ZHCMessagesInputToolbar *inputMessageBarView;
Expand Down Expand Up @@ -122,16 +53,8 @@ + (instancetype)messagesViewController
bundle:[NSBundle bundleForClass:[ZHCMessagesViewController class]]];
}

+ (void)initialize {
[super initialize];
if (self == [ZHCMessagesViewController self]) {
ZHCInstallWorkaroundForSheetPresentationIssue26295020();
}
}


#pragma mark - Initialization

- (void)zhc_configureMessagesViewController
{

Expand Down Expand Up @@ -401,9 +324,7 @@ - (void)scrollToBottomAnimated:(BOOL)animated
return;
}
NSInteger rows = [self.messageTableView numberOfRowsInSection:0];
NSLog(@"rows:%ld",(long)rows);
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:(rows - 1) inSection:0];

[self scrollToIndexPath:lastCellIndexPath animated:animated];
}

Expand All @@ -425,7 +346,6 @@ - (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
if (isContentTooSmall) {
// workaround for the first few messages not scrolling
// when the TableView view content size is too small, `scrollToItemAtIndexPath:` doesn't work properly
// this seems to be a UIKit bug, see #256 on GitHub
[self.messageTableView scrollRectToVisible:CGRectMake(0.0, tableViewContentHeight - 1.0f, 1.0f, 1.0f)
animated:animated];
return;
Expand All @@ -434,7 +354,6 @@ - (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
indexPath = [NSIndexPath indexPathForRow:row inSection:0];
// workaround for really long messages not scrolling
// if last message is too long, use scroll position bottom for better appearance, else use top
// possibly a UIKit bug, see #480 on GitHub
CGFloat cellHeight = [self tableView:self.messageTableView heightForRowAtIndexPath:indexPath];
CGFloat maxHeightForVisibleMessage = CGRectGetHeight(self.messageTableView.bounds)
- self.messageTableView.contentInset.top
Expand Down Expand Up @@ -490,19 +409,15 @@ -(CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForRowAtIndexPath:(N
}else{
avatarHeight = tableView.tableViewLayout.incomingAvatarViewSize.height;
}
if (size.height<=avatarHeight) {
NSLog(@"1111");
}else{
NSLog(@"0000");
}

CGFloat bubbleHeight = size.height>avatarHeight?size.height:avatarHeight;

CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath];
CGFloat cellsSpaceLabelHeight = [tableView.dataSource tableView:tableView tableViewCellSeparatorHeightAtIndexpath:indexPath];
CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForCellTopLabelAtIndexPath:indexPath];
CGFloat cellBubbleTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath];
CGFloat cellBottomLabelHeight = [tableView.dataSource tableView:tableView heightForCellBottomLabelAtIndexPath:indexPath];

height = kZHCMessagesTableViewCellSpaceDefault + cellTopLabelHeight + cellBubbleTopLabelHeight + cellBottomLabelHeight + bubbleHeight + 2.0*[UIScreen mainScreen].scale;
NSLog(@"cellHeight:%f",height);
height = cellsSpaceLabelHeight + cellTopLabelHeight + cellBubbleTopLabelHeight + cellBottomLabelHeight + bubbleHeight + 2.0*[UIScreen mainScreen].scale;
return height;

}
Expand Down Expand Up @@ -567,11 +482,12 @@ -(UITableViewCell *)tableView:(ZHCMessagesTableView *)tableView cellForRowAtInde
cell.cellTopLabel.attributedText = [tableView.dataSource tableView:tableView attributedTextForCellTopLabelAtIndexPath:indexPath];
cell.messageBubbleTopLabel.attributedText = [tableView.dataSource tableView:tableView attributedTextForMessageBubbleTopLabelAtIndexPath:indexPath];
cell.cellBottomLabel.attributedText = [tableView.dataSource tableView:tableView attributedTextForCellBottomLabelAtIndexPath:indexPath];

CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath];
CGFloat cellsSpaceLabelHeight = [tableView.dataSource tableView:tableView tableViewCellSeparatorHeightAtIndexpath:indexPath];
CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForCellTopLabelAtIndexPath:indexPath];
CGFloat cellBubbleTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath];
CGFloat cellBottomLabelHeight = [tableView.dataSource tableView:tableView heightForCellBottomLabelAtIndexPath:indexPath];

cell.cellsSpaceLabelHeight = cellsSpaceLabelHeight;
cell.cellTopLabelHeight = cellTopLabelHeight;
cell.messageBubbleTopLabelHeight = cellBubbleTopLabelHeight;
cell.cellBottomLabelHeight = cellBottomLabelHeight;
Expand Down Expand Up @@ -708,6 +624,17 @@ - (NSAttributedString *)tableView:(ZHCMessagesTableView *)tableView attributedTe
}

#pragma mark - Adjusting cell label heights

-(CGFloat)tableView:(ZHCMessagesTableView *)tableView tableViewCellSeparatorHeightAtIndexpath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 5.0f;
}else{
return kZHCMessagesTableViewCellSpaceDefault;
}
}


-(CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath
{
return 0.0f;
Expand Down Expand Up @@ -878,7 +805,6 @@ - (void)zhc_updateTableViewInsets
- (void)zhc_setTableViewInsetsTopValue:(CGFloat)top bottomValue:(CGFloat)bottom
{
bottom = bottom +10;
NSLog(@"Bottom Value:%f",bottom);
UIEdgeInsets insets = UIEdgeInsetsMake(top, 0.0f, bottom, 0.0f);
self.messageTableView.contentInset = insets;
self.messageTableView.scrollIndicatorInsets = insets;
Expand Down
Expand Up @@ -78,13 +78,11 @@ -(CGSize)messageBubbleSizeForMessageData:(id<ZHCMessageData>)messageData atIndex
{
NSValue *cachedSize = [self.cache objectForKey:@([messageData messageHash])];
if (cachedSize != nil) {
NSLog(@"hit cache Size");
return [cachedSize CGSizeValue];
}
CGSize finalSize = CGSizeZero;
if ([messageData isMediaMessage]) {
finalSize = [[messageData media] mediaViewDisplaySize];
NSLog(@"mediaSize:width:%f---height:%f",finalSize.width,finalSize.height);
}else{
ZHCMessagesTableviewLayoutAttributes *attributes = tableView.tableViewLayout;

Expand Down
14 changes: 12 additions & 2 deletions ZHCMessagesViewController/Models/ZHCMessagesTableViewDataSource.h
Expand Up @@ -133,7 +133,7 @@ NS_ASSUME_NONNULL_BEGIN
* Asks the delegate for the height of the `messageBubbleTopLabel` for the cell at the specified indexPath.
*
* @param tableView The tableView object displaying the flow layout.
* @param indexPath The index path of the cell.
* @param indexPath The index path of the cell.
*
* @return The height of the `messageBubbleTopLabel` for the cell at indexPath.
*
Expand All @@ -142,11 +142,21 @@ NS_ASSUME_NONNULL_BEGIN
- (CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForMessageBubbleTopLabelAtIndexPath:(NSIndexPath *)indexPath;


/**
* Asks the delegate for the height of the `cellsSpaceLabel` for the cell at the specified indexPath.
*
* @param tableView The tableView object displaying the flow layout.
* @param indexPath The index path of the cell.
*
* @return The height of the `cellsSpaceLabel` for the cell at indexPath.
*/
-(CGFloat)tableView:(ZHCMessagesTableView *)tableView tableViewCellSeparatorHeightAtIndexpath:(NSIndexPath *)indexPath;

/**
* Asks the delegate for the height of the `cellBottomLabel` for the cell at the specified indexPath.
*
* @param tableView The tableView object displaying the flow layout.
* @param indexPath The index path of the cell.
* @param indexPath The index path of the cell.
*
* @return The height of the `cellBottomLabel` for the cell at indexPath.
*
Expand Down
3 changes: 0 additions & 3 deletions ZHCMessagesViewController/Views/ZHCMessagesInputToolbar.m
Expand Up @@ -101,7 +101,6 @@ - (void)setPreferredDefaultHeight:(CGFloat)preferredDefaultHeight
*/
-(void)zhc_startRecordVoice:(UIButton *)sender
{
NSLog(@"startRecord");
sender.highlighted = YES;
[ZHCMessagesAudioProgressHUD zhc_show];
[_recorder zhc_startRecording];
Expand All @@ -112,7 +111,6 @@ -(void)zhc_startRecordVoice:(UIButton *)sender
*/
-(void)zhc_cancelRecordVoice:(UIButton *)sender
{
NSLog(@"cancelRecord");
sender.highlighted = NO;
[ZHCMessagesAudioProgressHUD zhc_dismissWithMessage:[NSBundle zhc_localizedStringForKey:@"Cancel_Recording"]];
[_recorder zhc_cancelRecord];
Expand All @@ -123,7 +121,6 @@ -(void)zhc_cancelRecordVoice:(UIButton *)sender
*/
-(void)zhc_confirmRecordVoice:(UIButton *)sender
{
NSLog(@"confirmRecord");
sender.highlighted = NO;
[_recorder zhc_stopRecording];
}
Expand Down
2 changes: 2 additions & 0 deletions ZHCMessagesViewController/Views/ZHCMessagesTableView.m
Expand Up @@ -41,6 +41,7 @@ -(void)awakeFromNib
{
[super awakeFromNib];
[self zhc_configureTableView];

}

- (void)zhc_configureTableView
Expand All @@ -50,6 +51,7 @@ - (void)zhc_configureTableView
self.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
self.alwaysBounceVertical = YES;
self.bounces = YES;
self.tableHeaderView = [UIView new];

[self registerNib:[ZHCMessagesTableViewCellIncoming nib] forCellReuseIdentifier:[ZHCMessagesTableViewCellIncoming cellReuseIdentifier]];
[self registerNib:[ZHCMessagesTableViewCellOutcoming nib] forCellReuseIdentifier:[ZHCMessagesTableViewCellOutcoming cellReuseIdentifier]];
Expand Down
9 changes: 9 additions & 0 deletions ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.h
Expand Up @@ -94,6 +94,15 @@ NS_ASSUME_NONNULL_BEGIN
@property (weak, nonatomic) id<ZHCMessagesTableViewCellDelegate> delegate;



/**
* The height of the `cellsSpaceLabel` of a `ZHCMessagesTableviewCell`.
* This value should be greater than or equal to `0.0`.
*
* @see ZHCMessagesTableviewCell.
*/
@property (assign, nonatomic) CGFloat cellsSpaceLabelHeight;

/**
* The height of the `cellTopLabel` of a `ZHCMessagesTableviewCell`.
* This value should be greater than or equal to `0.0`.
Expand Down
19 changes: 18 additions & 1 deletion ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.m
Expand Up @@ -279,6 +279,23 @@ - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

#pragma mark - Setters


-(void)setCellsSpaceLabelHeight:(CGFloat)cellsSpaceLabelHeight
{
NSParameterAssert(cellsSpaceLabelHeight >= 0.0f);
if (cellsSpaceLabelHeight == 0.0f) {
self.cellsSpaceLabel.text = nil;
}
if (self.cellsSpaceConstraint.constant == cellsSpaceLabelHeight) {
return;
}
_cellsSpaceLabelHeight = cellsSpaceLabelHeight;
[self zhc_updateConstraint:self.cellsSpaceConstraint withConstant:cellsSpaceLabelHeight];

}



-(void)setCellTopLabelHeight:(CGFloat)cellTopLabelHeight
{
NSParameterAssert(cellTopLabelHeight >= 0.0f);
Expand Down Expand Up @@ -311,7 +328,7 @@ -(void)setCellBottomLabelHeight:(CGFloat)cellBottomLabelHeight
{
NSParameterAssert(cellBottomLabelHeight >= 0.0f);
if (cellBottomLabelHeight == 0.0f) {
self.cellTopLabel.text = nil;
self.cellBottomLabel.text = nil;
}
if (self.cellBottomLabelHeightConstraint.constant == cellBottomLabelHeight) {
return;
Expand Down
2 changes: 1 addition & 1 deletion ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.h
Expand Up @@ -11,7 +11,7 @@



@interface ZHCDemoMessagesViewController : ZHCMessagesViewController<UIActionSheetDelegate>
@interface ZHCDemoMessagesViewController : ZHCMessagesViewController
@property (strong, nonatomic) ZHCModelData *demoData;
@property (assign, nonatomic) BOOL presentBool;
@end
13 changes: 0 additions & 13 deletions ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.m
Expand Up @@ -68,7 +68,6 @@ -(NSString *)senderId

-(void)tableView:(ZHCMessagesTableView *)tableView didDeleteMessageAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didDeleteMessageAtIndexPath:%ld",(long)indexPath.row);
[self.demoData.messages removeObjectAtIndex:indexPath.row];
}

Expand Down Expand Up @@ -214,22 +213,18 @@ -(CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForCellBottomLabelA
-(void)tableView:(ZHCMessagesTableView *)tableView didTapAvatarImageView:(UIImageView *)avatarImageView atIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didTapAvatarImageView:avatarImageView atIndexPath:indexPath];
NSLog(@"didTapAvatarImageViewIndexPath:%ld",(long)indexPath.row);
}


-(void)tableView:(ZHCMessagesTableView *)tableView didTapMessageBubbleAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didTapMessageBubbleAtIndexPath:indexPath];
NSLog(@"didTapMessageBubbleAtIndexPath:%ld",(long)indexPath.row);

}


-(void)tableView:(ZHCMessagesTableView *)tableView didTapCellAtIndexPath:(NSIndexPath *)indexPath touchLocation:(CGPoint)touchLocation
{
[super tableView:tableView didTapCellAtIndexPath:indexPath touchLocation:touchLocation];
NSLog(@"didTapCellAtIndexPath:%ld",(long)indexPath.row);
}


Expand Down Expand Up @@ -371,14 +366,6 @@ -(NSArray *)messagesMoreViewImgNames:(ZHCMessagesMoreView *)moreView
}



#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"click ActionSheet Button At index :%ld",(long)buttonIndex);
}

#pragma mark - PrivateMethods
-(void)closePressed:(id)sender
{
Expand Down

0 comments on commit 4f6afa6

Please sign in to comment.