RulesRegisterPredefineViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // RulesRegisterPredefineViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/19/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "RuleModel.h"
  10. #import "CustomLabel.h"
  11. #import "CustomImageView.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "JYRefreshController.h"
  14. #import "RulesRegisterPredefineViewController.h"
  15. #import "RulesRegisterViewController.h"
  16. #import "ImageUtil.h"
  17. @implementation PredefinedTitleViewCell
  18. - (void)awakeFromNib {
  19. _lblTitle.text = NSLocalizedString(@"샘플 규칙을 선택하세요", @"샘플 규칙을 선택하세요");
  20. self.backgroundColor = [UIColor clearColor];
  21. }
  22. @end
  23. @implementation PredefinedTableViewCell
  24. - (void)awakeFromNib {
  25. self.backgroundColor = [UIColor clearColor];
  26. }
  27. @end
  28. @interface RulesRegisterPredefineViewController () <UITableViewDataSource, UITableViewDelegate> {
  29. NSString *_pagingType, *_pagingId;
  30. NSMutableArray<RuleModel> *_predefinedRuleList;
  31. UIImage *_bgCellImage1, *_bgCellImage2;
  32. }
  33. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  34. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  35. @end
  36. #pragma mark - Class Definition
  37. @implementation RulesRegisterPredefineViewController
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. _predefinedRuleList = (NSMutableArray<RuleModel> *)[[NSMutableArray alloc] init];
  41. [self initUI];
  42. [self prepareViewDidLoad];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated {
  45. [super viewWillAppear:animated];
  46. self.title = NSLocalizedString(@"홈 규칙 만들기",nil);
  47. }
  48. - (void)initUI {
  49. //initialize tableview
  50. _tableView.dataSource = self;
  51. _tableView.delegate = self;
  52. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  53. _tableView.backgroundColor = [UIColor clearColor];
  54. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  55. //set refresh controls
  56. __weak typeof(self) weakSelf = self;
  57. self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  58. self.refreshController.pullToRefreshHandleAction = ^{
  59. [weakSelf requestPredefinedRulesRecently];
  60. };
  61. self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  62. self.loadMoreController.pullToLoadMoreHandleAction = ^{
  63. [weakSelf requestPredefinedRulesOlder];
  64. };
  65. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  66. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  67. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  68. }
  69. - (void)prepareViewDidLoad {
  70. [self performSelector:@selector(requestPredefinedRules) withObject:nil afterDelay:0.0f];
  71. }
  72. #pragma mark - Main Logic
  73. - (void)requestPredefinedRulesRecently {
  74. RuleModel *firstRule = [_predefinedRuleList firstObject];
  75. _pagingType = ksListPagingTypeUpward;
  76. _pagingId = firstRule.predRuleId;
  77. [self performSelector:@selector(requestPredefinedRules) withObject:nil afterDelay:0.0f];
  78. }
  79. - (void)requestPredefinedRulesOlder {
  80. RuleModel *lastRule = [_predefinedRuleList lastObject];
  81. _pagingType = ksListPagingTypeDownward;
  82. _pagingId = lastRule.predRuleId;
  83. [self performSelector:@selector(requestPredefinedRules) withObject:nil afterDelay:0.0f];
  84. }
  85. - (void)requestPredefinedRules {
  86. NSDictionary *parameter = @{@"pred_rule_id": _pagingId ? _pagingId : ksEmptyString,
  87. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  88. NSString *path = [NSString stringWithFormat:API_GET_PRERULE_LIST];
  89. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[RuleListModel class] completion:^(id responseObject) {
  90. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  91. return;
  92. }
  93. RuleListModel *fetchedPreRuleList = (RuleListModel *)responseObject;
  94. if (fetchedPreRuleList && fetchedPreRuleList.list && fetchedPreRuleList.list.count) {//API 성공,
  95. [_predefinedRuleList addObjectsFromArray:fetchedPreRuleList.list];
  96. } else {//실패 시,
  97. if (!_predefinedRuleList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  98. // NoContentView *noContentView = [NoContentView viewFromNib];
  99. // _tableView.tableFooterView = noContentView;
  100. }
  101. }
  102. [_tableView reloadData];
  103. } failure:^(id errorObject) {
  104. JDErrorModel *error = (JDErrorModel *)errorObject;
  105. [[JDFacade facade] alert:error.errorMessage];
  106. }];
  107. }
  108. #pragma mark - UITableView DataSource & Delegate
  109. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  110. return 1;
  111. }
  112. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  113. return _predefinedRuleList.count + 1;
  114. }
  115. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  116. CGFloat height = 100.0f;
  117. if (indexPath.row == 0) {
  118. height = 40.0f;
  119. }
  120. return height;
  121. }
  122. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  123. UITableViewCell *cell = nil;
  124. if (indexPath.row == 0) {//info cell
  125. cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCellIdentifier"];
  126. } else {
  127. PredefinedTableViewCell *pcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  128. if (pcell == nil) {
  129. pcell = [[PredefinedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  130. }
  131. RuleModel *predefinedRule = _predefinedRuleList[indexPath.row-1];
  132. pcell.lblRuleName.text = predefinedRule.ruleName;
  133. [pcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:predefinedRule.imageFileName] placeholderImage:nil];
  134. cell = pcell;
  135. }
  136. //set background image
  137. if (indexPath.row % 2 == 1) {
  138. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  139. } else {
  140. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  141. }
  142. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  143. return cell;
  144. }
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  146. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  147. if (indexPath.row == 0)
  148. return;
  149. //goto rule title register
  150. RulesRegisterViewController *vc = (RulesRegisterViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesRegisterViewController" storyboardName:@"Rules"];
  151. RuleModel *predefinedRule = _predefinedRuleList[indexPath.row-1];
  152. vc.tmpPredRule = predefinedRule;
  153. [self.navigationController pushViewController:vc animated:YES];
  154. }
  155. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  156. // Remove seperator inset
  157. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  158. [cell setSeparatorInset:UIEdgeInsetsZero];
  159. }
  160. // Prevent the cell from inheriting the Table View's margin settings
  161. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  162. [cell setPreservesSuperviewLayoutMargins:NO];
  163. }
  164. // Explictly set your cell's layout margins
  165. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  166. [cell setLayoutMargins:UIEdgeInsetsZero];
  167. }
  168. }
  169. #pragma mark - UI Events
  170. #pragma mark - MemoryWarning
  171. - (void)didReceiveMemoryWarning
  172. {
  173. [super didReceiveMemoryWarning];
  174. // Dispose of any resources that can be recreated.
  175. }
  176. @end