ScenesViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //
  2. // ScenesViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/1/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "SceneModel.h"
  11. #import "UIImageView+WebCache.h"
  12. #import "CustomLabel.h"
  13. #import "CustomImageView.h"
  14. #import "JYRefreshController.h"
  15. #import "ScenesDetailViewController.h"
  16. #import "ScenesViewController.h"
  17. #import "CustomButton.h"
  18. #define kfScenesTableViewCellHeight 100.0f
  19. @interface ScenesTableViewCell () {
  20. }
  21. @end
  22. @implementation ScenesTableViewCell
  23. - (void)awakeFromNib {
  24. self.backgroundColor = [UIColor clearColor];
  25. self.selectionStyle = UITableViewCellSelectionStyleNone;
  26. }
  27. @end
  28. @implementation ScenesAddTableViewCell
  29. - (void)awakeFromNib {
  30. self.backgroundColor = [UIColor clearColor];
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. _lblActionDesc.text = NSLocalizedString(@"버튼 하나로 여러 장치가 동작되도록\n지금 바로 만들어보세요", @"버튼 하나로 여러 장치가 동작되도록\n지금 바로 만들어보세요");
  33. }
  34. - (void)btnAddTouched:(id)sender {
  35. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ScenesRegisterTypeViewController" storyboardName:@"Scenes"];
  36. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  37. }
  38. @end
  39. @interface ScenesViewController () <UITableViewDataSource, UITableViewDelegate> {
  40. NSString *_pagingType, *_pagingId;
  41. NSMutableArray<SceneModel> *_sceneList;
  42. UIImage *_bgCellImage1, *_bgCellImage2;
  43. UIImage *_runImageNormal, *_runImageDisable;
  44. }
  45. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  46. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  47. @end
  48. #pragma mark - Class Definition
  49. @implementation ScenesViewController
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. _sceneList = (NSMutableArray<SceneModel> *)[[NSMutableArray alloc] init];
  53. [self initUI];
  54. [self prepareViewDidLoad];
  55. }
  56. - (void)viewWillAppear:(BOOL)animated {
  57. [super viewWillAppear:animated];
  58. self.title = NSLocalizedString(@"멀티 제어",nil);
  59. }
  60. - (void)initUI {
  61. [self generateOptionButton];
  62. //intialize tableview
  63. _tableView.dataSource = self;
  64. _tableView.delegate = self;
  65. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  66. _tableView.backgroundColor = [UIColor clearColor];
  67. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  68. //set refresh controls
  69. __weak typeof(self) weakSelf = self;
  70. self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  71. self.refreshController.pullToRefreshHandleAction = ^{
  72. [weakSelf requestRuleListRecently];
  73. };
  74. self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  75. self.loadMoreController.pullToLoadMoreHandleAction = ^{
  76. [weakSelf requestRuleListOlder];
  77. };
  78. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  79. _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  80. _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  81. _runImageNormal = [UIImage imageNamed:@"tp_01_img_scene_btn_play"];
  82. _runImageDisable = [UIImage imageNamed:@"tp_01_img_scene_btn_play_disable"];
  83. [self setThingsPopoverOptions];
  84. }
  85. - (void)setThingsPopoverOptions {
  86. //set Popover Contents
  87. __weak typeof(self) weakSelf = self;
  88. _popooverOptionArray = [[NSMutableArray alloc] init];
  89. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침", @"새로 고침"),
  90. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  91. @"target": weakSelf,
  92. @"selector": [NSValue valueWithPointer:@selector(refreshRuleList)]}];
  93. if ([JDFacade facade].loginHomeGroup.level > 10) {//파워유저 이상일 경우,
  94. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새 멀티 제어 추가", @"새 멀티 제어 추가"),
  95. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  96. @"target": weakSelf,
  97. @"selector": [NSValue valueWithPointer:@selector(addNewScene)]}];
  98. }
  99. }
  100. - (void)prepareViewDidLoad {
  101. [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
  102. }
  103. - (void)refreshRuleList {
  104. _pagingId = nil;
  105. _pagingType = nil;
  106. if (_sceneList && _sceneList.count) {
  107. [_sceneList removeAllObjects];
  108. }
  109. [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
  110. }
  111. - (void)addNewScene {
  112. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ScenesRegisterTypeViewController" storyboardName:@"Scenes"];
  113. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  114. }
  115. #pragma mark - Main Logic
  116. - (void)requestRuleListRecently {
  117. SceneModel *firstScene = [_sceneList firstObject];
  118. _pagingType = ksListPagingTypeUpward;
  119. _pagingId = firstScene.homegrpSceneId;
  120. [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
  121. }
  122. - (void)requestRuleListOlder {
  123. SceneModel *lastScene = [_sceneList lastObject];
  124. _pagingType = ksListPagingTypeDownward;
  125. _pagingId = lastScene.homegrpSceneId;
  126. [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
  127. }
  128. - (void)requestScenes {
  129. NSDictionary *parameter = @{@"homegrp_scene_id": _pagingId ? _pagingId : ksEmptyString,
  130. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  131. NSString *path = [NSString stringWithFormat:API_GET_SCENE];
  132. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[SceneListModel class] completion:^(id responseObject) {
  133. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  134. return;
  135. }
  136. SceneListModel *fetchedRuleList = (SceneListModel *)responseObject;
  137. if (fetchedRuleList && fetchedRuleList.list && fetchedRuleList.list.count) {//API 성공,
  138. [_sceneList addObjectsFromArray:fetchedRuleList.list];
  139. } else {//실패 시,
  140. if (!_sceneList.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)requestRunHomeGroupScene:(SceneModel *)scene {
  152. NSString *path = [NSString stringWithFormat:API_POST_SCENE_EXECUTE, scene.homegrpSceneId];
  153. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
  154. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  155. return;
  156. }
  157. JDJSONModel *result = (JDJSONModel *) responseObject;
  158. if (result) {//API 성공 ,
  159. if (result.msg && ![result.msg isEmptyString]) {
  160. [[JDFacade facade] alert:result.msg];
  161. } else {
  162. [[JDFacade facade] toast:NSLocalizedString(@"실행되었습니다", @"실행되었습니다")];
  163. }
  164. }
  165. } failure:^(id errorObject) {
  166. JDErrorModel *error = (JDErrorModel *)errorObject;
  167. [[JDFacade facade] alert:error.errorMessage];
  168. }];
  169. }
  170. - (void)updateScene:(SceneModel *)scene {
  171. for (SceneModel *tmpScene in _sceneList) {
  172. if ([scene.homegrpSceneId isEqualToString:tmpScene.homegrpSceneId]) {
  173. tmpScene.sceneName = scene.sceneName;
  174. tmpScene.deleteCnt = scene.deleteCnt;
  175. break;
  176. }
  177. }
  178. [_tableView reloadData];
  179. }
  180. #pragma mark - UITableView DataSource & Delegate
  181. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  182. return 1;
  183. }
  184. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  185. return _sceneList.count + ([JDFacade facade].loginHomeGroup.level > 10 ? 1 : 0);
  186. }
  187. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  188. CGFloat height = kfScenesTableViewCellHeight;
  189. if (indexPath.row == _sceneList.count) {
  190. height = _sceneList.count ? 190.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
  191. _tableView.scrollEnabled = _sceneList.count;
  192. }
  193. return height;
  194. }
  195. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  196. UITableViewCell *cell = nil;
  197. if (indexPath.row < _sceneList.count) {
  198. ScenesTableViewCell *rcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  199. if (rcell == nil) {
  200. rcell = [[ScenesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  201. }
  202. SceneModel *scene = _sceneList[indexPath.row];
  203. //활성/비활성 이미지 적용
  204. UIImage *nimage = ![scene.deleteCnt boolValue] ? _runImageNormal : _runImageDisable;
  205. [rcell.btnRun setImage:nimage forState:UIControlStateNormal];
  206. rcell.btnRun.value = indexPath;
  207. rcell.lblSceneName.text = scene.sceneName;
  208. rcell.lblSceneName.textColor = [UIColor whiteColor];
  209. [rcell.btnRun addTarget:self action:@selector(btnRunTouchedOnCell:) forControlEvents:UIControlEventTouchUpInside];
  210. cell = rcell;
  211. //set background image
  212. if (indexPath.row % 2 == 1) {
  213. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  214. } else {
  215. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  216. }
  217. } else {//씬 추가 셀
  218. ScenesAddTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
  219. if (acell == nil) {
  220. acell = [[ScenesAddTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCellIdentifier"];
  221. }
  222. cell = acell;
  223. }
  224. return cell;
  225. }
  226. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  227. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  228. if (indexPath.row == _sceneList.count) {//add cell 일 경우, 리턴,
  229. return;
  230. }
  231. //goto scene details
  232. ScenesDetailViewController *vc = (ScenesDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ScenesDetailViewController" storyboardName:@"Scenes"];
  233. vc.scene = _sceneList[indexPath.row];
  234. [self.navigationController pushViewController:vc animated:YES];
  235. }
  236. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  237. // Remove seperator inset
  238. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  239. [cell setSeparatorInset:UIEdgeInsetsZero];
  240. }
  241. // Prevent the cell from inheriting the Table View's margin settings
  242. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  243. [cell setPreservesSuperviewLayoutMargins:NO];
  244. }
  245. // Explictly set your cell's layout margins
  246. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  247. [cell setLayoutMargins:UIEdgeInsetsZero];
  248. }
  249. }
  250. #pragma mark - UI Events
  251. - (IBAction)btnRunTouchedOnCell:(id)sender {
  252. CustomButton *btn = (CustomButton *)sender;
  253. NSIndexPath *indexPath = (NSIndexPath *)btn.value;
  254. SceneModel *scene = _sceneList[indexPath.row];
  255. if ([scene.deleteCnt boolValue]) {
  256. [[JDFacade facade] toast:NSLocalizedString(@"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요", @"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요")];
  257. return;
  258. }
  259. [self requestRunHomeGroupScene:scene];
  260. }
  261. #pragma mark - MemoryWarning
  262. - (void)didReceiveMemoryWarning
  263. {
  264. [super didReceiveMemoryWarning];
  265. // Dispose of any resources that can be recreated.
  266. }
  267. @end