GroupNoticeViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // GroupNoticeViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/24.
  6. //
  7. #import "GroupNoticeViewController.h"
  8. #import "GroupNoticeCell.h"
  9. #import "GroupNoticeEditController.h"
  10. #import "LFPopupMenu.h"
  11. #import "GroupNoticeModel.h"
  12. @interface GroupNoticeViewController ()<UITableViewDelegate, UITableViewDataSource>
  13. @property (nonatomic, strong) UITableView *tableView;
  14. @end
  15. @implementation GroupNoticeViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view.
  19. [self allocTitle:@"群公告"];
  20. [self configUI];
  21. [self createNavButton];
  22. }
  23. - (void)createNavButton {
  24. if (self.canEdit) {
  25. [self rightButton:[UIImage imageNamed:@"edit_button"]];
  26. }
  27. }
  28. - (void)rightBtnClick {
  29. GroupNoticeEditController *eidtVC = [[GroupNoticeEditController alloc] init];
  30. eidtVC.groupId = self.groupId;
  31. eidtVC.isModify = NO;
  32. [self.navigationController pushViewController:eidtVC animated:YES];
  33. }
  34. - (void)configUI {
  35. [self.view addSubview:self.tableView];
  36. [self setPromptString:@"暂无群公告" imageName:@"wd_img_zwsj" inView:self.tableView];
  37. MJWeakSelf;
  38. self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{
  39. [weakSelf resetParamenter];
  40. [weakSelf requestData];
  41. }];
  42. self.tableView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  43. if (weakSelf.isLoadMore) {
  44. weakSelf.pages += 1;
  45. [weakSelf requestData];
  46. }
  47. else {
  48. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  49. }
  50. }];
  51. }
  52. - (void)endRefresh {
  53. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  54. [self.tableView.mj_header endRefreshing];
  55. [self.tableView.mj_footer endRefreshing];
  56. });
  57. }
  58. - (void)viewWillAppear:(BOOL)animated {
  59. [super viewWillAppear:animated];
  60. [self resetParamenter];
  61. [self requestData];
  62. }
  63. - (void)resetParamenter {
  64. self.isLoadMore = YES;
  65. self.pages = 1;
  66. self.rows = 10;
  67. self.dataArray = [NSMutableArray array];
  68. [self.tableView.mj_footer resetNoMoreData];
  69. [self setPromptString:@"暂无群公告" imageName:@"wd_img_zwsj" inView:self.tableView];
  70. [self.tableView reloadData];
  71. }
  72. - (void)requestData {
  73. [self showhud];
  74. [KSNetworkingManager queryNoticeListRequest:KS_POST groupId:self.groupId page:[NSString stringWithFormat:@"%zd", self.pages] rows:[NSString stringWithFormat:@"%zd", self.rows] success:^(NSDictionary * _Nonnull dic) {
  75. [self removehub];
  76. [self endRefresh];
  77. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  78. // 赋值
  79. NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  80. for (NSDictionary *parm in sourceArray) {
  81. GroupNoticeModel *model = [[GroupNoticeModel alloc] initWithDictionary:parm];
  82. [self.dataArray addObject:model];
  83. }
  84. if (sourceArray.count < self.rows) {
  85. self.isLoadMore = NO;
  86. }
  87. }
  88. else {
  89. [self MBPShow:MESSAGEKEY];
  90. }
  91. [self.tableView reloadData];
  92. [self changePromptLabelState];
  93. } faliure:^(NSError * _Nonnull error) {
  94. [self removehub];
  95. [self endRefresh];
  96. if (self.networkAvaiable == NO) {
  97. [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView];
  98. }
  99. [self.dataArray removeAllObjects];
  100. [self.tableView reloadData];
  101. [self changePromptLabelState];
  102. }];
  103. }
  104. #pragma mark ---- table data source
  105. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  106. return self.dataArray.count;
  107. }
  108. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  109. GroupNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupNoticeCell"];
  110. GroupNoticeModel *model = self.dataArray[indexPath.row];
  111. MJWeakSelf;
  112. [cell configWithSource:model enable:self.canEdit callback:^(id _Nonnull sourceModel, UIButton * _Nonnull sender) {
  113. [weakSelf showPopViewWithSource:sourceModel inView:sender];
  114. }];
  115. return cell;
  116. }
  117. - (void)showPopViewWithSource:(id)source inView:(UIView *)view {
  118. GroupNoticeModel *model = source;
  119. MJWeakSelf;
  120. LFPopupMenuItem *item1 = [LFPopupMenuItem createWithTitle:@"编辑公告" image:nil action:^{
  121. // 编辑公告
  122. [weakSelf editNotice:source];
  123. }];
  124. NSString *title = model.top ? @"取消置顶" : @"置顶";
  125. LFPopupMenuItem *item2 = [LFPopupMenuItem createWithTitle:title image:nil action:^{
  126. // 置顶或者取消置顶
  127. [weakSelf operationAction:source];
  128. }];
  129. LFPopupMenuItem *item3 = [LFPopupMenuItem createWithTitle:@"删除公告" image:nil action:^{
  130. // 删除公告
  131. [weakSelf deleteNotice:source];
  132. }];
  133. LFPopupMenu *menu = [[LFPopupMenu alloc] init];
  134. menu.needBorder = YES;
  135. menu.rowHeight = 40.0f;
  136. menu.minWidth = 150;
  137. menu.textColor = HexRGB(0x333333);
  138. menu.textFont = [UIFont systemFontOfSize:14.0f];
  139. [menu configWithItems:@[item1,item2,item3]];
  140. [menu showArrowToView:view];
  141. }
  142. - (void)deleteNotice:(id)source {
  143. GroupNoticeModel *model = source;
  144. [self showhud];
  145. [KSNetworkingManager imGroupNoticeDelRequest:KS_POST noticeID:[NSString stringWithFormat:@"%.0f",model.internalBaseClassIdentifier] success:^(NSDictionary * _Nonnull dic) {
  146. [self removehub];
  147. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  148. MJWeakSelf;
  149. [self KSShowMsg:@"删除成功" promptCompletion:^{
  150. [weakSelf resetParamenter];
  151. [weakSelf requestData];
  152. }];
  153. }
  154. else {
  155. [self MBPShow:MESSAGEKEY];
  156. }
  157. } faliure:^(NSError * _Nonnull error) {
  158. [self removehub];
  159. }];
  160. }
  161. - (void)editNotice:(id)source {
  162. GroupNoticeModel *model = source;
  163. GroupNoticeEditController *editVC = [[GroupNoticeEditController alloc] init];
  164. editVC.groupId = self.groupId;
  165. editVC.isModify = YES;
  166. editVC.titles = model.title;
  167. editVC.content = model.content;
  168. editVC.noticeID = [NSString stringWithFormat:@"%.0f",model.internalBaseClassIdentifier];
  169. editVC.isOn = model.top == 1 ? YES : NO;
  170. [self.navigationController pushViewController:editVC animated:YES];
  171. }
  172. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  173. {
  174. return [UIView new];
  175. }
  176. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  177. {
  178. return [UIView new];
  179. }
  180. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  181. return CGFLOAT_MIN;
  182. }
  183. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  184. return CGFLOAT_MIN;
  185. }
  186. - (void)operationAction:(id)source {
  187. GroupNoticeModel *model = source;
  188. NSString *isTop = nil;
  189. // 取反
  190. if (model.top == YES) {
  191. isTop = @"0";
  192. }
  193. else {
  194. isTop = @"1";
  195. }
  196. [self showhud];
  197. [KSNetworkingManager imGroupNoticeUpdateRequest:KS_POST groupId:self.groupId title:model.title content:model.content isTop:isTop noticeID:[NSString stringWithFormat:@"%.0f",model.internalBaseClassIdentifier] success:^(NSDictionary * _Nonnull dic) {
  198. [self removehub];
  199. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  200. [self resetParamenter];
  201. [self requestData];
  202. }
  203. else {
  204. [self MBPShow:MESSAGEKEY];
  205. }
  206. } faliure:^(NSError * _Nonnull error) {
  207. [self removehub];
  208. }];
  209. }
  210. #pragma mark --- lazy
  211. - (UITableView *)tableView {
  212. if (!_tableView) {
  213. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 1, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin) style:UITableViewStylePlain];
  214. _tableView.delegate = self;
  215. _tableView.dataSource = self;
  216. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  217. _tableView.backgroundColor = HexRGB(0xf5f5f5);
  218. _tableView.rowHeight = UITableViewAutomaticDimension;
  219. _tableView.estimatedRowHeight = 270.0f;
  220. _tableView.showsHorizontalScrollIndicator = NO;
  221. _tableView.showsVerticalScrollIndicator = NO;
  222. [_tableView registerNib:[UINib nibWithNibName:@"GroupNoticeCell" bundle:nil] forCellReuseIdentifier:@"GroupNoticeCell"];
  223. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
  224. bottomView.backgroundColor = HexRGB(0xf5f5f5);
  225. _tableView.tableFooterView = bottomView;
  226. }
  227. return _tableView;
  228. }
  229. /*
  230. #pragma mark - Navigation
  231. // In a storyboard-based application, you will often want to do a little preparation before navigation
  232. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  233. // Get the new view controller using [segue destinationViewController].
  234. // Pass the selected object to the new view controller.
  235. }
  236. */
  237. @end