RulesViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // RulesViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/18/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "RuleModel.h"
  11. #import "CustomTableView.h"
  12. #import "CustomLabel.h"
  13. #import "CustomImageView.h"
  14. #import "JYRefreshController.h"
  15. #import "RulesDetailViewController.h"
  16. #import "RulesViewController.h"
  17. #import "ImageUtil.h"
  18. #import "UIImageView+WebCache.h"
  19. #define kfRulesTableViewCellHeight 100.0f
  20. @implementation RulesTableViewCell
  21. @end
  22. @interface RulesViewController () <UITableViewDataSource, UITableViewDelegate> {
  23. NSMutableArray<RuleModel> *_ruleList;
  24. NSString *_pagingType, *_pagingId;
  25. UIImage *_stateImageRun, *_stateImageStop, *_stateImageDisable;
  26. BOOL _isNotFirstLoading, _isDeleteMode;
  27. }
  28. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  29. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  30. @end
  31. #pragma mark - Class Definition
  32. @implementation RulesViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _ruleList = (NSMutableArray<RuleModel> *)[[NSMutableArray alloc] init];
  36. [self initUI];
  37. [self prepareViewDidLoad];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated {
  40. [super viewWillAppear:animated];
  41. self.title = NSLocalizedString(@"홈 규칙",nil);
  42. }
  43. - (void)initUI {
  44. //initialize tableview
  45. [self initTableViewAsDefaultStyle:_tableView];
  46. //set refresh controls
  47. __weak typeof(self) weakSelf = self;
  48. self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  49. self.refreshController.pullToRefreshHandleAction = ^{
  50. [weakSelf requestRuleListRecently];
  51. };
  52. self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  53. self.loadMoreController.pullToLoadMoreHandleAction = ^{
  54. [weakSelf requestRuleListOlder];
  55. };
  56. //FIXME : delete?
  57. _stateImageRun = [UIImage imageNamed:@"tp_01_img_icon_rule_condition_play"];
  58. _stateImageStop = [UIImage imageNamed:@"tp_01_img_icon_rule_condition_stop"];
  59. _stateImageDisable = [UIImage imageNamed:@"tp_01_img_icon_rule_condition_disable"];
  60. [self setThingsPopoverOptions];
  61. }
  62. - (void)setThingsPopoverOptions {
  63. //set Popover Contents
  64. __weak typeof(self) weakSelf = self;
  65. _popooverOptionArray = [[NSMutableArray alloc] init];
  66. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  67. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  68. @"target": weakSelf,
  69. @"selector": [NSValue valueWithPointer:@selector(refreshRuleList)]}];
  70. if ([JDFacade facade].loginUser.level > 10) {//파워유저 이상일 경우,
  71. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"만들기", @"만들기"),
  72. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  73. @"target": weakSelf,
  74. @"selector": [NSValue valueWithPointer:@selector(addNewRule)]}];
  75. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
  76. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  77. @"target": weakSelf,
  78. @"selector": [NSValue valueWithPointer:@selector(toggleEditMode)]}];
  79. }
  80. }
  81. - (void)prepareViewDidLoad {
  82. if (_ruleList && _ruleList.count) {
  83. [_ruleList removeAllObjects];
  84. }
  85. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  86. }
  87. - (void)updateHomeHubStatusToRules {
  88. }
  89. - (void)refreshRuleList {
  90. _pagingId = nil;
  91. _pagingType = nil;
  92. if (_ruleList && _ruleList.count) {
  93. [_ruleList removeAllObjects];
  94. }
  95. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  96. }
  97. - (void)addNewRule {
  98. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesAddViewController" storyboardName:@"Rules"];
  99. [self presentViewController:vc animated:YES completion:nil];
  100. }
  101. - (void)toggleEditMode {
  102. _isDeleteMode = !_isDeleteMode;
  103. [_tableView reloadData];
  104. _constraintEditModeRight.constant = _isDeleteMode ? 0 : -_editModeView.width;
  105. [UIView animateWithDuration:kfAnimationDur animations:^{
  106. [self.view layoutIfNeeded];
  107. }];
  108. }
  109. #pragma mark - Main Logic
  110. - (void)requestRuleListRecently {
  111. RuleModel *firstRule = [_ruleList firstObject];
  112. _pagingType = ksListPagingTypeUpward;
  113. _pagingId = firstRule.ruleId;
  114. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  115. }
  116. - (void)requestRuleListOlder {
  117. RuleModel *lastRule = [_ruleList lastObject];
  118. _pagingType = ksListPagingTypeDownward;
  119. _pagingId = lastRule.ruleId;
  120. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  121. }
  122. - (void)requestRules {
  123. NSDictionary *parameter = @{@"rule_id": _pagingId ? _pagingId : ksEmptyString,
  124. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  125. NSString *path = [NSString stringWithFormat:API_GET_RULE];
  126. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[RuleListModel class] completion:^(id responseObject) {
  127. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  128. return;
  129. }
  130. RuleListModel *fetchedRuleList = (RuleListModel *)responseObject;
  131. if (fetchedRuleList && fetchedRuleList.rules && fetchedRuleList.rules.count) {//API 성공,
  132. [_ruleList addObjectsFromArray:fetchedRuleList.rules];
  133. _lblTitle.text = [NSString stringWithFormat:@"활성 %@ / 비활성 %@", fetchedRuleList.activeCount, fetchedRuleList.inactiveCount];
  134. [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%@", fetchedRuleList.activeCount]];
  135. [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%@", fetchedRuleList.inactiveCount]];
  136. } else {//실패 시,
  137. if (!_ruleList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  138. // NoContentView *noContentView = [NoContentView viewFromNib];
  139. // _tableView.tableFooterView = noContentView;
  140. }
  141. }
  142. [_tableView reloadData];
  143. } failure:^(id errorObject) {
  144. JDErrorModel *error = (JDErrorModel *)errorObject;
  145. [[JDFacade facade] alert:error.errorMessage];
  146. }];
  147. }
  148. - (void)updateRule:(RuleModel *)rule {
  149. for (RuleModel *tmpRule in _ruleList) {
  150. if ([rule.homegrpRuleId isEqualToString:tmpRule.homegrpRuleId]) {
  151. tmpRule.ruleName = rule.ruleName;
  152. tmpRule.useYn = rule.useYn;
  153. break;
  154. }
  155. }
  156. [_tableView reloadData];
  157. }
  158. #pragma mark - UITableView DataSource & Delegate
  159. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  160. return 1;
  161. }
  162. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  163. return _ruleList.count;
  164. }
  165. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  166. CGFloat height = kfRulesTableViewCellHeight;
  167. if (indexPath.row == _ruleList.count) {
  168. height = _ruleList.count ? 190.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
  169. _tableView.scrollEnabled = _ruleList.count;
  170. }
  171. return height;
  172. }
  173. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  174. UITableViewCell *cell = nil;
  175. RulesTableViewCell *rcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  176. if (rcell == nil) {
  177. rcell = [[RulesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  178. }
  179. RuleModel *rule = _ruleList[indexPath.row];
  180. [rcell.imgvTrigger sd_setImageWithURL:[NSURL URLWithString:rule.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  181. rcell.lblRuleName.text = rule.ruleName;
  182. cell = rcell;
  183. return cell;
  184. }
  185. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  186. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  187. if (indexPath.row == _ruleList.count) {
  188. return;
  189. }
  190. //goto rule details
  191. RulesDetailViewController *vc = (RulesDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesDetailViewController" storyboardName:@"Rules"];
  192. vc.rule = _ruleList[indexPath.row];
  193. [self.navigationController pushViewController:vc animated:YES];
  194. }
  195. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  196. // Remove seperator inset
  197. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  198. [cell setSeparatorInset:UIEdgeInsetsZero];
  199. }
  200. // Prevent the cell from inheriting the Table View's margin settings
  201. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  202. [cell setPreservesSuperviewLayoutMargins:NO];
  203. }
  204. // Explictly set your cell's layout margins
  205. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  206. [cell setLayoutMargins:UIEdgeInsetsZero];
  207. }
  208. }
  209. #pragma mark - UI Events
  210. - (IBAction)btnOptionTouched:(id)sender {
  211. [self toggleOptions:sender];
  212. }
  213. #pragma mark - MemoryWarning
  214. - (void)didReceiveMemoryWarning
  215. {
  216. [super didReceiveMemoryWarning];
  217. // Dispose of any resources that can be recreated.
  218. }
  219. @end