RulesViewController.m 11 KB

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