PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/My Style/NewChatBubble/Inbox/Inbox_list_ViewController.m

https://gitlab.com/pqhuy1987/Mahaikum
Objective C | 293 lines | 214 code | 72 blank | 7 comment | 20 complexity | 28ad1f766347b00803e2c2451e99b58b MD5 | raw file
  1. //
  2. // Inbox_list_ViewController.m
  3. // Mahalkum
  4. //
  5. // Created by user on 7/12/16.
  6. // Copyright © 2016 Kuwait E-Gate. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "Inbox_list_ViewController.h"
  10. #import "Inbox_info.h"
  11. #import "Inbox_list_cell.h"
  12. @interface Inbox_list_ViewController()<AVTagTextViewDelegate,Inbox_list_CellDelegate> {
  13. NSMutableArray *array_inboxs;
  14. }
  15. @end
  16. @interface Inbox_list_ViewController (PrivateStuff)
  17. @end
  18. @implementation Inbox_list_ViewController
  19. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. }
  23. return self;
  24. }
  25. - (IBAction)btn_back_click:(id)sender {
  26. [self.navigationController popViewControllerAnimated:YES];
  27. [self dismissViewControllerAnimated:YES completion:nil];
  28. }
  29. - (void)viewDidLoad
  30. {
  31. [super viewDidLoad];
  32. array_inboxs = [[NSMutableArray alloc]init];
  33. self.chat_viewObj=[[Chat_ViewController alloc]initWithNibName:@"Chat_ViewController" bundle:nil];
  34. }
  35. -(void)viewWillAppear:(BOOL)animated {
  36. [super viewWillAppear:animated];
  37. self.lbl_title.text = @"Me";
  38. if ([array_inboxs count])
  39. [array_inboxs removeAllObjects];
  40. ((AppDelegate *)[UIApplication sharedApplication].delegate).isChattingRoom = YES;
  41. }
  42. - (void)viewDidAppear:(BOOL)animated
  43. {
  44. [super viewDidAppear:animated];
  45. [self get_inbox_info];
  46. }
  47. #pragma mark - Get inbox info from server for current user
  48. -(void)get_inbox_info{
  49. [SVProgressHUD dismiss];
  50. [SVProgressHUD show];
  51. NSString *salt = [NSString stringWithFormat:@"%d",rand() % 10000];
  52. NSString *key = SIGNSALTAPIKEY;
  53. NSString *tempStr = [NSString stringWithFormat:@"%@%@",key,salt];
  54. NSString *sig = [StaticClass returnMD5Hash :tempStr];
  55. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"19209" object:nil];
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInboxInfoAPIResponce:) name:@"19209" object:nil];
  57. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"-19209" object:nil];
  58. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FaildgetInboxInfoAPIResponce:) name:@"-19209" object:nil];
  59. NSString *requestStr = [NSString stringWithFormat:@"%@get_chatting_inbox.php",[[Singleton sharedSingleton] getBaseURL]];
  60. NSLog(@"requestStr:%@",requestStr);
  61. NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
  62. sig, @"sign",
  63. salt, @"salt",
  64. self.userID, @"uid",
  65. nil];
  66. NSLog(@"params:%@",params);
  67. AFNetworkingQueue *networkQueue=[AFNetworkingQueue sharedSingleton];
  68. [networkQueue queueItems:requestStr :@"19209" :params];
  69. }
  70. -(void)getInboxInfoAPIResponce:(NSNotification *)notification {
  71. [SVProgressHUD dismiss];
  72. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"19209" object:nil];
  73. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"-19209" object:nil];
  74. NSDictionary* dict = [notification userInfo];
  75. NSError *err = nil;
  76. AFHTTPRequestOperation *response=[dict objectForKey:@"index"];
  77. NSDictionary *result = (NSDictionary *) [NSJSONSerialization JSONObjectWithData:[response responseData] options:kNilOptions error:&err];
  78. if ([[result valueForKey:@"success"] isEqualToString:@"1"]) {
  79. NSMutableArray *dict =(NSMutableArray *)[result valueForKey:@"data"];
  80. if ([dict count] == 0 || dict == nil)
  81. {
  82. return;
  83. }
  84. NSLog(@"%@",NSStringFromClass([[result valueForKey:@"data"] class]));
  85. for (int i = 0; i< [dict count]; i++) {
  86. NSDictionary *temp = (NSDictionary *)[dict objectAtIndex:i];
  87. NSString *chat_id = [temp objectForKey:@"id"];
  88. NSString *fromUserName = [temp objectForKey:@"fromUserName"];
  89. NSString *fromUserImage=[StaticClass urlDecode:[temp objectForKey:@"fromUserImage"]];
  90. NSString *message = [StaticClass urlDecode:[temp objectForKey:@"message"]];
  91. NSString *time = [StaticClass urlDecode:[temp objectForKey:@"sentDate"]];
  92. NSString *fromUserID = [temp objectForKey:@"fromUserID"];
  93. NSLog(@"Inbox->%@", fromUserName);
  94. [self addInboxData:chat_id username:fromUserName image_url:fromUserImage chat_content:message datecreated:time uid:fromUserID];
  95. }
  96. [self.inboxChatTable reloadData];
  97. }
  98. }
  99. -(void)FaildgetInboxInfoAPIResponce:(NSNotification *)notification {
  100. [SVProgressHUD dismiss];
  101. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"19209" object:nil];
  102. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"-19209" object:nil];
  103. [MKInfoPanel showPanelInView:self.view type:MKInfoPanelTypeError title:@"Network Failure!" subtitle:@"Check your internet connection and try again later!" hideAfter:2];
  104. }
  105. -(void)addInboxData:(NSString*)chat_id username:(NSString*)UserName image_url:(NSString*)imageURL chat_content:(NSString*)message datecreated:(NSString*)dateTime uid:(NSString*)user_id
  106. {
  107. Inbox_info *inbox_data = [[Inbox_info alloc]init];
  108. inbox_data.chat_id = chat_id;
  109. inbox_data.username = UserName;
  110. inbox_data.image_url=imageURL;
  111. inbox_data.chat_content=message;
  112. inbox_data.datecreated=dateTime;
  113. inbox_data.uid=user_id;
  114. [array_inboxs addObject:inbox_data];
  115. }
  116. #pragma mark UITableview Delegate
  117. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  118. return 1;
  119. }
  120. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  121. NSInteger indCount = indexPath.row;
  122. Inbox_info *obj = [array_inboxs objectAtIndex:indCount];
  123. CGSize size =[obj.chat_content sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0] constrainedToSize:CGSizeMake(255, HUGE_VALL) lineBreakMode:NSLineBreakByWordWrapping];
  124. if (size.height<25) {
  125. return 58.0f;
  126. }
  127. return 30.0f+size.height+20.0f;
  128. }
  129. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  130. return array_inboxs.count;
  131. }
  132. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  133. return 0.0f;
  134. }
  135. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  136. {
  137. return Nil;
  138. }
  139. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  140. UITableViewCell *cell;
  141. cell = [self Inbox_list_CellForTableView:tableView atIndexPath:indexPath];
  142. return cell;
  143. }
  144. - (Inbox_list_cell *)Inbox_list_CellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
  145. {
  146. static NSString *CellIdentifier = @"Inbox_list_cell";
  147. self.objInbox_list_cell = (Inbox_list_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  148. self.objInbox_list_cell.customdelegate = self;
  149. if(self.objInbox_list_cell == nil)
  150. {
  151. NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"Inbox_list_cell" owner:self options:nil];
  152. self.objInbox_list_cell = [nib objectAtIndex:0];
  153. [self.objInbox_list_cell draw_in_cell];
  154. self.objInbox_list_cell.showsReorderControl = NO;
  155. self.objInbox_list_cell.selectionStyle = UITableViewCellSelectionStyleNone;
  156. self.objInbox_list_cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"ChatBack.png"]];
  157. [self.objInbox_list_cell.btn_user_name addTarget:self action:@selector(btn_user_info_click:) forControlEvents:UIControlEventTouchUpInside];
  158. [self.objInbox_list_cell.img_user1 addTarget:self action:@selector(btn_user_info_click:) forControlEvents:UIControlEventTouchUpInside];
  159. }
  160. Inbox_info *shareObj = [array_inboxs objectAtIndex:indexPath.row];
  161. self.objInbox_list_cell.img_user1.tag = indexPath.row;
  162. self.objInbox_list_cell.btn_user_name.tag = indexPath.row;
  163. [self.objInbox_list_cell.img_user sd_setImageWithURL:[NSURL URLWithString:shareObj.image_url] placeholderImage:nil];
  164. self.objInbox_list_cell.img_user.layer.cornerRadius =22.0f;
  165. self.objInbox_list_cell.img_user.layer.masksToBounds =YES;
  166. [self.objInbox_list_cell.btn_user_name setTitle:shareObj.username forState:UIControlStateNormal];
  167. self.objInbox_list_cell.lbl_date.text =[self get_time_different:shareObj.datecreated];
  168. self.objInbox_list_cell.lbl_desc.text=shareObj.chat_content;
  169. NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.objInbox_list_cell.lbl_desc.text];
  170. [self.objInbox_list_cell.lbl_desc setAttributedText:string];
  171. CGSize size =[shareObj.chat_content sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0] constrainedToSize:CGSizeMake(255, 9999) lineBreakMode:NSLineBreakByWordWrapping];
  172. if (size.height<24) {
  173. self.objInbox_list_cell.view_line.frame = CGRectMake(0,54, kViewWidth,1);
  174. } else {
  175. self.objInbox_list_cell.view_line.frame = CGRectMake(0,34.f+size.height,kViewWidth,1);
  176. }
  177. self.objInbox_list_cell.view_line.hidden=YES;
  178. if (size.height<24)
  179. {
  180. self.objInbox_list_cell.lbl_desc.frame=CGRectMake(58,30, 255,24);
  181. }
  182. else
  183. {
  184. self.objInbox_list_cell.lbl_desc.frame=CGRectMake(58,30, 255, size.height);
  185. }
  186. self.objInbox_list_cell.tag=indexPath.row;
  187. self.objInbox_list_cell.lbl_desc.delegate = self;
  188. return self.objInbox_list_cell;
  189. }
  190. - (void)btn_user_info_click:(id)sender {
  191. NSLog(@"go to chatting room!");
  192. Inbox_info *shareObj=[array_inboxs objectAtIndex:((UIButton *)sender).tag];
  193. self.chat_viewObj.toUserID = shareObj.uid;
  194. self.chat_viewObj.toUserFullName = shareObj.name;
  195. self.chat_viewObj.toUserName = shareObj.username;
  196. self.chat_viewObj.toUserImage = shareObj.image_url;
  197. [self presentViewController:self.chat_viewObj animated:YES completion:nil];
  198. }
  199. #pragma mark Date Convert to s,m,h,d,M,y
  200. - (NSString *)get_time_different:(NSString *)datestring {
  201. NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
  202. [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
  203. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
  204. NSDate *current_date1 = [dateFormatter dateFromString:[[Singleton sharedSingleton]get_current_time]];
  205. NSDate *created_date = [dateFormatter dateFromString:datestring];
  206. NSTimeInterval diff = [current_date1 timeIntervalSinceDate:created_date];
  207. if (diff<60) {
  208. return [NSString stringWithFormat:@"%.fs",diff];
  209. }else if(diff/60<60){
  210. return [NSString stringWithFormat:@"%.fm",diff/60];
  211. }else if(diff/3600<24){
  212. return [NSString stringWithFormat:@"%.fh",diff/3600];
  213. }else if(diff/(3600*24)<31){
  214. return [NSString stringWithFormat:@"%.fd",diff/(3600*24)];
  215. }else if(diff/(3600*24*30)<12){
  216. return [NSString stringWithFormat:@"%.fM",diff/(3600*24*30)];
  217. }else{
  218. return [NSString stringWithFormat:@"%.fy",diff/(3600*24*30*12)];
  219. }
  220. }
  221. @end