QMChatRoomGuestBookViewController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // QMChatRoomGuestBookViewController.m
  3. // IMSDK-OC
  4. //
  5. // Created by HCF on 16/3/10.
  6. // Copyright © 2016年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomGuestBookViewController.h"
  9. #import <QMChatSDK/QMChatSDK-Swift.h>
  10. #import "QMLeaveMessageCell.h"
  11. /**
  12. 留言板
  13. */
  14. @interface QMChatRoomGuestBookViewController ()<UITextViewDelegate, UITableViewDelegate, UITableViewDataSource> {
  15. UITableView *_tableView;
  16. }
  17. @end
  18. @implementation QMChatRoomGuestBookViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.title = NSLocalizedString(@"title.messageBoard", nil);
  22. self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:246/255.0 alpha:1];
  23. [self.navigationController.navigationBar setTranslucent:NO];
  24. self.view.userInteractionEnabled = true;
  25. UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  26. [self.view addGestureRecognizer:tapGesture];
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
  28. if (!self.isScheduleLeave) {
  29. self.leaveMsg = [QMConnect leaveMessagePlaceholder];
  30. self.contactFields = [QMConnect leaveMessageContactInformation];
  31. }
  32. if ([self.leaveMsg isEqualToString:@""]) {
  33. self.leaveMsg = NSLocalizedString(@"title.pleaseLeave", nil);
  34. }
  35. _condition = [NSMutableDictionary dictionary];
  36. [self createUI];
  37. }
  38. - (void)createUI {
  39. self.messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(self.messageLabel.frame)+10, kScreenWidth-40, 120)];
  40. self.messageTextView.font = [UIFont systemFontOfSize:16];
  41. self.messageTextView.backgroundColor = [UIColor whiteColor];
  42. self.messageTextView.layer.borderColor = [[UIColor colorWithRed:215/255.0 green:215/255.0 blue:215/255.0 alpha:1.0] CGColor];
  43. self.messageTextView.layer.borderWidth = 0.6;
  44. self.messageTextView.delegate = self;
  45. self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.messageTextView.frame), CGRectGetMinY(self.messageTextView.frame), CGRectGetWidth(self.messageTextView.frame), [self calcLabelHeight:self.leaveMsg ?: NSLocalizedString(@"title.pleaseLeave", nil) font:[UIFont systemFontOfSize:15] width:CGRectGetWidth(self.messageTextView.frame)])];
  46. self.textLabel.backgroundColor = [UIColor clearColor];
  47. self.textLabel.textColor = [UIColor lightGrayColor];
  48. self.textLabel.font = [UIFont systemFontOfSize:15];
  49. self.textLabel.numberOfLines = 0;
  50. self.textLabel.text = self.leaveMsg ?: NSLocalizedString(@"title.pleaseLeave", nil);
  51. [self.messageTextView addSubview:self.textLabel];
  52. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
  53. bottomView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
  54. self.submitBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  55. self.submitBtn.frame = CGRectMake(20, 30, kScreenWidth-40, 44);
  56. [self.submitBtn setTitle:NSLocalizedString(@"title.leaving", nil) forState:UIControlStateNormal];
  57. self.submitBtn.backgroundColor = [UIColor colorWithRed:13/255.0 green:139/255.0 blue:249/255.0 alpha:1];
  58. [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  59. self.submitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
  60. [self.submitBtn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
  61. [bottomView addSubview:self.submitBtn];
  62. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:UITableViewStyleGrouped];
  63. _tableView.delegate = self;
  64. _tableView.dataSource = self;
  65. [self.view addSubview:_tableView];
  66. _tableView.tableHeaderView = self.messageTextView;
  67. _tableView.tableFooterView = bottomView;
  68. }
  69. - (void)tapAction {
  70. [self.messageTextView resignFirstResponder];
  71. }
  72. - (void)textViewDidBeginEditing:(UITextView *)textView {
  73. self.textLabel.text = @"";
  74. }
  75. - (void)textViewDidEndEditing:(UITextView *)textView {
  76. if ([textView.text isEqualToString:@""]) {
  77. self.textLabel.text = self.leaveMsg ?: NSLocalizedString(@"title.pleaseLeave", nil);
  78. }
  79. }
  80. - (void)submitAction: (UIButton *)sender {
  81. if (![[self.messageTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] && [self verifyRequired]) {
  82. [QMConnect sdkSubmitLeaveMessageWithInformation:self.peerId information:_condition leavemsgFields:self.contactFields message:self.messageTextView.text successBlock:^{
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. [self showAlertViewControllerWithTitle:NSLocalizedString(@"title.messageSuccess", nil)];
  85. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  86. [self.navigationController popToRootViewControllerAnimated:YES];
  87. });
  88. });
  89. } failBlock:^{
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. [self showAlertViewControllerWithTitle:NSLocalizedString(@"title.messageFailure", nil)];
  92. });
  93. }];
  94. }else {
  95. [self showAlertViewControllerWithTitle:NSLocalizedString(@"title.messageContent", nil)];
  96. }
  97. }
  98. - (BOOL)verifyRequired {
  99. for (NSDictionary *field in self.contactFields) {
  100. BOOL required = [[field objectForKey:@"required"] boolValue];
  101. NSString *_id = [NSString stringWithFormat:@"%@", [field objectForKey:@"_id"]];
  102. if (required == YES) {
  103. BOOL tempRequired = YES;
  104. if (!_condition[_id]) {
  105. tempRequired = NO;
  106. }
  107. for (NSString *key in _condition.allKeys) {
  108. if ([key isEqualToString:_id]) {
  109. if ([[_condition[key] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]) {
  110. tempRequired = NO;
  111. }
  112. }
  113. }
  114. if (!tempRequired) {
  115. NSString *alertTitle = [NSString stringWithFormat:@"%@%@", field[@"name"], NSLocalizedString(@"title.requireItem", nil)];
  116. [self showAlertViewControllerWithTitle:alertTitle];
  117. return NO;
  118. }
  119. }
  120. }
  121. return YES;
  122. }
  123. - (CGFloat)calcLabelHeight: (NSString *)text font: (UIFont *)font width: (CGFloat)width {
  124. NSDictionary *attribute = @{NSFontAttributeName: font};
  125. CGRect labelRect = [text boundingRectWithSize:CGSizeMake(width, CGRectGetHeight(self.messageTextView.frame)) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil];
  126. return labelRect.size.height;
  127. }
  128. - (void)showAlertViewControllerWithTitle: (NSString *)title {
  129. [self.view endEditing:YES];
  130. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:title preferredStyle:UIAlertControllerStyleAlert];
  131. [self presentViewController:alertController animated:YES completion:nil];
  132. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  133. [alertController dismissViewControllerAnimated:YES completion:nil];
  134. });
  135. }
  136. #pragma mark - UITableViewDelegate UITableViewDataSource
  137. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  138. return self.contactFields.count;
  139. }
  140. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  141. return 1;
  142. }
  143. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  144. return 10;
  145. }
  146. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  147. return [[UIView alloc] init];
  148. }
  149. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  150. return 0.1;
  151. }
  152. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  153. return [[UIView alloc] init];
  154. }
  155. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  156. return 44;
  157. }
  158. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. NSString *identifier = @"leaveMsgCell";
  160. QMLeaveMessageCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  161. if (!cell) {
  162. cell = [[QMLeaveMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  163. }
  164. [cell setData:self.contactFields[indexPath.section] defaultValue:self.condition[self.contactFields[indexPath.section][@"_id"]]];
  165. __weak QMChatRoomGuestBookViewController *strongSelf = self;
  166. cell.backInformation = ^(NSString *information) {
  167. if ([information isEqualToString:@""]) {
  168. [strongSelf.condition removeObjectForKey:strongSelf.contactFields[indexPath.section][@"_id"]];
  169. }else {
  170. [strongSelf.condition setValue:information forKey:strongSelf.contactFields[indexPath.section][@"_id"]];
  171. }
  172. };
  173. return cell;
  174. }
  175. #pragma mark - Push Notification
  176. // 键盘通知
  177. - (void)keyboardFrameChange: (NSNotification *)notification {
  178. NSDictionary * userInfo = notification.userInfo;
  179. NSValue * value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  180. CGRect newFrame = [value CGRectValue];
  181. if (newFrame.origin.y == [UIScreen mainScreen].bounds.size.height) {
  182. [UIView animateWithDuration:0.3 animations:^{
  183. _tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
  184. }];
  185. }else {
  186. [UIView animateWithDuration:0.3 animations:^{
  187. _tableView.frame = CGRectMake(0, 0, kScreenWidth, [UIScreen mainScreen].bounds.size.height-64-newFrame.size.height);
  188. }];
  189. }
  190. }
  191. - (void)dealloc {
  192. NSLog(@"留言板dealloc");
  193. }
  194. - (void)didReceiveMemoryWarning {
  195. [super didReceiveMemoryWarning];
  196. // Dispose of any resources that can be recreated.
  197. }
  198. @end