DiscoverViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // DiscoverViewController.m
  3. // Wildfire Chat
  4. //
  5. // Created by WF Chat on 2017/10/28.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "DiscoverViewController.h"
  9. #import "ChatroomListViewController.h"
  10. #import "DeviceTableViewController.h"
  11. #import <WFChatUIKit/WFChatUIKit.h>
  12. #import <WFChatClient/WFCCIMService.h>
  13. #import "DiscoverMomentsTableViewCell.h"
  14. #ifdef WFC_MOMENTS
  15. #import <WFMomentClient/WFMomentClient.h>
  16. #import <WFMomentUIKit/WFMomentUIKit.h>
  17. #endif
  18. #import "UIFont+YH.h"
  19. #import "UIColor+YH.h"
  20. @interface DiscoverViewController () <UITableViewDataSource, UITableViewDelegate>
  21. @property (nonatomic, strong)UITableView *tableView;
  22. @property (nonatomic, assign)BOOL hasMoments;
  23. @property (nonatomic, strong)NSMutableArray *dataSource;
  24. @end
  25. @implementation DiscoverViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.dataSource = [NSMutableArray arrayWithArray:@[@{@"title":LocalizedString(@"Chatroom"),@"image":@"discover_chatroom",@"des":@"chatroom"},
  29. @{@"title":LocalizedString(@"Rebot"),@"image":@"rebot",@"des":@"rebot"},
  30. @{@"title":LocalizedString(@"Channel"),
  31. @"image":@"chat_channel",@"des":@"channel"},
  32. @{@"title":LocalizedString(@"DevDocs"),
  33. @"image":@"dev_docs",@"des":@"Dev"},@{@"title":@"Things",
  34. @"image":@"discover_things",@"des":@"Things"}]];
  35. if(NSClassFromString(@"SDTimeLineTableViewController")) {
  36. [self.dataSource insertObject:@{@"title":LocalizedString(@"Moments"),@"image":@"AlbumReflashIcon",@"des":@"moment"} atIndex:0];
  37. self.hasMoments = YES;
  38. } else {
  39. self.hasMoments = NO;
  40. }
  41. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  42. self.tableView.delegate = self;
  43. self.tableView.dataSource = self;
  44. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.01)];
  45. [self.tableView reloadData];
  46. self.tableView.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  47. [self.view addSubview:self.tableView];
  48. #ifdef WFC_MOMENTS
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveComments:) name:kReceiveComments object:nil];
  50. #endif
  51. }
  52. - (void)onReceiveComments:(NSNotification *)notification {
  53. [self.tableView reloadData];
  54. }
  55. - (void)didReceiveMemoryWarning {
  56. [super didReceiveMemoryWarning];
  57. // Dispose of any resources that can be recreated.
  58. }
  59. - (void)viewWillAppear:(BOOL)animated {
  60. [super viewWillAppear:animated];
  61. [self updateUnreadStatus];
  62. }
  63. - (void)updateUnreadStatus {
  64. [self.tableView reloadData];
  65. #ifdef WFC_MOMENTS
  66. [self.tabBarController.tabBar showBadgeOnItemIndex:2 badgeValue:[[WFMomentService sharedService] getUnreadCount]];
  67. #endif
  68. }
  69. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  70. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 9)];
  71. view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  72. return view;
  73. }
  74. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  75. return 9;
  76. }
  77. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  78. return 53;
  79. }
  80. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  81. NSString *des = self.dataSource[indexPath.section][@"des"];
  82. if ([des isEqualToString:@"moment"]) {
  83. UIViewController *vc = [[NSClassFromString(@"SDTimeLineTableViewController") alloc] init];
  84. vc.hidesBottomBarWhenPushed = YES;
  85. [self.navigationController pushViewController:vc animated:YES];
  86. }
  87. if ([des isEqualToString:@"chatroom"]) {
  88. ChatroomListViewController *vc = [[ChatroomListViewController alloc] init];
  89. vc.hidesBottomBarWhenPushed = YES;
  90. [self.navigationController pushViewController:vc animated:YES];
  91. }
  92. if ([des isEqualToString:@"channel"]) {
  93. WFCUFavChannelTableViewController *channelVC = [[WFCUFavChannelTableViewController alloc] init];;
  94. channelVC.hidesBottomBarWhenPushed = YES;
  95. [self.navigationController pushViewController:channelVC animated:YES];
  96. }
  97. if ([des isEqualToString:@"rebot"]) {
  98. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  99. mvc.conversation = [[WFCCConversation alloc] init];
  100. mvc.conversation.type = Single_Type;
  101. mvc.conversation.target = @"FireRobot";
  102. mvc.conversation.line = 0;
  103. mvc.hidesBottomBarWhenPushed = YES;
  104. [self.navigationController pushViewController:mvc animated:YES];
  105. }
  106. if ([des isEqualToString:@"Dev"]) {
  107. WFCUBrowserViewController *vc = [[WFCUBrowserViewController alloc] init];
  108. vc.hidesBottomBarWhenPushed = YES;
  109. vc.url = @"http://docs.wildfirechat.cn";
  110. [self.navigationController pushViewController:vc animated:YES];
  111. }
  112. if ([des isEqualToString:@"Things"]) {
  113. DeviceTableViewController *vc = [[DeviceTableViewController alloc] init];
  114. vc.hidesBottomBarWhenPushed = YES;
  115. [self.navigationController pushViewController:vc animated:YES];
  116. }
  117. }
  118. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  119. return self.dataSource.count;
  120. }
  121. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  122. return 1;
  123. }
  124. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  125. view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  126. }
  127. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  128. UITableViewCell *cell;
  129. if (indexPath.section == 0 && self.hasMoments) {
  130. cell = [tableView dequeueReusableCellWithIdentifier:@"momentsCell"];
  131. if (cell == nil) {
  132. cell = [[DiscoverMomentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"momentsCell"];
  133. }
  134. } else {
  135. cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
  136. if (cell == nil) {
  137. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
  138. }
  139. }
  140. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  141. cell.accessoryView = nil;
  142. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  143. cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
  144. cell.textLabel.text = self.dataSource[indexPath.section][@"title"];
  145. cell.imageView.image = [UIImage imageNamed:self.dataSource[indexPath.section][@"image"]];
  146. if (indexPath.section == 0 && self.hasMoments) {
  147. DiscoverMomentsTableViewCell *momentsCell = (DiscoverMomentsTableViewCell *)cell;
  148. __weak typeof(self)ws = self;
  149. #ifdef WFC_MOMENTS
  150. int unread = [[WFMomentService sharedService] getUnreadCount];
  151. if (unread) {
  152. momentsCell.bubbleView.hidden = NO;
  153. [momentsCell.bubbleView setBubbleTipNumber:unread];
  154. } else {
  155. momentsCell.bubbleView.hidden = YES;
  156. }
  157. NSMutableArray<WFMFeed *> *feeds = [[WFMomentService sharedService] restoreCache:nil];
  158. if (feeds.count > 0) {
  159. momentsCell.lastFeed = [feeds objectAtIndex:0];
  160. } else {
  161. [[WFMomentService sharedService] getFeeds:0 count:10 fromUser:nil success:^(NSArray<WFMFeed *> * _Nonnull feeds) {
  162. if (feeds.count) {
  163. [[WFMomentService sharedService] storeCache:feeds forUser:nil];
  164. [ws.tableView reloadData];
  165. }
  166. } error:^(int error_code) {
  167. }];
  168. }
  169. #endif
  170. }
  171. return cell;
  172. }
  173. @end