ScenesRegisterPredefineViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // ScenesRegisterPredefineViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/1/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "SceneModel.h"
  10. #import "CustomLabel.h"
  11. #import "CustomImageView.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "ScenesRegisterViewController.h"
  14. #import "ScenesRegisterPredefineViewController.h"
  15. #import "JYRefreshController.h"
  16. @implementation PredefinedSceneTitleViewCell
  17. - (void)awakeFromNib {
  18. _lblTitle.text = NSLocalizedString(@"샘플을 선택하세요", @"샘플을 선택하세요");
  19. self.backgroundColor = [UIColor clearColor];
  20. }
  21. @end
  22. @implementation PredefinedSceneTableViewCell
  23. - (void)awakeFromNib {
  24. self.backgroundColor = [UIColor clearColor];
  25. }
  26. @end
  27. @interface ScenesRegisterPredefineViewController () <UITableViewDataSource, UITableViewDelegate> {
  28. NSString *_pagingType, *_pagingId;
  29. NSMutableArray<SceneModel> *_predefinedSceneList;
  30. UIImage *_bgCellImage1, *_bgCellImage2;
  31. }
  32. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  33. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  34. @end
  35. #pragma mark - Class Definition
  36. @implementation ScenesRegisterPredefineViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. _predefinedSceneList = (NSMutableArray<SceneModel> *)[[NSMutableArray alloc] init];
  40. _pagingId = nil;
  41. _pagingType = nil;
  42. [self initUI];
  43. [self prepareViewDidLoad];
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. self.title = NSLocalizedString(@"멀티 제어 만들기",nil);
  48. }
  49. - (void)initUI {
  50. //initialize tableview
  51. _tableView.dataSource = self;
  52. _tableView.delegate = self;
  53. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  54. _tableView.backgroundColor = [UIColor clearColor];
  55. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  56. //set refresh controls
  57. __weak typeof(self) weakSelf = self;
  58. self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  59. self.refreshController.pullToRefreshHandleAction = ^{
  60. [weakSelf requestPredefinedRulesRecently];
  61. };
  62. self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  63. self.loadMoreController.pullToLoadMoreHandleAction = ^{
  64. [weakSelf requestPredefinedRulesOlder];
  65. };
  66. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  67. _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  68. _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  69. }
  70. - (void)prepareViewDidLoad {
  71. [self performSelector:@selector(requestPredefinedScenes) withObject:nil afterDelay:0.0f];
  72. }
  73. #pragma mark - Main Logic
  74. - (void)requestPredefinedRulesRecently {
  75. SceneModel *firstScene = [_predefinedSceneList firstObject];
  76. _pagingType = ksListPagingTypeUpward;
  77. _pagingId = firstScene.predSceneId;
  78. [self performSelector:@selector(requestPredefinedScenes) withObject:nil afterDelay:0.0f];
  79. }
  80. - (void)requestPredefinedRulesOlder {
  81. SceneModel *lastScene = [_predefinedSceneList lastObject];
  82. _pagingType = ksListPagingTypeDownward;
  83. _pagingId = lastScene.predSceneId;
  84. [self performSelector:@selector(requestPredefinedScenes) withObject:nil afterDelay:0.0f];
  85. }
  86. - (void)requestPredefinedScenes {
  87. NSDictionary *parameter = @{@"pred_scene_id": _pagingId ? _pagingId : ksEmptyString,
  88. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  89. NSString *path = [NSString stringWithFormat:API_GET_PRESCENE_LIST];
  90. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[SceneListModel class] completion:^(id responseObject) {
  91. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  92. return;
  93. }
  94. SceneListModel *fetchedPreRuleList = (SceneListModel *)responseObject;
  95. if (fetchedPreRuleList && fetchedPreRuleList.list && fetchedPreRuleList.list.count) {//API 성공,
  96. [_predefinedSceneList addObjectsFromArray:fetchedPreRuleList.list];
  97. } else {//실패 시,
  98. if (!_predefinedSceneList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  99. // NoContentView *noContentView = [NoContentView viewFromNib];
  100. // _tableView.tableFooterView = noContentView;
  101. }
  102. }
  103. [_tableView reloadData];
  104. } failure:^(id errorObject) {
  105. JDErrorModel *error = (JDErrorModel *)errorObject;
  106. [[JDFacade facade] alert:error.errorMessage];
  107. }];
  108. }
  109. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  110. return 1;
  111. }
  112. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  113. return _predefinedSceneList.count + 1;
  114. }
  115. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  116. CGFloat height = 100.0f;
  117. if (indexPath.row == 0) {
  118. height = 40.0f;
  119. }
  120. return height;
  121. }
  122. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  123. UITableViewCell *cell = nil;
  124. if (indexPath.row == 0) {//info cell
  125. cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCellIdentifier"];
  126. } else {
  127. PredefinedSceneTableViewCell *pcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  128. if (pcell == nil) {
  129. pcell = [[PredefinedSceneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  130. }
  131. SceneModel *predefinedScene = _predefinedSceneList[indexPath.row-1];
  132. pcell.lblSceneName.text = predefinedScene.sceneName;
  133. [pcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:predefinedScene.imageFileName] placeholderImage:nil];
  134. cell = pcell;
  135. }
  136. //set background image
  137. if (indexPath.row % 2 == 1) {
  138. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  139. } else {
  140. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  141. }
  142. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  143. return cell;
  144. }
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  146. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  147. if (indexPath.row == 0)
  148. return;
  149. //goto rule title register
  150. ScenesRegisterViewController *vc = (ScenesRegisterViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ScenesRegisterViewController" storyboardName:@"Scenes"];
  151. SceneModel *predefinedScene = _predefinedSceneList[indexPath.row-1];
  152. vc.tmpPredScene = predefinedScene;
  153. [self.navigationController pushViewController:vc animated:YES];
  154. }
  155. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  156. // Remove seperator inset
  157. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  158. [cell setSeparatorInset:UIEdgeInsetsZero];
  159. }
  160. // Prevent the cell from inheriting the Table View's margin settings
  161. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  162. [cell setPreservesSuperviewLayoutMargins:NO];
  163. }
  164. // Explictly set your cell's layout margins
  165. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  166. [cell setLayoutMargins:UIEdgeInsetsZero];
  167. }
  168. }
  169. #pragma mark - UI Events
  170. #pragma mark - MemoryWarning
  171. - (void)didReceiveMemoryWarning
  172. {
  173. [super didReceiveMemoryWarning];
  174. // Dispose of any resources that can be recreated.
  175. }
  176. @end