ScenesViewController.m 12 KB

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