RulesViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "CustomLabel.h"
  12. #import "CustomImageView.h"
  13. #import "JYRefreshController.h"
  14. #import "RulesDetailViewController.h"
  15. #import "RulesViewController.h"
  16. #define kfRulesTableViewCellHeight 100.0f
  17. @implementation RulesTableViewCell
  18. - (void)awakeFromNib {
  19. self.backgroundColor = [UIColor clearColor];
  20. self.selectionStyle = UITableViewCellSelectionStyleNone;
  21. }
  22. @end
  23. @implementation RulesAddTableViewCell
  24. - (void)awakeFromNib {
  25. self.backgroundColor = [UIColor clearColor];
  26. self.selectionStyle = UITableViewCellSelectionStyleNone;
  27. _lblDesc.text = NSLocalizedString(@"원하는 시점에 알아서 실행되도록\n홈 규칙을 만들어 보세요!", @"원하는 시점에 알아서 실행되도록\n홈 규칙을 만들어 보세요!");
  28. }
  29. - (void)btnAddTouched:(id)sender {
  30. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RuleRegisterTypeViewController" storyboardName:@"Rules"];
  31. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  32. }
  33. @end
  34. @interface RulesViewController () <UITableViewDataSource, UITableViewDelegate> {
  35. NSString *_pagingType, *_pagingId;
  36. NSMutableArray<RuleModel> *_ruleList;
  37. UIImage *_bgCellImage1, *_bgCellImage2;
  38. UIImage *_stateImageRun, *_stateImageStop, *_stateImageDisable;
  39. }
  40. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  41. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  42. @end
  43. #pragma mark - Class Definition
  44. @implementation RulesViewController
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. _ruleList = (NSMutableArray<RuleModel> *)[[NSMutableArray alloc] init];
  48. [self initUI];
  49. [self prepareViewDidLoad];
  50. }
  51. - (void)viewWillAppear:(BOOL)animated {
  52. [super viewWillAppear:animated];
  53. self.title = NSLocalizedString(@"홈 규칙",nil);
  54. }
  55. - (void)initUI {
  56. [self generateOptionButton];
  57. //initialize tableview
  58. _tableView.dataSource = self;
  59. _tableView.delegate = self;
  60. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  61. _tableView.backgroundColor = [UIColor clearColor];
  62. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  63. //set refresh controls
  64. __weak typeof(self) weakSelf = self;
  65. self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  66. self.refreshController.pullToRefreshHandleAction = ^{
  67. [weakSelf requestRuleListRecently];
  68. };
  69. self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  70. self.loadMoreController.pullToLoadMoreHandleAction = ^{
  71. [weakSelf requestRuleListOlder];
  72. };
  73. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  74. _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  75. _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  76. _stateImageRun = [UIImage imageNamed:@"tp_01_img_icon_rule_condition_play"];
  77. _stateImageStop = [UIImage imageNamed:@"tp_01_img_icon_rule_condition_stop"];
  78. _stateImageDisable = [UIImage imageNamed:@"tp_01_img_icon_rule_condition_disable"];
  79. [self setThingsPopoverOptions];
  80. }
  81. - (void)setThingsPopoverOptions {
  82. //set Popover Contents
  83. __weak typeof(self) weakSelf = self;
  84. _popooverOptionArray = [[NSMutableArray alloc] init];
  85. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  86. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  87. @"target": weakSelf,
  88. @"selector": [NSValue valueWithPointer:@selector(refreshRuleList)]}];
  89. if ([JDFacade facade].loginHomeGroup.level > 10) {//파워유저 이상일 경우,
  90. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새 규칙 추가",nil),
  91. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  92. @"target": weakSelf,
  93. @"selector": [NSValue valueWithPointer:@selector(addNewRule)]}];
  94. }
  95. }
  96. - (void)prepareViewDidLoad {
  97. if (_ruleList && _ruleList.count) {
  98. [_ruleList removeAllObjects];
  99. }
  100. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  101. }
  102. - (void)refreshRuleList {
  103. _pagingId = nil;
  104. _pagingType = nil;
  105. if (_ruleList && _ruleList.count) {
  106. [_ruleList removeAllObjects];
  107. }
  108. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  109. }
  110. - (void)addNewRule {
  111. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RuleRegisterTypeViewController" storyboardName:@"Rules"];
  112. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  113. }
  114. #pragma mark - Main Logic
  115. - (void)requestRuleListRecently {
  116. RuleModel *firstRule = [_ruleList firstObject];
  117. _pagingType = ksListPagingTypeUpward;
  118. _pagingId = firstRule.ruleId;
  119. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  120. }
  121. - (void)requestRuleListOlder {
  122. RuleModel *lastRule = [_ruleList lastObject];
  123. _pagingType = ksListPagingTypeDownward;
  124. _pagingId = lastRule.ruleId;
  125. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  126. }
  127. - (void)requestRules {
  128. NSDictionary *parameter = @{@"rule_id": _pagingId ? _pagingId : ksEmptyString,
  129. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  130. NSString *path = [NSString stringWithFormat:API_GET_RULE];
  131. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[RuleListModel class] completion:^(id responseObject) {
  132. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  133. return;
  134. }
  135. RuleListModel *fetchedRuleList = (RuleListModel *)responseObject;
  136. if (fetchedRuleList && fetchedRuleList.list && fetchedRuleList.list.count) {//API 성공,
  137. [_ruleList addObjectsFromArray:fetchedRuleList.list];
  138. } else {//실패 시,
  139. if (!_ruleList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  140. // NoContentView *noContentView = [NoContentView viewFromNib];
  141. // _tableView.tableFooterView = noContentView;
  142. }
  143. }
  144. [_tableView reloadData];
  145. } failure:^(id errorObject) {
  146. JDErrorModel *error = (JDErrorModel *)errorObject;
  147. [[JDFacade facade] alert:error.errorMessage];
  148. }];
  149. }
  150. - (void)updateRule:(RuleModel *)rule {
  151. for (RuleModel *tmpRule in _ruleList) {
  152. if ([rule.homegrpRuleId isEqualToString:tmpRule.homegrpRuleId]) {
  153. tmpRule.ruleName = rule.ruleName;
  154. tmpRule.useYn = rule.useYn;
  155. break;
  156. }
  157. }
  158. [_tableView reloadData];
  159. }
  160. #pragma mark - UITableView DataSource & Delegate
  161. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  162. return 1;
  163. }
  164. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  165. return _ruleList.count + ([JDFacade facade].loginHomeGroup.level > 10 ? 1 : 0);
  166. }
  167. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  168. CGFloat height = kfRulesTableViewCellHeight;
  169. if (indexPath.row == _ruleList.count) {
  170. height = _ruleList.count ? 190.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
  171. _tableView.scrollEnabled = _ruleList.count;
  172. }
  173. return height;
  174. }
  175. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  176. UITableViewCell *cell = nil;
  177. if (indexPath.row < _ruleList.count) {
  178. RulesTableViewCell *rcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  179. if (rcell == nil) {
  180. rcell = [[RulesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  181. }
  182. RuleModel *rule = _ruleList[indexPath.row];
  183. rcell.lblRuleName.text = rule.ruleName;
  184. rcell.lblRuleName.textColor = [UIColor whiteColor];
  185. rcell.imgvState.image = [rule.useYn boolValue] ? _stateImageRun : _stateImageStop;
  186. rcell.imgvState.image = [rule.useYn isEqualToString:ksKneetRulesDisable] ? _stateImageDisable : rcell.imgvState.image;
  187. cell = rcell;
  188. //set background image
  189. if (indexPath.row % 2 == 1) {
  190. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  191. } else {
  192. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  193. }
  194. } else {
  195. RulesAddTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
  196. if (acell == nil) {
  197. acell = [[RulesAddTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCellIdentifier"];
  198. }
  199. cell = acell;
  200. }
  201. return cell;
  202. }
  203. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  204. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  205. if (indexPath.row == _ruleList.count) {
  206. return;
  207. }
  208. //goto rule details
  209. RulesDetailViewController *vc = (RulesDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesDetailViewController" storyboardName:@"Rules"];
  210. vc.rule = _ruleList[indexPath.row];
  211. [self.navigationController pushViewController:vc animated:YES];
  212. }
  213. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  214. // Remove seperator inset
  215. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  216. [cell setSeparatorInset:UIEdgeInsetsZero];
  217. }
  218. // Prevent the cell from inheriting the Table View's margin settings
  219. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  220. [cell setPreservesSuperviewLayoutMargins:NO];
  221. }
  222. // Explictly set your cell's layout margins
  223. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  224. [cell setLayoutMargins:UIEdgeInsetsZero];
  225. }
  226. }
  227. #pragma mark - UI Events
  228. #pragma mark - MemoryWarning
  229. - (void)didReceiveMemoryWarning
  230. {
  231. [super didReceiveMemoryWarning];
  232. // Dispose of any resources that can be recreated.
  233. }
  234. @end