2
0

ChatroomListViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // ChatroomListViewController.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain lee on 2018/8/24.
  6. // Copyright © 2018 WildFireChat. All rights reserved.
  7. //
  8. #import "ChatroomListViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "ChatroomItemCell.h"
  11. #import <WFChatUIKit/WFChatUIKit.h>
  12. @interface ChatroomListViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
  13. @property (nonatomic, strong) UICollectionView * collectionView;
  14. @property (nonatomic, strong) NSArray<NSString *> *chatroomIds;
  15. @property (nonatomic, strong) NSMutableArray<WFCCChatroomInfo *> *chatroomInfos;
  16. @end
  17. static NSString * identifier = @"cxCellID";
  18. @implementation ChatroomListViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self.view addSubview:self.collectionView];
  22. self.chatroomIds = @[@"chatroom1", @"chatroom2", @"chatroom3"];
  23. self.chatroomInfos = [[NSMutableArray alloc] init];
  24. for (NSString *chatroomId in self.chatroomIds) {
  25. WFCCChatroomInfo *info = [[WFCCChatroomInfo alloc] init];
  26. info.chatroomId = chatroomId;
  27. [self.chatroomInfos addObject:info];
  28. [[WFCCIMService sharedWFCIMService] getChatroomInfo:chatroomId upateDt:0 success:^(WFCCChatroomInfo *chatroomInfo) {
  29. [self updateChatroomInfo:chatroomInfo];
  30. } error:^(int error_code) {
  31. }];
  32. }
  33. }
  34. - (void)updateChatroomInfo:(WFCCChatroomInfo *)info {
  35. dispatch_async(dispatch_get_main_queue(), ^{
  36. for (WFCCChatroomInfo *crInfo in self.chatroomInfos) {
  37. if ([crInfo.chatroomId isEqualToString:info.chatroomId]) {
  38. NSUInteger index = [self.chatroomInfos indexOfObject:crInfo];
  39. [self.chatroomInfos removeObjectAtIndex:index];
  40. [self.chatroomInfos insertObject:info atIndex:index];
  41. [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
  42. break;
  43. }
  44. }
  45. });
  46. }
  47. - (void)didReceiveMemoryWarning {
  48. [super didReceiveMemoryWarning];
  49. // Dispose of any resources that can be recreated.
  50. }
  51. #pragma mark - set_and_get
  52. -(UICollectionView *)collectionView{
  53. if (!_collectionView) {
  54. //自动网格布局
  55. UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
  56. CGFloat edgeInset = 10;
  57. int countInLine = 2;
  58. flowLayout.sectionInset = UIEdgeInsetsMake(edgeInset, edgeInset, edgeInset, edgeInset);
  59. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  60. width = (width - edgeInset)/countInLine - edgeInset;
  61. flowLayout.itemSize = CGSizeMake(width, width + 20);
  62. _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
  63. [_collectionView registerClass:[ChatroomItemCell class] forCellWithReuseIdentifier:identifier];
  64. _collectionView.dataSource = self;
  65. _collectionView.delegate = self;
  66. [_collectionView setBackgroundColor:[WFCUConfigManager globalManager].backgroudColor];
  67. }
  68. return _collectionView;
  69. }
  70. #pragma mark - deleDate
  71. //每个分组里有多少个item
  72. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  73. return self.chatroomInfos.count;
  74. }
  75. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  76. ChatroomItemCell * cell = (ChatroomItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  77. cell.chatroomInfo = [self.chatroomInfos objectAtIndex:indexPath.row];
  78. return cell;
  79. }
  80. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  81. WFCCChatroomInfo *chatroomInfo = [self.chatroomInfos objectAtIndex:indexPath.row];
  82. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  83. mvc.conversation = [WFCCConversation conversationWithType:Chatroom_Type target:chatroomInfo.chatroomId line:0];
  84. [self.navigationController pushViewController:mvc animated:YES];
  85. }
  86. @end