NoticeViewController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // NoticeViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 10/8/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "CustomTableView.h"
  10. #import "JYRefreshController.h"
  11. #import "NoticeViewController.h"
  12. @implementation NoticeTableViewCell
  13. @end
  14. @interface NoticeViewController () {
  15. NSMutableArray<NoticeModel> *_noticeList;
  16. NSString *_pagingId, *_pagingType;
  17. }
  18. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  19. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  20. @end
  21. #pragma mark - Class Definition
  22. @implementation NoticeViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. _noticeList = [(NSMutableArray<NoticeModel> *)[NSMutableArray alloc] init];
  27. [self initUI];
  28. [self prepareViewDidLoad];
  29. }
  30. - (void)initUI {
  31. [self initTableViewAsDefaultStyle:_tableView];
  32. //set refresh controls
  33. __weak typeof(self) weakSelf = self;
  34. self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  35. self.refreshController.pullToRefreshHandleAction = ^{
  36. [weakSelf requestNoticeListRecently];
  37. };
  38. self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  39. self.loadMoreController.pullToLoadMoreHandleAction = ^{
  40. [weakSelf requestNoticeListOlder];
  41. };
  42. }
  43. - (void)prepareViewDidLoad {
  44. [self requestNoticeList];
  45. }
  46. #pragma mark - Main Logic
  47. - (void)requestNoticeListRecently {
  48. NoticeModel *notice = [_noticeList firstObject];
  49. _pagingType = ksListPagingTypeUpward;
  50. _pagingId = notice.noticeId;
  51. [self performSelector:@selector(requestNoticeList) withObject:nil afterDelay:0.0f];
  52. }
  53. - (void)requestNoticeListOlder {
  54. NoticeModel *notice = [_noticeList lastObject];
  55. _pagingType = ksListPagingTypeDownward;
  56. _pagingId = notice.noticeId;
  57. [self performSelector:@selector(requestNoticeList) withObject:nil afterDelay:0.0f];
  58. }
  59. - (void)requestNoticeList {
  60. //parameters
  61. NSDictionary *parameter = @{@"notice_id" : _pagingId ? _pagingId : ksEmptyString,
  62. @"paging_type" : _pagingType ? _pagingType : ksEmptyString,
  63. @"read_datetime": [JDFacade facade].loginUser.noticeReadTime ? [JDFacade facade].loginUser.noticeReadTime : ksEmptyString };
  64. NSString *path = [NSString stringWithFormat:API_GET_NOTICE];
  65. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[NoticeListModel class] completion:^(id responseObject) {
  66. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  67. return;
  68. }
  69. NoticeListModel *fetchedNoticeList = (NoticeListModel *) responseObject;
  70. if (fetchedNoticeList && fetchedNoticeList.noticeList &&
  71. fetchedNoticeList.noticeList.count) {//API 성공 ,
  72. if (_pagingType && [_pagingType isEqualToString:ksListPagingTypeUpward]) {
  73. [_noticeList insertObjects:fetchedNoticeList.noticeList atIndexes:[NSIndexSet indexSetWithIndex:0]];
  74. } else {
  75. [_noticeList addObjectsFromArray:fetchedNoticeList.noticeList];
  76. }
  77. //set notice title
  78. NoticeModel *notice = _noticeList[0];
  79. if (![JDFacade facade].loginUser.noticeReadTime || ![[JDFacade facade].loginUser.noticeReadTime isEqualToString:notice.createDatetime]) {//기존 읽은 날짜랑 틀린 경우,
  80. [JDFacade facade].loginUser.noticeReadTime = notice.createDatetime;
  81. }
  82. _lblTitle.text = [NSString stringWithFormat:@"공지사항 %@", fetchedNoticeList.noticeCount];
  83. [_lblTitle setColor:kUITextColor02 text:fetchedNoticeList.noticeCount];
  84. } else {
  85. }
  86. [self.refreshController stopRefreshWithAnimated:YES completion:nil];
  87. [self.loadMoreController stopLoadMoreCompletion:nil];
  88. self.loadMoreController.enable = _noticeList.count % kdListPagginSize == 0;
  89. [_tableView reloadData];
  90. } failure:^(id errorObject) {
  91. JDErrorModel *error = (JDErrorModel *)errorObject;
  92. [[JDFacade facade] alert:error.errorMessage];
  93. }];
  94. }
  95. #pragma mark - UITableView DataSource & Delegate
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  97. return 1;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  100. return _noticeList.count;
  101. }
  102. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  103. CGFloat height = 120;
  104. return height;
  105. }
  106. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. NoticeModel *notice = _noticeList[indexPath.row];
  108. NoticeTableViewCell *cell = (NoticeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"NoticeCellIdentifier"];
  109. cell.lblContent.text = notice.content;
  110. cell.lblCreateDatetime.text = [CommonUtil formattedDate3:notice.createDatetime];
  111. return cell;
  112. }
  113. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  114. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  115. }
  116. #pragma mark - UI Events
  117. - (IBAction)btnCloseTouched:(id)sender {
  118. [self dismissViewControllerAnimated:YES completion:nil];
  119. }
  120. #pragma mark - MemoryWarning
  121. - (void)didReceiveMemoryWarning
  122. {
  123. [super didReceiveMemoryWarning];
  124. // Dispose of any resources that can be recreated.
  125. }
  126. @end