RulesViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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].loginUser.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)updateHomeHubStatusToRules {
  104. }
  105. - (void)refreshRuleList {
  106. _pagingId = nil;
  107. _pagingType = nil;
  108. if (_ruleList && _ruleList.count) {
  109. [_ruleList removeAllObjects];
  110. }
  111. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  112. }
  113. - (void)addNewRule {
  114. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RuleRegisterTypeViewController" storyboardName:@"Rules"];
  115. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  116. }
  117. #pragma mark - Main Logic
  118. - (void)requestRuleListRecently {
  119. RuleModel *firstRule = [_ruleList firstObject];
  120. _pagingType = ksListPagingTypeUpward;
  121. _pagingId = firstRule.ruleId;
  122. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  123. }
  124. - (void)requestRuleListOlder {
  125. RuleModel *lastRule = [_ruleList lastObject];
  126. _pagingType = ksListPagingTypeDownward;
  127. _pagingId = lastRule.ruleId;
  128. [self performSelector:@selector(requestRules) withObject:nil afterDelay:0.0f];
  129. }
  130. - (void)requestRules {
  131. NSDictionary *parameter = @{@"rule_id": _pagingId ? _pagingId : ksEmptyString,
  132. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  133. NSString *path = [NSString stringWithFormat:API_GET_RULE];
  134. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[RuleListModel class] completion:^(id responseObject) {
  135. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  136. return;
  137. }
  138. RuleListModel *fetchedRuleList = (RuleListModel *)responseObject;
  139. if (fetchedRuleList && fetchedRuleList.list && fetchedRuleList.list.count) {//API 성공,
  140. [_ruleList addObjectsFromArray:fetchedRuleList.list];
  141. } else {//실패 시,
  142. if (!_ruleList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  143. // NoContentView *noContentView = [NoContentView viewFromNib];
  144. // _tableView.tableFooterView = noContentView;
  145. }
  146. }
  147. [_tableView reloadData];
  148. } failure:^(id errorObject) {
  149. JDErrorModel *error = (JDErrorModel *)errorObject;
  150. [[JDFacade facade] alert:error.errorMessage];
  151. }];
  152. }
  153. - (void)updateRule:(RuleModel *)rule {
  154. for (RuleModel *tmpRule in _ruleList) {
  155. if ([rule.homegrpRuleId isEqualToString:tmpRule.homegrpRuleId]) {
  156. tmpRule.ruleName = rule.ruleName;
  157. tmpRule.useYn = rule.useYn;
  158. break;
  159. }
  160. }
  161. [_tableView reloadData];
  162. }
  163. #pragma mark - UITableView DataSource & Delegate
  164. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  165. return 1;
  166. }
  167. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  168. return _ruleList.count + ([JDFacade facade].loginUser.level > 10 ? 1 : 0);
  169. }
  170. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  171. CGFloat height = kfRulesTableViewCellHeight;
  172. if (indexPath.row == _ruleList.count) {
  173. height = _ruleList.count ? 190.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
  174. _tableView.scrollEnabled = _ruleList.count;
  175. }
  176. return height;
  177. }
  178. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  179. UITableViewCell *cell = nil;
  180. if (indexPath.row < _ruleList.count) {
  181. RulesTableViewCell *rcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  182. if (rcell == nil) {
  183. rcell = [[RulesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  184. }
  185. RuleModel *rule = _ruleList[indexPath.row];
  186. rcell.lblRuleName.text = rule.ruleName;
  187. rcell.lblRuleName.textColor = [UIColor whiteColor];
  188. rcell.imgvState.image = [rule.useYn boolValue] ? _stateImageRun : _stateImageStop;
  189. rcell.imgvState.image = [rule.useYn isEqualToString:ksKneetRulesDisable] ? _stateImageDisable : rcell.imgvState.image;
  190. cell = rcell;
  191. //set background image
  192. if (indexPath.row % 2 == 1) {
  193. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  194. } else {
  195. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  196. }
  197. } else {
  198. RulesAddTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
  199. if (acell == nil) {
  200. acell = [[RulesAddTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCellIdentifier"];
  201. }
  202. cell = acell;
  203. }
  204. return cell;
  205. }
  206. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  207. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  208. if (indexPath.row == _ruleList.count) {
  209. return;
  210. }
  211. //goto rule details
  212. RulesDetailViewController *vc = (RulesDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesDetailViewController" storyboardName:@"Rules"];
  213. vc.rule = _ruleList[indexPath.row];
  214. [self.navigationController pushViewController:vc animated:YES];
  215. }
  216. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  217. // Remove seperator inset
  218. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  219. [cell setSeparatorInset:UIEdgeInsetsZero];
  220. }
  221. // Prevent the cell from inheriting the Table View's margin settings
  222. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  223. [cell setPreservesSuperviewLayoutMargins:NO];
  224. }
  225. // Explictly set your cell's layout margins
  226. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  227. [cell setLayoutMargins:UIEdgeInsetsZero];
  228. }
  229. }
  230. #pragma mark - UI Events
  231. #pragma mark - MemoryWarning
  232. - (void)didReceiveMemoryWarning
  233. {
  234. [super didReceiveMemoryWarning];
  235. // Dispose of any resources that can be recreated.
  236. }
  237. @end